[PATCH] D130437: [clang] Fix incorrect constant folding of `if consteval`
Corentin Jabot via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 24 02:07:44 PDT 2022
cor3ntin updated this revision to Diff 447118.
cor3ntin added a comment.
Fix test
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D130437/new/
https://reviews.llvm.org/D130437
Files:
clang/docs/ReleaseNotes.rst
clang/lib/AST/ExprConstant.cpp
clang/test/CodeGenCXX/cxx2b-consteval-if.cpp
Index: clang/test/CodeGenCXX/cxx2b-consteval-if.cpp
===================================================================
--- clang/test/CodeGenCXX/cxx2b-consteval-if.cpp
+++ clang/test/CodeGenCXX/cxx2b-consteval-if.cpp
@@ -26,3 +26,30 @@
void g() {
f();
}
+
+namespace GH55638 {
+
+constexpr bool is_constant_evaluated() noexcept {
+ if consteval { return true; } else { return false; }
+}
+
+constexpr int compiletime(int) {
+ return 2;
+}
+
+constexpr int runtime(int) {
+ return 1;
+}
+
+constexpr int test(int x) {
+ if(is_constant_evaluated())
+ return compiletime(x); // CHECK-NOT: call {{.*}}compiletime
+ return runtime(x); // CHECK: call {{.*}}runtime
+}
+
+int f(int x) {
+ x = test(x);
+ return x;
+}
+
+}
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -5266,10 +5266,14 @@
}
}
bool Cond;
- if (IS->isConsteval())
+ if (IS->isConsteval()) {
Cond = IS->isNonNegatedConsteval();
- else if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(),
- Cond))
+ // If we are not in a constant context, if consteval should not evaluate
+ // to true.
+ if (!Info.InConstantContext)
+ Cond = !Cond;
+ } else if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(),
+ Cond))
return ESR_Failed;
if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -194,6 +194,8 @@
move assignment operator. Fixes `Issue 56456 <https://github.com/llvm/llvm-project/issues/56456>`_.
- Fixed a crash when a variable with a bool enum type that has no definition
used in comparison operators. Fixes `Issue 56560 <https://github.com/llvm/llvm-project/issues/56560>`_.
+- Fix that ``if consteval`` could evaluate to ``true`` at runtime because it was incorrectly
+ constant folded. Fixes `Issue 55638 <https://github.com/llvm/llvm-project/issues/55638>`_.
Improvements to Clang's diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130437.447118.patch
Type: text/x-patch
Size: 2293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220724/2ffcfbea/attachment.bin>
More information about the cfe-commits
mailing list