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;
223 /// Strips any positional args and possible argv[0] from a command-line
224 /// provided by the user to construct a FixedCompilationDatabase.
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.
233 /// FIXME: This functionality should probably be made available by
234 /// clang::driver::Driver although what the interface should look like is not
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) {
245 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
246 llvm::raw_string_ostream Output(ErrorMsg);
247 TextDiagnosticPrinter DiagnosticPrinter(Output, &*DiagOpts);
248 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
249 DiagnosticsEngine Diagnostics(
250 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
251 &*DiagOpts, &DiagClient, false);
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(),
258 NewDriver->setCheckInputsExist(false);
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");
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");
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");
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"))),
284 const std::unique_ptr<driver::Compilation> Compilation(
285 NewDriver->BuildCompilation(Args));
289 const driver::JobList &Jobs = Compilation->getJobs();
291 CompileJobAnalyzer CompileAnalyzer;
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());
302 if (CompileAnalyzer.Inputs.empty()) {
303 ErrorMsg = "warning: no compile jobs found\n";
307 // Remove all compilation input files from the command line. This is
308 // necessary so that getCompileCommands() can construct a command line for
310 std::vector<const char *>::iterator End = std::remove_if(
311 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
313 // Remove all inputs deemed unused for compilation.
314 End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
316 // Remove the -c add above as well. It will be at the end right now.
317 assert(strcmp(*(End - 1), "-c") == 0);
320 Result = std::vector<std::string>(Args.begin() + 1, End);
324 std::unique_ptr<FixedCompilationDatabase>
325 FixedCompilationDatabase::loadFromCommandLine(int &Argc,
326 const char *const *Argv,
327 std::string &ErrorMsg,
332 const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
333 if (DoubleDash == Argv + Argc)
335 std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
336 Argc = DoubleDash - Argv;
338 std::vector<std::string> StrippedArgs;
339 if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg))
341 return llvm::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
344 std::unique_ptr<FixedCompilationDatabase>
345 FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) {
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();
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));
359 FixedCompilationDatabase::
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),
369 std::vector<CompileCommand>
370 FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const {
371 std::vector<CompileCommand> Result(CompileCommands);
372 Result[0].CommandLine.push_back(FilePath);
373 Result[0].Filename = FilePath;
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);
390 static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
391 X("fixed-compilation-database", "Reads plain-text flags file");
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;
401 } // namespace tooling