1//===- JSONCompilationDatabase.cpp ----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of the JSONCompilationDatabase.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Tooling/JSONCompilationDatabase.h"
14#include "clang/Basic/LLVM.h"
15#include "clang/Tooling/CompilationDatabase.h"
16#include "clang/Tooling/CompilationDatabasePluginRegistry.h"
17#include "clang/Tooling/Tooling.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Allocator.h"
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/ErrorOr.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/Path.h"
28#include "llvm/Support/StringSaver.h"
29#include "llvm/Support/VirtualFileSystem.h"
30#include "llvm/Support/YAMLParser.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/TargetParser/Host.h"
33#include "llvm/TargetParser/Triple.h"
34#include <cassert>
35#include <memory>
36#include <optional>
37#include <string>
38#include <system_error>
39#include <tuple>
40#include <utility>
41#include <vector>
42
43using namespace clang;
44using namespace tooling;
45
46namespace {
47
48/// A parser for escaped strings of command line arguments.
49///
50/// Assumes \-escaping for quoted arguments (see the documentation of
51/// unescapeCommandLine(...)).
52class CommandLineArgumentParser {
53 public:
54 CommandLineArgumentParser(StringRef CommandLine)
55 : Input(CommandLine), Position(Input.begin()-1) {}
56
57 std::vector<std::string> parse() {
58 bool HasMoreInput = true;
59 while (HasMoreInput && nextNonWhitespace()) {
60 std::string Argument;
61 HasMoreInput = parseStringInto(String&: Argument);
62 CommandLine.push_back(x: Argument);
63 }
64 return CommandLine;
65 }
66
67 private:
68 // All private methods return true if there is more input available.
69
70 bool parseStringInto(std::string &String) {
71 do {
72 if (*Position == '"') {
73 if (!parseDoubleQuotedStringInto(String)) return false;
74 } else if (*Position == '\'') {
75 if (!parseSingleQuotedStringInto(String)) return false;
76 } else {
77 if (!parseFreeStringInto(String)) return false;
78 }
79 } while (*Position != ' ');
80 return true;
81 }
82
83 bool parseDoubleQuotedStringInto(std::string &String) {
84 if (!next()) return false;
85 while (*Position != '"') {
86 if (!skipEscapeCharacter()) return false;
87 String.push_back(c: *Position);
88 if (!next()) return false;
89 }
90 return next();
91 }
92
93 bool parseSingleQuotedStringInto(std::string &String) {
94 if (!next()) return false;
95 while (*Position != '\'') {
96 String.push_back(c: *Position);
97 if (!next()) return false;
98 }
99 return next();
100 }
101
102 bool parseFreeStringInto(std::string &String) {
103 do {
104 if (!skipEscapeCharacter()) return false;
105 String.push_back(c: *Position);
106 if (!next()) return false;
107 } while (*Position != ' ' && *Position != '"' && *Position != '\'');
108 return true;
109 }
110
111 bool skipEscapeCharacter() {
112 if (*Position == '\\') {
113 return next();
114 }
115 return true;
116 }
117
118 bool nextNonWhitespace() {
119 do {
120 if (!next()) return false;
121 } while (*Position == ' ');
122 return true;
123 }
124
125 bool next() {
126 ++Position;
127 return Position != Input.end();
128 }
129
130 const StringRef Input;
131 StringRef::iterator Position;
132 std::vector<std::string> CommandLine;
133};
134
135std::vector<std::string> unescapeCommandLine(JSONCommandLineSyntax Syntax,
136 StringRef EscapedCommandLine) {
137 if (Syntax == JSONCommandLineSyntax::AutoDetect) {
138#ifdef _WIN32
139 // Assume Windows command line parsing on Win32
140 Syntax = JSONCommandLineSyntax::Windows;
141#else
142 Syntax = JSONCommandLineSyntax::Gnu;
143#endif
144 }
145
146 if (Syntax == JSONCommandLineSyntax::Windows) {
147 llvm::BumpPtrAllocator Alloc;
148 llvm::StringSaver Saver(Alloc);
149 llvm::SmallVector<const char *, 64> T;
150 llvm::cl::TokenizeWindowsCommandLine(Source: EscapedCommandLine, Saver, NewArgv&: T);
151 std::vector<std::string> Result(T.begin(), T.end());
152 return Result;
153 }
154 assert(Syntax == JSONCommandLineSyntax::Gnu);
155 CommandLineArgumentParser parser(EscapedCommandLine);
156 return parser.parse();
157}
158
159// This plugin locates a nearby compile_command.json file, and also infers
160// compile commands for files not present in the database.
161class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
162 std::unique_ptr<CompilationDatabase>
163 loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
164 SmallString<1024> JSONDatabasePath(Directory);
165 llvm::sys::path::append(path&: JSONDatabasePath, a: "compile_commands.json");
166 auto Base = JSONCompilationDatabase::loadFromFile(
167 FilePath: JSONDatabasePath, ErrorMessage, Syntax: JSONCommandLineSyntax::AutoDetect);
168 return Base ? inferTargetAndDriverMode(
169 Base: inferMissingCompileCommands(expandResponseFiles(
170 Base: std::move(Base), FS: llvm::vfs::getRealFileSystem())))
171 : nullptr;
172 }
173};
174
175} // namespace
176
177// Register the JSONCompilationDatabasePlugin with the
178// CompilationDatabasePluginRegistry using this statically initialized variable.
179static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin>
180X("json-compilation-database", "Reads JSON formatted compilation databases");
181
182namespace clang {
183namespace tooling {
184
185// This anchor is used to force the linker to link in the generated object file
186// and thus register the JSONCompilationDatabasePlugin.
187volatile int JSONAnchorSource = 0;
188
189} // namespace tooling
190} // namespace clang
191
192std::unique_ptr<JSONCompilationDatabase>
193JSONCompilationDatabase::loadFromFile(StringRef FilePath,
194 std::string &ErrorMessage,
195 JSONCommandLineSyntax Syntax) {
196 // Don't mmap: if we're a long-lived process, the build system may overwrite.
197 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer =
198 llvm::MemoryBuffer::getFile(Filename: FilePath, /*IsText=*/false,
199 /*RequiresNullTerminator=*/true,
200 /*IsVolatile=*/true);
201 if (std::error_code Result = DatabaseBuffer.getError()) {
202 ErrorMessage = "Error while opening JSON database: " + Result.message();
203 return nullptr;
204 }
205 std::unique_ptr<JSONCompilationDatabase> Database(
206 new JSONCompilationDatabase(std::move(*DatabaseBuffer), Syntax));
207 if (!Database->parse(ErrorMessage))
208 return nullptr;
209 return Database;
210}
211
212std::unique_ptr<JSONCompilationDatabase>
213JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
214 std::string &ErrorMessage,
215 JSONCommandLineSyntax Syntax) {
216 std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer(
217 llvm::MemoryBuffer::getMemBufferCopy(InputData: DatabaseString));
218 std::unique_ptr<JSONCompilationDatabase> Database(
219 new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax));
220 if (!Database->parse(ErrorMessage))
221 return nullptr;
222 return Database;
223}
224
225std::vector<CompileCommand>
226JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const {
227 SmallString<128> NativeFilePath;
228 llvm::sys::path::native(path: FilePath, result&: NativeFilePath);
229
230 std::string Error;
231 llvm::raw_string_ostream ES(Error);
232 StringRef Match = MatchTrie.findEquivalent(FileName: NativeFilePath, Error&: ES);
233 if (Match.empty())
234 return {};
235 const auto CommandsRefI = IndexByFile.find(Key: Match);
236 if (CommandsRefI == IndexByFile.end())
237 return {};
238 std::vector<CompileCommand> Commands;
239 getCommands(CommandsRef: CommandsRefI->getValue(), Commands);
240 return Commands;
241}
242
243std::vector<std::string>
244JSONCompilationDatabase::getAllFiles() const {
245 std::vector<std::string> Result;
246 for (const auto &CommandRef : IndexByFile)
247 Result.push_back(x: CommandRef.first().str());
248 return Result;
249}
250
251std::vector<CompileCommand>
252JSONCompilationDatabase::getAllCompileCommands() const {
253 std::vector<CompileCommand> Commands;
254 getCommands(CommandsRef: AllCommands, Commands);
255 return Commands;
256}
257
258static llvm::StringRef stripExecutableExtension(llvm::StringRef Name) {
259 Name.consume_back(Suffix: ".exe");
260 return Name;
261}
262
263// There are compiler-wrappers (ccache, distcc) that take the "real"
264// compiler as an argument, e.g. distcc gcc -O3 foo.c.
265// These end up in compile_commands.json when people set CC="distcc gcc".
266// Clang's driver doesn't understand this, so we need to unwrap.
267static bool unwrapCommand(std::vector<std::string> &Args) {
268 if (Args.size() < 2)
269 return false;
270 StringRef Wrapper =
271 stripExecutableExtension(Name: llvm::sys::path::filename(path: Args.front()));
272 if (Wrapper == "distcc" || Wrapper == "ccache" || Wrapper == "sccache") {
273 // Most of these wrappers support being invoked 3 ways:
274 // `distcc g++ file.c` This is the mode we're trying to match.
275 // We need to drop `distcc`.
276 // `distcc file.c` This acts like compiler is cc or similar.
277 // Clang's driver can handle this, no change needed.
278 // `g++ file.c` g++ is a symlink to distcc.
279 // We don't even notice this case, and all is well.
280 //
281 // We need to distinguish between the first and second case.
282 // The wrappers themselves don't take flags, so Args[1] is a compiler flag,
283 // an input file, or a compiler. Inputs have extensions, compilers don't.
284 bool HasCompiler =
285 (Args[1][0] != '-') &&
286 !llvm::sys::path::has_extension(path: stripExecutableExtension(Name: Args[1]));
287 if (HasCompiler) {
288 Args.erase(position: Args.begin());
289 return true;
290 }
291 // If !HasCompiler, wrappers act like GCC. Fine: so do we.
292 }
293 return false;
294}
295
296static std::vector<std::string>
297nodeToCommandLine(JSONCommandLineSyntax Syntax,
298 const std::vector<llvm::yaml::ScalarNode *> &Nodes) {
299 SmallString<1024> Storage;
300 std::vector<std::string> Arguments;
301 if (Nodes.size() == 1)
302 Arguments = unescapeCommandLine(Syntax, EscapedCommandLine: Nodes[0]->getValue(Storage));
303 else
304 for (const auto *Node : Nodes)
305 Arguments.push_back(x: std::string(Node->getValue(Storage)));
306 // There may be multiple wrappers: using distcc and ccache together is common.
307 while (unwrapCommand(Args&: Arguments))
308 ;
309 return Arguments;
310}
311
312void JSONCompilationDatabase::getCommands(
313 ArrayRef<CompileCommandRef> CommandsRef,
314 std::vector<CompileCommand> &Commands) const {
315 for (const auto &CommandRef : CommandsRef) {
316 SmallString<8> DirectoryStorage;
317 SmallString<32> FilenameStorage;
318 SmallString<32> OutputStorage;
319 auto Output = std::get<3>(t: CommandRef);
320 Commands.emplace_back(
321 args: std::get<0>(t: CommandRef)->getValue(Storage&: DirectoryStorage),
322 args: std::get<1>(t: CommandRef)->getValue(Storage&: FilenameStorage),
323 args: nodeToCommandLine(Syntax, Nodes: std::get<2>(t: CommandRef)),
324 args: Output ? Output->getValue(Storage&: OutputStorage) : "");
325 }
326}
327
328bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
329 llvm::yaml::document_iterator I = YAMLStream.begin();
330 if (I == YAMLStream.end()) {
331 ErrorMessage = "Error while parsing YAML.";
332 return false;
333 }
334 llvm::yaml::Node *Root = I->getRoot();
335 if (!Root) {
336 ErrorMessage = "Error while parsing YAML.";
337 return false;
338 }
339 auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Val: Root);
340 if (!Array) {
341 ErrorMessage = "Expected array.";
342 return false;
343 }
344 for (auto &NextObject : *Array) {
345 auto *Object = dyn_cast<llvm::yaml::MappingNode>(Val: &NextObject);
346 if (!Object) {
347 ErrorMessage = "Expected object.";
348 return false;
349 }
350 llvm::yaml::ScalarNode *Directory = nullptr;
351 std::optional<std::vector<llvm::yaml::ScalarNode *>> Command;
352 llvm::yaml::ScalarNode *File = nullptr;
353 llvm::yaml::ScalarNode *Output = nullptr;
354 for (auto& NextKeyValue : *Object) {
355 auto *KeyString = dyn_cast<llvm::yaml::ScalarNode>(Val: NextKeyValue.getKey());
356 if (!KeyString) {
357 ErrorMessage = "Expected strings as key.";
358 return false;
359 }
360 SmallString<10> KeyStorage;
361 StringRef KeyValue = KeyString->getValue(Storage&: KeyStorage);
362 llvm::yaml::Node *Value = NextKeyValue.getValue();
363 if (!Value) {
364 ErrorMessage = "Expected value.";
365 return false;
366 }
367 auto *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Val: Value);
368 auto *SequenceString = dyn_cast<llvm::yaml::SequenceNode>(Val: Value);
369 if (KeyValue == "arguments") {
370 if (!SequenceString) {
371 ErrorMessage = "Expected sequence as value.";
372 return false;
373 }
374 Command = std::vector<llvm::yaml::ScalarNode *>();
375 for (auto &Argument : *SequenceString) {
376 auto *Scalar = dyn_cast<llvm::yaml::ScalarNode>(Val: &Argument);
377 if (!Scalar) {
378 ErrorMessage = "Only strings are allowed in 'arguments'.";
379 return false;
380 }
381 Command->push_back(x: Scalar);
382 }
383 } else {
384 if (!ValueString) {
385 ErrorMessage = "Expected string as value.";
386 return false;
387 }
388 if (KeyValue == "directory") {
389 Directory = ValueString;
390 } else if (KeyValue == "command") {
391 if (!Command)
392 Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString);
393 } else if (KeyValue == "file") {
394 File = ValueString;
395 } else if (KeyValue == "output") {
396 Output = ValueString;
397 } else {
398 ErrorMessage =
399 ("Unknown key: \"" + KeyString->getRawValue() + "\"").str();
400 return false;
401 }
402 }
403 }
404 if (!File) {
405 ErrorMessage = "Missing key: \"file\".";
406 return false;
407 }
408 if (!Command) {
409 ErrorMessage = "Missing key: \"command\" or \"arguments\".";
410 return false;
411 }
412 if (!Directory) {
413 ErrorMessage = "Missing key: \"directory\".";
414 return false;
415 }
416 SmallString<8> FileStorage;
417 StringRef FileName = File->getValue(Storage&: FileStorage);
418 SmallString<128> NativeFilePath;
419 if (llvm::sys::path::is_relative(path: FileName)) {
420 SmallString<8> DirectoryStorage;
421 SmallString<128> AbsolutePath(Directory->getValue(Storage&: DirectoryStorage));
422 llvm::sys::path::append(path&: AbsolutePath, a: FileName);
423 llvm::sys::path::native(path: AbsolutePath, result&: NativeFilePath);
424 } else {
425 llvm::sys::path::native(path: FileName, result&: NativeFilePath);
426 }
427 llvm::sys::path::remove_dots(path&: NativeFilePath, /*remove_dot_dot=*/true);
428 auto Cmd = CompileCommandRef(Directory, File, *Command, Output);
429 IndexByFile[NativeFilePath].push_back(x: Cmd);
430 AllCommands.push_back(x: Cmd);
431 MatchTrie.insert(NewPath: NativeFilePath);
432 }
433 return true;
434}
435