[clang] [Clang][ExprConstant] fix constant expression did not evaluate to integer (PR #97146)

Zhikai Zeng via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 29 00:30:23 PDT 2024


https://github.com/Backl1ght created https://github.com/llvm/llvm-project/pull/97146

fixes https://github.com/llvm/llvm-project/issues/96670

The cause is that we might return a lvalue here at

https://github.com/llvm/llvm-project/blob/3e53c97d33210db68188e731e93ee48dbaeeae32/clang/lib/AST/ExprConstant.cpp#L15861-L15865

This PR will make sure we return a rvalue in `FastEvaluateAsRValue`.

>From 5a443296eecbdf90d1cf274c3e52797be380bdd3 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 29 Jun 2024 15:26:21 +0800
Subject: [PATCH] fix

---
 clang/docs/ReleaseNotes.rst         |  1 +
 clang/lib/AST/ExprConstant.cpp      |  2 +-
 clang/test/SemaCXX/eval-crashes.cpp | 10 ++++++++++
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index feba3c7ba8d77..8ec1c105cef9f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -936,6 +936,7 @@ Bug Fixes to C++ Support
   forward-declared class. (#GH93512).
 - Fixed a bug in access checking inside return-type-requirement of compound requirements. (#GH93788).
 - Fixed an assertion failure about invalid conversion when calling lambda. (#GH96205).
+- Fixed an assertion failure about constant expression did not evaluate to integer. (#GH96670).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 374a3acf7aa26..0ccdc5eaabeef 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -15858,7 +15858,7 @@ static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result,
   }
 
   if (const auto *CE = dyn_cast<ConstantExpr>(Exp)) {
-    if (CE->hasAPValueResult()) {
+    if (CE->hasAPValueResult() && !CE->getAPValueResult().isLValue()) {
       Result.Val = CE->getAPValueResult();
       IsConst = true;
       return true;
diff --git a/clang/test/SemaCXX/eval-crashes.cpp b/clang/test/SemaCXX/eval-crashes.cpp
index 017df977b26b7..0865dafe4bf92 100644
--- a/clang/test/SemaCXX/eval-crashes.cpp
+++ b/clang/test/SemaCXX/eval-crashes.cpp
@@ -61,3 +61,13 @@ struct array {
   array() : data(*new int[1][2]) {}
 };
 }
+
+namespace GH96670 {
+inline constexpr long ullNil = -1;
+
+template<typename T = long, const T &Nil = ullNil>
+struct Test {};
+
+inline constexpr long lNil = -1;
+Test<long, lNil> c;
+}



More information about the cfe-commits mailing list