28 #include "llvm/Support/CommandLine.h" 35 "-p <build-path> is used to read a compile command database.\n" 37 "\tFor example, it can be a CMake build directory in which a file named\n" 38 "\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n" 39 "\tCMake option to get this output). When no build path is specified,\n" 40 "\ta search for compile_commands.json will be attempted through all\n" 41 "\tparent paths of the first input file . See:\n" 42 "\thttps://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n" 43 "\texample of setting up Clang Tooling on a source tree.\n" 45 "<source0> ... specify the paths of source files. These paths are\n" 46 "\tlooked up in the compile command database. If the path of a file is\n" 47 "\tabsolute, it needs to point into CMake's source tree. If the path is\n" 48 "\trelative, the current working directory needs to be in the CMake\n" 49 "\tsource tree and the file must be in a subdirectory of the current\n" 50 "\tworking directory. \"./\" prefixes in the relative files will be\n" 51 "\tautomatically removed, but the rest of a relative path must be a\n" 52 "\tsuffix of a path in the compile command database.\n" 57 Adjusters.push_back(std::move(Adjuster));
61 StringRef FilePath)
const {
62 return adjustCommands(Compilations->getCompileCommands(FilePath));
65 std::vector<std::string>
67 return Compilations->getAllFiles();
70 std::vector<CompileCommand>
72 return adjustCommands(Compilations->getAllCompileCommands());
75 std::vector<CompileCommand> ArgumentsAdjustingCompilations::adjustCommands(
76 std::vector<CompileCommand> Commands)
const {
78 for (
const auto &Adjuster : Adjusters)
79 Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename);
84 int &argc,
const char **argv, cl::OptionCategory &
Category,
85 llvm::cl::NumOccurrencesFlag OccurrencesFlag,
const char *Overview) {
87 static cl::opt<std::string> BuildPath(
"p", cl::desc(
"Build path"),
89 cl::sub(*cl::AllSubCommands));
91 static cl::list<std::string> SourcePaths(
92 cl::Positional, cl::desc(
"<source0> [... <sourceN>]"), OccurrencesFlag,
95 static cl::list<std::string> ArgsAfter(
97 cl::desc(
"Additional argument to append to the compiler command line"),
100 static cl::list<std::string> ArgsBefore(
102 cl::desc(
"Additional argument to prepend to the compiler command line"),
105 cl::ResetAllOptionOccurrences();
109 std::string ErrorMessage;
112 if (!ErrorMessage.empty())
113 ErrorMessage.append(
"\n");
114 llvm::raw_string_ostream OS(ErrorMessage);
116 if (!cl::ParseCommandLineOptions(argc, argv, Overview, &OS)) {
118 return llvm::make_error<llvm::StringError>(
"[CommonOptionsParser]: " +
120 llvm::inconvertibleErrorCode());
123 cl::PrintOptionValues();
125 SourcePathList = SourcePaths;
126 if ((OccurrencesFlag == cl::ZeroOrMore || OccurrencesFlag ==
cl::Optional) &&
127 SourcePathList.empty())
128 return llvm::Error::success();
130 if (!BuildPath.empty()) {
138 llvm::errs() <<
"Error while trying to load a compilation database:\n" 139 << ErrorMessage <<
"Running without flags.\n";
144 auto AdjustingCompilations =
145 std::make_unique<ArgumentsAdjustingCompilations>(
146 std::move(Compilations));
152 AdjustingCompilations->appendArgumentsAdjuster(Adjuster);
153 Compilations = std::move(AdjustingCompilations);
154 return llvm::Error::success();
158 int &argc,
const char **argv, llvm::cl::OptionCategory &
Category,
159 llvm::cl::NumOccurrencesFlag OccurrencesFlag,
const char *Overview) {
162 Parser.init(argc, argv, Category, OccurrencesFlag, Overview);
164 return std::move(Err);
165 return std::move(Parser);
168 CommonOptionsParser::CommonOptionsParser(
169 int &argc,
const char **argv, cl::OptionCategory &
Category,
170 llvm::cl::NumOccurrencesFlag OccurrencesFlag,
const char *Overview) {
173 llvm::report_fatal_error(
174 "CommonOptionsParser: failed to parse command-line arguments. " +
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Parser - This implements a parser for the C family of languages.
llvm::Optional< T > Optional
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.