diff options
author | Balazs Benics <benicsbalazs@gmail.com> | 2025-01-07 15:19:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-07 15:19:16 +0100 |
commit | 5f6b7145077386afac806eec1bb8e866c6166034 (patch) | |
tree | e4e00cf4965f8ef26c73515b9725538ef848d4bf | |
parent | ef391dbc29db097952e71d81cd88e9bd7e81a3fa (diff) | |
download | llvm-5f6b7145077386afac806eec1bb8e866c6166034.zip llvm-5f6b7145077386afac806eec1bb8e866c6166034.tar.gz llvm-5f6b7145077386afac806eec1bb8e866c6166034.tar.bz2 |
[analyzer][NFC] Simplify PositiveAnalyzerOption handling (#121910)
This simplifies #120239
Addresses my comment at:
https://github.com/llvm/llvm-project/pull/120239#issuecomment-2574600543
CPP-5920
-rw-r--r-- | clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h | 19 | ||||
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 4 | ||||
-rw-r--r-- | clang/unittests/StaticAnalyzer/Z3CrosscheckOracleTest.cpp | 11 |
3 files changed, 14 insertions, 20 deletions
diff --git a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h index 3f341ec..2c97030 100644 --- a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -126,11 +126,18 @@ enum class CTUPhase1InliningKind { None, Small, All }; class PositiveAnalyzerOption { public: - PositiveAnalyzerOption() = default; - PositiveAnalyzerOption(const PositiveAnalyzerOption &) = default; - PositiveAnalyzerOption &operator=(const PositiveAnalyzerOption &) = default; + constexpr PositiveAnalyzerOption() = default; + constexpr PositiveAnalyzerOption(unsigned Value) : Value(Value) { + assert(Value > 0 && "only positive values are accepted"); + } + constexpr PositiveAnalyzerOption(const PositiveAnalyzerOption &) = default; + constexpr PositiveAnalyzerOption & + operator=(const PositiveAnalyzerOption &Other) { + Value = Other.Value; + return *this; + } - static std::optional<PositiveAnalyzerOption> create(unsigned Val) { + static constexpr std::optional<PositiveAnalyzerOption> create(unsigned Val) { if (Val == 0) return std::nullopt; return PositiveAnalyzerOption{Val}; @@ -141,11 +148,9 @@ public: return std::nullopt; return PositiveAnalyzerOption::create(Parsed); } - operator unsigned() const { return Value; } + constexpr operator unsigned() const { return Value; } private: - explicit constexpr PositiveAnalyzerOption(unsigned Value) : Value(Value) {} - unsigned Value = 1; }; diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 6e47b37..d711df0 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1274,9 +1274,7 @@ static void initOption(AnalyzerOptions::ConfigTable &Config, Diags->Report(diag::err_analyzer_config_invalid_input) << Name << "a positive"; - auto Default = PositiveAnalyzerOption::create(DefaultVal); - assert(Default.has_value()); - OptionField = Default.value(); + OptionField = DefaultVal; } static void parseAnalyzerConfigs(AnalyzerOptions &AnOpts, diff --git a/clang/unittests/StaticAnalyzer/Z3CrosscheckOracleTest.cpp b/clang/unittests/StaticAnalyzer/Z3CrosscheckOracleTest.cpp index ed8627c..626f5c1 100644 --- a/clang/unittests/StaticAnalyzer/Z3CrosscheckOracleTest.cpp +++ b/clang/unittests/StaticAnalyzer/Z3CrosscheckOracleTest.cpp @@ -27,22 +27,13 @@ static constexpr std::optional<bool> UNDEF = std::nullopt; static unsigned operator""_ms(unsigned long long ms) { return ms; } static unsigned operator""_step(unsigned long long rlimit) { return rlimit; } -template <class Ret, class Arg> static Ret makeDefaultOption(Arg Value) { - return Value; -} -template <> PositiveAnalyzerOption makeDefaultOption(int Value) { - auto DefaultVal = PositiveAnalyzerOption::create(Value); - assert(DefaultVal.has_value()); - return DefaultVal.value(); -} - static const AnalyzerOptions DefaultOpts = [] { AnalyzerOptions Config; #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \ SHALLOW_VAL, DEEP_VAL) \ ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEEP_VAL) #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \ - Config.NAME = makeDefaultOption<TYPE>(DEFAULT_VAL); + Config.NAME = DEFAULT_VAL; #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def" // Remember to update the tests in this file when these values change. |