Skip to content

Commit 3ac4299

Browse files
committed
[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
1 parent 333ffaf commit 3ac4299

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15313,7 +15313,7 @@ bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
1531315313
assert(!isValueDependent() &&
1531415314
"Expression evaluator can't be called on a dependent expression.");
1531515315
bool IsConst;
15316-
if (FastEvaluateAsRValue(this, Result, Ctx, IsConst))
15316+
if (FastEvaluateAsRValue(this, Result, Ctx, IsConst) && Result.Val.hasValue())
1531715317
return true;
1531815318

1531915319
ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");

clang/test/CodeGen/2007-06-15-AnnotateAttribute.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ int foo(int y __attribute__((annotate("LocalValAnnotation")))) {
1414
return y + x;
1515
}
1616

17+
/* Attribute with struct argument. */
18+
struct TestStruct {
19+
int a;
20+
int b;
21+
};
22+
int Y __attribute__((annotate(
23+
"GlobalValAnnotationWithArgs",
24+
42,
25+
(struct TestStruct) { .a = 1, .b = 2 }
26+
)));
27+
28+
// CHECK: @.str.3 = private unnamed_addr constant [28 x i8] c"GlobalValAnnotationWithArgs\00", section "llvm.metadata"
29+
// CHECK-NEXT: @.args = private unnamed_addr constant { i32, %struct.TestStruct } { i32 42, %struct.TestStruct { i32 1, i32 2 } }, section "llvm.metadata"
30+
1731
int main(void) {
1832
static int a __attribute__((annotate("GlobalValAnnotation")));
1933
a = foo(2);

0 commit comments

Comments
 (0)