26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Option/Arg.h"
28 #include "llvm/Support/Host.h"
29 #include "llvm/Support/Path.h"
31 #include <system_error>
32 using namespace clang;
33 using namespace tooling;
37 std::unique_ptr<CompilationDatabase>
39 std::string &ErrorMessage) {
40 std::stringstream ErrorStream;
41 for (CompilationDatabasePluginRegistry::iterator
42 It = CompilationDatabasePluginRegistry::begin(),
43 Ie = CompilationDatabasePluginRegistry::end();
45 std::string DatabaseErrorMessage;
46 std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
47 if (std::unique_ptr<CompilationDatabase> DB =
48 Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
50 ErrorStream << It->getName() <<
": " << DatabaseErrorMessage <<
"\n";
52 ErrorMessage = ErrorStream.str();
56 static std::unique_ptr<CompilationDatabase>
58 std::string &ErrorMessage) {
59 std::stringstream ErrorStream;
60 bool HasErrorMessage =
false;
61 while (!Directory.empty()) {
62 std::string LoadErrorMessage;
64 if (std::unique_ptr<CompilationDatabase> DB =
68 if (!HasErrorMessage) {
69 ErrorStream <<
"No compilation database found in " << Directory.str()
70 <<
" or any parent directory\n" << LoadErrorMessage;
71 HasErrorMessage =
true;
74 Directory = llvm::sys::path::parent_path(Directory);
76 ErrorMessage = ErrorStream.str();
80 std::unique_ptr<CompilationDatabase>
82 std::string &ErrorMessage) {
84 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
86 std::unique_ptr<CompilationDatabase> DB =
90 ErrorMessage = (
"Could not auto-detect compilation database for file \"" +
91 SourceFile +
"\"\n" + ErrorMessage).str();
95 std::unique_ptr<CompilationDatabase>
97 std::string &ErrorMessage) {
100 std::unique_ptr<CompilationDatabase> DB =
104 ErrorMessage = (
"Could not auto-detect compilation database from directory \"" +
105 SourceDir +
"\"\n" + ErrorMessage).str();
114 struct CompileJobAnalyzer {
124 bool CollectChildren = Collect;
127 CollectChildren =
true;
142 for (driver::ActionList::const_iterator I = A->
begin(), E = A->
end();
144 runImpl(*I, CollectChildren);
153 UnusedInputDiagConsumer() : Other(nullptr) {}
161 if (Info.
getID() == clang::diag::warn_drv_input_file_unused) {
177 bool operator() (StringRef
S) {
178 for (
const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
208 std::vector<std::string> &Result) {
210 UnusedInputDiagConsumer DiagClient;
213 &*DiagOpts, &DiagClient,
false);
218 "", llvm::sys::getDefaultTargetTriple(),
220 NewDriver->setCheckInputsExist(
false);
224 Args.insert(Args.begin(),
"clang-tool");
232 Args.push_back(
"-c");
238 Args.push_back(
"placeholder.cpp");
242 Args.erase(std::remove_if(Args.begin(), Args.end(),
243 MatchesAny(std::string(
"-no-integrated-as"))),
246 const std::unique_ptr<driver::Compilation> Compilation(
247 NewDriver->BuildCompilation(Args));
251 CompileJobAnalyzer CompileAnalyzer;
253 for (
const auto &Cmd : Jobs) {
257 CompileAnalyzer.run(&Cmd.getSource());
260 if (CompileAnalyzer.Inputs.empty()) {
269 std::vector<const char *>::iterator
End = std::remove_if(
270 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
273 End = std::remove_if(Args.begin(),
End, MatchesAny(DiagClient.UnusedInputs));
276 assert(strcmp(*(End - 1),
"-c") == 0);
279 Result = std::vector<std::string>(Args.begin() + 1,
End);
284 int &Argc,
const char *
const *Argv, Twine Directory) {
285 const char *
const *DoubleDash = std::find(Argv, Argv + Argc, StringRef(
"--"));
286 if (DoubleDash == Argv + Argc)
288 std::vector<const char *>
CommandLine(DoubleDash + 1, Argv + Argc);
289 Argc = DoubleDash - Argv;
291 std::vector<std::string> StrippedArgs;
299 std::vector<std::string> ToolCommandLine(1,
"clang-tool");
300 ToolCommandLine.insert(ToolCommandLine.end(),
301 CommandLine.begin(), CommandLine.end());
302 CompileCommands.emplace_back(Directory, std::move(ToolCommandLine));
305 std::vector<CompileCommand>
307 std::vector<CompileCommand>
Result(CompileCommands);
308 Result[0].CommandLine.push_back(FilePath);
312 std::vector<std::string>
314 return std::vector<std::string>();
317 std::vector<CompileCommand>
319 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
Concrete class used by the front-end to report problems and issues.
JobList - A sequence of jobs to perform.
The result type of a method or function.
static bool stripPositionalArgs(std::vector< const char * > Args, std::vector< std::string > &Result)
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.
std::vector< std::string > CommandLine
Defines the Diagnostic-related interfaces.
Level
The level of the diagnostic, after it has been through mapping.
const list_type & getJobs() const