#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/LLVM.h"
#include "clang/Driver/Action.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Job.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/CompilationDatabasePluginRegistry.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/Arg.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iterator>
#include <memory>
#include <sstream>
#include <string>
#include <system_error>
#include <utility>
#include <vector>
using namespace clang;
using namespace tooling;
LLVM_INSTANTIATE_REGISTRY(CompilationDatabasePluginRegistry)
CompilationDatabase::~CompilationDatabase() = default;
std::unique_ptr<CompilationDatabase>
CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
std::string &ErrorMessage) {
llvm::raw_string_ostream ErrorStream(ErrorMessage);
for (const CompilationDatabasePluginRegistry::entry &Database :
CompilationDatabasePluginRegistry::entries()) {
std::string DatabaseErrorMessage;
std::unique_ptr<CompilationDatabasePlugin> Plugin(Database.instantiate());
if (std::unique_ptr<CompilationDatabase> DB =
Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
return DB;
ErrorStream << Database.getName() << ": " << DatabaseErrorMessage << "\n";
}
return nullptr;
}
static std::unique_ptr<CompilationDatabase>
findCompilationDatabaseFromDirectory(StringRef Directory,
std::string &ErrorMessage) {
std::stringstream ErrorStream;
bool HasErrorMessage = false;
while (!Directory.empty()) {
std::string LoadErrorMessage;
if (std::unique_ptr<CompilationDatabase> DB =
CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
return DB;
if (!HasErrorMessage) {
ErrorStream << "No compilation database found in " << Directory.str()
<< " or any parent directory\n" << LoadErrorMessage;
HasErrorMessage = true;
}
Directory = llvm::sys::path::parent_path(Directory);
}
ErrorMessage = ErrorStream.str();
return nullptr;
}
std::unique_ptr<CompilationDatabase>
CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
std::string &ErrorMessage) {
SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
std::unique_ptr<CompilationDatabase> DB =
findCompilationDatabaseFromDirectory(Directory, ErrorMessage);
if (!DB)
ErrorMessage = ("Could not auto-detect compilation database for file \"" +
SourceFile + "\"\n" + ErrorMessage).str();
return DB;
}
std::unique_ptr<CompilationDatabase>
CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir,
std::string &ErrorMessage) {
SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
std::unique_ptr<CompilationDatabase> DB =
findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage);
if (!DB)
ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
SourceDir + "\"\n" + ErrorMessage).str();
return DB;
}
std::vector<CompileCommand> CompilationDatabase::getAllCompileCommands() const {
std::vector<CompileCommand> Result;
for (const auto &File : getAllFiles()) {
auto C = getCompileCommands(File);
std::move(C.begin(), C.end(), std::back_inserter(Result));
}
return Result;
}
CompilationDatabasePlugin::~CompilationDatabasePlugin() = default;
namespace {
struct CompileJobAnalyzer {
SmallVector<std::string, 2> Inputs;
void run(const driver::Action *A) {
runImpl(A, false);
}
private:
void runImpl(const driver::Action *A, bool Collect) {
bool CollectChildren = Collect;
switch (A->getKind()) {
case driver::Action::CompileJobClass:
CollectChildren = true;
break;
case driver::Action::InputClass:
if (Collect) {
const auto *IA = cast<driver::InputAction>(A);
Inputs.push_back(std::string(IA->getInputArg().getSpelling()));
}
break;
default:
break;
}
for (const driver::Action *AI : A->inputs())
runImpl(AI, CollectChildren);
}
};
class UnusedInputDiagConsumer : public DiagnosticConsumer {
public:
UnusedInputDiagConsumer(DiagnosticConsumer &Other) : Other(Other) {}
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info) override {
if (Info.getID() == diag::warn_drv_input_file_unused) {
UnusedInputs.push_back(Info.getArgStdStr(0));
} else if (DiagLevel >= DiagnosticsEngine::Error) {
Other.HandleDiagnostic(DiagLevel, Info);
}
}
DiagnosticConsumer &Other;
SmallVector<std::string, 2> UnusedInputs;
};
struct FilterUnusedFlags {
bool operator() (StringRef S) {
return (S == "-no-integrated-as") || S.startswith("-Wa,");
}
};
std::string GetClangToolCommand() {
static int Dummy;
std::string ClangExecutable =
llvm::sys::fs::getMainExecutable("clang", (void *)&Dummy);
SmallString<128> ClangToolPath;
ClangToolPath = llvm::sys::path::parent_path(ClangExecutable);
llvm::sys::path::append(ClangToolPath, "clang-tool");
return std::string(ClangToolPath.str());
}
}
static bool stripPositionalArgs(std::vector<const char *> Args,
std::vector<std::string> &Result,
std::string &ErrorMsg) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
llvm::raw_string_ostream Output(ErrorMsg);
TextDiagnosticPrinter DiagnosticPrinter(Output, &*DiagOpts);
UnusedInputDiagConsumer DiagClient(DiagnosticPrinter);
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
&*DiagOpts, &DiagClient, false);
std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
"", llvm::sys::getDefaultTargetTriple(),
Diagnostics));
NewDriver->setCheckInputsExist(false);
std::string Argv0 = GetClangToolCommand();
Args.insert(Args.begin(), Argv0.c_str());
Args.push_back("-c");
Args.push_back("placeholder.cpp");
llvm::erase_if(Args, FilterUnusedFlags());
const std::unique_ptr<driver::Compilation> Compilation(
NewDriver->BuildCompilation(Args));
if (!Compilation)
return false;
const driver::JobList &Jobs = Compilation->getJobs();
CompileJobAnalyzer CompileAnalyzer;
for (const auto &Cmd : Jobs) {
if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass ||
Cmd.getSource().getKind() == driver::Action::BackendJobClass ||
Cmd.getSource().getKind() == driver::Action::CompileJobClass) {
CompileAnalyzer.run(&Cmd.getSource());
}
}
if (CompileAnalyzer.Inputs.empty()) {
ErrorMsg = "warning: no compile jobs found\n";
return false;
}
std::vector<const char *>::iterator End =
llvm::remove_if(Args, [&](StringRef S) {
return llvm::is_contained(CompileAnalyzer.Inputs, S) ||
llvm::is_contained(DiagClient.UnusedInputs, S);
});
assert(strcmp(*(End - 1), "-c") == 0);
--End;
Result = std::vector<std::string>(Args.begin() + 1, End);
return true;
}
std::unique_ptr<FixedCompilationDatabase>
FixedCompilationDatabase::loadFromCommandLine(int &Argc,
const char *const *Argv,
std::string &ErrorMsg,
const Twine &Directory) {
ErrorMsg.clear();
if (Argc == 0)
return nullptr;
const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
if (DoubleDash == Argv + Argc)
return nullptr;
std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
Argc = DoubleDash - Argv;
std::vector<std::string> StrippedArgs;
if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg))
return nullptr;
return std::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
}
std::unique_ptr<FixedCompilationDatabase>
FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) {
ErrorMsg.clear();
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
llvm::MemoryBuffer::getFile(Path);
if (std::error_code Result = File.getError()) {
ErrorMsg = "Error while opening fixed database: " + Result.message();
return nullptr;
}
return loadFromBuffer(llvm::sys::path::parent_path(Path),
(*File)->getBuffer(), ErrorMsg);
}
std::unique_ptr<FixedCompilationDatabase>
FixedCompilationDatabase::loadFromBuffer(StringRef Directory, StringRef Data,
std::string &ErrorMsg) {
ErrorMsg.clear();
std::vector<std::string> Args;
StringRef Line;
while (!Data.empty()) {
std::tie(Line, Data) = Data.split('\n');
Line = Line.trim();
if (!Line.empty())
Args.push_back(Line.str());
}
return std::make_unique<FixedCompilationDatabase>(Directory, std::move(Args));
}
FixedCompilationDatabase::FixedCompilationDatabase(
const Twine &Directory, ArrayRef<std::string> CommandLine) {
std::vector<std::string> ToolCommandLine(1, GetClangToolCommand());
ToolCommandLine.insert(ToolCommandLine.end(),
CommandLine.begin(), CommandLine.end());
CompileCommands.emplace_back(Directory, StringRef(),
std::move(ToolCommandLine),
StringRef());
}
std::vector<CompileCommand>
FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const {
std::vector<CompileCommand> Result(CompileCommands);
Result[0].CommandLine.push_back(std::string(FilePath));
Result[0].Filename = std::string(FilePath);
return Result;
}
namespace {
class FixedCompilationDatabasePlugin : public CompilationDatabasePlugin {
std::unique_ptr<CompilationDatabase>
loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
SmallString<1024> DatabasePath(Directory);
llvm::sys::path::append(DatabasePath, "compile_flags.txt");
return FixedCompilationDatabase::loadFromFile(DatabasePath, ErrorMessage);
}
};
}
static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
X("fixed-compilation-database", "Reads plain-text flags file");
namespace clang {
namespace tooling {
extern volatile int JSONAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource;
} }