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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
From a8016e296e6ec161897e7421c5efbc25a6aa3a9f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9s=20Villegas?= <andresvi@google.com>
Date: Tue, 25 Jul 2023 23:38:09 +0000
Subject: [PATCH] [sancov] Switch to OptTable from llvm::cl
Switch the parse of command line options from llvm::cl to OptTable.
The motivation for this change is to continue adding llvm based tools
to the llvm driver multicall. For more information about the proposal
and motivation, please see https://discourse.llvm.org/t/rfc-llvm-busybox-proposal/58494
Reviewed By: leonardchan
Drop this patch when upgrading llvm in rust to 18.x or newer
Upstream-Status: Backport [https://github.com/llvm/llvm-project/commit/a8016e296e6ec161897e7421c5efbc25a6aa3a9f]
Signed-off-by: Khem Raj <raj.khem@gmail.com>
Differential Revision: https://reviews.llvm.org/D155119
---
llvm/tools/sancov/CMakeLists.txt | 8 ++
llvm/tools/sancov/Opts.td | 58 +++++++++
llvm/tools/sancov/sancov.cpp | 194 +++++++++++++++++++++----------
3 files changed, 196 insertions(+), 64 deletions(-)
create mode 100644 llvm/tools/sancov/Opts.td
--- a/tools/sancov/CMakeLists.txt
+++ b/tools/sancov/CMakeLists.txt
@@ -5,11 +5,19 @@ set(LLVM_LINK_COMPONENTS
MC
MCDisassembler
Object
+ Option
Support
Symbolize
TargetParser
)
+set(LLVM_TARGET_DEFINITIONS Opts.td)
+tablegen(LLVM Opts.inc -gen-opt-parser-defs)
+add_public_tablegen_target(SancovOptsTableGen)
+
add_llvm_tool(sancov
sancov.cpp
+
+ DEPENDS
+ SancovOptsTableGen
)
--- /dev/null
+++ b/tools/sancov/Opts.td
@@ -0,0 +1,58 @@
+include "llvm/Option/OptParser.td"
+
+class F<string name, string help> : Flag<["-", "--"], name>, HelpText<help>;
+
+multiclass B<string name, string help1, string help2> {
+ def NAME: Flag<["-", "--"], name>, HelpText<help1>;
+ def no_ # NAME: Flag<["-", "--"], "no-" # name>, HelpText<help2>;
+}
+
+multiclass Eq<string name, string help> {
+ def NAME #_EQ : Joined<["-", "--"], name #"=">,
+ HelpText<help>;
+ def : Separate<["-", "--"], name>, Alias<!cast<Joined>(NAME #_EQ)>;
+}
+
+def generic_grp : OptionGroup<"Genric Options">, HelpText<"Generic Options">;
+def help : F<"help", "Display this help">, Group<generic_grp>;
+def : Flag<["-"], "h">, Alias<help>, HelpText<"Alias for --help">, Group<generic_grp>;
+def version : F<"version", "Display the version">, Group<generic_grp>;
+def : Flag<["-"], "v">, Alias<version>, HelpText<"Alias for --version">, Group<generic_grp>;
+
+def action_grp : OptionGroup<"Action">, HelpText<"Action (required)">;
+def print : F<"print", "Print coverage addresses">,
+ Group<action_grp>;
+def printCoveragePcs : F<"print-coverage-pcs", "Print coverage instrumentation points addresses.">,
+ Group<action_grp>;
+def coveredFunctions : F<"covered-functions", "Print all covered funcions.">,
+ Group<action_grp>;
+def notCoveredFunctions : F<"not-covered-functions", "Print all not covered funcions.">,
+ Group<action_grp>;
+def printCoverageStats : F<"print-coverage-stats", "Print coverage statistics.">,
+ Group<action_grp>;
+def htmlReport : F<"html-report", "REMOVED. Use -symbolize & coverage-report-server.py.">,
+ Group<action_grp>;
+def symbolize : F<"symbolize", "Produces a symbolized JSON report from binary report.">,
+ Group<action_grp>;
+def merge : F<"merge", "Merges reports.">,
+ Group<action_grp>;
+
+defm demangle : B<"demangle", "Demangle function names", "Do not demangle function names">;
+defm skipDeadFiles : B<"skip-dead-files", "Do not list dead source files in reports",
+ "List dead source files in reports">;
+defm useDefaultIgnoreList :
+ B<"use_default_ignorelist", "Use the default ignore list", "Don't use the default ignore list">,
+ Flags<[HelpHidden]>;
+
+// Compatibility aliases
+def : Flag<["-"], "demangle=0">, Alias<no_demangle>, HelpText<"Alias for --no-demangle">;
+def : Flag<["-"], "skip-dead-files=0">, Alias<no_skipDeadFiles>, HelpText<"Alias for --no-skip-dead-files">;
+def : Flag<["-"], "use_default_ignorelist=0">, Alias<no_useDefaultIgnoreList>, HelpText<"Alias for --no-use_default_ignore_list">;
+
+defm stripPathPrefix
+ : Eq<"strip_path_prefix", "Strip this prefix from files paths in reports">,
+ MetaVarName<"<string>">;
+
+defm ignorelist
+ : Eq<"ignorelist", "Ignorelist file (sanitizer ignorelist format)">,
+ MetaVarName<"<string>">;
--- a/tools/sancov/sancov.cpp
+++ b/tools/sancov/sancov.cpp
@@ -29,6 +29,8 @@
#include "llvm/Object/COFF.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/Option.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Errc.h"
@@ -55,9 +57,44 @@ using namespace llvm;
namespace {
-// --------- COMMAND LINE FLAGS ---------
+// Command-line option boilerplate.
+namespace {
+using namespace llvm::opt;
+enum ID {
+ OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
+ HELPTEXT, METAVAR, VALUES) \
+ OPT_##ID,
+#include "Opts.inc"
+#undef OPTION
+};
-cl::OptionCategory Cat("sancov Options");
+#define PREFIX(NAME, VALUE) \
+ static constexpr StringLiteral NAME##_init[] = VALUE; \
+ static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
+ std::size(NAME##_init) - 1);
+#include "Opts.inc"
+#undef PREFIX
+
+static constexpr opt::OptTable::Info InfoTable[] = {
+#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
+ HELPTEXT, METAVAR, VALUES) \
+ { \
+ PREFIX, NAME, HELPTEXT, \
+ METAVAR, OPT_##ID, opt::Option::KIND##Class, \
+ PARAM, FLAGS, OPT_##GROUP, \
+ OPT_##ALIAS, ALIASARGS, VALUES},
+#include "Opts.inc"
+#undef OPTION
+};
+
+class SancovOptTable : public opt::GenericOptTable {
+public:
+ SancovOptTable() : GenericOptTable(InfoTable) {}
+};
+} // namespace
+
+// --------- COMMAND LINE FLAGS ---------
enum ActionType {
CoveredFunctionsAction,
@@ -70,53 +107,13 @@ enum ActionType {
SymbolizeAction
};
-cl::opt<ActionType> Action(
- cl::desc("Action (required)"), cl::Required,
- cl::values(
- clEnumValN(PrintAction, "print", "Print coverage addresses"),
- clEnumValN(PrintCovPointsAction, "print-coverage-pcs",
- "Print coverage instrumentation points addresses."),
- clEnumValN(CoveredFunctionsAction, "covered-functions",
- "Print all covered funcions."),
- clEnumValN(NotCoveredFunctionsAction, "not-covered-functions",
- "Print all not covered funcions."),
- clEnumValN(StatsAction, "print-coverage-stats",
- "Print coverage statistics."),
- clEnumValN(HtmlReportAction, "html-report",
- "REMOVED. Use -symbolize & coverage-report-server.py."),
- clEnumValN(SymbolizeAction, "symbolize",
- "Produces a symbolized JSON report from binary report."),
- clEnumValN(MergeAction, "merge", "Merges reports.")),
- cl::cat(Cat));
-
-static cl::list<std::string>
- ClInputFiles(cl::Positional, cl::OneOrMore,
- cl::desc("<action> <binary files...> <.sancov files...> "
- "<.symcov files...>"),
- cl::cat(Cat));
-
-static cl::opt<bool> ClDemangle("demangle", cl::init(true),
- cl::desc("Print demangled function name"),
- cl::cat(Cat));
-
-static cl::opt<bool>
- ClSkipDeadFiles("skip-dead-files", cl::init(true),
- cl::desc("Do not list dead source files in reports"),
- cl::cat(Cat));
-
-static cl::opt<std::string>
- ClStripPathPrefix("strip_path_prefix", cl::init(""),
- cl::desc("Strip this prefix from file paths in reports"),
- cl::cat(Cat));
-
-static cl::opt<std::string>
- ClIgnorelist("ignorelist", cl::init(""),
- cl::desc("Ignorelist file (sanitizer ignorelist format)"),
- cl::cat(Cat));
-
-static cl::opt<bool> ClUseDefaultIgnorelist(
- "use_default_ignorelist", cl::init(true), cl::Hidden,
- cl::desc("Controls if default ignorelist should be used"), cl::cat(Cat));
+static ActionType Action;
+static std::vector<std::string> ClInputFiles;
+static bool ClDemangle;
+static bool ClSkipDeadFiles;
+static bool ClUseDefaultIgnorelist;
+static std::string ClStripPathPrefix;
+static std::string ClIgnorelist;
static const char *const DefaultIgnorelistStr = "fun:__sanitizer_.*\n"
"src:/usr/include/.*\n"
@@ -699,8 +696,7 @@ findSanitizerCovFunctions(const object::
// Ported from
// compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h:GetPreviousInstructionPc
// GetPreviousInstructionPc.
-static uint64_t getPreviousInstructionPc(uint64_t PC,
- Triple TheTriple) {
+static uint64_t getPreviousInstructionPc(uint64_t PC, Triple TheTriple) {
if (TheTriple.isARM())
return (PC - 3) & (~1);
if (TheTriple.isMIPS() || TheTriple.isSPARC())
@@ -1145,31 +1141,101 @@ readSymbolizeAndMergeCmdArguments(std::v
} // namespace
+static void parseArgs(int Argc, char **Argv) {
+ SancovOptTable Tbl;
+ llvm::BumpPtrAllocator A;
+ llvm::StringSaver Saver{A};
+ opt::InputArgList Args =
+ Tbl.parseArgs(Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
+ llvm::errs() << Msg << '\n';
+ std::exit(1);
+ });
+
+ if (Args.hasArg(OPT_help)) {
+ Tbl.printHelp(
+ llvm::outs(),
+ "sancov [options] <action> <binary files...> <.sancov files...> "
+ "<.symcov files...>",
+ "Sanitizer Coverage Processing Tool (sancov)\n\n"
+ " This tool can extract various coverage-related information from: \n"
+ " coverage-instrumented binary files, raw .sancov files and their "
+ "symbolized .symcov version.\n"
+ " Depending on chosen action the tool expects different input files:\n"
+ " -print-coverage-pcs - coverage-instrumented binary files\n"
+ " -print-coverage - .sancov files\n"
+ " <other actions> - .sancov files & corresponding binary "
+ "files, .symcov files\n");
+ std::exit(0);
+ }
+
+ if (Args.hasArg(OPT_version)) {
+ cl::PrintVersionMessage();
+ std::exit(0);
+ }
+
+ if (Args.hasMultipleArgs(OPT_action_grp)) {
+ fail("Only one action option is allowed");
+ }
+
+ for (const opt::Arg *A : Args.filtered(OPT_INPUT)) {
+ ClInputFiles.emplace_back(A->getValue());
+ }
+
+ if (const llvm::opt::Arg *A = Args.getLastArg(OPT_action_grp)) {
+ switch (A->getOption().getID()) {
+ case OPT_print:
+ Action = ActionType::PrintAction;
+ break;
+ case OPT_printCoveragePcs:
+ Action = ActionType::PrintCovPointsAction;
+ break;
+ case OPT_coveredFunctions:
+ Action = ActionType::CoveredFunctionsAction;
+ break;
+ case OPT_notCoveredFunctions:
+ Action = ActionType::NotCoveredFunctionsAction;
+ break;
+ case OPT_printCoverageStats:
+ Action = ActionType::StatsAction;
+ break;
+ case OPT_htmlReport:
+ Action = ActionType::HtmlReportAction;
+ break;
+ case OPT_symbolize:
+ Action = ActionType::SymbolizeAction;
+ break;
+ case OPT_merge:
+ Action = ActionType::MergeAction;
+ break;
+ default:
+ fail("Invalid Action");
+ }
+ }
+
+ ClDemangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
+ ClSkipDeadFiles = Args.hasFlag(OPT_skipDeadFiles, OPT_no_skipDeadFiles, true);
+ ClUseDefaultIgnorelist =
+ Args.hasFlag(OPT_useDefaultIgnoreList, OPT_no_useDefaultIgnoreList, true);
+
+ ClStripPathPrefix = Args.getLastArgValue(OPT_stripPathPrefix_EQ);
+ ClIgnorelist = Args.getLastArgValue(OPT_ignorelist_EQ);
+}
+
int main(int Argc, char **Argv) {
llvm::InitLLVM X(Argc, Argv);
- cl::HideUnrelatedOptions(Cat);
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllDisassemblers();
- cl::ParseCommandLineOptions(Argc, Argv,
- "Sanitizer Coverage Processing Tool (sancov)\n\n"
- " This tool can extract various coverage-related information from: \n"
- " coverage-instrumented binary files, raw .sancov files and their "
- "symbolized .symcov version.\n"
- " Depending on chosen action the tool expects different input files:\n"
- " -print-coverage-pcs - coverage-instrumented binary files\n"
- " -print-coverage - .sancov files\n"
- " <other actions> - .sancov files & corresponding binary "
- "files, .symcov files\n"
- );
+ parseArgs(Argc, Argv);
// -print doesn't need object files.
if (Action == PrintAction) {
readAndPrintRawCoverage(ClInputFiles, outs());
return 0;
- } else if (Action == PrintCovPointsAction) {
+ }
+ if (Action == PrintCovPointsAction) {
// -print-coverage-points doesn't need coverage files.
for (const std::string &ObjFile : ClInputFiles) {
printCovPoints(ObjFile, outs());
|