clang  7.0.0svn
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 } // namespace
222 
223 /// Strips any positional args and possible argv[0] from a command-line
224 /// provided by the user to construct a FixedCompilationDatabase.
225 ///
226 /// FixedCompilationDatabase requires a command line to be in this format as it
227 /// constructs the command line for each file by appending the name of the file
228 /// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the
229 /// start of the command line although its value is not important as it's just
230 /// ignored by the Driver invoked by the ClangTool using the
231 /// FixedCompilationDatabase.
232 ///
233 /// FIXME: This functionality should probably be made available by
234 /// clang::driver::Driver although what the interface should look like is not
235 /// clear.
236 ///
237 /// \param[in] Args Args as provided by the user.
238 /// \return Resulting stripped command line.
239 /// \li true if successful.
240 /// \li false if \c Args cannot be used for compilation jobs (e.g.
241 /// contains an option like -E or -version).
242 static bool stripPositionalArgs(std::vector<const char *> Args,
243  std::vector<std::string> &Result,
244  std::string &ErrorMsg) {
246  llvm::raw_string_ostream Output(ErrorMsg);
247  TextDiagnosticPrinter DiagnosticPrinter(Output, &*DiagOpts);
248  UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
249  DiagnosticsEngine Diagnostics(
251  &*DiagOpts, &DiagClient, false);
252 
253  // The clang executable path isn't required since the jobs the driver builds
254  // will not be executed.
255  std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
256  /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
257  Diagnostics));
258  NewDriver->setCheckInputsExist(false);
259 
260  // This becomes the new argv[0]. The value is actually not important as it
261  // isn't used for invoking Tools.
262  Args.insert(Args.begin(), "clang-tool");
263 
264  // By adding -c, we force the driver to treat compilation as the last phase.
265  // It will then issue warnings via Diagnostics about un-used options that
266  // would have been used for linking. If the user provided a compiler name as
267  // the original argv[0], this will be treated as a linker input thanks to
268  // insertng a new argv[0] above. All un-used options get collected by
269  // UnusedInputdiagConsumer and get stripped out later.
270  Args.push_back("-c");
271 
272  // Put a dummy C++ file on to ensure there's at least one compile job for the
273  // driver to construct. If the user specified some other argument that
274  // prevents compilation, e.g. -E or something like -version, we may still end
275  // up with no jobs but then this is the user's fault.
276  Args.push_back("placeholder.cpp");
277 
278  // Remove -no-integrated-as; it's not used for syntax checking,
279  // and it confuses targets which don't support this option.
280  Args.erase(std::remove_if(Args.begin(), Args.end(),
281  MatchesAny(std::string("-no-integrated-as"))),
282  Args.end());
283 
284  const std::unique_ptr<driver::Compilation> Compilation(
285  NewDriver->BuildCompilation(Args));
286  if (!Compilation)
287  return false;
288 
289  const driver::JobList &Jobs = Compilation->getJobs();
290 
291  CompileJobAnalyzer CompileAnalyzer;
292 
293  for (const auto &Cmd : Jobs) {
294  // Collect only for Assemble and Compile jobs. If we do all jobs we get
295  // duplicates since Link jobs point to Assemble jobs as inputs.
296  if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass ||
297  Cmd.getSource().getKind() == driver::Action::CompileJobClass) {
298  CompileAnalyzer.run(&Cmd.getSource());
299  }
300  }
301 
302  if (CompileAnalyzer.Inputs.empty()) {
303  ErrorMsg = "warning: no compile jobs found\n";
304  return false;
305  }
306 
307  // Remove all compilation input files from the command line. This is
308  // necessary so that getCompileCommands() can construct a command line for
309  // each file.
310  std::vector<const char *>::iterator End = std::remove_if(
311  Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
312 
313  // Remove all inputs deemed unused for compilation.
314  End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
315 
316  // Remove the -c add above as well. It will be at the end right now.
317  assert(strcmp(*(End - 1), "-c") == 0);
318  --End;
319 
320  Result = std::vector<std::string>(Args.begin() + 1, End);
321  return true;
322 }
323 
324 std::unique_ptr<FixedCompilationDatabase>
326  const char *const *Argv,
327  std::string &ErrorMsg,
328  Twine Directory) {
329  ErrorMsg.clear();
330  if (Argc == 0)
331  return nullptr;
332  const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
333  if (DoubleDash == Argv + Argc)
334  return nullptr;
335  std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
336  Argc = DoubleDash - Argv;
337 
338  std::vector<std::string> StrippedArgs;
339  if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg))
340  return nullptr;
341  return llvm::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
342 }
343 
344 std::unique_ptr<FixedCompilationDatabase>
345 FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) {
346  ErrorMsg.clear();
347  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
348  llvm::MemoryBuffer::getFile(Path);
349  if (std::error_code Result = File.getError()) {
350  ErrorMsg = "Error while opening fixed database: " + Result.message();
351  return nullptr;
352  }
353  std::vector<std::string> Args{llvm::line_iterator(**File),
354  llvm::line_iterator()};
355  return llvm::make_unique<FixedCompilationDatabase>(
356  llvm::sys::path::parent_path(Path), std::move(Args));
357 }
358 
360 FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
361  std::vector<std::string> ToolCommandLine(1, "clang-tool");
362  ToolCommandLine.insert(ToolCommandLine.end(),
363  CommandLine.begin(), CommandLine.end());
364  CompileCommands.emplace_back(Directory, StringRef(),
365  std::move(ToolCommandLine),
366  StringRef());
367 }
368 
369 std::vector<CompileCommand>
371  std::vector<CompileCommand> Result(CompileCommands);
372  Result[0].CommandLine.push_back(FilePath);
373  Result[0].Filename = FilePath;
374  return Result;
375 }
376 
377 namespace {
378 
379 class FixedCompilationDatabasePlugin : public CompilationDatabasePlugin {
380  std::unique_ptr<CompilationDatabase>
381  loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
382  SmallString<1024> DatabasePath(Directory);
383  llvm::sys::path::append(DatabasePath, "compile_flags.txt");
384  return FixedCompilationDatabase::loadFromFile(DatabasePath, ErrorMessage);
385  }
386 };
387 
388 } // namespace
389 
390 static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
391 X("fixed-compilation-database", "Reads plain-text flags file");
392 
393 namespace clang {
394 namespace tooling {
395 
396 // This anchor is used to force the linker to link in the generated object file
397 // and thus register the JSONCompilationDatabasePlugin.
398 extern volatile int JSONAnchorSource;
399 static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource;
400 
401 } // namespace tooling
402 } // 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:149
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:1487
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:59
ActionClass getKind() const
Definition: Action.h:139
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:202
JobList - A sequence of jobs to perform.
Definition: Job.h:167
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:525
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:187
virtual std::vector< CompileCommand > getCompileCommands(StringRef FilePath) const =0
Returns all compile commands in which the specified file was compiled.