[clang] [clang][bytecode] Fix two small builtin_constant_p cases (PR #137587)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 27 23:54:31 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/137587
Only accept string literals if we're pointing to the first index and do accept complex literals.
>From 2005584f52d7fa2bd964eed80303a8cc1a6dda4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 28 Apr 2025 08:50:44 +0200
Subject: [PATCH] [clang][bytecode] Fix two small builtin_constant_p cases
Only accept string literals if we're pointing to the first index and
do accept complex literals.
---
clang/lib/AST/ByteCode/Interp.cpp | 5 ++++-
clang/test/AST/ByteCode/builtin-constant-p.cpp | 15 +++++++++++++--
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 37111343178dd..dfc00ffc297e1 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