[clang] Fix a crash in constant evaluation of ExtVectorElementExprs (PR #136771)

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 22 14:04:05 PDT 2025


https://github.com/ahatanak created https://github.com/llvm/llvm-project/pull/136771

Handle the case where the base expression is a pointer to a vector type.

rdar://149223362

>From c8df1a51efd6349c5905b337059d597d3c1d7146 Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <ahatanak at gmail.com>
Date: Tue, 22 Apr 2025 12:56:46 -0700
Subject: [PATCH] Fix a crash in constant evaluation of ExtVectorElementExprs

Handle the case where the base expression is a pointer to a vector
type.

rdar://149223362
---
 clang/lib/AST/ExprConstant.cpp                           | 5 ++++-
 clang/test/SemaCXX/constexpr-vectors-access-elements.cpp | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f598ef5929aa4..fd870c84796a5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9197,7 +9197,10 @@ bool LValueExprEvaluator::VisitExtVectorElementExpr(
 
   if (Success) {
     Result.setFrom(Info.Ctx, Val);
-    const auto *VT = E->getBase()->getType()->castAs<VectorType>();
+    QualType BaseType = E->getBase()->getType();
+    if (E->isArrow())
+      BaseType = BaseType->getPointeeType();
+    const auto *VT = BaseType->castAs<VectorType>();
     HandleLValueVectorElement(Info, E, Result, VT->getElementType(),
                               VT->getNumElements(), Indices[0]);
   }
diff --git a/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp b/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp
index 08223e15feb72..58efcde414af2 100644
--- a/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp
+++ b/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp
@@ -43,4 +43,6 @@ static_assert(b.lo.lo == 1); // expected-error {{not an integral constant expres
 // make sure clang rejects taking address of a vector element
 static_assert(&b[1]); // expected-error {{address of vector element requested}}
 
+constexpr const FourIntsExtVec *p = &b;
+static_assert(p->x == 1);
 }



More information about the cfe-commits mailing list