clang  3.9.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 //===----------------------------------------------------------------------===//
14 
16 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Driver/Action.h"
20 #include "clang/Driver/Driver.h"
22 #include "clang/Driver/Job.h"
25 #include "clang/Tooling/Tooling.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Option/Arg.h"
28 #include "llvm/Support/Host.h"
29 #include "llvm/Support/Path.h"
30 #include <sstream>
31 #include <system_error>
32 using namespace clang;
33 using namespace tooling;
34 
36 
37 std::unique_ptr<CompilationDatabase>
38 CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
39  std::string &ErrorMessage) {
40  std::stringstream ErrorStream;
44  It != Ie; ++It) {
45  std::string DatabaseErrorMessage;
46  std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
47  if (std::unique_ptr<CompilationDatabase> DB =
48  Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
49  return DB;
50  ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n";
51  }
52  ErrorMessage = ErrorStream.str();
53  return nullptr;
54 }
55 
56 static std::unique_ptr<CompilationDatabase>
58  std::string &ErrorMessage) {
59  std::stringstream ErrorStream;
60  bool HasErrorMessage = false;
61  while (!Directory.empty()) {
62  std::string LoadErrorMessage;
63 
64  if (std::unique_ptr<CompilationDatabase> DB =
65  CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
66  return DB;
67 
68  if (!HasErrorMessage) {
69  ErrorStream << "No compilation database found in " << Directory.str()
70  << " or any parent directory\n" << LoadErrorMessage;
71  HasErrorMessage = true;
72  }
73 
74  Directory = llvm::sys::path::parent_path(Directory);
75  }
76  ErrorMessage = ErrorStream.str();
77  return nullptr;
78 }
79 
80 std::unique_ptr<CompilationDatabase>
82  std::string &ErrorMessage) {
83  SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
84  StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
85 
86  std::unique_ptr<CompilationDatabase> DB =
87  findCompilationDatabaseFromDirectory(Directory, ErrorMessage);
88 
89  if (!DB)
90  ErrorMessage = ("Could not auto-detect compilation database for file \"" +
91  SourceFile + "\"\n" + ErrorMessage).str();
92  return DB;
93 }
94 
95 std::unique_ptr<CompilationDatabase>
97  std::string &ErrorMessage) {
98  SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
99 
100  std::unique_ptr<CompilationDatabase> DB =
101  findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage);
102 
103  if (!DB)
104  ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
105  SourceDir + "\"\n" + ErrorMessage).str();
106  return DB;
107 }
108 
110 
111 namespace {
112 // Helper for recursively searching through a chain of actions and collecting
113 // all inputs, direct and indirect, of compile jobs.
114 struct CompileJobAnalyzer {
115  void run(const driver::Action *A) {
116  runImpl(A, false);
117  }
118 
120 
121 private:
122 
123  void runImpl(const driver::Action *A, bool Collect) {
124  bool CollectChildren = Collect;
125  switch (A->getKind()) {
127  CollectChildren = true;
128  break;
129 
131  if (Collect) {
132  const driver::InputAction *IA = cast<driver::InputAction>(A);
133  Inputs.push_back(IA->getInputArg().getSpelling());
134  }
135  } break;
136 
137  default:
138  // Don't care about others
139  ;
140  }
141 
142  for (const driver::Action *AI : A->inputs())
143  runImpl(AI, CollectChildren);
144  }
145 };
146 
147 // Special DiagnosticConsumer that looks for warn_drv_input_file_unused
148 // diagnostics from the driver and collects the option strings for those unused
149 // options.
150 class UnusedInputDiagConsumer : public DiagnosticConsumer {
151 public:
152  UnusedInputDiagConsumer() : Other(nullptr) {}
153 
154  // Useful for debugging, chain diagnostics to another consumer after
155  // recording for our own purposes.
156  UnusedInputDiagConsumer(DiagnosticConsumer *Other) : Other(Other) {}
157 
158  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
159  const Diagnostic &Info) override {
160  if (Info.getID() == clang::diag::warn_drv_input_file_unused) {
161  // Arg 1 for this diagnostic is the option that didn't get used.
162  UnusedInputs.push_back(Info.getArgStdStr(0));
163  }
164  if (Other)
165  Other->HandleDiagnostic(DiagLevel, Info);
166  }
167 
168  DiagnosticConsumer *Other;
169  SmallVector<std::string, 2> UnusedInputs;
170 };
171 
172 // Unary functor for asking "Given a StringRef S1, does there exist a string
173 // S2 in Arr where S1 == S2?"
174 struct MatchesAny {
175  MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {}
176  bool operator() (StringRef S) {
177  for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
178  if (*I == S)
179  return true;
180  return false;
181  }
182 private:
184 };
185 } // namespace
186 
187 /// \brief Strips any positional args and possible argv[0] from a command-line
188 /// provided by the user to construct a FixedCompilationDatabase.
189 ///
190 /// FixedCompilationDatabase requires a command line to be in this format as it
191 /// constructs the command line for each file by appending the name of the file
192 /// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the
193 /// start of the command line although its value is not important as it's just
194 /// ignored by the Driver invoked by the ClangTool using the
195 /// FixedCompilationDatabase.
196 ///
197 /// FIXME: This functionality should probably be made available by
198 /// clang::driver::Driver although what the interface should look like is not
199 /// clear.
200 ///
201 /// \param[in] Args Args as provided by the user.
202 /// \return Resulting stripped command line.
203 /// \li true if successful.
204 /// \li false if \c Args cannot be used for compilation jobs (e.g.
205 /// contains an option like -E or -version).
206 static bool stripPositionalArgs(std::vector<const char *> Args,
207  std::vector<std::string> &Result) {
209  UnusedInputDiagConsumer DiagClient;
210  DiagnosticsEngine Diagnostics(
212  &*DiagOpts, &DiagClient, false);
213 
214  // The clang executable path isn't required since the jobs the driver builds
215  // will not be executed.
216  std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
217  /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
218  Diagnostics));
219  NewDriver->setCheckInputsExist(false);
220 
221  // This becomes the new argv[0]. The value is actually not important as it
222  // isn't used for invoking Tools.
223  Args.insert(Args.begin(), "clang-tool");
224 
225  // By adding -c, we force the driver to treat compilation as the last phase.
226  // It will then issue warnings via Diagnostics about un-used options that
227  // would have been used for linking. If the user provided a compiler name as
228  // the original argv[0], this will be treated as a linker input thanks to
229  // insertng a new argv[0] above. All un-used options get collected by
230  // UnusedInputdiagConsumer and get stripped out later.
231  Args.push_back("-c");
232 
233  // Put a dummy C++ file on to ensure there's at least one compile job for the
234  // driver to construct. If the user specified some other argument that
235  // prevents compilation, e.g. -E or something like -version, we may still end
236  // up with no jobs but then this is the user's fault.
237  Args.push_back("placeholder.cpp");
238 
239  // Remove -no-integrated-as; it's not used for syntax checking,
240  // and it confuses targets which don't support this option.
241  Args.erase(std::remove_if(Args.begin(), Args.end(),
242  MatchesAny(std::string("-no-integrated-as"))),
243  Args.end());
244 
245  const std::unique_ptr<driver::Compilation> Compilation(
246  NewDriver->BuildCompilation(Args));
247 
248  const driver::JobList &Jobs = Compilation->getJobs();
249 
250  CompileJobAnalyzer CompileAnalyzer;
251 
252  for (const auto &Cmd : Jobs) {
253  // Collect only for Assemble jobs. If we do all jobs we get duplicates
254  // since Link jobs point to Assemble jobs as inputs.
255  if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass)
256  CompileAnalyzer.run(&Cmd.getSource());
257  }
258 
259  if (CompileAnalyzer.Inputs.empty()) {
260  // No compile jobs found.
261  // FIXME: Emit a warning of some kind?
262  return false;
263  }
264 
265  // Remove all compilation input files from the command line. This is
266  // necessary so that getCompileCommands() can construct a command line for
267  // each file.
268  std::vector<const char *>::iterator End = std::remove_if(
269  Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
270 
271  // Remove all inputs deemed unused for compilation.
272  End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
273 
274  // Remove the -c add above as well. It will be at the end right now.
275  assert(strcmp(*(End - 1), "-c") == 0);
276  --End;
277 
278  Result = std::vector<std::string>(Args.begin() + 1, End);
279  return true;
280 }
281 
283  int &Argc, const char *const *Argv, Twine Directory) {
284  const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
285  if (DoubleDash == Argv + Argc)
286  return nullptr;
287  std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
288  Argc = DoubleDash - Argv;
289 
290  std::vector<std::string> StrippedArgs;
291  if (!stripPositionalArgs(CommandLine, StrippedArgs))
292  return nullptr;
293  return new FixedCompilationDatabase(Directory, StrippedArgs);
294 }
295 
298  std::vector<std::string> ToolCommandLine(1, "clang-tool");
299  ToolCommandLine.insert(ToolCommandLine.end(),
300  CommandLine.begin(), CommandLine.end());
301  CompileCommands.emplace_back(Directory, StringRef(),
302  std::move(ToolCommandLine));
303 }
304 
305 std::vector<CompileCommand>
307  std::vector<CompileCommand> Result(CompileCommands);
308  Result[0].CommandLine.push_back(FilePath);
309  Result[0].Filename = FilePath;
310  return Result;
311 }
312 
313 std::vector<std::string>
315  return std::vector<std::string>();
316 }
317 
318 std::vector<CompileCommand>
320  return std::vector<CompileCommand>();
321 }
322 
323 namespace clang {
324 namespace tooling {
325 
326 // This anchor is used to force the linker to link in the generated object file
327 // and thus register the JSONCompilationDatabasePlugin.
328 extern volatile int JSONAnchorSource;
329 static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource;
330 
331 } // end namespace tooling
332 } // end 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)
const std::string & getArgStdStr(unsigned Idx) const
Return the provided argument string specified by Idx.
Definition: Diagnostic.h:1173
input_range inputs()
Definition: Action.h:132
std::vector< CompileCommand > getCompileCommands(StringRef FilePath) const override
Returns the given compile command.
unsigned getID() const
Definition: Diagnostic.h:1153
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1314
iterator begin() const
Definition: Type.h:4235
const StringRef FilePath
ActionClass getKind() const
Definition: Action.h:122
Action - Represent an abstract compilation step to perform.
Definition: Action.h:45
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:66
iterator end() const
detail::InMemoryDirectory::const_iterator I
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
static FixedCompilationDatabase * loadFromCommandLine(int &Argc, const char *const *Argv, Twine Directory=".")
Creates a FixedCompilationDatabase from the arguments after "--".
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:158
JobList - A sequence of jobs to perform.
Definition: Job.h:156
const llvm::opt::Arg & getInputArg() const
Definition: Action.h:182
The result type of a method or function.
static bool stripPositionalArgs(std::vector< const char * > Args, std::vector< std::string > &Result)
Strips any positional args and possible argv[0] from a command-line provided by the user to construct...
const TemplateArgument * iterator
Definition: Type.h:4233
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.
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:399
std::vector< CompileCommand > getAllCompileCommands() const override
Returns all compile commands for all the files in the compilation database.
Used for handling and querying diagnostic IDs.
detail::InMemoryDirectory::const_iterator E
std::vector< std::string > CommandLine
Defines the Diagnostic-related interfaces.
FixedCompilationDatabase(Twine Directory, ArrayRef< std::string > CommandLine)
Constructs a compilation data base from a specified directory and command line.
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:141
const list_type & getJobs() const
Definition: Job.h:176
std::vector< std::string > getAllFiles() const override
Returns the list of all files available in the compilation database.
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1144
static std::unique_ptr< CompilationDatabase > loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage)
Loads a compilation database from a build directory.
static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest
A compilation database that returns a single compile command line.