Skip to content

Commit 7726291

Browse files
author
Vladislav Kalugin
committed
refactoring after review 4
1 parent e9eeb2d commit 7726291

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

server/src/building/BuildDatabase.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,17 @@ fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::sh
5959
}
6060

6161
void BuildDatabase::createClangCompileCommandsJson() {
62-
CollectionUtils::MapFileTo<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> fileCompileCommands;
63-
for (const auto &[compileCommand, objectInfo]: compileCommands_temp) {
64-
const fs::path &sourcePath = objectInfo->getSourcePath();
65-
if (CollectionUtils::contains(fileCompileCommands, sourcePath)) {
66-
LOG_S(WARNING) << "Multiple compile commands for file \"" << sourcePath
67-
<< "\" use command for \"" << objectInfo->getOutputFile() << "\"";
68-
} else if (CollectionUtils::contains(objectFileInfos, objectInfo->getOutputFile())) {
69-
fileCompileCommands[sourcePath] = {compileCommand, objectInfo};
70-
}
62+
CollectionUtils::MapFileTo<nlohmann::json> fileCompileCommands;
63+
for (const auto &[sourcePath, objectInfos]: sourceFileInfos) {
64+
const std::shared_ptr<ObjectFileInfo> &objectInfo = objectInfos.front();
65+
fileCompileCommands[sourcePath] = {{"directory", objectInfo->command.getDirectory()},
66+
{"command", objectInfo->command.toString()},
67+
{"file", objectInfo->command.getSourcePath()}};
7168
}
7269

7370
nlohmann::json compileCommandsSingleFilesJson;
7471
for (const auto &compileCommand: fileCompileCommands) {
75-
compileCommandsSingleFilesJson.push_back(compileCommand.second.first);
72+
compileCommandsSingleFilesJson.push_back(compileCommand.second);
7673
}
7774

7875
fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath);
@@ -334,6 +331,11 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const {
334331
throw CompilationDatabaseException("File is not a compilation unit or an object file: " + filepath.string());
335332
}
336333

334+
335+
[[nodiscard]] bool BuildDatabase::hasUnitInfo(const fs::path &filepath) const {
336+
return CollectionUtils::contains(sourceFileInfos, filepath) || CollectionUtils::contains(objectFileInfos, filepath);
337+
}
338+
337339
std::shared_ptr<const BuildDatabase::TargetInfo> BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const {
338340
if (Paths::isSourceFile(filepath)) {
339341
auto compilationInfo = getClientCompilationUnitInfo(filepath);
@@ -543,7 +545,3 @@ CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path &
543545
std::shared_ptr<BuildDatabase::TargetInfo> BuildDatabase::getTargetInfo(const fs::path &_target) {
544546
return targetInfos[_target];
545547
}
546-
547-
std::vector<std::pair<nlohmann::json, std::shared_ptr<BuildDatabase::ObjectFileInfo>>> BuildDatabase::getCompileCommands_temp() {
548-
return compileCommands_temp;
549-
}

server/src/building/BuildDatabase.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ class BuildDatabase {
158158
*/
159159
[[nodiscard]] std::shared_ptr<const ObjectFileInfo> getClientCompilationUnitInfo(const fs::path &filepath) const;
160160

161+
/**
162+
* @brief Returns true if BuildDatabase contains information for current source file or object file
163+
*
164+
* @param filepath Path to source file or object file
165+
* @return true if contains current source file or object file
166+
*/
167+
[[nodiscard]] bool hasUnitInfo(const fs::path &filepath) const;
168+
161169
/**
162170
* @brief Returns link command information for current executable or library
163171
*
@@ -220,9 +228,6 @@ class BuildDatabase {
220228

221229
std::shared_ptr<TargetInfo> getTargetInfo(const fs::path &_target);
222230

223-
//TODO change
224-
std::vector<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> getCompileCommands_temp();
225-
226231
virtual bool hasAutoTarget() const = 0;
227232

228233
virtual fs::path getTargetPath() const = 0;
@@ -240,9 +245,6 @@ class BuildDatabase {
240245
CollectionUtils::MapFileTo<std::shared_ptr<TargetInfo>> targetInfos;
241246
CollectionUtils::MapFileTo<std::vector<fs::path>> objectFileTargets;
242247

243-
//TODO change
244-
std::vector<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> compileCommands_temp;
245-
246248
BuildDatabase(
247249
fs::path serverBuildDir,
248250
fs::path buildCommandsJsonPath,

server/src/building/ProjectBuildDatabse.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ void ProjectBuildDatabase::initObjects(const nlohmann::json &compileCommandsJson
118118
} else {
119119
objectFileInfos[outputFile] = objectInfo;
120120
}
121-
compileCommands_temp.emplace_back(compileCommand, objectInfo);
122121
const fs::path &sourcePath = objectInfo->getSourcePath();
123122
sourceFileInfos[sourcePath].emplace_back(objectInfo);
124123
}

server/src/building/TargetBuildDatabase.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ TargetBuildDatabase::TargetBuildDatabase(BuildDatabase *baseBuildDatabase,
3030
}
3131
}
3232

33-
compileCommands_temp = baseBuildDatabase->getCompileCommands_temp();
3433
createClangCompileCommandsJson();
3534
}
3635

server/src/testgens/BaseTestGen.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,11 @@ std::shared_ptr<TargetBuildDatabase> BaseTestGen::getTargetBuildDatabase() {
8181
std::shared_ptr<const BuildDatabase::ObjectFileInfo>
8282
BaseTestGen::getClientCompilationUnitInfo(const fs::path &path, bool fullProject) const {
8383
std::shared_ptr<const BuildDatabase::ObjectFileInfo> objectFileInfo;
84-
try {
84+
if (targetBuildDatabase->hasUnitInfo(path) || !fullProject) {
8585
objectFileInfo = targetBuildDatabase->getClientCompilationUnitInfo(path);
86-
} catch (CompilationDatabaseException &e) {
87-
if (fullProject) {
88-
objectFileInfo = projectBuildDatabase->getClientCompilationUnitInfo(path);
89-
LOG_S(WARNING) << "Can't find in target: " << path;
90-
} else {
91-
throw CompilationDatabaseException(e.what());
92-
}
86+
} else {
87+
objectFileInfo = projectBuildDatabase->getClientCompilationUnitInfo(path);
88+
LOG_S(WARNING) << "Can't find in target: " << path;
9389
}
9490
return objectFileInfo;
9591
}

0 commit comments

Comments
 (0)