Coverage Report

Created: 2022-05-21 09:15

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