forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryDriverDatabase.cpp
371 lines (340 loc) · 13 KB
/
QueryDriverDatabase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//===--- QueryDriverDatabase.cpp ---------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Some compiler drivers have implicit search mechanism for system headers.
// This compilation database implementation tries to extract that information by
// executing the driver in verbose mode. gcc-compatible drivers print something
// like:
// ....
// ....
// #include <...> search starts here:
// /usr/lib/gcc/x86_64-linux-gnu/7/include
// /usr/local/include
// /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
// /usr/include/x86_64-linux-gnu
// /usr/include
// End of search list.
// ....
// ....
// This component parses that output and adds each path to command line args
// provided by Base, after prepending them with -isystem. Therefore current
// implementation would not work with a driver that is not gcc-compatible.
//
// First argument of the command line received from underlying compilation
// database is used as compiler driver path. Due to this arbitrary binary
// execution, this mechanism is not used by default and only executes binaries
// in the paths that are explicitly included by the user.
#include "GlobalCompilationDatabase.h"
#include "support/Logger.h"
#include "support/Path.h"
#include "support/Trace.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Driver/Types.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/ScopedPrinter.h"
#include <algorithm>
#include <map>
#include <string>
#include <vector>
namespace clang {
namespace clangd {
namespace {
struct DriverInfo {
std::vector<std::string> SystemIncludes;
std::string Target;
};
bool isValidTarget(llvm::StringRef Triple) {
std::shared_ptr<TargetOptions> TargetOpts(new TargetOptions);
TargetOpts->Triple = Triple.str();
DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions,
new IgnoringDiagConsumer);
IntrusiveRefCntPtr<TargetInfo> Target =
TargetInfo::CreateTargetInfo(Diags, TargetOpts);
return bool(Target);
}
llvm::Optional<DriverInfo> parseDriverOutput(llvm::StringRef Output) {
DriverInfo Info;
const char SIS[] = "#include <...> search starts here:";
const char SIE[] = "End of search list.";
const char TS[] = "Target: ";
llvm::SmallVector<llvm::StringRef> Lines;
Output.split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
enum {
Initial, // Initial state: searching for target or includes list.
IncludesExtracting, // Includes extracting.
Done // Includes and target extraction done.
} State = Initial;
bool SeenIncludes = false;
bool SeenTarget = false;
for (auto *It = Lines.begin(); State != Done && It != Lines.end(); ++It) {
auto Line = *It;
switch (State) {
case Initial:
if (!SeenIncludes && Line.trim() == SIS) {
SeenIncludes = true;
State = IncludesExtracting;
} else if (!SeenTarget && Line.trim().startswith(TS)) {
SeenTarget = true;
llvm::StringRef TargetLine = Line.trim();
TargetLine.consume_front(TS);
// Only detect targets that clang understands
if (!isValidTarget(TargetLine)) {
elog("System include extraction: invalid target \"{0}\", ignoring",
TargetLine);
} else {
Info.Target = TargetLine.str();
vlog("System include extraction: target extracted: \"{0}\"",
TargetLine);
}
}
break;
case IncludesExtracting:
if (Line.trim() == SIE) {
State = SeenTarget ? Done : Initial;
} else {
Info.SystemIncludes.push_back(Line.trim().str());
vlog("System include extraction: adding {0}", Line);
}
break;
default:
llvm_unreachable("Impossible state of the driver output parser");
break;
}
}
if (!SeenIncludes) {
elog("System include extraction: start marker not found: {0}", Output);
return llvm::None;
}
if (State == IncludesExtracting) {
elog("System include extraction: end marker missing: {0}", Output);
return llvm::None;
}
return std::move(Info);
}
llvm::Optional<DriverInfo>
extractSystemIncludesAndTarget(llvm::SmallString<128> Driver,
llvm::StringRef Lang,
llvm::ArrayRef<std::string> CommandLine,
const llvm::Regex &QueryDriverRegex) {
trace::Span Tracer("Extract system includes and target");
if (!llvm::sys::path::is_absolute(Driver)) {
assert(llvm::none_of(
Driver, [](char C) { return llvm::sys::path::is_separator(C); }));
auto DriverProgram = llvm::sys::findProgramByName(Driver);
if (DriverProgram) {
vlog("System include extraction: driver {0} expanded to {1}", Driver,
*DriverProgram);
Driver = *DriverProgram;
} else {
elog("System include extraction: driver {0} not found in PATH", Driver);
return llvm::None;
}
}
SPAN_ATTACH(Tracer, "driver", Driver);
SPAN_ATTACH(Tracer, "lang", Lang);
if (!QueryDriverRegex.match(Driver)) {
vlog("System include extraction: not allowed driver {0}", Driver);
return llvm::None;
}
llvm::SmallString<128> StdErrPath;
if (auto EC = llvm::sys::fs::createTemporaryFile("system-includes", "clangd",
StdErrPath)) {
elog("System include extraction: failed to create temporary file with "
"error {0}",
EC.message());
return llvm::None;
}
auto CleanUp = llvm::make_scope_exit(
[&StdErrPath]() { llvm::sys::fs::remove(StdErrPath); });
llvm::Optional<llvm::StringRef> Redirects[] = {{""}, {""}, StdErrPath.str()};
llvm::SmallVector<llvm::StringRef> Args = {Driver, "-E", "-x",
Lang, "-", "-v"};
// These flags will be preserved
const llvm::StringRef FlagsToPreserve[] = {
"-nostdinc", "--no-standard-includes", "-nostdinc++", "-nobuiltininc"};
// Preserves these flags and their values, either as separate args or with an
// equalsbetween them
const llvm::StringRef ArgsToPreserve[] = {"--sysroot", "-isysroot"};
for (size_t I = 0, E = CommandLine.size(); I < E; ++I) {
llvm::StringRef Arg = CommandLine[I];
if (llvm::any_of(FlagsToPreserve,
[&Arg](llvm::StringRef S) { return S == Arg; })) {
Args.push_back(Arg);
} else {
const auto *Found =
llvm::find_if(ArgsToPreserve, [&Arg](llvm::StringRef S) {
return Arg.startswith(S);
});
if (Found == std::end(ArgsToPreserve))
continue;
Arg = Arg.drop_front(Found->size());
if (Arg.empty() && I + 1 < E) {
Args.push_back(CommandLine[I]);
Args.push_back(CommandLine[++I]);
} else if (Arg.startswith("=")) {
Args.push_back(CommandLine[I]);
}
}
}
std::string ErrMsg;
if (int RC = llvm::sys::ExecuteAndWait(Driver, Args, /*Env=*/llvm::None,
Redirects, /*SecondsToWait=*/0,
/*MemoryLimit=*/0, &ErrMsg)) {
elog("System include extraction: driver execution failed with return code: "
"{0} - '{1}'. Args: [{2}]",
llvm::to_string(RC), ErrMsg, printArgv(Args));
return llvm::None;
}
auto BufOrError = llvm::MemoryBuffer::getFile(StdErrPath);
if (!BufOrError) {
elog("System include extraction: failed to read {0} with error {1}",
StdErrPath, BufOrError.getError().message());
return llvm::None;
}
llvm::Optional<DriverInfo> Info =
parseDriverOutput(BufOrError->get()->getBuffer());
if (!Info)
return llvm::None;
log("System includes extractor: successfully executed {0}\n\tgot includes: "
"\"{1}\"\n\tgot target: \"{2}\"",
Driver, llvm::join(Info->SystemIncludes, ", "), Info->Target);
return Info;
}
tooling::CompileCommand &
addSystemIncludes(tooling::CompileCommand &Cmd,
llvm::ArrayRef<std::string> SystemIncludes) {
for (llvm::StringRef Include : SystemIncludes) {
// FIXME(kadircet): This doesn't work when we have "--driver-mode=cl"
Cmd.CommandLine.push_back("-isystem");
Cmd.CommandLine.push_back(Include.str());
}
return Cmd;
}
tooling::CompileCommand &setTarget(tooling::CompileCommand &Cmd,
const std::string &Target) {
if (!Target.empty()) {
// We do not want to override existing target with extracted one.
for (llvm::StringRef Arg : Cmd.CommandLine) {
if (Arg == "-target" || Arg.startswith("--target="))
return Cmd;
}
Cmd.CommandLine.push_back("--target=" + Target);
}
return Cmd;
}
/// Converts a glob containing only ** or * into a regex.
std::string convertGlobToRegex(llvm::StringRef Glob) {
std::string RegText;
llvm::raw_string_ostream RegStream(RegText);
RegStream << '^';
for (size_t I = 0, E = Glob.size(); I < E; ++I) {
if (Glob[I] == '*') {
if (I + 1 < E && Glob[I + 1] == '*') {
// Double star, accept any sequence.
RegStream << ".*";
// Also skip the second star.
++I;
} else {
// Single star, accept any sequence without a slash.
RegStream << "[^/]*";
}
} else {
RegStream << llvm::Regex::escape(Glob.substr(I, 1));
}
}
RegStream << '$';
RegStream.flush();
return RegText;
}
/// Converts a glob containing only ** or * into a regex.
llvm::Regex convertGlobsToRegex(llvm::ArrayRef<std::string> Globs) {
assert(!Globs.empty() && "Globs cannot be empty!");
std::vector<std::string> RegTexts;
RegTexts.reserve(Globs.size());
for (llvm::StringRef Glob : Globs)
RegTexts.push_back(convertGlobToRegex(Glob));
llvm::Regex Reg(llvm::join(RegTexts, "|"));
assert(Reg.isValid(RegTexts.front()) &&
"Created an invalid regex from globs");
return Reg;
}
/// Extracts system includes from a trusted driver by parsing the output of
/// include search path and appends them to the commands coming from underlying
/// compilation database.
class QueryDriverDatabase : public DelegatingCDB {
public:
QueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
std::unique_ptr<GlobalCompilationDatabase> Base)
: DelegatingCDB(std::move(Base)),
QueryDriverRegex(convertGlobsToRegex(QueryDriverGlobs)) {}
llvm::Optional<tooling::CompileCommand>
getCompileCommand(PathRef File) const override {
auto Cmd = DelegatingCDB::getCompileCommand(File);
if (!Cmd || Cmd->CommandLine.empty())
return Cmd;
llvm::StringRef Lang;
for (size_t I = 0, E = Cmd->CommandLine.size(); I < E; ++I) {
llvm::StringRef Arg = Cmd->CommandLine[I];
if (Arg == "-x" && I + 1 < E)
Lang = Cmd->CommandLine[I + 1];
else if (Arg.startswith("-x"))
Lang = Arg.drop_front(2).trim();
}
if (Lang.empty()) {
llvm::StringRef Ext = llvm::sys::path::extension(File).trim('.');
auto Type = driver::types::lookupTypeForExtension(Ext);
if (Type == driver::types::TY_INVALID) {
elog("System include extraction: invalid file type for {0}", Ext);
return {};
}
Lang = driver::types::getTypeName(Type);
}
llvm::SmallString<128> Driver(Cmd->CommandLine.front());
if (llvm::any_of(Driver,
[](char C) { return llvm::sys::path::is_separator(C); }))
// Driver is a not a single executable name but instead a path (either
// relative or absolute).
llvm::sys::fs::make_absolute(Cmd->Directory, Driver);
if (auto Info =
QueriedDrivers.get(/*Key=*/(Driver + ":" + Lang).str(), [&] {
return extractSystemIncludesAndTarget(
Driver, Lang, Cmd->CommandLine, QueryDriverRegex);
})) {
setTarget(addSystemIncludes(*Cmd, Info->SystemIncludes), Info->Target);
}
return Cmd;
}
private:
// Caches includes extracted from a driver. Key is driver:lang.
Memoize<llvm::StringMap<llvm::Optional<DriverInfo>>> QueriedDrivers;
llvm::Regex QueryDriverRegex;
};
} // namespace
std::unique_ptr<GlobalCompilationDatabase>
getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
std::unique_ptr<GlobalCompilationDatabase> Base) {
assert(Base && "Null base to SystemIncludeExtractor");
if (QueryDriverGlobs.empty())
return Base;
return std::make_unique<QueryDriverDatabase>(QueryDriverGlobs,
std::move(Base));
}
} // namespace clangd
} // namespace clang