27 #include "llvm/Support/CommandLine.h"
32 using namespace clang::tooling;
37 "-p <build-path> is used to read a compile command database.\n"
39 "\tFor example, it can be a CMake build directory in which a file named\n"
40 "\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n"
41 "\tCMake option to get this output). When no build path is specified,\n"
42 "\ta search for compile_commands.json will be attempted through all\n"
43 "\tparent paths of the first input file . See:\n"
44 "\thttp://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n"
45 "\texample of setting up Clang Tooling on a source tree.\n"
47 "<source0> ... specify the paths of source files. These paths are\n"
48 "\tlooked up in the compile command database. If the path of a file is\n"
49 "\tabsolute, it needs to point into CMake's source tree. If the path is\n"
50 "\trelative, the current working directory needs to be in the CMake\n"
51 "\tsource tree and the file must be in a subdirectory of the current\n"
52 "\tworking directory. \"./\" prefixes in the relative files will be\n"
53 "\tautomatically removed, but the rest of a relative path must be a\n"
54 "\tsuffix of a path in the compile command database.\n"
60 ArgumentsAdjustingCompilations(
61 std::unique_ptr<CompilationDatabase> Compilations)
62 : Compilations(std::move(Compilations)) {}
65 Adjusters.push_back(std::move(Adjuster));
68 std::vector<CompileCommand>
69 getCompileCommands(StringRef
FilePath)
const override {
70 return adjustCommands(Compilations->getCompileCommands(FilePath));
73 std::vector<std::string> getAllFiles()
const override {
74 return Compilations->getAllFiles();
77 std::vector<CompileCommand> getAllCompileCommands()
const override {
78 return adjustCommands(Compilations->getAllCompileCommands());
82 std::unique_ptr<CompilationDatabase> Compilations;
83 std::vector<ArgumentsAdjuster> Adjusters;
85 std::vector<CompileCommand>
86 adjustCommands(std::vector<CompileCommand> Commands)
const {
88 for (
const auto &Adjuster : Adjusters)
89 Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename);
96 int &argc,
const char **argv, cl::OptionCategory &
Category,
97 llvm::cl::NumOccurrencesFlag OccurrencesFlag,
const char *Overview) {
98 static cl::opt<bool> Help(
"h", cl::desc(
"Alias for -help"), cl::Hidden);
100 static cl::opt<std::string> BuildPath(
"p", cl::desc(
"Build path"),
101 cl::Optional, cl::cat(Category));
103 static cl::list<std::string> SourcePaths(
104 cl::Positional, cl::desc(
"<source0> [... <sourceN>]"), OccurrencesFlag,
107 static cl::list<std::string> ArgsAfter(
109 cl::desc(
"Additional argument to append to the compiler command line"),
112 static cl::list<std::string> ArgsBefore(
114 cl::desc(
"Additional argument to prepend to the compiler command line"),
117 cl::HideUnrelatedOptions(Category);
120 cl::ParseCommandLineOptions(argc, argv, Overview);
121 cl::PrintOptionValues();
123 SourcePathList = SourcePaths;
124 if ((OccurrencesFlag == cl::ZeroOrMore || OccurrencesFlag == cl::Optional) &&
125 SourcePathList.empty())
128 std::string ErrorMessage;
129 if (!BuildPath.empty()) {
137 llvm::errs() <<
"Error while trying to load a compilation database:\n"
138 << ErrorMessage <<
"Running without flags.\n";
143 auto AdjustingCompilations =
144 llvm::make_unique<ArgumentsAdjustingCompilations>(
145 std::move(Compilations));
146 AdjustingCompilations->appendArgumentsAdjuster(
148 AdjustingCompilations->appendArgumentsAdjuster(
150 Compilations = std::move(AdjustingCompilations);