clang: lib/Tooling/CommonOptionsParser.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
28#include "llvm/Support/CommandLine.h"
29
31using namespace llvm;
32
34 "\n"
35 "-p is used to read a compile command database.\n"
36 "\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"
44 "\n"
45 " ... 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"
53 "\n";
54
57 Adjusters.push_back(std::move(Adjuster));
58}
59
61 StringRef FilePath) const {
62 return adjustCommands(Compilations->getCompileCommands(FilePath));
63}
64
65std::vectorstd::string
67 return Compilations->getAllFiles();
68}
69
70std::vector
72 return adjustCommands(Compilations->getAllCompileCommands());
73}
74
75std::vector ArgumentsAdjustingCompilations::adjustCommands(
76 std::vector Commands) const {
78 for (const auto &Adjuster : Adjusters)
79 Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename);
80 return Commands;
81}
82
83llvm::Error CommonOptionsParser::init(
84 int &argc, const char **argv, cl::OptionCategory &Category,
85 llvm:🆑:NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
86
87 static cl::optstd::string BuildPath("p", cl::desc("Build path"),
88 cl::Optional, cl::cat(Category),
89 cl::sub(cl::SubCommand::getAll()));
90
91 static cl::liststd::string SourcePaths(
92 cl::Positional, cl::desc(" [... ]"), OccurrencesFlag,
93 cl::cat(Category), cl::sub(cl::SubCommand::getAll()));
94
95 static cl::liststd::string ArgsAfter(
96 "extra-arg",
97 cl::desc("Additional argument to append to the compiler command line"),
98 cl::cat(Category), cl::sub(cl::SubCommand::getAll()));
99
100 static cl::liststd::string ArgsBefore(
101 "extra-arg-before",
102 cl::desc("Additional argument to prepend to the compiler command line"),
103 cl::cat(Category), cl::sub(cl::SubCommand::getAll()));
104
105 cl::ResetAllOptionOccurrences();
106
107 cl::HideUnrelatedOptions(Category);
108
109 std::string ErrorMessage;
110 Compilations =
112 if (!ErrorMessage.empty())
113 ErrorMessage.append("\n");
114 llvm::raw_string_ostream OS(ErrorMessage);
115
116 if (!cl::ParseCommandLineOptions(argc, argv, Overview, &OS)) {
117 return llvm::make_errorllvm::StringError(ErrorMessage,
118 llvm::inconvertibleErrorCode());
119 }
120
121 cl::PrintOptionValues();
122
123 SourcePathList = SourcePaths;
124 if ((OccurrencesFlag == cl::ZeroOrMore || OccurrencesFlag == cl::Optional) &&
125 SourcePathList.empty())
126 return llvm::Error::success();
127 if (!Compilations) {
128 if (!BuildPath.empty()) {
129 Compilations =
131 } else {
133 ErrorMessage);
134 }
135 if (!Compilations) {
136 llvm::errs() << "Error while trying to load a compilation database:\n"
137 << ErrorMessage << "Running without flags.\n";
138 Compilations.reset(
140 }
141 }
142 auto AdjustingCompilations =
143 std::make_unique(
144 std::move(Compilations));
145 Adjuster =
148 std::move(Adjuster),
150 AdjustingCompilations->appendArgumentsAdjuster(Adjuster);
151 Compilations = std::move(AdjustingCompilations);
152 return llvm::Error::success();
153}
154
156 int &argc, const char **argv, llvm:🆑:OptionCategory &Category,
157 llvm:🆑:NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
159 llvm::Error Err =
160 Parser.init(argc, argv, Category, OccurrencesFlag, Overview);
161 if (Err)
162 return std::move(Err);
163 return std::move(Parser);
164}
165
166CommonOptionsParser::CommonOptionsParser(
167 int &argc, const char **argv, cl::OptionCategory &Category,
168 llvm:🆑:NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
169 llvm::Error Err = init(argc, argv, Category, OccurrencesFlag, Overview);
170 if (Err) {
171 llvm::report_fatal_error(
172 Twine("CommonOptionsParser: failed to parse command-line arguments. ") +
173 llvm::toString(std::move(Err)));
174 }
175}
Parser - This implements a parser for the C family of languages.
Diagnostic wrappers for TextAPI types for error reporting.