Skip to content

Commit 73b4689

Browse files
author
Vladislav Kalugin
committed
refactoring
1 parent 1329d88 commit 73b4689

File tree

9 files changed

+26
-124
lines changed

9 files changed

+26
-124
lines changed

server/src/KleeGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class KleeGenerator {
3535
public:
3636
/**
3737
* @brief Also creates tmp directories for build files.
38-
* @param _testGen contains context.
38+
* @param _testGen contains projectContext, settingsContext, BuildDatabase.
3939
* @param typesHandler provides additional information about types.
4040
* @param filePathsSubstitution Mapping from source file path to modified file. Required for
4141
* line test generation requests.

server/src/building/BuildDatabase.cpp

Lines changed: 19 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -56,40 +56,15 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath,
5656
target = GrpcUtils::UTBOT_AUTO_TARGET_PATH;
5757
}
5858

59-
BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase,
59+
BuildDatabase::BuildDatabase(BuildDatabase &baseBuildDatabase,
6060
const std::string &_target) :
6161
serverBuildDir(baseBuildDatabase.serverBuildDir),
6262
projectContext(baseBuildDatabase.projectContext),
6363
buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath),
6464
linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath),
6565
compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath),
66-
isAutoTarget(false) {
67-
68-
// BuildDatabase baseBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, false);
69-
70-
71-
// fs::path target;
72-
//TODO target incorrect name now
73-
if (Paths::isSourceFile(_target)) {
74-
fs::path root = baseBuildDatabase.getRootForSource(_target);
75-
target = root;
76-
} else if (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _target.empty()) {
77-
fs::path root = baseBuildDatabase.getRootForFirstSource();
78-
target = root;
79-
isAutoTarget = true;
80-
} else {
81-
auto new_target = GenerationUtils::findTarget(baseBuildDatabase.getAllTargets(), _target);
82-
if (new_target.has_value()) {
83-
target = new_target.value();
84-
} else {
85-
throw CompilationDatabaseException("Can't find target: " + _target);
86-
}
87-
}
88-
89-
90-
// CollectionUtils::MapFileTo<std::vector<std::shared_ptr<ObjectFileInfo>>> sourceFileInfos;
91-
// CollectionUtils::MapFileTo<std::shared_ptr<ObjectFileInfo>> objectFileInfos;
92-
// CollectionUtils::MapFileTo<std::vector<fs::path>> objectFileTargets;
66+
target(_target),
67+
isAutoTarget(_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
9368
{
9469
auto objectFilesList = baseBuildDatabase.getArchiveObjectFiles(target);
9570
for (const auto &objectFilePath: objectFilesList) {
@@ -103,23 +78,32 @@ BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase,
10378
baseBuildDatabase.objectFileTargets[objectFileInfo->getOutputFile()];
10479
}
10580
}
106-
// CollectionUtils::MapFileTo<std::shared_ptr<TargetInfo>> targetInfos;
107-
// CollectionUtils::MapFileTo<CollectionUtils::FileSet> linkUnitToStubFiles;
81+
10882
{
10983
auto targetFilesList = baseBuildDatabase.getArchiveTargetFiles(target);
11084
for (const auto &objectFilePath: targetFilesList) {
11185
targetInfos[objectFilePath] = baseBuildDatabase.targetInfos[objectFilePath];
112-
linkUnitToStubFiles[objectFilePath] = baseBuildDatabase.linkUnitToStubFiles[objectFilePath];
11386
}
11487
}
11588

116-
// std::vector<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> compileCommands_temp;
11789
compileCommands_temp = baseBuildDatabase.compileCommands_temp;
118-
11990
createClangCompileCommandsJson();
12091
}
12192

122-
std::shared_ptr<BuildDatabase> BuildDatabase::createBaseForTarget(const std::string &_target) {
93+
std::shared_ptr<BuildDatabase> BuildDatabase::createBuildDatabaseForSourceOrTarget(const std::string &_targetOrSourcePath) {
94+
fs::path _target;
95+
if (Paths::isSourceFile(_targetOrSourcePath)) {
96+
_target = getRootForSource(_targetOrSourcePath);
97+
} else if (_targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _targetOrSourcePath.empty()) {
98+
_target = getRootForFirstSource();
99+
} else {
100+
auto new_target = GenerationUtils::findTarget(getAllTargets(), _targetOrSourcePath);
101+
if (new_target.has_value()) {
102+
_target = new_target.value();
103+
} else {
104+
throw CompilationDatabaseException("Can't find target: " + _targetOrSourcePath);
105+
}
106+
}
123107
return std::make_shared<BuildDatabase>(std::move(BuildDatabase(*this, _target)));
124108
}
125109

@@ -220,8 +204,7 @@ void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) {
220204
sourceFileInfos[sourcePath].emplace_back(objectInfo);
221205
}
222206
for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
223-
//Need stable sort for save order of 32 and 64 bits files
224-
std::stable_sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore);
207+
std::sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore);
225208
}
226209
}
227210

@@ -292,27 +275,6 @@ void BuildDatabase::createClangCompileCommandsJson() {
292275
compilationDatabase = CompilationUtils::getCompilationDatabase(clangCompileCommandsJsonPath);
293276
}
294277

295-
//void BuildDatabase::updateTarget(const fs::path &_target) {
296-
// if (_target.string() == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
297-
// return;
298-
// }
299-
//
300-
// for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
301-
// std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr<ObjectFileInfo> &left,
302-
// const std::shared_ptr<ObjectFileInfo> &right) {
303-
// if (CollectionUtils::containsKey(targetInfos, target)) {
304-
// if (CollectionUtils::containsKey(targetInfos[target]->files, left->getOutputFile())) {
305-
// return true;
306-
// }
307-
// if (CollectionUtils::containsKey(targetInfos[target]->files, right->getOutputFile())) {
308-
// return false;
309-
// }
310-
// }
311-
// return false;
312-
// });
313-
// }
314-
//}
315-
316278
void BuildDatabase::mergeLibraryOptions(std::vector<std::string> &jsonArguments) const {
317279
for (auto it = jsonArguments.begin(); it != jsonArguments.end(); it++) {
318280
if (*it == DynamicLibraryUtils::libraryDirOption || *it == DynamicLibraryUtils::linkFlag) {
@@ -718,21 +680,6 @@ fs::path BuildDatabase::TargetInfo::getOutput() const {
718680
return commands[0].getOutput();
719681
}
720682

721-
//CollectionUtils::FileSet BuildDatabase::getStubFiles(
722-
// const std::shared_ptr<const BuildDatabase::TargetInfo> &linkUnitInfo) const {
723-
// auto iterator = linkUnitToStubFiles.find(linkUnitInfo->getOutput());
724-
// if (iterator != linkUnitToStubFiles.end()) {
725-
// return iterator->second;
726-
// }
727-
// return {};
728-
//}
729-
730-
void BuildDatabase::assignStubFilesToLinkUnit(
731-
std::shared_ptr<const BuildDatabase::TargetInfo> linkUnitInfo,
732-
CollectionUtils::FileSet stubs) {
733-
linkUnitToStubFiles.emplace(linkUnitInfo->getOutput(), std::move(stubs));
734-
}
735-
736683
std::vector<std::shared_ptr<BuildDatabase::TargetInfo>> BuildDatabase::getRootTargets() const {
737684
return CollectionUtils::filterOut(
738685
CollectionUtils::getValues(targetInfos),

server/src/building/BuildDatabase.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class BuildDatabase {
100100
utbot::ProjectContext _projectContext);
101101

102102
static std::shared_ptr<BuildDatabase> create(const utbot::ProjectContext &projectContext);
103-
std::shared_ptr<BuildDatabase> createBaseForTarget(const std::string &target);
103+
std::shared_ptr<BuildDatabase> createBuildDatabaseForSourceOrTarget(const std::string &_targetOrSourcePath);
104104

105105
const fs::path &getCompileCommandsJson();
106106
const fs::path &getLinkCommandsJson();
@@ -196,16 +196,6 @@ class BuildDatabase {
196196
*/
197197
std::vector<std::shared_ptr<ObjectFileInfo>> getAllCompileCommands() const;
198198

199-
/**
200-
* @brief Assign set of file paths to stubs to given link unit
201-
*
202-
* @param linkUnitInfo link unit info (preferably library)
203-
* @param stubs set of file paths to stubs
204-
*/
205-
void assignStubFilesToLinkUnit(
206-
std::shared_ptr<const BuildDatabase::TargetInfo> linkUnitInfo,
207-
CollectionUtils::FileSet stubs);
208-
209199
std::vector<std::shared_ptr<TargetInfo>> getRootTargets() const;
210200
std::vector<std::shared_ptr<TargetInfo>> getAllTargets() const;
211201

@@ -239,7 +229,6 @@ class BuildDatabase {
239229
CollectionUtils::MapFileTo<std::shared_ptr<ObjectFileInfo>> objectFileInfos;
240230
CollectionUtils::MapFileTo<std::shared_ptr<TargetInfo>> targetInfos;
241231
CollectionUtils::MapFileTo<std::vector<fs::path>> objectFileTargets;
242-
CollectionUtils::MapFileTo<CollectionUtils::FileSet> linkUnitToStubFiles;
243232

244233

245234
std::vector<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> compileCommands_temp;

server/src/building/Linker.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ void Linker::linkForOneFile(const fs::path &sourceFilePath) {
106106
auto [targetBitcode, stubsSet, _] = result.getOpt().value();
107107
addToGenerated({ objectFile }, targetBitcode);
108108
auto&& targetUnitInfo = testGen.buildDatabase->getClientLinkUnitInfo(target);
109-
testGen.buildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, stubsSet);
110109
return;
111110
} else {
112111
LOG_S(DEBUG) << "Linkage for target " << target.filename() << " failed: " << result.getError()->c_str();
@@ -193,7 +192,6 @@ void Linker::linkForProject() {
193192
});
194193
addToGenerated(objectFiles, linkres.bitcodeOutput);
195194
auto &&targetUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(target);
196-
testGen.baseBuildDatabase->assignStubFilesToLinkUnit(targetUnitInfo, linkres.stubsSet);
197195
break;
198196
} else {
199197
std::stringstream ss;

server/src/testgens/BaseTestGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void BaseTestGen::setInitializedTestsMap() {
4646

4747
void BaseTestGen::setTargetPath(fs::path _targetPath) {
4848
if (buildDatabase->hasAutoTarget() && buildDatabase->getTargetPath() != _targetPath) {
49-
buildDatabase = std::move(baseBuildDatabase->createBaseForTarget(_targetPath));
49+
buildDatabase = std::move(baseBuildDatabase->createBuildDatabaseForSourceOrTarget(_targetPath));
5050
updateTargetSources(_targetPath);
5151
}
5252
}

server/src/testgens/ProjectTestGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request,
1717
compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath(
1818
projectContext.projectPath, projectContext.buildDirRelativePath);
1919
baseBuildDatabase = std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
20-
buildDatabase = baseBuildDatabase->createBaseForTarget(request.targetpath());
20+
buildDatabase = baseBuildDatabase->createBuildDatabaseForSourceOrTarget(request.targetpath());
2121
if (autoSrcPaths) {
2222
autoDetectSourcePathsIfNotEmpty();
2323
} else {

server/src/testgens/SnippetTestGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request,
1919
compileCommandsJsonPath = serverBuildDir;
2020
utbot::ProjectContext projectContext{request, serverBuildDir};
2121
baseBuildDatabase = std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
22-
buildDatabase = baseBuildDatabase->createBaseForTarget(serverBuildDir / SNIPPET_TARGET);
22+
buildDatabase = baseBuildDatabase->createBuildDatabaseForSourceOrTarget(serverBuildDir / SNIPPET_TARGET);
2323
setTargetForSource(filePath);
2424
setInitializedTestsMap();
2525
}

server/test/framework/KleeGen_Tests.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ namespace {
1515

1616
struct TestSuite {
1717
std::string name;
18-
fs::path buildPath;
1918
CollectionUtils::FileSet sourcesFilePaths;
2019
};
2120

22-
fs::path tmpDirPath = baseSuitePath / buildDirRelativePath;
2321
TestSuite testSuite;
2422

2523
void SetUp() override {
2624
clearEnv(CompilationUtils::CompilerName::CLANG);
2725

2826
testSuite = { suiteName,
29-
buildPath,
3027
{ getTestFilePath("assertion_failures.c"),
3128
getTestFilePath("basic_functions.c"),
3229
getTestFilePath("complex_structs.c"),
@@ -43,27 +40,6 @@ namespace {
4340
getTestFilePath("types.c"),
4441
getTestFilePath("inner/inner_basic_functions.c") } };
4542
}
46-
47-
// KleeGenerator initKleeGenerator(const TestSuite &suite, std::string &errorMessage) {
48-
// types::TypesHandler::SizeContext sizeContext;
49-
// types::TypeMaps typeMaps;
50-
// types::TypesHandler typesHandler(typeMaps, sizeContext);
51-
// fs::path testsDirPath = tmpDirPath / "test";
52-
// auto projectContext = GrpcUtils::createProjectContext(suite.name, suitePath, testsDirPath, buildDirRelativePath);
53-
// auto settingsContext = GrpcUtils::createSettingsContext(true, true, 15, 0, true, false);
54-
//
55-
// auto request = GrpcUtils::createProjectRequest(std::move(projectContext),
56-
// std::move(settingsContext),
57-
// srcPaths,
58-
// GrpcUtils::UTBOT_AUTO_TARGET_PATH);
59-
//
60-
// auto request = testUtils::createProjectRequest(suite.name, suitePath, buildDirRelativePath, {});
61-
//
62-
// auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE);
63-
//
64-
// KleeGenerator generator(testGen, typesHandler, {});
65-
// return generator;
66-
// }
6743
};
6844

6945
TEST_F(KleeGen_Test, BuildByCDb) {

server/test/framework/Stub_Tests.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "streams/stubs/ServerStubsWriter.h"
1111

1212
#include <fstream>
13+
#include <stubs/StubGen.h>
14+
#include <Synchronizer.h>
1315

1416
namespace {
1517
using testUtils::createFileRequest;
@@ -166,16 +168,6 @@ namespace {
166168
Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get());
167169
ASSERT_TRUE(status.ok()) << status.error_message();
168170
EXPECT_EQ(testUtils::getNumberOfTests(testGen.tests), 2);
169-
170-
// auto root = testGen.baseBuildDatabase->getRootForSource(foreign_bar_c);
171-
// auto linkUnitInfo = testGen.baseBuildDatabase->getClientLinkUnitInfo(root);
172-
// auto stubFiles = testGen.baseBuildDatabase->getStubFiles(linkUnitInfo);
173-
// auto stubCandidates = { calc_sum_c };
174-
// auto expectedStubFiles = CollectionUtils::transformTo<decltype(stubFiles)>(
175-
// stubCandidates, [&testGen](fs::path const &path) {
176-
// return Paths::sourcePathToStubPath(testGen.projectContext, path);
177-
// });
178-
// EXPECT_EQ(expectedStubFiles, stubFiles);
179171
}
180172

181173
TEST_F(Stub_Test, File_Tests_With_Stubs) {

0 commit comments

Comments
 (0)