29 #include "llvm/ADT/SmallString.h" 30 #include "llvm/Option/Arg.h" 31 #include "llvm/Support/Host.h" 32 #include "llvm/Support/LineIterator.h" 33 #include "llvm/Support/Path.h" 34 #include "llvm/Support/raw_ostream.h" 36 #include <system_error> 37 using namespace clang;
38 using namespace tooling;
44 std::unique_ptr<CompilationDatabase>
46 std::string &ErrorMessage) {
47 llvm::raw_string_ostream ErrorStream(ErrorMessage);
48 for (CompilationDatabasePluginRegistry::iterator
49 It = CompilationDatabasePluginRegistry::begin(),
50 Ie = CompilationDatabasePluginRegistry::end();
52 std::string DatabaseErrorMessage;
53 std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
54 if (std::unique_ptr<CompilationDatabase> DB =
55 Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
57 ErrorStream << It->getName() <<
": " << DatabaseErrorMessage <<
"\n";
62 static std::unique_ptr<CompilationDatabase>
64 std::string &ErrorMessage) {
65 std::stringstream ErrorStream;
66 bool HasErrorMessage =
false;
67 while (!Directory.empty()) {
68 std::string LoadErrorMessage;
70 if (std::unique_ptr<CompilationDatabase> DB =
74 if (!HasErrorMessage) {
75 ErrorStream <<
"No compilation database found in " << Directory.str()
76 <<
" or any parent directory\n" << LoadErrorMessage;
77 HasErrorMessage =
true;
80 Directory = llvm::sys::path::parent_path(Directory);
82 ErrorMessage = ErrorStream.str();
86 std::unique_ptr<CompilationDatabase>
88 std::string &ErrorMessage) {
90 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
92 std::unique_ptr<CompilationDatabase> DB =
96 ErrorMessage = (
"Could not auto-detect compilation database for file \"" +
97 SourceFile +
"\"\n" + ErrorMessage).str();
101 std::unique_ptr<CompilationDatabase>
103 std::string &ErrorMessage) {
106 std::unique_ptr<CompilationDatabase> DB =
110 ErrorMessage = (
"Could not auto-detect compilation database from directory \"" +
111 SourceDir +
"\"\n" + ErrorMessage).str();
116 std::vector<CompileCommand> Result;
119 std::move(C.begin(), C.end(), std::back_inserter(Result));
129 struct CompileJobAnalyzer {
139 bool CollectChildren = Collect;
142 CollectChildren =
true;
158 runImpl(AI, CollectChildren);
171 if (Info.getID() == clang::diag::warn_drv_input_file_unused) {
173 UnusedInputs.push_back(Info.getArgStdStr(0));
189 bool operator() (StringRef S) {
190 for (
const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
220 std::vector<std::string> &Result,
221 std::string &ErrorMsg) {
223 llvm::raw_string_ostream Output(ErrorMsg);
225 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
228 &*DiagOpts, &DiagClient,
false);
233 "", llvm::sys::getDefaultTargetTriple(),
235 NewDriver->setCheckInputsExist(
false);
239 Args.insert(Args.begin(),
"clang-tool");
247 Args.push_back(
"-c");
253 Args.push_back(
"placeholder.cpp");
257 Args.erase(std::remove_if(Args.begin(), Args.end(),
258 MatchesAny(std::string(
"-no-integrated-as"))),
261 const std::unique_ptr<driver::Compilation> Compilation(
262 NewDriver->BuildCompilation(Args));
268 CompileJobAnalyzer CompileAnalyzer;
270 for (
const auto &Cmd : Jobs) {
275 CompileAnalyzer.run(&Cmd.getSource());
279 if (CompileAnalyzer.Inputs.empty()) {
280 ErrorMsg =
"warning: no compile jobs found\n";
287 std::vector<const char *>::iterator
End = std::remove_if(
288 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
291 End = std::remove_if(Args.begin(),
End, MatchesAny(DiagClient.UnusedInputs));
294 assert(strcmp(*(End - 1),
"-c") == 0);
297 Result = std::vector<std::string>(Args.begin() + 1,
End);
301 std::unique_ptr<FixedCompilationDatabase>
303 const char *
const *Argv,
304 std::string &ErrorMsg,
309 const char *
const *DoubleDash = std::find(Argv, Argv + Argc, StringRef(
"--"));
310 if (DoubleDash == Argv + Argc)
312 std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
313 Argc = DoubleDash - Argv;
315 std::vector<std::string> StrippedArgs;
318 return llvm::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
321 std::unique_ptr<FixedCompilationDatabase>
324 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
325 llvm::MemoryBuffer::getFile(Path);
326 if (std::error_code Result = File.getError()) {
327 ErrorMsg =
"Error while opening fixed database: " + Result.message();
330 std::vector<std::string> Args{llvm::line_iterator(**File),
331 llvm::line_iterator()};
332 return llvm::make_unique<FixedCompilationDatabase>(
333 llvm::sys::path::parent_path(Path), std::move(Args));
338 std::vector<std::string> ToolCommandLine(1,
"clang-tool");
339 ToolCommandLine.insert(ToolCommandLine.end(),
340 CommandLine.begin(), CommandLine.end());
341 CompileCommands.emplace_back(Directory, StringRef(),
342 std::move(ToolCommandLine),
346 std::vector<CompileCommand>
348 std::vector<CompileCommand> Result(CompileCommands);
349 Result[0].CommandLine.push_back(FilePath);
350 Result[0].Filename = FilePath;
357 std::unique_ptr<CompilationDatabase>
360 llvm::sys::path::append(DatabasePath,
"compile_flags.txt");
365 static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
366 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.
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.
const list_type & getJobs() const