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;
64 std::unique_ptr<CompilationDatabase>
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>
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 =
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>
108 std::string &ErrorMessage) {
110 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
112 std::unique_ptr<CompilationDatabase> DB =
116 ErrorMessage = (
"Could not auto-detect compilation database for file \"" +
117 SourceFile +
"\"\n" + ErrorMessage).str();
121 std::unique_ptr<CompilationDatabase>
123 std::string &ErrorMessage) {
126 std::unique_ptr<CompilationDatabase> DB =
130 ErrorMessage = (
"Could not auto-detect compilation database from directory \"" +
131 SourceDir +
"\"\n" + ErrorMessage).str();
136 std::vector<CompileCommand> Result;
139 std::move(C.begin(), C.end(), std::back_inserter(Result));
150 struct CompileJobAnalyzer {
159 bool CollectChildren = Collect;
162 CollectChildren =
true;
167 const auto *IA = cast<driver::InputAction>(A);
168 Inputs.push_back(IA->getInputArg().getSpelling());
178 runImpl(AI, CollectChildren);
191 if (Info.getID() == diag::warn_drv_input_file_unused) {
193 UnusedInputs.push_back(Info.getArgStdStr(0));
210 bool operator() (StringRef S) {
211 for (
const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
243 std::vector<std::string> &Result,
244 std::string &ErrorMsg) {
246 llvm::raw_string_ostream Output(ErrorMsg);
248 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
251 &*DiagOpts, &DiagClient,
false);
256 "", llvm::sys::getDefaultTargetTriple(),
258 NewDriver->setCheckInputsExist(
false);
262 Args.insert(Args.begin(),
"clang-tool");
270 Args.push_back(
"-c");
276 Args.push_back(
"placeholder.cpp");
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));
291 CompileJobAnalyzer CompileAnalyzer;
293 for (
const auto &
Cmd : Jobs) {
298 CompileAnalyzer.run(&
Cmd.getSource());
302 if (CompileAnalyzer.Inputs.empty()) {
303 ErrorMsg =
"warning: no compile jobs found\n";
310 std::vector<const char *>::iterator
End = std::remove_if(
311 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
314 End = std::remove_if(Args.begin(),
End, MatchesAny(DiagClient.UnusedInputs));
317 assert(strcmp(*(End - 1),
"-c") == 0);
320 Result = std::vector<std::string>(Args.begin() + 1,
End);
324 std::unique_ptr<FixedCompilationDatabase>
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;
341 return llvm::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
344 std::unique_ptr<FixedCompilationDatabase>
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));
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>
371 std::vector<CompileCommand> Result(CompileCommands);
372 Result[0].CommandLine.push_back(FilePath);
373 Result[0].Filename = FilePath;
380 std::unique_ptr<CompilationDatabase>
383 llvm::sys::path::append(DatabasePath,
"compile_flags.txt");
390 static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
391 X(
"fixed-compilation-database",
"Reads plain-text flags file");
static std::unique_ptr< CompilationDatabase > findCompilationDatabaseFromDirectory(StringRef Directory, std::string &ErrorMessage)
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Action - Represent an abstract compilation step to perform.
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.
Defines the Diagnostic-related interfaces.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
ActionClass getKind() const
JobList - A sequence of jobs to perform.
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...
Options for controlling the compiler diagnostics engine.
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.
Dataflow Directional Tag Classes.
Used for handling and querying diagnostic IDs.
Level
The level of the diagnostic, after it has been through mapping.
Defines the Diagnostic IDs-related interfaces.
const list_type & getJobs() const