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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

...  as "pointer to subobject".

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/EvaluationResult.cpp (+2-1) 
- (modified) clang/test/AST/ByteCode/new-delete.cpp (+4) 


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

``````````

</details>


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


More information about the cfe-commits mailing list