Skip to content

Commit 60b3a5b

Browse files
authored
[clang][bytecode] Fix two small builtin_constant_p cases (llvm#137587)
Only accept string literals if we're pointing to the first index and do accept complex literals.
1 parent 59fbb9e commit 60b3a5b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,11 @@ bool CheckBCPResult(InterpState &S, const Pointer &Ptr) {
321321
if (Ptr.isTypeidPointer())
322322
return true;
323323

324+
if (Ptr.getType()->isAnyComplexType())
325+
return true;
326+
324327
if (const Expr *Base = Ptr.getDeclDesc()->asExpr())
325-
return isa<StringLiteral>(Base);
328+
return isa<StringLiteral>(Base) && Ptr.getIndex() == 0;
326329
return false;
327330
}
328331

clang/test/AST/ByteCode/builtin-constant-p.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
2-
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
1+
// RUN: %clang_cc1 -std=c++20 -verify=expected,both %s -fexperimental-new-constant-interpreter
2+
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
33

44
using intptr_t = __INTPTR_TYPE__;
55

@@ -129,3 +129,14 @@ void g() {
129129
const float f = __builtin_is_constant_evaluated();
130130
static_assert(fold(f == 0.0f));
131131
}
132+
133+
void test17(void) {
134+
#define ASSERT(...) { enum { folded = (__VA_ARGS__) }; int arr[folded ? 1 : -1]; }
135+
#define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
136+
#define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))
137+
138+
T(3i + 5);
139+
T("string literal");
140+
F("string literal" + 1); // both-warning {{adding}} \
141+
// both-note {{use array indexing}}
142+
}

0 commit comments

Comments
 (0)