[clang] 36f6743 - [AST] Stop evaluate constant expression if the condition expression which in switch statement contains errors
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 7 04:57:06 PDT 2023
Author: yronglin
Date: 2023-07-07T19:56:47+08:00
New Revision: 36f67434f724f2cdf36735b243fdaace726afb85
URL: https://github.com/llvm/llvm-project/commit/36f67434f724f2cdf36735b243fdaace726afb85
DIFF: https://github.com/llvm/llvm-project/commit/36f67434f724f2cdf36735b243fdaace726afb85.diff
LOG: [AST] Stop evaluate constant expression if the condition expression which in switch statement contains errors
This fix issue: https://github.com/llvm/llvm-project/issues/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
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/AST/ExprConstant.cpp
clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8672b483cf836b..35caf85725c140 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -570,6 +570,9 @@ Bug Fixes in This Version
- Clang now correctly evaluates ``__has_extension (cxx_defaulted_functions)``
and ``__has_extension (cxx_default_function_template_args)`` to 1.
(`#61758 <https://github.com/llvm/llvm-project/issues/61758>`_)
+- Stop evaluating a constant expression if the condition expression which in
+ switch statement contains errors.
+ (`#63453 <https://github.com/llvm/llvm-project/issues/63453>_`)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 39e6b634eb2520..c740aecf32f154 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -5007,12 +5007,13 @@ static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
!EvaluateDecl(Info, SS->getConditionVariable()))
return ESR_Failed;
if (SS->getCond()->isValueDependent()) {
- if (!EvaluateDependentExpr(SS->getCond(), Info))
- return ESR_Failed;
- } else {
- if (!EvaluateInteger(SS->getCond(), Value, Info))
- return ESR_Failed;
+ // We don't know what the value is, and which branch should jump to.
+ EvaluateDependentExpr(SS->getCond(), Info);
+ return ESR_Failed;
}
+ if (!EvaluateInteger(SS->getCond(), Value, Info))
+ return ESR_Failed;
+
if (!CondScope.destroy())
return ESR_Failed;
}
diff --git a/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp b/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
index 07bc5e57ae13ce..8bada7c0b6df3e 100644
--- a/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
+++ b/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
@@ -87,6 +87,7 @@ constexpr int force12 = test12(); // expected-error {{must be initializ
// We're not checking specific recovery here so don't assert diagnostics.
TEST_EVALUATE(Switch, switch (!!){}); // expected-error + {{}}
TEST_EVALUATE(SwitchInit, switch (auto x = !!){}); // expected-error + {{}}
+TEST_EVALUATE(SwitchCondValDep, switch (invalid_value) { default: break; }); // expected-error + {{}}
TEST_EVALUATE(For, for (!!){}); // expected-error + {{}}
// FIXME: should bail out instead of looping.
// expected-note at -2 + {{infinite loop}}
More information about the cfe-commits
mailing list