Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][Interp] Decay arrays to the first element #72660

Merged
merged 1 commit into from
Dec 12, 2023

Conversation

tbaederr
Copy link
Contributor

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

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 17, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 17, 2023

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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


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

2 Files Affected:

  • (modified) clang/lib/AST/Interp/Interp.h (+4-2)
  • (modified) clang/test/AST/Interp/arrays.cpp (+3)
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, "");

@tbaederr tbaederr changed the title [clang][Interp] Decay pointers to the first element [clang][Interp] Decay arrays to the first element Nov 17, 2023
@tbaederr
Copy link
Contributor Author

Ping

1 similar comment
@tbaederr
Copy link
Contributor Author

tbaederr commented Dec 8, 2023

Ping

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a testing nit

clang/test/AST/Interp/arrays.cpp Outdated Show resolved Hide resolved
@tbaederr tbaederr merged commit c8c9af1 into llvm:main Dec 12, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants