[clang] [clang][VarDecl] Reset un-evaluated constant for all C++ modes (PR #65818)

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 8 15:38:53 PDT 2023


https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/65818:

After commit 610ec954e1f8 ("[clang] allow const structs/unions/arrays to
be constant expressions for C"), attempts to evaluate
structs/unions/arrays as constants are also performed for C++98 and
C++03.

An assertion was getting tripped up since the potentially-partially
evaluated value was not being reset for those 2 language modes.  Make
sure to reset it now for all C++ modes.

Fixes: 65784


>From bed8af1a6dd2851542fbb92b72d799ccdd504eb4 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 8 Sep 2023 15:30:51 -0700
Subject: [PATCH] [clang][VarDecl] Reset un-evaluated constant for all C++
 modes

After commit 610ec954e1f8 ("[clang] allow const structs/unions/arrays to
be constant expressions for C"), attempts to evaluate
structs/unions/arrays as constants are also performed for C++98 and
C++03.

An assertion was getting tripped up since the potentially-partially
evaluated value was not being reset for those 2 language modes.  Make
sure to reset it now for all C++ modes.

Fixes: 65784
---
 clang/lib/AST/Decl.cpp                     | 4 ++--
 clang/test/SemaCXX/constant-expression.cpp | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 60c80f2b075336b..6109829cf20a678 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2555,10 +2555,10 @@ APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes,
   bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes,
                                             IsConstantInitialization);
 
-  // In C++11, this isn't a constant initializer if we produced notes. In that
+  // In C++, this isn't a constant initializer if we produced notes. In that
   // case, we can't keep the result, because it may only be correct under the
   // assumption that the initializer is a constant context.
-  if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus11 &&
+  if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus &&
       !Notes.empty())
     Result = false;
 
diff --git a/clang/test/SemaCXX/constant-expression.cpp b/clang/test/SemaCXX/constant-expression.cpp
index 65c93c99002e1f0..cc041a4acd18cbb 100644
--- a/clang/test/SemaCXX/constant-expression.cpp
+++ b/clang/test/SemaCXX/constant-expression.cpp
@@ -153,3 +153,8 @@ namespace PR31701 {
     const C c = C::n<i>;
   }
 }
+
+struct PR65784s{
+  int *ptr;
+} const PR65784[] = {(int *)""};
+PR65784s PR65784f() { return *PR65784; }



More information about the cfe-commits mailing list