12 #include "clang/Tooling/CompilationDatabase.h" 13 #include "llvm/Support/FileSystem.h" 14 #include "llvm/Support/Path.h" 19 tooling::CompileCommand
21 std::vector<std::string> Argv = {
"clang"};
24 if (llvm::sys::path::extension(File) ==
".h")
25 Argv.push_back(
"-xobjective-c++-header");
27 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
28 llvm::sys::path::filename(File),
36 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
41 llvm::Optional<tooling::CompileCommand>
43 if (
auto CDB = getCDBForFile(File)) {
44 auto Candidates = CDB->getCompileCommands(File);
45 if (!Candidates.empty()) {
46 addExtraFlags(File, Candidates.front());
47 return std::move(Candidates.front());
50 log(
"Failed to find compilation database for {0}", File);
55 tooling::CompileCommand
59 addExtraFlags(File, C);
64 std::lock_guard<std::mutex> Lock(Mutex);
65 CompileCommandsDir = P;
66 CompilationDatabases.clear();
70 PathRef File, std::vector<std::string> ExtraFlags) {
71 std::lock_guard<std::mutex> Lock(Mutex);
72 ExtraFlagsForFile[
File] = std::move(ExtraFlags);
75 void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
77 std::lock_guard<std::mutex> Lock(Mutex);
79 auto It = ExtraFlagsForFile.find(File);
80 if (It == ExtraFlagsForFile.end())
83 auto &Args = C.CommandLine;
84 assert(Args.size() >= 2 &&
"Expected at least [compiler, source file]");
87 Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
90 tooling::CompilationDatabase *
91 DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(
PathRef Dir)
const {
93 auto CachedIt = CompilationDatabases.find(Dir);
94 if (CachedIt != CompilationDatabases.end())
95 return CachedIt->second.get();
96 std::string Error =
"";
97 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
99 CDB = tooling::inferMissingCompileCommands(std::move(CDB));
100 auto Result = CDB.get();
101 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
105 tooling::CompilationDatabase *
106 DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
PathRef File)
const {
107 namespace path = llvm::sys::path;
108 assert((path::is_absolute(File, path::Style::posix) ||
109 path::is_absolute(File, path::Style::windows)) &&
110 "path must be absolute");
112 std::lock_guard<std::mutex> Lock(Mutex);
115 for (
auto Path = path::parent_path(File); !
Path.empty();
117 if (
auto CDB = getCDBInDirLocked(
Path))
124 : InnerCDB(InnerCDB) {}
126 llvm::Optional<tooling::CompileCommand>
128 std::unique_lock<std::mutex> Lock(Mut);
129 auto It = Cached.find(File);
130 if (It != Cached.end())
134 llvm::Optional<tooling::CompileCommand>
Command =
137 return Cached.try_emplace(File, std::move(Command)).first->getValue();
140 tooling::CompileCommand
146 std::unique_lock<std::mutex> Lock(Mut);
151 std::unique_lock<std::mutex> Lock(Mut);
void invalidate(PathRef File)
Removes an entry for File if it's present in the cache.
CachingCompilationDb(const GlobalCompilationDatabase &InnerCDB)
void setExtraFlagsForFile(PathRef File, std::vector< std::string > ExtraFlags)
Sets the extra flags that should be added to a file.
virtual llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File) const =0
If there are any known-good commands for building this file, returns one.
llvm::StringRef PathRef
A typedef to represent a ref to file path.
~DirectoryBasedGlobalCompilationDatabase() override
Documents should not be synced at all.
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File) const override
Gets compile command for File from cache or CDB if it's not in the cache.
Provides compilation arguments used for parsing C and C++ files.
void setCompileCommandsDir(Path P)
Set the compile commands directory to P.
void log(const char *Fmt, Ts &&... Vals)
std::string Path
A typedef to represent a file path.
tooling::CompileCommand getFallbackCommand(PathRef File) const override
Uses the default fallback command, adding any extra flags.
static llvm::cl::opt< Path > CompileCommandsDir("compile-commands-dir", llvm::cl::desc("Specify a path to look for compile_commands.json. If path " "is invalid, clangd will look in the current directory and " "parent paths of each source file."))
DirectoryBasedGlobalCompilationDatabase(llvm::Optional< Path > CompileCommandsDir)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
tooling::CompileCommand getFallbackCommand(PathRef File) const override
Forwards to the inner CDB. Results of this function are not cached.
void clear()
Removes all cached compile commands.
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File) const override
Scans File's parents looking for compilation databases.
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const
Makes a guess at how to build a file.