Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/CompilationDatabase.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CompilationDatabase.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 implementations of the CompilationDatabase base class
10
//  and the FixedCompilationDatabase.
11
//
12
//  FIXME: Various functions that take a string &ErrorMessage should be upgraded
13
//  to Expected.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "clang/Tooling/CompilationDatabase.h"
18
#include "clang/Basic/Diagnostic.h"
19
#include "clang/Basic/DiagnosticIDs.h"
20
#include "clang/Basic/DiagnosticOptions.h"
21
#include "clang/Basic/LLVM.h"
22
#include "clang/Driver/Action.h"
23
#include "clang/Driver/Compilation.h"
24
#include "clang/Driver/Driver.h"
25
#include "clang/Driver/DriverDiagnostic.h"
26
#include "clang/Driver/Job.h"
27
#include "clang/Frontend/TextDiagnosticPrinter.h"
28
#include "clang/Tooling/CompilationDatabasePluginRegistry.h"
29
#include "clang/Tooling/Tooling.h"
30
#include "llvm/ADT/ArrayRef.h"
31
#include "llvm/ADT/IntrusiveRefCntPtr.h"
32
#include "llvm/ADT/STLExtras.h"
33
#include "llvm/ADT/SmallString.h"
34
#include "llvm/ADT/SmallVector.h"
35
#include "llvm/ADT/StringRef.h"
36
#include "llvm/Option/Arg.h"
37
#include "llvm/Support/Casting.h"
38
#include "llvm/Support/Compiler.h"
39
#include "llvm/Support/ErrorOr.h"
40
#include "llvm/Support/Host.h"
41
#include "llvm/Support/LineIterator.h"
42
#include "llvm/Support/MemoryBuffer.h"
43
#include "llvm/Support/Path.h"
44
#include "llvm/Support/raw_ostream.h"
45
#include <algorithm>
46
#include <cassert>
47
#include <cstring>
48
#include <iterator>
49
#include <memory>
50
#include <sstream>
51
#include <string>
52
#include <system_error>
53
#include <utility>
54
#include <vector>
55
56
using namespace clang;
57
using namespace tooling;
58
59
LLVM_INSTANTIATE_REGISTRY(CompilationDatabasePluginRegistry)
60
61
620
CompilationDatabase::~CompilationDatabase() = default;
62
63
std::unique_ptr<CompilationDatabase>
64
CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
65
62
                                       std::string &ErrorMessage) {
66
62
  llvm::raw_string_ostream ErrorStream(ErrorMessage);
67
62
  for (const CompilationDatabasePluginRegistry::entry &Database :
68
122
       CompilationDatabasePluginRegistry::entries()) {
69
122
    std::string DatabaseErrorMessage;
70
122
    std::unique_ptr<CompilationDatabasePlugin> Plugin(Database.instantiate());
71
122
    if (std::unique_ptr<CompilationDatabase> DB =
72
21
            Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
73
21
      return DB;
74
101
    ErrorStream << Database.getName() << ": " << DatabaseErrorMessage << "\n";
75
101
  }
76
41
  return nullptr;
77
62
}
78
79
static std::unique_ptr<CompilationDatabase>
80
findCompilationDatabaseFromDirectory(StringRef Directory,
81
22
                                     std::string &ErrorMessage) {
82
22
  std::stringstream ErrorStream;
83
22
  bool HasErrorMessage = false;
84
63
  while (!Directory.empty()) {
85
61
    std::string LoadErrorMessage;
86
61
87
61
    if (std::unique_ptr<CompilationDatabase> DB =
88
20
            CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
89
20
      return DB;
90
41
91
41
    if (!HasErrorMessage) {
92
10
      ErrorStream << "No compilation database found in " << Directory.str()
93
10
                  << " or any parent directory\n" << LoadErrorMessage;
94
10
      HasErrorMessage = true;
95
10
    }
96
41
97
41
    Directory = llvm::sys::path::parent_path(Directory);
98
41
  }
99
2
  ErrorMessage = ErrorStream.str();
100
2
  return nullptr;
101
22
}
102
103
std::unique_ptr<CompilationDatabase>
104
CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
105
13
                                          std::string &ErrorMessage) {
106
13
  SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
107
13
  StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
108
13
109
13
  std::unique_ptr<CompilationDatabase> DB =
110
13
      findCompilationDatabaseFromDirectory(Directory, ErrorMessage);
111
13
112
13
  if (!DB)
113
2
    ErrorMessage = ("Could not auto-detect compilation database for file \"" +
114
2
                   SourceFile + "\"\n" + ErrorMessage).str();
115
13
  return DB;
116
13
}
117
118
std::unique_ptr<CompilationDatabase>
119
CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir,
120
9
                                             std::string &ErrorMessage) {
121
9
  SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
122
9
123
9
  std::unique_ptr<CompilationDatabase> DB =
124
9
      findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage);
125
9
126
9
  if (!DB)
127
0
    ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
128
0
                   SourceDir + "\"\n" + ErrorMessage).str();
129
9
  return DB;
130
9
}
131
132
1
std::vector<CompileCommand> CompilationDatabase::getAllCompileCommands() const {
133
1
  std::vector<CompileCommand> Result;
134
0
  for (const auto &File : getAllFiles()) {
135
0
    auto C = getCompileCommands(File);
136
0
    std::move(C.begin(), C.end(), std::back_inserter(Result));
137
0
  }
138
1
  return Result;
139
1
}
140
141
122
CompilationDatabasePlugin::~CompilationDatabasePlugin() = default;
142
143
namespace {
144
145
// Helper for recursively searching through a chain of actions and collecting
146
// all inputs, direct and indirect, of compile jobs.
147
struct CompileJobAnalyzer {
148
  SmallVector<std::string, 2> Inputs;
149
150
121
  void run(const driver::Action *A) {
151
121
    runImpl(A, false);
152
121
  }
153
154
private:
155
607
  void runImpl(const driver::Action *A, bool Collect) {
156
607
    bool CollectChildren = Collect;
157
607
    switch (A->getKind()) {
158
122
    case driver::Action::CompileJobClass:
159
122
      CollectChildren = true;
160
122
      break;
161
0
162
122
    case driver::Action::InputClass:
163
122
      if (Collect) {
164
122
        const auto *IA = cast<driver::InputAction>(A);
165
122
        Inputs.push_back(std::string(IA->getInputArg().getSpelling()));
166
122
      }
167
122
      break;
168
0
169
363
    default:
170
      // Don't care about others
171
363
      break;
172
607
    }
173
607
174
607
    for (const driver::Action *AI : A->inputs())
175
486
      runImpl(AI, CollectChildren);
176
607
  }
177
};
178
179
// Special DiagnosticConsumer that looks for warn_drv_input_file_unused
180
// diagnostics from the driver and collects the option strings for those unused
181
// options.
182
class UnusedInputDiagConsumer : public DiagnosticConsumer {
183
public:
184
117
  UnusedInputDiagConsumer(DiagnosticConsumer &Other) : Other(Other) {}
185
186
  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
187
3
                        const Diagnostic &Info) override {
188
3
    if (Info.getID() == diag::warn_drv_input_file_unused) {
189
      // Arg 1 for this diagnostic is the option that didn't get used.
190
1
      UnusedInputs.push_back(Info.getArgStdStr(0));
191
2
    } else if (DiagLevel >= DiagnosticsEngine::Error) {
192
      // If driver failed to create compilation object, show the diagnostics
193
      // to user.
194
2
      Other.HandleDiagnostic(DiagLevel, Info);
195
2
    }
196
3
  }
197
198
  DiagnosticConsumer &Other;
199
  SmallVector<std::string, 2> UnusedInputs;
200
};
201
202
// Unary functor for asking "Given a StringRef S1, does there exist a string
203
// S2 in Arr where S1 == S2?"
204
struct MatchesAny {
205
234
  MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {}
206
207
702
  bool operator() (StringRef S) {
208
1.02k
    for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; 
++I319
)
209
440
      if (*I == S)
210
121
        return true;
211
581
    return false;
212
702
  }
213
214
private:
215
  ArrayRef<std::string> Arr;
216
};
217
218
// Filter of tools unused flags such as -no-integrated-as and -Wa,*.
219
// They are not used for syntax checking, and could confuse targets
220
// which don't support these options.
221
struct FilterUnusedFlags {
222
417
  bool operator() (StringRef S) {
223
417
    return (S == "-no-integrated-as") || 
S.startswith("-Wa,")412
;
224
417
  }
225
};
226
227
261
std::string GetClangToolCommand() {
228
261
  static int Dummy;
229
261
  std::string ClangExecutable =
230
261
      llvm::sys::fs::getMainExecutable("clang", (void *)&Dummy);
231
261
  SmallString<128> ClangToolPath;
232
261
  ClangToolPath = llvm::sys::path::parent_path(ClangExecutable);
233
261
  llvm::sys::path::append(ClangToolPath, "clang-tool");
234
261
  return std::string(ClangToolPath.str());
235
261
}
236
237
} // namespace
238
239
/// Strips any positional args and possible argv[0] from a command-line
240
/// provided by the user to construct a FixedCompilationDatabase.
241
///
242
/// FixedCompilationDatabase requires a command line to be in this format as it
243
/// constructs the command line for each file by appending the name of the file
244
/// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the
245
/// start of the command line although its value is not important as it's just
246
/// ignored by the Driver invoked by the ClangTool using the
247
/// FixedCompilationDatabase.
248
///
249
/// FIXME: This functionality should probably be made available by
250
/// clang::driver::Driver although what the interface should look like is not
251
/// clear.
252
///
253
/// \param[in] Args Args as provided by the user.
254
/// \return Resulting stripped command line.
255
///          \li true if successful.
256
///          \li false if \c Args cannot be used for compilation jobs (e.g.
257
///          contains an option like -E or -version).
258
static bool stripPositionalArgs(std::vector<const char *> Args,
259
                                std::vector<std::string> &Result,
260
117
                                std::string &ErrorMsg) {
261
117
  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
262
117
  llvm::raw_string_ostream Output(ErrorMsg);
263
117
  TextDiagnosticPrinter DiagnosticPrinter(Output, &*DiagOpts);
264
117
  UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
265
117
  DiagnosticsEngine Diagnostics(
266
117
      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
267
117
      &*DiagOpts, &DiagClient, false);
268
117
269
  // The clang executable path isn't required since the jobs the driver builds
270
  // will not be executed.
271
117
  std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
272
117
      /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
273
117
      Diagnostics));
274
117
  NewDriver->setCheckInputsExist(false);
275
117
276
  // This becomes the new argv[0]. The value is used to detect libc++ include
277
  // dirs on Mac, it isn't used for other platforms.
278
117
  std::string Argv0 = GetClangToolCommand();
279
117
  Args.insert(Args.begin(), Argv0.c_str());
280
117
281
  // By adding -c, we force the driver to treat compilation as the last phase.
282
  // It will then issue warnings via Diagnostics about un-used options that
283
  // would have been used for linking. If the user provided a compiler name as
284
  // the original argv[0], this will be treated as a linker input thanks to
285
  // insertng a new argv[0] above. All un-used options get collected by
286
  // UnusedInputdiagConsumer and get stripped out later.
287
117
  Args.push_back("-c");
288
117
289
  // Put a dummy C++ file on to ensure there's at least one compile job for the
290
  // driver to construct. If the user specified some other argument that
291
  // prevents compilation, e.g. -E or something like -version, we may still end
292
  // up with no jobs but then this is the user's fault.
293
117
  Args.push_back("placeholder.cpp");
294
117
295
117
  Args.erase(std::remove_if(Args.begin(), Args.end(), FilterUnusedFlags()),
296
117
             Args.end());
297
117
298
117
  const std::unique_ptr<driver::Compilation> Compilation(
299
117
      NewDriver->BuildCompilation(Args));
300
117
  if (!Compilation)
301
0
    return false;
302
117
303
117
  const driver::JobList &Jobs = Compilation->getJobs();
304
117
305
117
  CompileJobAnalyzer CompileAnalyzer;
306
117
307
123
  for (const auto &Cmd : Jobs) {
308
    // Collect only for Assemble, Backend, and Compile jobs. If we do all jobs
309
    // we get duplicates since Link jobs point to Assemble jobs as inputs.
310
    // -flto* flags make the BackendJobClass, which still needs analyzer.
311
123
    if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass ||
312
7
        Cmd.getSource().getKind() == driver::Action::BackendJobClass ||
313
121
        
Cmd.getSource().getKind() == driver::Action::CompileJobClass4
) {
314
121
      CompileAnalyzer.run(&Cmd.getSource());
315
121
    }
316
123
  }
317
117
318
117
  if (CompileAnalyzer.Inputs.empty()) {
319
0
    ErrorMsg = "warning: no compile jobs found\n";
320
0
    return false;
321
0
  }
322
117
323
  // Remove all compilation input files from the command line. This is
324
  // necessary so that getCompileCommands() can construct a command line for
325
  // each file.
326
117
  std::vector<const char *>::iterator End = std::remove_if(
327
117
      Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
328
117
329
  // Remove all inputs deemed unused for compilation.
330
117
  End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
331
117
332
  // Remove the -c add above as well. It will be at the end right now.
333
117
  assert(strcmp(*(End - 1), "-c") == 0);
334
117
  --End;
335
117
336
117
  Result = std::vector<std::string>(Args.begin() + 1, End);
337
117
  return true;
338
117
}
339
340
std::unique_ptr<FixedCompilationDatabase>
341
FixedCompilationDatabase::loadFromCommandLine(int &Argc,
342
                                              const char *const *Argv,
343
                                              std::string &ErrorMsg,
344
143
                                              Twine Directory) {
345
143
  ErrorMsg.clear();
346
143
  if (Argc == 0)
347
1
    return nullptr;
348
142
  const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
349
142
  if (DoubleDash == Argv + Argc)
350
25
    return nullptr;
351
117
  std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
352
117
  Argc = DoubleDash - Argv;
353
117
354
117
  std::vector<std::string> StrippedArgs;
355
117
  if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg))
356
0
    return nullptr;
357
117
  return std::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
358
117
}
359
360
std::unique_ptr<FixedCompilationDatabase>
361
62
FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) {
362
62
  ErrorMsg.clear();
363
62
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
364
62
      llvm::MemoryBuffer::getFile(Path);
365
62
  if (std::error_code Result = File.getError()) {
366
60
    ErrorMsg = "Error while opening fixed database: " + Result.message();
367
60
    return nullptr;
368
60
  }
369
2
  std::vector<std::string> Args;
370
2
  for (llvm::StringRef Line :
371
3
       llvm::make_range(llvm::line_iterator(**File), llvm::line_iterator())) {
372
    // Stray whitespace is almost certainly unintended.
373
3
    Line = Line.trim();
374
3
    if (!Line.empty())
375
3
      Args.push_back(Line.str());
376
3
  }
377
2
  return std::make_unique<FixedCompilationDatabase>(
378
2
      llvm::sys::path::parent_path(Path), std::move(Args));
379
2
}
380
381
FixedCompilationDatabase::
382
144
FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
383
144
  std::vector<std::string> ToolCommandLine(1, GetClangToolCommand());
384
144
  ToolCommandLine.insert(ToolCommandLine.end(),
385
144
                         CommandLine.begin(), CommandLine.end());
386
144
  CompileCommands.emplace_back(Directory, StringRef(),
387
144
                               std::move(ToolCommandLine),
388
144
                               StringRef());
389
144
}
390
391
std::vector<CompileCommand>
392
315
FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const {
393
315
  std::vector<CompileCommand> Result(CompileCommands);
394
315
  Result[0].CommandLine.push_back(std::string(FilePath));
395
315
  Result[0].Filename = std::string(FilePath);
396
315
  return Result;
397
315
}
398
399
namespace {
400
401
class FixedCompilationDatabasePlugin : public CompilationDatabasePlugin {
402
  std::unique_ptr<CompilationDatabase>
403
62
  loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
404
62
    SmallString<1024> DatabasePath(Directory);
405
62
    llvm::sys::path::append(DatabasePath, "compile_flags.txt");
406
62
    return FixedCompilationDatabase::loadFromFile(DatabasePath, ErrorMessage);
407
62
  }
408
};
409
410
} // namespace
411
412
static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
413
X("fixed-compilation-database", "Reads plain-text flags file");
414
415
namespace clang {
416
namespace tooling {
417
418
// This anchor is used to force the linker to link in the generated object file
419
// and thus register the JSONCompilationDatabasePlugin.
420
extern volatile int JSONAnchorSource;
421
static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource;
422
423
} // namespace tooling
424
} // namespace clang