Skip to content

Commit 5f6b714

Browse files
authored
[analyzer][NFC] Simplify PositiveAnalyzerOption handling (#121910)
This simplifies #120239 Addresses my comment at: #120239 (comment) CPP-5920
1 parent ef391db commit 5f6b714

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,18 @@ enum class CTUPhase1InliningKind { None, Small, All };
126126

127127
class PositiveAnalyzerOption {
128128
public:
129-
PositiveAnalyzerOption() = default;
130-
PositiveAnalyzerOption(const PositiveAnalyzerOption &) = default;
131-
PositiveAnalyzerOption &operator=(const PositiveAnalyzerOption &) = default;
129+
constexpr PositiveAnalyzerOption() = default;
130+
constexpr PositiveAnalyzerOption(unsigned Value) : Value(Value) {
131+
assert(Value > 0 && "only positive values are accepted");
132+
}
133+
constexpr PositiveAnalyzerOption(const PositiveAnalyzerOption &) = default;
134+
constexpr PositiveAnalyzerOption &
135+
operator=(const PositiveAnalyzerOption &Other) {
136+
Value = Other.Value;
137+
return *this;
138+
}
132139

133-
static std::optional<PositiveAnalyzerOption> create(unsigned Val) {
140+
static constexpr std::optional<PositiveAnalyzerOption> create(unsigned Val) {
134141
if (Val == 0)
135142
return std::nullopt;
136143
return PositiveAnalyzerOption{Val};
@@ -141,11 +148,9 @@ class PositiveAnalyzerOption {
141148
return std::nullopt;
142149
return PositiveAnalyzerOption::create(Parsed);
143150
}
144-
operator unsigned() const { return Value; }
151+
constexpr operator unsigned() const { return Value; }
145152

146153
private:
147-
explicit constexpr PositiveAnalyzerOption(unsigned Value) : Value(Value) {}
148-
149154
unsigned Value = 1;
150155
};
151156

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,9 +1274,7 @@ static void initOption(AnalyzerOptions::ConfigTable &Config,
12741274
Diags->Report(diag::err_analyzer_config_invalid_input)
12751275
<< Name << "a positive";
12761276

1277-
auto Default = PositiveAnalyzerOption::create(DefaultVal);
1278-
assert(Default.has_value());
1279-
OptionField = Default.value();
1277+
OptionField = DefaultVal;
12801278
}
12811279

12821280
static void parseAnalyzerConfigs(AnalyzerOptions &AnOpts,

clang/unittests/StaticAnalyzer/Z3CrosscheckOracleTest.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,13 @@ static constexpr std::optional<bool> UNDEF = std::nullopt;
2727
static unsigned operator""_ms(unsigned long long ms) { return ms; }
2828
static unsigned operator""_step(unsigned long long rlimit) { return rlimit; }
2929

30-
template <class Ret, class Arg> static Ret makeDefaultOption(Arg Value) {
31-
return Value;
32-
}
33-
template <> PositiveAnalyzerOption makeDefaultOption(int Value) {
34-
auto DefaultVal = PositiveAnalyzerOption::create(Value);
35-
assert(DefaultVal.has_value());
36-
return DefaultVal.value();
37-
}
38-
3930
static const AnalyzerOptions DefaultOpts = [] {
4031
AnalyzerOptions Config;
4132
#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
4233
SHALLOW_VAL, DEEP_VAL) \
4334
ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEEP_VAL)
4435
#define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
45-
Config.NAME = makeDefaultOption<TYPE>(DEFAULT_VAL);
36+
Config.NAME = DEFAULT_VAL;
4637
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
4738

4839
// Remember to update the tests in this file when these values change.

0 commit comments

Comments
 (0)