[clang] 3ac4299 - [clang] Don't short-circuit constant evaluation for array or record types

Charles Magahern via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 19 18:04:33 PST 2023


Author: Charles Magahern
Date: 2023-01-19T18:04:00-08:00
New Revision: 3ac4299d3798eb7078905d5fc8f23781556c90a1

URL: https://github.com/llvm/llvm-project/commit/3ac4299d3798eb7078905d5fc8f23781556c90a1
DIFF: https://github.com/llvm/llvm-project/commit/3ac4299d3798eb7078905d5fc8f23781556c90a1.diff

LOG: [clang] Don't short-circuit constant evaluation for array or record types

FastEvaluateAsRValue returns `true` without setting a result value for when a
given constant expression is an array or record type.

Clang attributes must be able to support constant expressions that are array or
record types, so proceed with the slower path for evaluation in the case where
`FastEvaluateAsRValue` does not yield an evaluation result.

Differential Revision: https://reviews.llvm.org/D141745

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/test/CodeGen/2007-06-15-AnnotateAttribute.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 7105c7dfa7530..912a210fd2545 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -15313,7 +15313,7 @@ bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
   assert(!isValueDependent() &&
          "Expression evaluator can't be called on a dependent expression.");
   bool IsConst;
-  if (FastEvaluateAsRValue(this, Result, Ctx, IsConst))
+  if (FastEvaluateAsRValue(this, Result, Ctx, IsConst) && Result.Val.hasValue())
     return true;
 
   ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");

diff  --git a/clang/test/CodeGen/2007-06-15-AnnotateAttribute.c b/clang/test/CodeGen/2007-06-15-AnnotateAttribute.c
index 754d7169aabbe..05a9e6d7dc394 100644
--- a/clang/test/CodeGen/2007-06-15-AnnotateAttribute.c
+++ b/clang/test/CodeGen/2007-06-15-AnnotateAttribute.c
@@ -14,6 +14,20 @@ int foo(int y __attribute__((annotate("LocalValAnnotation")))) {
   return y + x;
 }
 
+/* Attribute with struct argument. */
+struct TestStruct {
+  int a;
+  int b;
+};
+int Y __attribute__((annotate(
+  "GlobalValAnnotationWithArgs", 
+  42,
+  (struct TestStruct) { .a = 1, .b = 2 }
+)));
+
+// CHECK: @.str.3 = private unnamed_addr constant [28 x i8] c"GlobalValAnnotationWithArgs\00", section "llvm.metadata"
+// CHECK-NEXT: @.args = private unnamed_addr constant { i32, %struct.TestStruct } { i32 42, %struct.TestStruct { i32 1, i32 2 } }, section "llvm.metadata"
+
 int main(void) {
   static int a __attribute__((annotate("GlobalValAnnotation")));
   a = foo(2);


        


More information about the cfe-commits mailing list