30#include "llvm/ADT/STLExtras.h"
31#include "llvm/ADT/StringRef.h"
32#include "llvm/Option/Arg.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/ErrorOr.h"
35#include "llvm/Support/LineIterator.h"
36#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/Path.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/TargetParser/Host.h"
47#include <system_error>
59std::unique_ptr<CompilationDatabase>
61 std::string &ErrorMessage) {
62 llvm::raw_string_ostream ErrorStream(ErrorMessage);
63 for (
const CompilationDatabasePluginRegistry::entry &Database :
64 CompilationDatabasePluginRegistry::entries()) {
65 std::string DatabaseErrorMessage;
66 std::unique_ptr<CompilationDatabasePlugin> Plugin(Database.instantiate());
67 if (std::unique_ptr<CompilationDatabase> DB =
68 Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
70 ErrorStream << Database.getName() <<
": " << DatabaseErrorMessage <<
"\n";
75static std::unique_ptr<CompilationDatabase>
77 std::string &ErrorMessage) {
78 std::stringstream ErrorStream;
79 bool HasErrorMessage =
false;
80 while (!Directory.empty()) {
81 std::string LoadErrorMessage;
83 if (std::unique_ptr<CompilationDatabase> DB =
87 if (!HasErrorMessage) {
88 ErrorStream <<
"No compilation database found in " << Directory.str()
89 <<
" or any parent directory\n" << LoadErrorMessage;
90 HasErrorMessage =
true;
93 Directory = llvm::sys::path::parent_path(Directory);
95 ErrorMessage = ErrorStream.str();
99std::unique_ptr<CompilationDatabase>
101 std::string &ErrorMessage) {
103 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
105 std::unique_ptr<CompilationDatabase> DB =
109 ErrorMessage = (
"Could not auto-detect compilation database for file \"" +
110 SourceFile +
"\"\n" + ErrorMessage).str();
114std::unique_ptr<CompilationDatabase>
116 std::string &ErrorMessage) {
119 std::unique_ptr<CompilationDatabase> DB =
123 ErrorMessage = (
"Could not auto-detect compilation database from directory \"" +
124 SourceDir +
"\"\n" + ErrorMessage).str();
129 std::vector<CompileCommand>
Result;
132 std::move(
C.begin(),
C.end(), std::back_inserter(
Result));
143struct CompileJobAnalyzer {
152 bool CollectChildren = Collect;
156 CollectChildren =
true;
162 Inputs.push_back(std::string(IA->getInputArg().getSpelling()));
171 for (
const driver::Action *AI : A->
inputs())
172 runImpl(AI, CollectChildren);
179class UnusedInputDiagConsumer :
public DiagnosticConsumer {
184 const Diagnostic &Info)
override {
185 if (Info.
getID() == diag::warn_drv_input_file_unused) {
191 Other.HandleDiagnostic(DiagLevel, Info);
195 DiagnosticConsumer &
Other;
196 SmallVector<std::string, 2> UnusedInputs;
202struct FilterUnusedFlags {
203 bool operator() (StringRef S) {
204 return (S ==
"-no-integrated-as") || S.starts_with(
"-Wa,");
208std::string GetClangToolCommand() {
210 std::string ClangExecutable =
211 llvm::sys::fs::getMainExecutable(
"clang", (
void *)&Dummy);
213 ClangToolPath = llvm::sys::path::parent_path(ClangExecutable);
214 llvm::sys::path::append(ClangToolPath,
"clang-tool");
215 return std::string(ClangToolPath);
240 std::vector<std::string> &
Result,
241 std::string &ErrorMsg) {
243 llvm::raw_string_ostream Output(ErrorMsg);
245 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
252 "", llvm::sys::getDefaultTargetTriple(),
254 NewDriver->setCheckInputsExist(
false);
258 std::string Argv0 = GetClangToolCommand();
259 Args.insert(Args.begin(), Argv0.c_str());
267 Args.push_back(
"-c");
273 Args.push_back(
"placeholder.cpp");
275 llvm::erase_if(Args, FilterUnusedFlags());
277 const std::unique_ptr<driver::Compilation> Compilation(
278 NewDriver->BuildCompilation(Args));
284 CompileJobAnalyzer CompileAnalyzer;
286 for (
const auto &Cmd : Jobs) {
294 CompileAnalyzer.run(&Cmd.getSource());
298 if (CompileAnalyzer.Inputs.empty()) {
299 ErrorMsg =
"warning: no compile jobs found\n";
306 std::vector<const char *>::iterator End =
307 llvm::remove_if(Args, [&](StringRef S) {
308 return llvm::is_contained(CompileAnalyzer.Inputs, S) ||
309 llvm::is_contained(DiagClient.UnusedInputs, S);
312 assert(strcmp(*(End - 1),
"-c") == 0);
315 Result = std::vector<std::string>(Args.begin() + 1, End);
319std::unique_ptr<FixedCompilationDatabase>
321 const char *
const *Argv,
322 std::string &ErrorMsg,
323 const Twine &Directory) {
327 const char *
const *DoubleDash = std::find(Argv, Argv + Argc, StringRef(
"--"));
328 if (DoubleDash == Argv + Argc)
330 std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
331 Argc = DoubleDash - Argv;
333 std::vector<std::string> StrippedArgs;
336 return std::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
339std::unique_ptr<FixedCompilationDatabase>
342 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
File =
343 llvm::MemoryBuffer::getFile(Path);
344 if (std::error_code
Result =
File.getError()) {
345 ErrorMsg =
"Error while opening fixed database: " +
Result.message();
349 (*File)->getBuffer(), ErrorMsg);
352std::unique_ptr<FixedCompilationDatabase>
354 std::string &ErrorMsg) {
356 std::vector<std::string> Args;
358 while (!
Data.empty()) {
363 Args.push_back(
Line.str());
365 return std::make_unique<FixedCompilationDatabase>(Directory, std::move(Args));
370 std::vector<std::string> ToolCommandLine(1, GetClangToolCommand());
371 ToolCommandLine.insert(ToolCommandLine.end(),
372 CommandLine.begin(), CommandLine.end());
373 CompileCommands.emplace_back(Directory, StringRef(),
374 std::move(ToolCommandLine),
378std::vector<CompileCommand>
380 std::vector<CompileCommand>
Result(CompileCommands);
381 Result[0].CommandLine.push_back(std::string(FilePath));
382 Result[0].Filename = std::string(FilePath);
389 std::unique_ptr<CompilationDatabase>
390 loadFromDirectory(StringRef Directory, std::string &ErrorMessage)
override {
392 llvm::sys::path::append(DatabasePath,
"compile_flags.txt");
399static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
400X(
"fixed-compilation-database",
"Reads plain-text flags file");
Defines the Diagnostic-related interfaces.
static std::unique_ptr< CompilationDatabase > findCompilationDatabaseFromDirectory(StringRef Directory, std::string &ErrorMessage)
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...
Defines the Diagnostic IDs-related interfaces.
LLVM_INSTANTIATE_REGISTRY_EX(CLANG_ABI_EXPORT, clang::tooling::ToolExecutorPluginRegistry) namespace clang
Result
Implement __builtin_bit_cast and related operations.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static llvm::IntrusiveRefCntPtr< DiagnosticIDs > create()
Options for controlling the compiler diagnostics engine.
const std::string & getArgStdStr(unsigned Idx) const
Return the provided argument string specified by Idx.
Concrete class used by the front-end to report problems and issues.
Level
The level of the diagnostic, after it has been through mapping.
Action - Represent an abstract compilation step to perform.
ActionClass getKind() const
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
JobList - A sequence of jobs to perform.
const list_type & getJobs() const
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
U cast(CodeGen::Address addr)
@ Other
Other implicit parameter.