Skip to content

Commit 36f6743

Browse files
committed
[AST] Stop evaluate constant expression if the condition expression which in switch statement contains errors
This fix issue: #63453 ``` constexpr int foo(unsigned char c) { switch (f) { case 0: return 7; default: break; } return 0; } static_assert(foo('d')); ``` Reviewed By: aaron.ballman, erichkeane, hokein Differential Revision: https://reviews.llvm.org/D153296
1 parent 73d411d commit 36f6743

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ Bug Fixes in This Version
570570
- Clang now correctly evaluates ``__has_extension (cxx_defaulted_functions)``
571571
and ``__has_extension (cxx_default_function_template_args)`` to 1.
572572
(`#61758 <https://github.com/llvm/llvm-project/issues/61758>`_)
573+
- Stop evaluating a constant expression if the condition expression which in
574+
switch statement contains errors.
575+
(`#63453 <https://github.com/llvm/llvm-project/issues/63453>_`)
573576

574577
Bug Fixes to Compiler Builtins
575578
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5007,12 +5007,13 @@ static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
50075007
!EvaluateDecl(Info, SS->getConditionVariable()))
50085008
return ESR_Failed;
50095009
if (SS->getCond()->isValueDependent()) {
5010-
if (!EvaluateDependentExpr(SS->getCond(), Info))
5011-
return ESR_Failed;
5012-
} else {
5013-
if (!EvaluateInteger(SS->getCond(), Value, Info))
5014-
return ESR_Failed;
5010+
// We don't know what the value is, and which branch should jump to.
5011+
EvaluateDependentExpr(SS->getCond(), Info);
5012+
return ESR_Failed;
50155013
}
5014+
if (!EvaluateInteger(SS->getCond(), Value, Info))
5015+
return ESR_Failed;
5016+
50165017
if (!CondScope.destroy())
50175018
return ESR_Failed;
50185019
}

clang/test/SemaCXX/constexpr-function-recovery-crash.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ constexpr int force12 = test12(); // expected-error {{must be initializ
8787
// We're not checking specific recovery here so don't assert diagnostics.
8888
TEST_EVALUATE(Switch, switch (!!){}); // expected-error + {{}}
8989
TEST_EVALUATE(SwitchInit, switch (auto x = !!){}); // expected-error + {{}}
90+
TEST_EVALUATE(SwitchCondValDep, switch (invalid_value) { default: break; }); // expected-error + {{}}
9091
TEST_EVALUATE(For, for (!!){}); // expected-error + {{}}
9192
// FIXME: should bail out instead of looping.
9293
// expected-note@-2 + {{infinite loop}}

0 commit comments

Comments
 (0)