9
9
10
10
#include " GlobalCompilationDatabase.h"
11
11
#include " Logger.h"
12
+ #include " clang/Frontend/CompilerInvocation.h"
13
+ #include " clang/Tooling/ArgumentsAdjusters.h"
12
14
#include " clang/Tooling/CompilationDatabase.h"
15
+ #include " llvm/ADT/Optional.h"
13
16
#include " llvm/Support/FileSystem.h"
14
17
#include " llvm/Support/Path.h"
15
18
16
19
namespace clang {
17
20
namespace 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
18
42
19
43
static std::string getFallbackClangPath () {
20
44
static int Dummy;
@@ -106,8 +130,11 @@ DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
106
130
}
107
131
108
132
OverlayCDB::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)) {
111
138
if (Base)
112
139
BaseChanged = Base->watch ([this ](const std::vector<std::string> Changes) {
113
140
OnCommandChanged.broadcast (Changes);
@@ -116,16 +143,22 @@ OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
116
143
117
144
llvm::Optional<tooling::CompileCommand>
118
145
OverlayCDB::getCompileCommand (PathRef File, ProjectInfo *Project) const {
146
+ llvm::Optional<tooling::CompileCommand> Cmd;
119
147
{
120
148
std::lock_guard<std::mutex> Lock (Mutex);
121
149
auto It = Commands.find (File);
122
150
if (It != Commands.end ()) {
123
151
if (Project)
124
152
Project->SourceRoot = " " ;
125
- return It->second ;
153
+ Cmd = It->second ;
126
154
}
127
155
}
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;
129
162
}
130
163
131
164
tooling::CompileCommand OverlayCDB::getFallbackCommand (PathRef File) const {
0 commit comments