[clang] [clang][Interp] Decay pointers to the first element (PR #72660)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 17 07:11:43 PST 2023


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

I _think_ this makes sense to do here. This way we have a pointer to the first element on the stack.

>From 7ea07d2aa8be337e1ff4ff2e28ae407c57a382fc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 17 Nov 2023 15:54:02 +0100
Subject: [PATCH] [clang][Interp] Decay pointers to the first element

---
 clang/lib/AST/Interp/Interp.h    | 6 ++++--
 clang/test/AST/Interp/arrays.cpp | 3 +++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 026a95d65488da9..adbeab06923e49e 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1822,10 +1822,12 @@ inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
 /// Just takes a pointer and checks if its' an incomplete
 /// array type.
 inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
-  const Pointer &Ptr = S.Stk.peek<Pointer>();
+  const Pointer &Ptr = S.Stk.pop<Pointer>();
 
-  if (!Ptr.isUnknownSizeArray())
+  if (!Ptr.isUnknownSizeArray()) {
+    S.Stk.push<Pointer>(Ptr.atIndex(0));
     return true;
+  }
 
   const SourceInfo &E = S.Current->getSource(OpPC);
   S.FFDiag(E, diag::note_constexpr_unsupported_unsized_array);
diff --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index 34e0086fb9ee8ca..3babfed5e2b28a5 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -27,6 +27,9 @@ static_assert(foo[2][3] == &m, "");
 static_assert(foo[2][4] == nullptr, "");
 
 
+const int SomeInt[] = {1};
+int getSomeInt() { return *SomeInt; }
+
 /// A init list for a primitive value.
 constexpr int f{5};
 static_assert(f == 5, "");



More information about the cfe-commits mailing list