Skip to content

[clang][bytecode] Fix two small builtin_constant_p cases #137587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,11 @@ bool CheckBCPResult(InterpState &S, const Pointer &Ptr) {
if (Ptr.isTypeidPointer())
return true;

if (Ptr.getType()->isAnyComplexType())
return true;

if (const Expr *Base = Ptr.getDeclDesc()->asExpr())
return isa<StringLiteral>(Base);
return isa<StringLiteral>(Base) && Ptr.getIndex() == 0;
return false;
}

Expand Down
15 changes: 13 additions & 2 deletions clang/test/AST/ByteCode/builtin-constant-p.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
// RUN: %clang_cc1 -std=c++20 -verify=expected,both %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s

using intptr_t = __INTPTR_TYPE__;

Expand Down Expand Up @@ -129,3 +129,14 @@ void g() {
const float f = __builtin_is_constant_evaluated();
static_assert(fold(f == 0.0f));
}

void test17(void) {
#define ASSERT(...) { enum { folded = (__VA_ARGS__) }; int arr[folded ? 1 : -1]; }
#define T(...) ASSERT(__builtin_constant_p(__VA_ARGS__))
#define F(...) ASSERT(!__builtin_constant_p(__VA_ARGS__))

T(3i + 5);
T("string literal");
F("string literal" + 1); // both-warning {{adding}} \
// both-note {{use array indexing}}
}