30 #include "llvm/ADT/ArrayRef.h" 31 #include "llvm/ADT/IntrusiveRefCntPtr.h" 32 #include "llvm/ADT/STLExtras.h" 33 #include "llvm/ADT/SmallString.h" 34 #include "llvm/ADT/SmallVector.h" 35 #include "llvm/ADT/StringRef.h" 36 #include "llvm/Option/Arg.h" 37 #include "llvm/Support/Casting.h" 38 #include "llvm/Support/Compiler.h" 39 #include "llvm/Support/ErrorOr.h" 40 #include "llvm/Support/Host.h" 41 #include "llvm/Support/LineIterator.h" 42 #include "llvm/Support/MemoryBuffer.h" 43 #include "llvm/Support/Path.h" 44 #include "llvm/Support/raw_ostream.h" 52 #include <system_error> 56 using namespace clang;
57 using namespace tooling;
63 std::unique_ptr<CompilationDatabase>
65 std::string &ErrorMessage) {
66 llvm::raw_string_ostream ErrorStream(ErrorMessage);
67 for (CompilationDatabasePluginRegistry::iterator
68 It = CompilationDatabasePluginRegistry::begin(),
69 Ie = CompilationDatabasePluginRegistry::end();
71 std::string DatabaseErrorMessage;
72 std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
73 if (std::unique_ptr<CompilationDatabase> DB =
74 Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
76 ErrorStream << It->getName() <<
": " << DatabaseErrorMessage <<
"\n";
81 static std::unique_ptr<CompilationDatabase>
83 std::string &ErrorMessage) {
84 std::stringstream ErrorStream;
85 bool HasErrorMessage =
false;
86 while (!Directory.empty()) {
87 std::string LoadErrorMessage;
89 if (std::unique_ptr<CompilationDatabase> DB =
93 if (!HasErrorMessage) {
94 ErrorStream <<
"No compilation database found in " << Directory.str()
95 <<
" or any parent directory\n" << LoadErrorMessage;
96 HasErrorMessage =
true;
99 Directory = llvm::sys::path::parent_path(Directory);
101 ErrorMessage = ErrorStream.str();
105 std::unique_ptr<CompilationDatabase>
107 std::string &ErrorMessage) {
109 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
111 std::unique_ptr<CompilationDatabase> DB =
115 ErrorMessage = (
"Could not auto-detect compilation database for file \"" +
116 SourceFile +
"\"\n" + ErrorMessage).
str();
120 std::unique_ptr<CompilationDatabase>
122 std::string &ErrorMessage) {
125 std::unique_ptr<CompilationDatabase> DB =
129 ErrorMessage = (
"Could not auto-detect compilation database from directory \"" +
130 SourceDir +
"\"\n" + ErrorMessage).
str();
135 std::vector<CompileCommand> Result;
138 std::move(C.begin(), C.end(), std::back_inserter(Result));
149 struct CompileJobAnalyzer {
158 bool CollectChildren = Collect;
161 CollectChildren =
true;
166 const auto *IA = cast<driver::InputAction>(A);
167 Inputs.push_back(IA->getInputArg().getSpelling());
177 runImpl(AI, CollectChildren);
190 if (Info.getID() == diag::warn_drv_input_file_unused) {
192 UnusedInputs.push_back(Info.getArgStdStr(0));
209 bool operator() (StringRef S) {
210 for (
const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
223 struct FilterUnusedFlags {
224 bool operator() (StringRef S) {
225 return (S ==
"-no-integrated-as") || S.startswith(
"-Wa,");
229 std::string GetClangToolCommand() {
231 std::string ClangExecutable =
232 llvm::sys::fs::getMainExecutable(
"clang", (
void *)&Dummy);
234 ClangToolPath = llvm::sys::path::parent_path(ClangExecutable);
235 llvm::sys::path::append(ClangToolPath,
"clang-tool");
236 return ClangToolPath.str();
261 std::vector<std::string> &Result,
262 std::string &ErrorMsg) {
264 llvm::raw_string_ostream Output(ErrorMsg);
266 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
269 &*DiagOpts, &DiagClient,
false);
274 "", llvm::sys::getDefaultTargetTriple(),
276 NewDriver->setCheckInputsExist(
false);
280 std::string Argv0 = GetClangToolCommand();
281 Args.insert(Args.begin(), Argv0.c_str());
289 Args.push_back(
"-c");
295 Args.push_back(
"placeholder.cpp");
297 Args.erase(std::remove_if(Args.begin(), Args.end(), FilterUnusedFlags()),
300 const std::unique_ptr<driver::Compilation> Compilation(
301 NewDriver->BuildCompilation(Args));
307 CompileJobAnalyzer CompileAnalyzer;
309 for (
const auto &
Cmd : Jobs) {
316 CompileAnalyzer.run(&
Cmd.getSource());
320 if (CompileAnalyzer.Inputs.empty()) {
321 ErrorMsg =
"warning: no compile jobs found\n";
328 std::vector<const char *>::iterator
End = std::remove_if(
329 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
332 End = std::remove_if(Args.begin(),
End, MatchesAny(DiagClient.UnusedInputs));
335 assert(strcmp(*(End - 1),
"-c") == 0);
338 Result = std::vector<std::string>(Args.begin() + 1,
End);
342 std::unique_ptr<FixedCompilationDatabase>
344 const char *
const *Argv,
345 std::string &ErrorMsg,
350 const char *
const *DoubleDash = std::find(Argv, Argv + Argc, StringRef(
"--"));
351 if (DoubleDash == Argv + Argc)
353 std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
354 Argc = DoubleDash - Argv;
356 std::vector<std::string> StrippedArgs;
359 return llvm::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
362 std::unique_ptr<FixedCompilationDatabase>
365 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
366 llvm::MemoryBuffer::getFile(Path);
367 if (std::error_code Result = File.getError()) {
368 ErrorMsg =
"Error while opening fixed database: " + Result.message();
371 std::vector<std::string> Args{llvm::line_iterator(**File),
372 llvm::line_iterator()};
373 return llvm::make_unique<FixedCompilationDatabase>(
374 llvm::sys::path::parent_path(Path), std::move(Args));
379 std::vector<std::string> ToolCommandLine(1, GetClangToolCommand());
380 ToolCommandLine.insert(ToolCommandLine.end(),
381 CommandLine.begin(), CommandLine.end());
382 CompileCommands.emplace_back(Directory, StringRef(),
383 std::move(ToolCommandLine),
387 std::vector<CompileCommand>
389 std::vector<CompileCommand> Result(CompileCommands);
390 Result[0].CommandLine.push_back(FilePath);
391 Result[0].Filename = FilePath;
398 std::unique_ptr<CompilationDatabase>
401 llvm::sys::path::append(DatabasePath,
"compile_flags.txt");
408 static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
409 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