Skip to content

Commit caeb565

Browse files
committed
[clang][cli] Convert Analyzer option string based options to new option parsing system
Depends on D84185 Reviewed By: dexonsmith Original patch by Daniel Grumberg. Differential Revision: https://reviews.llvm.org/D84186
1 parent 8c1f2d1 commit caeb565

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4110,7 +4110,8 @@ def analyzer_display_progress : Flag<["-"], "analyzer-display-progress">,
41104110
HelpText<"Emit verbose output about the analyzer's progress">,
41114111
MarshallingInfoFlag<"AnalyzerOpts->AnalyzerDisplayProgress">;
41124112
def analyze_function : Separate<["-"], "analyze-function">,
4113-
HelpText<"Run analysis on specific function (for C++ include parameters in name)">;
4113+
HelpText<"Run analysis on specific function (for C++ include parameters in name)">,
4114+
MarshallingInfoString<"AnalyzerOpts->AnalyzeSpecificFunction">;
41144115
def analyze_function_EQ : Joined<["-"], "analyze-function=">, Alias<analyze_function>;
41154116
def trim_egraph : Flag<["-"], "trim-egraph">,
41164117
HelpText<"Only show error-related paths in the analysis graph">,
@@ -4124,7 +4125,9 @@ def analyzer_dump_egraph : Separate<["-"], "analyzer-dump-egraph">,
41244125
def analyzer_dump_egraph_EQ : Joined<["-"], "analyzer-dump-egraph=">, Alias<analyzer_dump_egraph>;
41254126

41264127
def analyzer_inline_max_stack_depth : Separate<["-"], "analyzer-inline-max-stack-depth">,
4127-
HelpText<"Bound on stack depth while inlining (4 by default)">;
4128+
HelpText<"Bound on stack depth while inlining (4 by default)">,
4129+
// Cap the stack depth at 4 calls (5 stack frames, base + 4 calls).
4130+
MarshallingInfoStringInt<"AnalyzerOpts->InlineMaxStackDepth", "5">;
41284131
def analyzer_inline_max_stack_depth_EQ : Joined<["-"], "analyzer-inline-max-stack-depth=">,
41294132
Alias<analyzer_inline_max_stack_depth>;
41304133

@@ -4137,7 +4140,8 @@ def analyzer_disable_retry_exhausted : Flag<["-"], "analyzer-disable-retry-exhau
41374140
MarshallingInfoFlag<"AnalyzerOpts->NoRetryExhausted">;
41384141

41394142
def analyzer_max_loop : Separate<["-"], "analyzer-max-loop">,
4140-
HelpText<"The maximum number of times the analyzer will go through a loop">;
4143+
HelpText<"The maximum number of times the analyzer will go through a loop">,
4144+
MarshallingInfoStringInt<"AnalyzerOpts->maxBlockVisitOnPath", "4">;
41414145
def analyzer_stats : Flag<["-"], "analyzer-stats">,
41424146
HelpText<"Print internal analyzer statistics.">,
41434147
MarshallingInfoFlag<"AnalyzerOpts->PrintStats">;

clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
259259
bool AnalyzerWerror : 1;
260260

261261
/// The inlining stack depth limit.
262-
// Cap the stack depth at 4 calls (5 stack frames, base + 4 calls).
263-
unsigned InlineMaxStackDepth = 5;
262+
unsigned InlineMaxStackDepth;
264263

265264
/// The mode of function selection used during inlining.
266265
AnalysisInliningMode InliningMode = NoRedundancy;

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#include <memory>
9494
#include <string>
9595
#include <tuple>
96+
#include <type_traits>
9697
#include <utility>
9798
#include <vector>
9899

@@ -282,12 +283,38 @@ static Optional<std::string> normalizeString(OptSpecifier Opt, int TableIndex,
282283

283284
static void denormalizeString(SmallVectorImpl<const char *> &Args,
284285
const char *Spelling,
285-
CompilerInvocation::StringAllocator SA,
286-
unsigned TableIndex, const std::string &Value) {
286+
CompilerInvocation::StringAllocator SA, unsigned,
287+
Twine Value) {
287288
Args.push_back(Spelling);
288289
Args.push_back(SA(Value));
289290
}
290291

292+
template <typename T,
293+
std::enable_if_t<!std::is_convertible<T, Twine>::value &&
294+
std::is_constructible<Twine, T>::value,
295+
bool> = false>
296+
static void denormalizeString(SmallVectorImpl<const char *> &Args,
297+
const char *Spelling,
298+
CompilerInvocation::StringAllocator SA,
299+
unsigned TableIndex, T Value) {
300+
denormalizeString(Args, Spelling, SA, TableIndex, Twine(Value));
301+
}
302+
303+
template <typename IntTy>
304+
static Optional<IntTy> normalizeStringIntegral(OptSpecifier Opt, int,
305+
const ArgList &Args,
306+
DiagnosticsEngine &Diags) {
307+
auto *Arg = Args.getLastArg(Opt);
308+
if (!Arg)
309+
return None;
310+
IntTy Res;
311+
if (StringRef(Arg->getValue()).getAsInteger(0, Res)) {
312+
Diags.Report(diag::err_drv_invalid_int_value)
313+
<< Arg->getAsString(Args) << Arg->getValue();
314+
}
315+
return Res;
316+
}
317+
291318
static Optional<std::string> normalizeTriple(OptSpecifier Opt, int TableIndex,
292319
const ArgList &Args,
293320
DiagnosticsEngine &Diags) {
@@ -522,14 +549,6 @@ static bool ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args,
522549
.Case("false", false)
523550
.Default(false);
524551

525-
Opts.AnalyzeSpecificFunction =
526-
std::string(Args.getLastArgValue(OPT_analyze_function));
527-
Opts.maxBlockVisitOnPath =
528-
getLastArgIntValue(Args, OPT_analyzer_max_loop, 4, Diags);
529-
Opts.InlineMaxStackDepth =
530-
getLastArgIntValue(Args, OPT_analyzer_inline_max_stack_depth,
531-
Opts.InlineMaxStackDepth, Diags);
532-
533552
Opts.CheckersAndPackages.clear();
534553
for (const Arg *A :
535554
Args.filtered(OPT_analyzer_checker, OPT_analyzer_disable_checker)) {

llvm/include/llvm/Option/OptParser.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ class MarshallingInfoString<code keypath, code defaultvalue="std::string()">
161161
code Denormalizer = "denormalizeString";
162162
}
163163

164+
class MarshallingInfoStringInt<code keypath, code defaultvalue="0", code type="unsigned">
165+
: MarshallingInfo<keypath, defaultvalue> {
166+
code Normalizer = "normalizeStringIntegral<"#type#">";
167+
code Denormalizer = "denormalizeString";
168+
}
169+
164170
class MarshallingInfoFlag<code keypath, code defaultvalue = "false">
165171
: MarshallingInfo<keypath, defaultvalue> {
166172
code Normalizer = "normalizeSimpleFlag";

0 commit comments

Comments
 (0)