[clang] [clang][bytecode] Diagnose heap-allocated array elem pointers... (PR #137523)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 27 08:34:15 PDT 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/137523

...  as "pointer to subobject".

>From af8eadcf3930667b5b6b215df8312c21e9f4381a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 27 Apr 2025 17:33:13 +0200
Subject: [PATCH] [clang][bytecode] Diagnose heap-allocated array elem
 pointers...

...  as "pointer to subobject".
---
 clang/lib/AST/ByteCode/EvaluationResult.cpp | 3 ++-
 clang/test/AST/ByteCode/new-delete.cpp      | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/EvaluationResult.cpp b/clang/lib/AST/ByteCode/EvaluationResult.cpp
index d603e08c7bb4d..f59612bf00015 100644
--- a/clang/lib/AST/ByteCode/EvaluationResult.cpp
+++ b/clang/lib/AST/ByteCode/EvaluationResult.cpp
@@ -230,8 +230,9 @@ bool EvaluationResult::checkReturnValue(InterpState &S, const Context &Ctx,
       assert(B->getDescriptor());
       assert(B->getDescriptor()->asExpr());
 
+      bool IsSubobj = !Ptr.isRoot() || Ptr.isArrayElement();
       S.FFDiag(Info, diag::note_constexpr_dynamic_alloc)
-          << Ptr.getType()->isReferenceType() << !Ptr.isRoot();
+          << Ptr.getType()->isReferenceType() << IsSubobj;
       S.Note(B->getDescriptor()->asExpr()->getExprLoc(),
              diag::note_constexpr_dynamic_alloc_here);
       return false;
diff --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp
index e1b81e9a7963e..3de527c881680 100644
--- a/clang/test/AST/ByteCode/new-delete.cpp
+++ b/clang/test/AST/ByteCode/new-delete.cpp
@@ -619,6 +619,10 @@ namespace std {
   }
 }
 
+constexpr int *escape = std::allocator<int>().allocate(3); // both-error {{constant expression}} \
+                                                           // both-note {{pointer to subobject of heap-allocated}} \
+                                                           // both-note {{heap allocation performed here}}
+
 /// Specialization for float, using operator new/delete.
 namespace std {
   using size_t = decltype(sizeof(0));



More information about the cfe-commits mailing list