llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Hana Dusíková (hanickadot)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/196377.diff


2 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+1-1) 
- (added) clang/test/SemaCXX/builtin-object-size-cxx23.cpp (+20) 


``````````diff
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);
+
+
+

``````````

</details>


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

Reply via email to