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