1 //===- CompilationDatabase.cpp --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains implementations of the CompilationDatabase base class
11 // and the FixedCompilationDatabase.
13 // FIXME: Various functions that take a string &ErrorMessage should be upgraded
16 //===----------------------------------------------------------------------===//
18 #include "clang/Tooling/CompilationDatabase.h"
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/DiagnosticIDs.h"
21 #include "clang/Basic/DiagnosticOptions.h"
22 #include "clang/Basic/LLVM.h"
23 #include "clang/Driver/Action.h"
24 #include "clang/Driver/Compilation.h"
25 #include "clang/Driver/Driver.h"
26 #include "clang/Driver/DriverDiagnostic.h"
27 #include "clang/Driver/Job.h"
28 #include "clang/Frontend/TextDiagnosticPrinter.h"
29 #include "clang/Tooling/CompilationDatabasePluginRegistry.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"
53 #include <system_error>
57 using namespace clang;
58 using namespace tooling;
60 LLVM_INSTANTIATE_REGISTRY(CompilationDatabasePluginRegistry)
62 CompilationDatabase::~CompilationDatabase() = default;
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();
72 std::string DatabaseErrorMessage;
73 std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
74 if (std::unique_ptr<CompilationDatabase> DB =
75 Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
77 ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n";
82 static std::unique_ptr<CompilationDatabase>
83 findCompilationDatabaseFromDirectory(StringRef Directory,
84 std::string &ErrorMessage) {
85 std::stringstream ErrorStream;
86 bool HasErrorMessage = false;
87 while (!Directory.empty()) {
88 std::string LoadErrorMessage;
90 if (std::unique_ptr<CompilationDatabase> DB =
91 CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
94 if (!HasErrorMessage) {
95 ErrorStream << "No compilation database found in " << Directory.str()
96 << " or any parent directory\n" << LoadErrorMessage;
97 HasErrorMessage = true;
100 Directory = llvm::sys::path::parent_path(Directory);
102 ErrorMessage = ErrorStream.str();
106 std::unique_ptr<CompilationDatabase>
107 CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
108 std::string &ErrorMessage) {
109 SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
110 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
112 std::unique_ptr<CompilationDatabase> DB =
113 findCompilationDatabaseFromDirectory(Directory, ErrorMessage);
116 ErrorMessage = ("Could not auto-detect compilation database for file \"" +
117 SourceFile + "\"\n" + ErrorMessage).str();
121 std::unique_ptr<CompilationDatabase>
122 CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir,
123 std::string &ErrorMessage) {
124 SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
126 std::unique_ptr<CompilationDatabase> DB =
127 findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage);
130 ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
131 SourceDir + "\"\n" + ErrorMessage).str();
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));
144 CompilationDatabasePlugin::~CompilationDatabasePlugin() = default;
148 // Helper for recursively searching through a chain of actions and collecting
149 // all inputs, direct and indirect, of compile jobs.
150 struct CompileJobAnalyzer {
151 SmallVector<std::string, 2> Inputs;
153 void run(const driver::Action *A) {
158 void runImpl(const driver::Action *A, bool Collect) {
159 bool CollectChildren = Collect;
160 switch (A->getKind()) {
161 case driver::Action::CompileJobClass:
162 CollectChildren = true;
165 case driver::Action::InputClass:
167 const auto *IA = cast<driver::InputAction>(A);
168 Inputs.push_back(IA->getInputArg().getSpelling());
173 // Don't care about others
177 for (const driver::Action *AI : A->inputs())
178 runImpl(AI, CollectChildren);
182 // Special DiagnosticConsumer that looks for warn_drv_input_file_unused
183 // diagnostics from the driver and collects the option strings for those unused
185 class UnusedInputDiagConsumer : public DiagnosticConsumer {
187 UnusedInputDiagConsumer(DiagnosticConsumer &Other) : Other(Other) {}
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
197 Other.HandleDiagnostic(DiagLevel, Info);
201 DiagnosticConsumer &Other;
202 SmallVector<std::string, 2> UnusedInputs;
205 // Unary functor for asking "Given a StringRef S1, does there exist a string
206 // S2 in Arr where S1 == S2?"
208 MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {}
210 bool operator() (StringRef S) {
211 for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
218 ArrayRef<std::string> Arr;
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,");
230 std::string GetClangToolCommand() {
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();
242 /// Strips any positional args and possible argv[0] from a command-line
243 /// provided by the user to construct a FixedCompilationDatabase.
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.
252 /// FIXME: This functionality should probably be made available by
253 /// clang::driver::Driver although what the interface should look like is not
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) {
264 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
265 llvm::raw_string_ostream Output(ErrorMsg);
266 TextDiagnosticPrinter DiagnosticPrinter(Output, &*DiagOpts);
267 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
268 DiagnosticsEngine Diagnostics(
269 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
270 &*DiagOpts, &DiagClient, false);
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(),
277 NewDriver->setCheckInputsExist(false);
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());
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");
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");
298 Args.erase(std::remove_if(Args.begin(), Args.end(), FilterUnusedFlags()),
301 const std::unique_ptr<driver::Compilation> Compilation(
302 NewDriver->BuildCompilation(Args));
306 const driver::JobList &Jobs = Compilation->getJobs();
308 CompileJobAnalyzer CompileAnalyzer;
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());
321 if (CompileAnalyzer.Inputs.empty()) {
322 ErrorMsg = "warning: no compile jobs found\n";
326 // Remove all compilation input files from the command line. This is
327 // necessary so that getCompileCommands() can construct a command line for
329 std::vector<const char *>::iterator End = std::remove_if(
330 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
332 // Remove all inputs deemed unused for compilation.
333 End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
335 // Remove the -c add above as well. It will be at the end right now.
336 assert(strcmp(*(End - 1), "-c") == 0);
339 Result = std::vector<std::string>(Args.begin() + 1, End);
343 std::unique_ptr<FixedCompilationDatabase>
344 FixedCompilationDatabase::loadFromCommandLine(int &Argc,
345 const char *const *Argv,
346 std::string &ErrorMsg,
351 const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
352 if (DoubleDash == Argv + Argc)
354 std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
355 Argc = DoubleDash - Argv;
357 std::vector<std::string> StrippedArgs;
358 if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg))
360 return llvm::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
363 std::unique_ptr<FixedCompilationDatabase>
364 FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) {
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();
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));
378 FixedCompilationDatabase::
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),
388 std::vector<CompileCommand>
389 FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const {
390 std::vector<CompileCommand> Result(CompileCommands);
391 Result[0].CommandLine.push_back(FilePath);
392 Result[0].Filename = FilePath;
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);
409 static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
410 X("fixed-compilation-database", "Reads plain-text flags file");
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;
420 } // namespace tooling