26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Option/Arg.h"
28 #include "llvm/Support/Host.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/raw_ostream.h"
32 #include <system_error>
33 using namespace clang;
34 using namespace tooling;
40 std::unique_ptr<CompilationDatabase>
42 std::string &ErrorMessage) {
43 llvm::raw_string_ostream ErrorStream(ErrorMessage);
44 for (CompilationDatabasePluginRegistry::iterator
45 It = CompilationDatabasePluginRegistry::begin(),
46 Ie = CompilationDatabasePluginRegistry::end();
48 std::string DatabaseErrorMessage;
49 std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
50 if (std::unique_ptr<CompilationDatabase> DB =
51 Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
53 ErrorStream << It->getName() <<
": " << DatabaseErrorMessage <<
"\n";
58 static std::unique_ptr<CompilationDatabase>
60 std::string &ErrorMessage) {
61 std::stringstream ErrorStream;
62 bool HasErrorMessage =
false;
63 while (!Directory.empty()) {
64 std::string LoadErrorMessage;
66 if (std::unique_ptr<CompilationDatabase> DB =
70 if (!HasErrorMessage) {
71 ErrorStream <<
"No compilation database found in " << Directory.str()
72 <<
" or any parent directory\n" << LoadErrorMessage;
73 HasErrorMessage =
true;
76 Directory = llvm::sys::path::parent_path(Directory);
78 ErrorMessage = ErrorStream.str();
82 std::unique_ptr<CompilationDatabase>
84 std::string &ErrorMessage) {
86 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
88 std::unique_ptr<CompilationDatabase> DB =
92 ErrorMessage = (
"Could not auto-detect compilation database for file \"" +
93 SourceFile +
"\"\n" + ErrorMessage).str();
97 std::unique_ptr<CompilationDatabase>
99 std::string &ErrorMessage) {
102 std::unique_ptr<CompilationDatabase> DB =
106 ErrorMessage = (
"Could not auto-detect compilation database from directory \"" +
107 SourceDir +
"\"\n" + ErrorMessage).str();
116 struct CompileJobAnalyzer {
126 bool CollectChildren = Collect;
129 CollectChildren =
true;
145 runImpl(AI, CollectChildren);
158 if (Info.
getID() == clang::diag::warn_drv_input_file_unused) {
176 bool operator() (StringRef
S) {
177 for (
const std::string *
I = Arr.begin(), *
E = Arr.end();
I !=
E; ++
I)
207 std::vector<std::string> &Result,
208 std::string &ErrorMsg) {
210 llvm::raw_string_ostream Output(ErrorMsg);
212 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
215 &*DiagOpts, &DiagClient,
false);
220 "", llvm::sys::getDefaultTargetTriple(),
222 NewDriver->setCheckInputsExist(
false);
226 Args.insert(Args.begin(),
"clang-tool");
234 Args.push_back(
"-c");
240 Args.push_back(
"placeholder.cpp");
244 Args.erase(std::remove_if(Args.begin(), Args.end(),
245 MatchesAny(std::string(
"-no-integrated-as"))),
248 const std::unique_ptr<driver::Compilation> Compilation(
249 NewDriver->BuildCompilation(Args));
255 CompileJobAnalyzer CompileAnalyzer;
257 for (
const auto &Cmd : Jobs) {
262 CompileAnalyzer.run(&Cmd.getSource());
266 if (CompileAnalyzer.Inputs.empty()) {
267 ErrorMsg =
"warning: no compile jobs found\n";
274 std::vector<const char *>::iterator
End = std::remove_if(
275 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
278 End = std::remove_if(Args.begin(),
End, MatchesAny(DiagClient.UnusedInputs));
281 assert(strcmp(*(End - 1),
"-c") == 0);
284 Result = std::vector<std::string>(Args.begin() + 1,
End);
288 std::unique_ptr<FixedCompilationDatabase>
290 const char *
const *Argv,
291 std::string &ErrorMsg,
296 const char *
const *DoubleDash = std::find(Argv, Argv + Argc, StringRef(
"--"));
297 if (DoubleDash == Argv + Argc)
299 std::vector<const char *>
CommandLine(DoubleDash + 1, Argv + Argc);
300 Argc = DoubleDash - Argv;
302 std::vector<std::string> StrippedArgs;
305 return std::unique_ptr<FixedCompilationDatabase>(
311 std::vector<std::string> ToolCommandLine(1,
"clang-tool");
312 ToolCommandLine.insert(ToolCommandLine.end(),
313 CommandLine.begin(), CommandLine.end());
314 CompileCommands.emplace_back(Directory, StringRef(),
315 std::move(ToolCommandLine),
319 std::vector<CompileCommand>
321 std::vector<CompileCommand>
Result(CompileCommands);
322 Result[0].CommandLine.push_back(FilePath);
327 std::vector<std::string>
329 return std::vector<std::string>();
332 std::vector<CompileCommand>
334 return std::vector<CompileCommand>();
static std::unique_ptr< CompilationDatabase > findCompilationDatabaseFromDirectory(StringRef Directory, std::string &ErrorMessage)
const std::string & getArgStdStr(unsigned Idx) const
Return the provided argument string specified by Idx.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
ActionClass getKind() const
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...
detail::InMemoryDirectory::const_iterator I
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.
Used for handling and querying diagnostic IDs.
detail::InMemoryDirectory::const_iterator E
std::vector< std::string > CommandLine
Level
The level of the diagnostic, after it has been through mapping.
const list_type & getJobs() const
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...