https://github.com/hanickadot created 
https://github.com/llvm/llvm-project/pull/196377

Recently GCC made these builtin constexpr, Tomasz Kaminski asked me about 
incompatibility of clang implementation. I found out it doesn't work on 
StringLiteral. This fixes that, I tried to look at the bytecode interpreter, 
but I'm not comfortable fixing it there.

From da87f5083da8d3fb72e80f0ee0aa084302489101 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hana=20Dusi=CC=81kova=CC=81?= <[email protected]>
Date: Thu, 7 May 2026 19:43:08 +0200
Subject: [PATCH] [clang] make __buildin_object_size and
 __builtin_dynamic_object_size compatible with GCC's constexpr implementation
 (it should work on string literals)

---
 clang/lib/AST/ExprConstant.cpp                |  2 +-
 .../SemaCXX/builtin-object-size-cxx23.cpp     | 20 +++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/builtin-object-size-cxx23.cpp

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3f3a80f5b77a3..581120e87d88b 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -15911,7 +15911,7 @@ static QualType getObjectType(APValue::LValueBase B) {
     if (const VarDecl *VD = dyn_cast<VarDecl>(D))
       return VD->getType();
   } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
-    if (isa<CompoundLiteralExpr>(E))
+    if (isa<CompoundLiteralExpr>(E) || isa<StringLiteral>(E))
       return E->getType();
   } else if (B.is<TypeInfoLValue>()) {
     return B.getTypeInfoType();
diff --git a/clang/test/SemaCXX/builtin-object-size-cxx23.cpp 
b/clang/test/SemaCXX/builtin-object-size-cxx23.cpp
new file mode 100644
index 0000000000000..3c892c160fe15
--- /dev/null
+++ b/clang/test/SemaCXX/builtin-object-size-cxx23.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++23 %s
+
+constexpr int stringLength(const char *p) {
+  return __builtin_dynamic_object_size(p, 0);
+}
+
+static_assert(stringLength("hello") == 6);
+
+constexpr int allocation(unsigned n) {
+  const char * ptr = new char[n];
+  int res = stringLength(ptr);
+  delete[] ptr;
+  return res;
+}
+
+static_assert(allocation(1) == 1);
+static_assert(allocation(14) == 14);
+
+
+

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to