Skip to content

Commit 5d659e6

Browse files
author
Vladislav Kalugin
committed
temp commit
1 parent 97851b1 commit 5d659e6

File tree

5 files changed

+69
-45
lines changed

5 files changed

+69
-45
lines changed

server/src/Server.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@ Status Server::TestsGenServiceImpl::PrintModulesContent(ServerContext *context,
569569

570570
fs::path serverBuildDir = Paths::getTmpDir(request->projectname());
571571
utbot::ProjectContext projectContext{ *request };
572-
std::shared_ptr<BuildDatabase> buildDatabase = BuildDatabase::create(projectContext);
572+
std::shared_ptr<BuildDatabase> buildDatabase = BuildDatabase::create(projectContext,
573+
GrpcUtils::UTBOT_AUTO_TARGET_PATH);
573574
StubSourcesFinder(buildDatabase).printAllModules();
574575
return Status::OK;
575576
}
@@ -644,7 +645,7 @@ Status Server::TestsGenServiceImpl::GetProjectTargets(ServerContext *context,
644645

645646
try {
646647
utbot::ProjectContext projectContext{ request->projectcontext() };
647-
auto buildDatabase = BuildDatabase::create(projectContext);
648+
auto buildDatabase = BuildDatabase::create(projectContext, GrpcUtils::UTBOT_AUTO_TARGET_PATH);
648649
auto targets = buildDatabase->getAllTargets();
649650
ProjectTargetsWriter targetsWriter{ response };
650651
targetsWriter.writeResponse(projectContext, targets);
@@ -666,7 +667,7 @@ Status Server::TestsGenServiceImpl::GetFileTargets(ServerContext *context,
666667

667668
try {
668669
utbot::ProjectContext projectContext{ request->projectcontext() };
669-
auto buildDatabase = BuildDatabase::create(projectContext);
670+
auto buildDatabase = BuildDatabase::create(projectContext, GrpcUtils::UTBOT_AUTO_TARGET_PATH);
670671
fs::path path = request->path();
671672
auto targets = buildDatabase->getTargetsForSourceFile(path);
672673
FileTargetsWriter targetsWriter{ response };

server/src/building/BuildDatabase.cpp

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <queue>
1616
#include <set>
1717
#include <unordered_map>
18+
#include <utils/GrpcUtils.h>
1819

1920
static std::string tryConvertOptionToPath(const std::string &possibleFilePath,
2021
const fs::path &dirPath) {
@@ -32,7 +33,8 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath,
3233

3334
BuildDatabase::BuildDatabase(const fs::path& buildCommandsJsonPath,
3435
fs::path serverBuildDir,
35-
utbot::ProjectContext projectContext)
36+
utbot::ProjectContext projectContext,
37+
const std::string &target)
3638
: serverBuildDir(std::move(serverBuildDir)), projectContext(std::move(projectContext)) {
3739
linkCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "link_commands.json");
3840
compileCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "compile_commands.json");
@@ -42,20 +44,23 @@ BuildDatabase::BuildDatabase(const fs::path& buildCommandsJsonPath,
4244
auto linkCommandsJson = JsonUtils::getJsonFromFile(linkCommandsJsonPath);
4345
auto compileCommandsJson = JsonUtils::getJsonFromFile(compileCommandsJsonPath);
4446

45-
createClangCompileCommandsJson(buildCommandsJsonPath, compileCommandsJson);
47+
initObjects(compileCommandsJson);
4648
initInfo(linkCommandsJson);
4749
filterInstalledFiles();
4850
addLocalSharedLibraries();
4951
fillTargetInfoParents();
52+
createClangCompileCommandsJson(target, buildCommandsJsonPath);
53+
//filter
5054
}
5155

52-
std::shared_ptr<BuildDatabase> BuildDatabase::create(const utbot::ProjectContext &projectContext) {
56+
std::shared_ptr<BuildDatabase> BuildDatabase::create(const utbot::ProjectContext &projectContext,
57+
const std::string &target) {
5358
fs::path compileCommandsJsonPath =
54-
CompilationUtils::substituteRemotePathToCompileCommandsJsonPath(
55-
projectContext.projectPath, projectContext.buildDirRelativePath);
59+
CompilationUtils::substituteRemotePathToCompileCommandsJsonPath(
60+
projectContext.projectPath, projectContext.buildDirRelativePath);
5661
fs::path serverBuildDir = Paths::getTmpDir(projectContext.projectName);
5762
std::shared_ptr<BuildDatabase> buildDatabase =
58-
std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
63+
std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext, target);
5964
return buildDatabase;
6065
}
6166

@@ -78,10 +83,8 @@ fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::sh
7883
}
7984
}
8085

81-
void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommandsJsonPath,
82-
const nlohmann::json &compileCommandsJson) {
83-
CollectionUtils::MapFileTo<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> fileCompileCommands;
84-
for (auto const& compileCommand: compileCommandsJson) {
86+
void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) {
87+
for (const nlohmann::json &compileCommand: compileCommandsJson) {
8588
auto objectInfo = std::make_shared<ObjectFileInfo>();
8689

8790
fs::path directory = compileCommand.at("directory").get<std::string>();
@@ -103,11 +106,12 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands
103106
objectInfo->command.removeWerror();
104107
fs::path outputFile = objectInfo->getOutputFile();
105108
fs::path kleeFilePathTemplate =
106-
Paths::createNewDirForFile(sourceFile, projectContext.buildDir, serverBuildDir);
109+
Paths::createNewDirForFile(sourceFile, projectContext.buildDir, serverBuildDir);
107110
fs::path kleeFile = Paths::addSuffix(kleeFilePathTemplate, "_klee");
108111
objectInfo->kleeFilesInfo = std::make_shared<KleeFilesInfo>(kleeFile);
109112

110-
if (CollectionUtils::containsKey(objectFileInfos, outputFile) || CollectionUtils::containsKey(targetInfos, outputFile)) {
113+
if (CollectionUtils::containsKey(objectFileInfos, outputFile) ||
114+
CollectionUtils::containsKey(targetInfos, outputFile)) {
111115
/*
112116
* If the condition above is true, that means that the output file
113117
* is built from multiple sources. Hence, it is not an object file,
@@ -129,9 +133,9 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands
129133
//create targetInfo
130134
targetInfo = targetInfos[outputFile] = std::make_shared<TargetInfo>();
131135
targetInfo->commands.emplace_back(
132-
std::initializer_list<std::string>{ targetObjectInfo->command.getCompiler(),
133-
"-o", outputFile, tmpObjectFileName },
134-
directory);
136+
std::initializer_list<std::string>{targetObjectInfo->command.getCompiler(),
137+
"-o", outputFile, tmpObjectFileName},
138+
directory);
135139
targetInfo->addFile(tmpObjectFileName);
136140
}
137141
//redirect new compilation command to temporary file
@@ -143,22 +147,13 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands
143147
} else {
144148
objectFileInfos[outputFile] = objectInfo;
145149
}
150+
compileCommands_temp.emplace_back(compileCommand, objectInfo);
146151
const fs::path &sourcePath = objectInfo->getSourcePath();
147-
if (!CollectionUtils::containsKey(sourceFileInfos, sourcePath) ||
148-
conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second)) {
149-
fileCompileCommands[sourcePath] = { compileCommand, objectInfo };
150-
}
151152
sourceFileInfos[sourcePath].emplace_back(objectInfo);
152153
}
153154
for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
154155
std::sort(objectInfos.begin(), objectInfos.end(), conflictPriorityMore);
155156
}
156-
nlohmann::json compileCommandsSingleFilesJson;
157-
for (const auto &compileCommand: fileCompileCommands) {
158-
compileCommandsSingleFilesJson.push_back(compileCommand.second.first);
159-
}
160-
fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath);
161-
JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson);
162157
}
163158

164159
void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) {
@@ -204,6 +199,27 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) {
204199
}
205200
}
206201

202+
void BuildDatabase::createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath) {
203+
CollectionUtils::MapFileTo<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> fileCompileCommands;
204+
for (const auto &[compileCommand, objectInfo]: compileCommands_temp) {
205+
const fs::path &sourcePath = objectInfo->getSourcePath();
206+
if ((targetInfos[target]->files.contains(objectInfo->getOutputFile()) ||
207+
target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) &&
208+
(!CollectionUtils::containsKey(fileCompileCommands, sourcePath) ||
209+
conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second))) {
210+
fileCompileCommands[sourcePath] = {compileCommand, objectInfo};
211+
}
212+
}
213+
214+
nlohmann::json compileCommandsSingleFilesJson;
215+
for (const auto &compileCommand: fileCompileCommands) {
216+
compileCommandsSingleFilesJson.push_back(compileCommand.second.first);
217+
}
218+
219+
fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath);
220+
JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson);
221+
}
222+
207223
void BuildDatabase::mergeLibraryOptions(std::vector<std::string> &jsonArguments) const {
208224
for (auto it = jsonArguments.begin(); it != jsonArguments.end(); it++) {
209225
if (*it == DynamicLibraryUtils::libraryDirOption || *it == DynamicLibraryUtils::linkFlag) {
@@ -468,7 +484,8 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const {
468484
throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string());
469485
}
470486
return sourceFileInfos.at(filepath)[0];
471-
} if (Paths::isObjectFile(filepath)) {
487+
}
488+
if (Paths::isObjectFile(filepath)) {
472489
if (!CollectionUtils::contains(objectFileInfos, filepath)) {
473490
throw CompilationDatabaseException("File not found in compilation_commands.json: " + filepath.string());
474491
}
@@ -477,8 +494,7 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const {
477494
throw CompilationDatabaseException("File is not a compilation unit or an object file: " + filepath.string());
478495
}
479496

480-
std::shared_ptr<const BuildDatabase::TargetInfo>
481-
BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const {
497+
std::shared_ptr<const BuildDatabase::TargetInfo> BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const {
482498
if (Paths::isSourceFile(filepath)) {
483499
auto compilationInfo = getClientCompilationUnitInfo(filepath);
484500
return targetInfos.at(compilationInfo->linkUnit);
@@ -491,8 +507,8 @@ BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const {
491507
}
492508

493509
bool BuildDatabase::conflictPriorityMore(
494-
const std::shared_ptr<BuildDatabase::ObjectFileInfo> &left,
495-
const std::shared_ptr<BuildDatabase::ObjectFileInfo> &right) {
510+
const std::shared_ptr<BuildDatabase::ObjectFileInfo> &left,
511+
const std::shared_ptr<BuildDatabase::ObjectFileInfo> &right) {
496512
if (StringUtils::contains(left->getOutputFile().string(), "64")) {
497513
return true;
498514
}
@@ -675,6 +691,7 @@ std::shared_ptr<BuildDatabase::TargetInfo> BuildDatabase::getPriorityTarget() co
675691
});
676692
return *it;
677693
}
694+
678695
fs::path BuildDatabase::newDirForFile(const fs::path &file) const {
679696
fs::path base = Paths::longestCommonPrefixPath(this->projectContext.buildDir,
680697
this->projectContext.projectPath);

server/src/building/BuildDatabase.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <unordered_set>
1616
#include <variant>
1717
#include <vector>
18+
#include <vector>
1819

1920
class BuildDatabase {
2021
public:
@@ -101,9 +102,11 @@ class BuildDatabase {
101102
public:
102103
BuildDatabase(const fs::path &buildCommandsJsonPath,
103104
fs::path serverBuildDir,
104-
utbot::ProjectContext projectContext);
105+
utbot::ProjectContext projectContext,
106+
const std::string &target);
105107

106-
static std::shared_ptr<BuildDatabase> create(const utbot::ProjectContext &projectContext);
108+
static std::shared_ptr<BuildDatabase> create(const utbot::ProjectContext &projectContext,
109+
const std::string &target);
107110

108111
const fs::path &getCompileCommandsJson();
109112
const fs::path &getLinkCommandsJson();
@@ -212,20 +215,22 @@ class BuildDatabase {
212215
std::unordered_map<std::shared_ptr<const BuildDatabase::TargetInfo>, CollectionUtils::FileSet>
213216
linkUnitToStubFiles;
214217

218+
std::vector<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> compileCommands_temp;
219+
std::vector<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> linkCommands_temp;
220+
215221
static bool conflictPriorityMore(const std::shared_ptr<ObjectFileInfo> &left,
216222
const std::shared_ptr<ObjectFileInfo> &right);
217223

218224
void filterInstalledFiles();
219225
void addLocalSharedLibraries();
220226
void fillTargetInfoParents();
221227
static fs::path getCorrespondingBitcodeFile(const fs::path &filepath);
222-
void createClangCompileCommandsJson(const fs::path &buildCommandsJsonPath,
223-
const nlohmann::json &compileCommandsJson);
228+
void initObjects(const nlohmann::json &compileCommandsJson);
224229
void initInfo(const nlohmann::json &linkCommandsJson);
230+
void createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath);
225231
void mergeLibraryOptions(std::vector<std::string> &jsonArguments) const;
226232
fs::path newDirForFile(fs::path const& file) const;
227-
fs::path
228-
createExplicitObjectFileCompilationCommand(const std::shared_ptr<ObjectFileInfo> &objectInfo);
233+
fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr<ObjectFileInfo> &objectInfo);
229234

230235
using sharedLibrariesMap = std::unordered_map<std::string, CollectionUtils::MapFileTo<fs::path>>;
231236

server/src/testgens/ProjectTestGen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ ProjectTestGen::ProjectTestGen(const testsgen::ProjectRequest &request,
1515
testMode), request(&request) {
1616
fs::create_directories(projectContext.testDirPath);
1717
compileCommandsJsonPath = CompilationUtils::substituteRemotePathToCompileCommandsJsonPath(
18-
projectContext.projectPath, projectContext.buildDirRelativePath);
19-
buildDatabase =
20-
std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
18+
projectContext.projectPath, projectContext.buildDirRelativePath);
19+
buildDatabase = std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext,
20+
request.targetpath());
2121
compilationDatabase = CompilationUtils::getCompilationDatabase(compileCommandsJsonPath);
2222
if (autoDetect) {
2323
autoDetectSourcePathsIfNotEmpty();

server/src/testgens/SnippetTestGen.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ SnippetTestGen::SnippetTestGen(const testsgen::SnippetRequest &request,
1111
progressWriter,
1212
testMode) {
1313
filePath = fs::weakly_canonical(request.filepath());
14-
sourcePaths = { filePath };
14+
sourcePaths = {filePath};
1515
testingMethodsSourcePaths = sourcePaths;
1616
printer::CCJsonPrinter::createDummyBuildDB(sourcePaths, serverBuildDir);
1717
compileCommandsJsonPath = serverBuildDir;
18-
utbot::ProjectContext projectContext{ request, serverBuildDir };
18+
utbot::ProjectContext projectContext{request, serverBuildDir};
1919
buildDatabase =
20-
std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
20+
std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext,
21+
GrpcUtils::UTBOT_AUTO_TARGET_PATH);
2122
compilationDatabase = CompilationUtils::getCompilationDatabase(serverBuildDir);
2223
setTargetForSource(filePath);
2324
setInitializedTestsMap();

0 commit comments

Comments
 (0)