Files
clice/include/Clang/CompileDatabase.h
2024-07-04 22:42:29 +08:00

36 lines
979 B
C++

#include <Clang/Clang.h>
namespace clice {
class CompileDatabase {
private:
std::unique_ptr<clang::tooling::CompilationDatabase> database;
public:
static auto& instance() {
static CompileDatabase instance;
return instance;
}
void load(std::string_view path) {
std::string error;
database = clang::tooling::CompilationDatabase::loadFromDirectory(path, error);
if(!database) {
llvm::errs() << "Failed to load compilation database. " << error << "\n";
std::terminate();
}
}
auto lookup(std::string_view path) {
auto commands = database->getCompileCommands(path);
llvm::ArrayRef command = commands[0].CommandLine;
std::vector<const char*> args = {command.front().c_str(), "-Xclang", "-no-round-trip-args"};
for(auto& arg: command.drop_front()) {
args.push_back(arg.c_str());
}
return args;
}
};
} // namespace clice