summaryrefslogtreecommitdiffstats
path: root/clangd/GlobalCompilationDatabase.cpp
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2017-12-04 10:08:45 +0000
committerSam McCall <sam.mccall@gmail.com>2017-12-04 10:08:45 +0000
commitaa49372548ff984ae9c48879a0d05833538a76b3 (patch)
tree3a2dade0d624e66f39f689c8ab7bcc7e915c0078 /clangd/GlobalCompilationDatabase.cpp
parent68be76534dd276c6c849c367b2f7d2495cfdd540 (diff)
[clangd] GlobalCompilationDatabase interface changes
Summary: - GlobalCompilationDatabase now returns a single command (that's all we use) - fallback flags are now part of the GlobalCompilationDatabase. There's a default implementation that they can optionally customize. - this allows us to avoid invoking the fallback logic on two separate codepaths - race on extra flags fixed by locking the mutex - made GCD const-correct (DBGCD does have mutating methods) Reviewers: hokein Subscribers: klimek, cfe-commits, ilya-biryukov Differential Revision: https://reviews.llvm.org/D40733 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@319647 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clangd/GlobalCompilationDatabase.cpp')
-rw-r--r--clangd/GlobalCompilationDatabase.cpp75
1 files changed, 40 insertions, 35 deletions
diff --git a/clangd/GlobalCompilationDatabase.cpp b/clangd/GlobalCompilationDatabase.cpp
index c6dc9afb..8767b2d9 100644
--- a/clangd/GlobalCompilationDatabase.cpp
+++ b/clangd/GlobalCompilationDatabase.cpp
@@ -16,24 +16,11 @@
namespace clang {
namespace clangd {
-static void addExtraFlags(tooling::CompileCommand &Command,
- const std::vector<std::string> &ExtraFlags) {
- if (ExtraFlags.empty())
- return;
- assert(Command.CommandLine.size() >= 2 &&
- "Expected a command line containing at least 2 arguments, the "
- "compiler binary and the output file");
- // The last argument of CommandLine is the name of the input file.
- // Add ExtraFlags before it.
- auto It = Command.CommandLine.end();
- --It;
- Command.CommandLine.insert(It, ExtraFlags.begin(), ExtraFlags.end());
-}
-
-tooling::CompileCommand getDefaultCompileCommand(PathRef File) {
- std::vector<std::string> CommandLine{"clang", File.str()};
+tooling::CompileCommand
+GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
return tooling::CompileCommand(llvm::sys::path::parent_path(File),
- llvm::sys::path::filename(File), CommandLine,
+ llvm::sys::path::filename(File),
+ {"clang", File.str()},
/*Output=*/"");
}
@@ -42,33 +29,50 @@ DirectoryBasedGlobalCompilationDatabase::
clangd::Logger &Logger, llvm::Optional<Path> CompileCommandsDir)
: Logger(Logger), CompileCommandsDir(std::move(CompileCommandsDir)) {}
-std::vector<tooling::CompileCommand>
-DirectoryBasedGlobalCompilationDatabase::getCompileCommands(PathRef File) {
- std::vector<tooling::CompileCommand> Commands;
-
- auto CDB = getCompilationDatabase(File);
- if (CDB)
- Commands = CDB->getCompileCommands(File);
- if (Commands.empty())
- Commands.push_back(getDefaultCompileCommand(File));
-
- auto It = ExtraFlagsForFile.find(File);
- if (It != ExtraFlagsForFile.end()) {
- // Append the user-specified flags to the compile commands.
- for (tooling::CompileCommand &Command : Commands)
- addExtraFlags(Command, It->second);
+llvm::Optional<tooling::CompileCommand>
+DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
+ if (auto CDB = getCompilationDatabase(File)) {
+ auto Candidates = CDB->getCompileCommands(File);
+ if (!Candidates.empty()) {
+ addExtraFlags(File, Candidates.front());
+ return std::move(Candidates.front());
+ }
}
+ return llvm::None;
+}
- return Commands;
+tooling::CompileCommand
+DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
+ PathRef File) const {
+ auto C = GlobalCompilationDatabase::getFallbackCommand(File);
+ addExtraFlags(File, C);
+ return C;
}
void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
PathRef File, std::vector<std::string> ExtraFlags) {
+ std::lock_guard<std::mutex> Lock(Mutex);
ExtraFlagsForFile[File] = std::move(ExtraFlags);
}
+void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
+ PathRef File, tooling::CompileCommand &C) const {
+ std::lock_guard<std::mutex> Lock(Mutex);
+
+ auto It = ExtraFlagsForFile.find(File);
+ if (It == ExtraFlagsForFile.end())
+ return;
+
+ auto &Args = C.CommandLine;
+ assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
+ // The last argument of CommandLine is the name of the input file.
+ // Add ExtraFlags before it.
+ Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
+}
+
tooling::CompilationDatabase *
-DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(PathRef File) {
+DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(
+ PathRef File) const {
namespace path = llvm::sys::path;
auto CachedIt = CompilationDatabases.find(File);
@@ -91,7 +95,8 @@ DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(PathRef File) {
}
tooling::CompilationDatabase *
-DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) {
+DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(
+ PathRef File) const {
std::lock_guard<std::mutex> Lock(Mutex);
namespace path = llvm::sys::path;