99
1010#include " GlobalCompilationDatabase.h"
1111#include " Logger.h"
12+ #include " clang/Frontend/CompilerInvocation.h"
13+ #include " clang/Tooling/ArgumentsAdjusters.h"
1214#include " clang/Tooling/CompilationDatabase.h"
15+ #include " llvm/ADT/Optional.h"
1316#include " llvm/Support/FileSystem.h"
1417#include " llvm/Support/Path.h"
1518
1619namespace clang {
1720namespace clangd {
21+ namespace {
22+
23+ void adjustArguments (tooling::CompileCommand &Cmd,
24+ llvm::StringRef ResourceDir) {
25+ // Strip plugin related command line arguments. Clangd does
26+ // not support plugins currently. Therefore it breaks if
27+ // compiler tries to load plugins.
28+ Cmd.CommandLine =
29+ tooling::getStripPluginsAdjuster ()(Cmd.CommandLine , Cmd.Filename );
30+ // Inject the resource dir.
31+ // FIXME: Don't overwrite it if it's already there.
32+ if (!ResourceDir.empty ())
33+ Cmd.CommandLine .push_back ((" -resource-dir=" + ResourceDir).str ());
34+ }
35+
36+ std::string getStandardResourceDir () {
37+ static int Dummy; // Just an address in this process.
38+ return CompilerInvocation::GetResourcesPath (" clangd" , (void *)&Dummy);
39+ }
40+
41+ } // namespace
1842
1943static std::string getFallbackClangPath () {
2044 static int Dummy;
@@ -106,8 +130,11 @@ DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
106130}
107131
108132OverlayCDB::OverlayCDB (const GlobalCompilationDatabase *Base,
109- std::vector<std::string> FallbackFlags)
110- : Base(Base), FallbackFlags(std::move(FallbackFlags)) {
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)) {
111138 if (Base)
112139 BaseChanged = Base->watch ([this ](const std::vector<std::string> Changes) {
113140 OnCommandChanged.broadcast (Changes);
@@ -116,16 +143,22 @@ OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
116143
117144llvm::Optional<tooling::CompileCommand>
118145OverlayCDB::getCompileCommand (PathRef File, ProjectInfo *Project) const {
146+ llvm::Optional<tooling::CompileCommand> Cmd;
119147 {
120148 std::lock_guard<std::mutex> Lock (Mutex);
121149 auto It = Commands.find (File);
122150 if (It != Commands.end ()) {
123151 if (Project)
124152 Project->SourceRoot = " " ;
125- return It->second ;
153+ Cmd = It->second ;
126154 }
127155 }
128- return Base ? Base->getCompileCommand (File, Project) : None;
156+ if (!Cmd && Base)
157+ Cmd = Base->getCompileCommand (File, Project);
158+ if (!Cmd)
159+ return llvm::None;
160+ adjustArguments (*Cmd, ResourceDir);
161+ return Cmd;
129162}
130163
131164tooling::CompileCommand OverlayCDB::getFallbackCommand (PathRef File) const {
0 commit comments