diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 1f66a877c50b4..4fb1160713def 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -307,6 +307,8 @@ Bug Fixes in This Version bit-fields. This fixes: (`#61355 `_) and (`#61417 `_) +- Fix crash after suggesting typo correction to constexpr if condition. + (`#61885 `_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index c12660081e7c4..b05238d0352e6 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -12843,20 +12843,22 @@ class Sema final { Decl *ConditionVar; FullExprArg Condition; bool Invalid; - bool HasKnownValue; - bool KnownValue; + std::optional KnownValue; friend class Sema; ConditionResult(Sema &S, Decl *ConditionVar, FullExprArg Condition, bool IsConstexpr) - : ConditionVar(ConditionVar), Condition(Condition), Invalid(false), - HasKnownValue(IsConstexpr && Condition.get() && - !Condition.get()->isValueDependent()), - KnownValue(HasKnownValue && - !!Condition.get()->EvaluateKnownConstInt(S.Context)) {} + : ConditionVar(ConditionVar), Condition(Condition), Invalid(false) { + if (IsConstexpr && Condition.get()) { + if (std::optional Val = + Condition.get()->getIntegerConstantExpr(S.Context)) { + KnownValue = !!(*Val); + } + } + } explicit ConditionResult(bool Invalid) : ConditionVar(nullptr), Condition(nullptr), Invalid(Invalid), - HasKnownValue(false), KnownValue(false) {} + KnownValue(std::nullopt) {} public: ConditionResult() : ConditionResult(false) {} @@ -12865,11 +12867,7 @@ class Sema final { return std::make_pair(cast_or_null(ConditionVar), Condition.get()); } - std::optional getKnownValue() const { - if (!HasKnownValue) - return std::nullopt; - return KnownValue; - } + std::optional getKnownValue() const { return KnownValue; } }; static ConditionResult ConditionError() { return ConditionResult(true); } diff --git a/clang/test/SemaCXX/invalid-if-constexpr.cpp b/clang/test/SemaCXX/invalid-if-constexpr.cpp new file mode 100644 index 0000000000000..7643c47488f05 --- /dev/null +++ b/clang/test/SemaCXX/invalid-if-constexpr.cpp @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -verify -std=c++20 %s + +namespace GH61885 { +void similar() { // expected-note {{'similar' declared here}} + if constexpr (similer<>) {} // expected-error {{use of undeclared identifier 'similer'; did you mean 'similar'?}} +} +void a() { if constexpr (__adl_swap<>) {}} // expected-error{{use of undeclared identifier '__adl_swap'; did you mean '__sync_swap'?}} \ + // expected-note {{'__sync_swap' declared here}} + +int AA() { return true;} // expected-note {{'AA' declared here}} + +void b() { if constexpr (AAA<>) {}} // expected-error {{use of undeclared identifier 'AAA'; did you mean 'AA'?}} +} +