[clang] [clang][bytecode] Fix two small builtin_constant_p cases (PR #137587)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 27 23:55:07 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Only accept string literals if we're pointing to the first index and do accept complex literals.
---
Full diff: https://github.com/llvm/llvm-project/pull/137587.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+4-1)
- (modified) clang/test/AST/ByteCode/builtin-constant-p.cpp (+13-2)
``````````diff
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}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/137587
More information about the cfe-commits
mailing list