[clang] [clang][bytecode] Handle non-primitive array index expressions (PR #128479)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 24 00:06:39 PST 2025


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

By rejecting them instead of asserting in `classifyPrim()`.

>From 5fc1574ccca3303b936f7fba0a0e8e1a0707a6df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 24 Feb 2025 09:05:17 +0100
Subject: [PATCH] [clang][bytecode] Handle non-primitive array index
 expressions

By rejecting them instead of asserting in `classifyPrim()`.
---
 clang/lib/AST/ByteCode/Compiler.cpp | 9 ++++++---
 clang/test/AST/ByteCode/arrays.cpp  | 8 ++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 91e0484da0e58..b5745e516174d 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1689,14 +1689,17 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
   if (!Success)
     return false;
 
-  PrimType IndexT = classifyPrim(Index->getType());
+  std::optional<PrimType> IndexT = classify(Index->getType());
+  // In error-recovery cases, the index expression has a dependent type.
+  if (!IndexT)
+    return this->emitError(E);
   // If the index is first, we need to change that.
   if (LHS == Index) {
-    if (!this->emitFlip(PT_Ptr, IndexT, E))
+    if (!this->emitFlip(PT_Ptr, *IndexT, E))
       return false;
   }
 
-  return this->emitArrayElemPtrPop(IndexT, E);
+  return this->emitArrayElemPtrPop(*IndexT, E);
 }
 
 template <class Emitter>
diff --git a/clang/test/AST/ByteCode/arrays.cpp b/clang/test/AST/ByteCode/arrays.cpp
index 4097c65f7c6fb..9204179ad1a46 100644
--- a/clang/test/AST/ByteCode/arrays.cpp
+++ b/clang/test/AST/ByteCode/arrays.cpp
@@ -654,3 +654,11 @@ namespace ZeroSizeTypes {
                               // both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}
   }
 }
+
+namespace InvalidIndex {
+  constexpr int foo(int i) { // both-error {{no return statement in constexpr function}}
+    int a[] = {1,2,3};
+    return a[_z]; // both-error {{use of undeclared identifier}}
+  }
+  static_assert(foo(0) == 1, "");
+}



More information about the cfe-commits mailing list