[clang] 60b3a5b - [clang][bytecode] Fix two small builtin_constant_p cases (#137587)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 28 00:21:53 PDT 2025
Author: Timm Baeder
Date: 2025-04-28T09:21:49+02:00
New Revision: 60b3a5b7e7db40ec74d3c4839a3fae8b73d98851
URL: https://github.com/llvm/llvm-project/commit/60b3a5b7e7db40ec74d3c4839a3fae8b73d98851
DIFF: https://github.com/llvm/llvm-project/commit/60b3a5b7e7db40ec74d3c4839a3fae8b73d98851.diff
LOG: [clang][bytecode] Fix two small builtin_constant_p cases (#137587)
Only accept string literals if we're pointing to the first index and do
accept complex literals.
Added:
Modified:
clang/lib/AST/ByteCode/Interp.cpp
clang/test/AST/ByteCode/builtin-constant-p.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 080f694e27da2..4f94bb5a85192 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -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;
}
diff --git a/clang/test/AST/ByteCode/builtin-constant-p.cpp b/clang/test/AST/ByteCode/builtin-constant-p.cpp
index f5b16761bfdc9..9f5521590833d 100644
--- a/clang/test/AST/ByteCode/builtin-constant-p.cpp
+++ b/clang/test/AST/ByteCode/builtin-constant-p.cpp
@@ -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__;
@@ -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}}
+}
More information about the cfe-commits
mailing list