16
16
17
17
using namespace tests ;
18
18
19
- KleeGenerator::KleeGenerator (
20
- utbot::ProjectContext projectContext,
21
- utbot::SettingsContext settingsContext,
22
- fs::path serverBuildDir,
23
- std::shared_ptr<CompilationDatabase> compilationDatabase,
24
- types::TypesHandler &typesHandler,
25
- PathSubstitution filePathsSubstitution,
26
- std::shared_ptr<BuildDatabase> buildDatabase,
27
- ProgressWriter const *progressWriter)
28
- : projectContext(std::move(projectContext)),
29
- settingsContext(std::move(settingsContext)), projectTmpPath(std::move(serverBuildDir)),
30
- compilationDatabase(std::move(compilationDatabase)),
31
- typesHandler(typesHandler), pathSubstitution(std::move(filePathsSubstitution)),
32
- buildDatabase(std::move(buildDatabase)), progressWriter(progressWriter) {
19
+ // KleeGenerator::KleeGenerator(
20
+ // utbot::testGen.projectContext testGen.projectContext,
21
+ // utbot::SettingsContext settingsContext,
22
+ // fs::path serverBuildDir,
23
+ // std::shared_ptr<CompilationDatabase> compilationDatabase,
24
+ // types::TypesHandler &typesHandler,
25
+ // PathSubstitution filePathsSubstitution,
26
+ // std::shared_ptr<testGen.buildDatabase> testGen.buildDatabase,
27
+ // ProgressWriter const *progressWriter)
28
+ // : testGen.projectContext(std::move(testGen.projectContext)),
29
+ // settingsContext(std::move(settingsContext)),
30
+ // testGen.serverBuildDir(std::move(serverBuildDir)),
31
+ // compilationDatabase(std::move(compilationDatabase)),
32
+ // typesHandler(typesHandler),
33
+ // pathSubstitution(std::move(filePathsSubstitution)),
34
+ // testGen.buildDatabase(std::move(testGen.buildDatabase)),
35
+ // progressWriter(progressWriter) {
36
+ // try {
37
+ // fs::create_directories(this->testGen.serverBuildDir);
38
+ // fs::create_directories(Paths::getLogDir(this->testGen.projectContext.projectName));
39
+ // } catch (const fs::filesystem_error &e) {
40
+ // throw FileSystemException("create_directories failed", e);
41
+ // }
42
+ // }
43
+ //
44
+ // auto generator = std::make_shared<KleeGenerator>(
45
+ // testGen.testGen.projectContext, testGen.settingsContext,
46
+ // testGen.serverBuildDir, testGen.testGen.buildDatabase->compilationDatabase, typesHandler,
47
+ // pathSubstitution, testGen.testGen.buildDatabase, testGen.progressWriter);
48
+
49
+ KleeGenerator::KleeGenerator (BaseTestGen &_testGen, types::TypesHandler &typesHandler,
50
+ PathSubstitution filePathsSubstitution)
51
+ : testGen(_testGen), typesHandler(typesHandler),
52
+ pathSubstitution(std::move(filePathsSubstitution)) {
33
53
try {
34
- fs::create_directories (this ->projectTmpPath );
35
- fs::create_directories (Paths::getLogDir (this ->projectContext .projectName ));
54
+ fs::create_directories (this ->testGen . serverBuildDir );
55
+ fs::create_directories (Paths::getLogDir (this ->testGen . projectContext .projectName ));
36
56
} catch (const fs::filesystem_error &e) {
37
57
throw FileSystemException (" create_directories failed" , e);
38
58
}
@@ -55,10 +75,10 @@ KleeGenerator::buildByCDb(const CollectionUtils::MapFileTo<fs::path> &filesToBui
55
75
}
56
76
57
77
makefilePrinter.declareTarget (" all" , outfilePaths, {});
58
- fs::path makefile = projectTmpPath / " GenerationCompileMakefile.mk" ;
78
+ fs::path makefile = testGen. serverBuildDir / " GenerationCompileMakefile.mk" ;
59
79
FileSystemUtils::writeToFile (makefile, makefilePrinter.ss .str ());
60
80
61
- auto command = MakefileUtils::MakefileCommand (projectContext, makefile, " all" );
81
+ auto command = MakefileUtils::MakefileCommand (testGen. projectContext , makefile, " all" );
62
82
ExecUtils::ExecutionResult res = command.run ();
63
83
if (res.status != 0 ) {
64
84
LOG_S (ERROR) << StringUtils::stringFormat (" Make for \" %s\" failed.\n Command: \" %s\"\n %s\n " ,
@@ -81,7 +101,7 @@ KleeGenerator::buildByCDb(const CollectionUtils::FileSet &filesToBuild,
81
101
const CollectionUtils::FileSet &stubSources) {
82
102
CollectionUtils::MapFileTo<fs::path> filesMap;
83
103
for (fs::path const &file : filesToBuild) {
84
- filesMap[file] = buildDatabase->getBitcodeFile (file);
104
+ filesMap[file] = testGen. buildDatabase ->getBitcodeFile (file);
85
105
}
86
106
return buildByCDb (filesMap, stubSources);
87
107
}
@@ -127,20 +147,24 @@ static const std::unordered_set<std::string> UNSUPPORTED_FLAGS_AND_OPTIONS_KLEE
127
147
std::optional<utbot::CompileCommand>
128
148
KleeGenerator::getCompileCommandForKlee (const fs::path &hintPath,
129
149
const CollectionUtils::FileSet &stubSources,
130
- const std::vector<std::string> &flags) const {
131
- auto compilationUnitInfo = buildDatabase->getClientCompilationUnitInfo (hintPath);
150
+ const std::vector<std::string> &flags,
151
+ bool forStub) const {
152
+
153
+ auto compilationUnitInfo = forStub ? testGen.baseBuildDatabase ->getClientCompilationUnitInfo (hintPath)
154
+ : testGen.buildDatabase ->getClientCompilationUnitInfo (hintPath);
132
155
auto command = compilationUnitInfo->command ;
133
156
auto srcFilePath = compilationUnitInfo->getSourcePath ();
134
157
std::string newCompilerPath = getUTBotClangCompilerPath (command.getCompiler ());
135
158
command.setCompiler (newCompilerPath);
136
159
137
160
srcFilePath = pathSubstitution.substituteLineFlag (srcFilePath);
138
161
if (CollectionUtils::contains (stubSources, srcFilePath)) {
139
- srcFilePath = Paths::sourcePathToStubPath (projectContext, srcFilePath);
162
+ srcFilePath = Paths::sourcePathToStubPath (testGen. projectContext , srcFilePath);
140
163
}
141
164
command.setSourcePath (srcFilePath);
142
165
143
- auto outFilePath = buildDatabase->getBitcodeFile (compilationUnitInfo->getOutputFile ());
166
+ auto outFilePath = forStub ? testGen.baseBuildDatabase ->getBitcodeFile (compilationUnitInfo->getOutputFile ())
167
+ : testGen.buildDatabase ->getBitcodeFile (compilationUnitInfo->getOutputFile ());
144
168
fs::create_directories (outFilePath.parent_path ());
145
169
command.setOutput (outFilePath);
146
170
command.setOptimizationLevel (" -O0" );
@@ -172,7 +196,7 @@ KleeGenerator::getCompileCommandsForKlee(const CollectionUtils::MapFileTo<fs::pa
172
196
std::vector<utbot::CompileCommand> compileCommands;
173
197
compileCommands.reserve (filesToBuild.size ());
174
198
for (const auto &[fileToBuild, bitcode] : filesToBuild) {
175
- auto optionalCommand = getCompileCommandForKlee (fileToBuild, stubSources, {});
199
+ auto optionalCommand = getCompileCommandForKlee (fileToBuild, stubSources, {}, false );
176
200
if (optionalCommand.has_value ()) {
177
201
auto command = std::move (optionalCommand).value ();
178
202
command.setOutput (bitcode);
@@ -188,8 +212,8 @@ Result<fs::path> KleeGenerator::defaultBuild(const fs::path &hintPath,
188
212
const fs::path &buildDirPath,
189
213
const std::vector<std::string> &flags) {
190
214
LOG_SCOPE_FUNCTION (DEBUG);
191
- auto bitcodeFilePath = buildDatabase->getBitcodeFile (sourceFilePath);
192
- auto optionalCommand = getCompileCommandForKlee (hintPath, {}, flags);
215
+ auto bitcodeFilePath = testGen. buildDatabase ->getBitcodeFile (sourceFilePath);
216
+ auto optionalCommand = getCompileCommandForKlee (hintPath, {}, flags, false );
193
217
if (!optionalCommand.has_value ()) {
194
218
std::string message = StringUtils::stringFormat (
195
219
" Couldn't get command for klee file: %s\n "
@@ -204,10 +228,10 @@ Result<fs::path> KleeGenerator::defaultBuild(const fs::path &hintPath,
204
228
printer::DefaultMakefilePrinter makefilePrinter;
205
229
auto commandWithChangingDirectory = utbot::CompileCommand (command, true );
206
230
makefilePrinter.declareTarget (" build" , {commandWithChangingDirectory.getSourcePath ()}, {commandWithChangingDirectory.toStringWithChangingDirectory ()});
207
- fs::path makefile = projectTmpPath / " BCForKLEE.mk" ;
231
+ fs::path makefile = testGen. serverBuildDir / " BCForKLEE.mk" ;
208
232
FileSystemUtils::writeToFile (makefile, makefilePrinter.ss .str ());
209
233
210
- auto makefileCommand = MakefileUtils::MakefileCommand (projectContext, makefile, " build" );
234
+ auto makefileCommand = MakefileUtils::MakefileCommand (testGen. projectContext , makefile, " build" );
211
235
auto [out, status, _] = makefileCommand.run ();
212
236
if (status != 0 ) {
213
237
LOG_S (ERROR) << " Compilation for " << sourceFilePath << " failed.\n "
@@ -233,10 +257,10 @@ fs::path KleeGenerator::writeKleeFile(
233
257
const std::function<bool (tests::Tests::MethodDescription const &)> &methodFilter) {
234
258
if (lineInfo) {
235
259
return kleePrinter.writeTmpKleeFile (
236
- tests, projectTmpPath , pathSubstitution, lineInfo->predicateInfo , lineInfo->methodName ,
260
+ tests, testGen. serverBuildDir , pathSubstitution, lineInfo->predicateInfo , lineInfo->methodName ,
237
261
lineInfo->scopeName , lineInfo->forMethod , lineInfo->forClass , methodFilter);
238
262
} else {
239
- return kleePrinter.writeTmpKleeFile (tests, projectTmpPath , pathSubstitution, std::nullopt,
263
+ return kleePrinter.writeTmpKleeFile (tests, testGen. serverBuildDir , pathSubstitution, std::nullopt,
240
264
" " , " " , false , false , methodFilter);
241
265
}
242
266
}
@@ -245,23 +269,23 @@ std::vector<fs::path> KleeGenerator::buildKleeFiles(const tests::TestsMap &tests
245
269
const std::shared_ptr<LineInfo> &lineInfo) {
246
270
std::vector<fs::path> outFiles;
247
271
LOG_S (DEBUG) << " Building generated klee files..." ;
248
- printer::KleePrinter kleePrinter (&typesHandler, buildDatabase, utbot::Language::UNKNOWN);
272
+ printer::KleePrinter kleePrinter (&typesHandler, testGen. buildDatabase , utbot::Language::UNKNOWN);
249
273
ExecUtils::doWorkWithProgress (
250
- testsMap, progressWriter, " Building generated klee files" ,
274
+ testsMap, testGen. progressWriter , " Building generated klee files" ,
251
275
[&](auto const &it) {
252
276
const auto &[filename, tests] = it;
253
277
if (lineInfo != nullptr && filename != lineInfo->filePath ) {
254
278
return ;
255
279
}
256
280
kleePrinter.srcLanguage = Paths::getSourceLanguage (filename);
257
281
std::vector<std::string> includeFlags = {
258
- StringUtils::stringFormat (" -I%s" , Paths::getFlagsDir (projectContext))};
282
+ StringUtils::stringFormat (" -I%s" , Paths::getFlagsDir (testGen. projectContext ))};
259
283
auto buildDirPath =
260
- buildDatabase->getClientCompilationUnitInfo (filename)->getDirectory ();
284
+ testGen. buildDatabase ->getClientCompilationUnitInfo (filename)->getDirectory ();
261
285
262
286
fs::path kleeFilePath = writeKleeFile (kleePrinter, tests, lineInfo);
263
287
auto kleeFilesInfo =
264
- buildDatabase->getClientCompilationUnitInfo (tests.sourceFilePath )->kleeFilesInfo ;
288
+ testGen. buildDatabase ->getClientCompilationUnitInfo (tests.sourceFilePath )->kleeFilesInfo ;
265
289
auto kleeBitcodeFile = defaultBuild (filename, kleeFilePath, buildDirPath, includeFlags);
266
290
if (kleeBitcodeFile.isSuccess ()) {
267
291
outFiles.emplace_back (kleeBitcodeFile.getOpt ().value ());
@@ -281,7 +305,7 @@ std::vector<fs::path> KleeGenerator::buildKleeFiles(const tests::TestsMap &tests
281
305
std::unordered_set<std::string> correctMethods;
282
306
for (const auto &[methodName, methodDescription] : tests.methods ) {
283
307
fs::path currentKleeFilePath = kleePrinter.writeTmpKleeFile (
284
- tests, projectTmpPath , pathSubstitution, std::nullopt,
308
+ tests, testGen. serverBuildDir , pathSubstitution, std::nullopt,
285
309
methodDescription.name ,
286
310
methodDescription.getClassName (),
287
311
true , false );
@@ -358,7 +382,11 @@ void KleeGenerator::parseKTestsToFinalCode(
358
382
}
359
383
360
384
std::shared_ptr<BuildDatabase> KleeGenerator::getBuildDatabase () const {
361
- return buildDatabase;
385
+ return testGen.buildDatabase ;
386
+ }
387
+
388
+ std::shared_ptr<BuildDatabase> KleeGenerator::getBaseBuildDatabase () const {
389
+ return testGen.baseBuildDatabase ;
362
390
}
363
391
364
392
void KleeGenerator::handleFailedFunctions (tests::TestsMap &testsMap) {
0 commit comments