12 #include "clang/Frontend/CompilerInvocation.h" 13 #include "clang/Tooling/ArgumentsAdjusters.h" 14 #include "clang/Tooling/CompilationDatabase.h" 15 #include "llvm/ADT/Optional.h" 16 #include "llvm/Support/FileSystem.h" 17 #include "llvm/Support/Path.h" 23 void adjustArguments(tooling::CompileCommand &Cmd,
29 tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename);
32 if (!ResourceDir.empty())
33 Cmd.CommandLine.push_back((
"-resource-dir=" + ResourceDir).str());
36 std::string getStandardResourceDir() {
38 return CompilerInvocation::GetResourcesPath(
"clangd", (
void *)&Dummy);
45 std::string ClangdExecutable =
46 llvm::sys::fs::getMainExecutable(
"clangd", (
void *)&Dummy);
47 SmallString<128> ClangPath;
48 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
49 llvm::sys::path::append(ClangPath,
"clang");
50 return ClangPath.str();
53 tooling::CompileCommand
58 if (llvm::sys::path::extension(File) ==
".h")
59 Argv.push_back(
"-xobjective-c++-header");
61 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
62 llvm::sys::path::filename(File),
70 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
75 llvm::Optional<tooling::CompileCommand>
78 if (
auto CDB = getCDBForFile(File, Project)) {
79 auto Candidates = CDB->getCompileCommands(File);
80 if (!Candidates.empty()) {
81 return std::move(Candidates.front());
84 log(
"Failed to find compilation database for {0}", File);
89 std::pair<tooling::CompilationDatabase *,
bool>
90 DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(
PathRef Dir)
const {
92 auto CachedIt = CompilationDatabases.find(Dir);
93 if (CachedIt != CompilationDatabases.end())
94 return {CachedIt->second.get(),
true};
95 std::string Error =
"";
96 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
98 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
102 tooling::CompilationDatabase *
103 DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
105 namespace path = llvm::sys::path;
106 assert((path::is_absolute(File, path::Style::posix) ||
107 path::is_absolute(File, path::Style::windows)) &&
108 "path must be absolute");
110 tooling::CompilationDatabase *CDB =
nullptr;
112 std::lock_guard<std::mutex> Lock(Mutex);
118 for (
auto Path = path::parent_path(File); !CDB && !
Path.empty();
120 std::tie(CDB, Cached) = getCDBInDirLocked(
Path);
133 std::vector<std::string> FallbackFlags,
134 llvm::Optional<std::string> ResourceDir)
135 : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir)
136 : getStandardResourceDir()),
137 FallbackFlags(std::move(FallbackFlags)) {
139 BaseChanged = Base->
watch([
this](
const std::vector<std::string> Changes) {
144 llvm::Optional<tooling::CompileCommand>
146 llvm::Optional<tooling::CompileCommand> Cmd;
148 std::lock_guard<std::mutex> Lock(Mutex);
160 adjustArguments(*Cmd, ResourceDir);
167 std::lock_guard<std::mutex> Lock(Mutex);
168 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
169 FallbackFlags.end());
174 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
176 std::unique_lock<std::mutex> Lock(Mutex);
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File, ProjectInfo *=nullptr) const override
Scans File's parents looking for compilation databases.
static std::string getFallbackClangPath()
virtual llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File, ProjectInfo *=nullptr) const =0
If there are any known-good commands for building this file, returns one.
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
llvm::StringRef PathRef
A typedef to represent a ref to file path.
~DirectoryBasedGlobalCompilationDatabase() override
Documents should not be synced at all.
Provides compilation arguments used for parsing C and C++ files.
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File, ProjectInfo *=nullptr) const override
If there are any known-good commands for building this file, returns one.
void broadcast(const T &V)
void log(const char *Fmt, Ts &&... Vals)
std::string Path
A typedef to represent a file path.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
DirectoryBasedGlobalCompilationDatabase(llvm::Optional< Path > CompileCommandsDir)
tooling::CompileCommand getFallbackCommand(PathRef File) const override
Makes a guess at how to build a file.
void setCompileCommand(PathRef File, llvm::Optional< tooling::CompileCommand > CompilationCommand)
Sets or clears the compilation command for a particular file.
static llvm::cl::opt< Path > ResourceDir("resource-dir", llvm::cl::desc("Directory for system clang headers"), llvm::cl::init(""), llvm::cl::Hidden)
OverlayCDB(const GlobalCompilationDatabase *Base, std::vector< std::string > FallbackFlags={}, llvm::Optional< std::string > ResourceDir=llvm::None)
CommandChanged::Subscription watch(CommandChanged::Listener L) const
The callback is notified when files may have new compile commands.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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."))
CommandChanged OnCommandChanged
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const
Makes a guess at how to build a file.