[clang] [clang][ExprConst] allow single element access of vector object to be constant expression (PR #72607)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 6 11:53:54 PST 2023


================
@@ -3806,6 +3840,21 @@ findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
         return handler.found(Index ? O->getComplexFloatImag()
                                    : O->getComplexFloatReal(), ObjType);
       }
+    } else if (ObjType->isVectorType()) {
+      uint64_t Index = Sub.Entries[I].getAsArrayIndex();
+      if (Index >= ObjType->castAs<VectorType>()->getNumElements()) {
+        if (Info.getLangOpts().CPlusPlus11)
+          Info.FFDiag(E, diag::note_constexpr_access_past_end)
+            << handler.AccessKind;
+        else
+          Info.FFDiag(E);
+        return handler.failed();
+      }
+
+      ObjType = ObjType->castAs<VectorType>()->getElementType();
+
+      assert(I == N - 1 && "extracting subobject of scalar?");
+      return handler.found(O->getVectorElt(Index), ObjType);
----------------
AaronBallman wrote:

```suggestion
    } else if (const auto *VT = ObjType->getAs<VectorType>()) {
      uint64_t Index = Sub.Entries[I].getAsArrayIndex();
      if (Index >= VT->getNumElements()) {
        if (Info.getLangOpts().CPlusPlus11)
          Info.FFDiag(E, diag::note_constexpr_access_past_end)
            << handler.AccessKind;
        else
          Info.FFDiag(E);
        return handler.failed();
      }

      ObjType = VT->getElementType();

      assert(I == N - 1 && "extracting subobject of scalar?");
      return handler.found(O->getVectorElt(Index), ObjType);
```

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


More information about the cfe-commits mailing list