[clang] [clang] Preserve ext_vector_type locations during template substitution (PR #235689)

Divyanshu Tata via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 26 03:49:49 PDT 2026


https://github.com/divyanshutata09 created https://github.com/llvm/llvm-project/pull/235689

Instantiating a fixed-length `ext_vector_type` with a dependent element type
emitted an invalid-element diagnostic without any source location.

Record the attribute location and size expression in `VectorTypeLocInfo`
(used by `VectorTypeLoc`, `DependentVectorTypeLoc`, and `DependentSizedExtVectorTypeLoc`)
and preserve them through template substitution in `TreeTransform`, including
when a dependent-sized vector becomes fixed-sized. Also preserve them across
AST traversal and PCH serialization/deserialization.

Fixes #229300

>From 81755c5b0d3589846d0d77ddf3f25014653f0a2f Mon Sep 17 00:00:00 2001
From: Divyanshu Tata <divyanshutataiitian at gmail.com>
Date: Mon, 26 Oct 2026 16:03:56 +0530
Subject: [PATCH] [clang] Preserve ext_vector_type locations during template
 substitution

Instantiating a fixed-length ext_vector_type with a dependent element type
could emit an invalid-element diagnostic without a source location.

Record the attribute location and size expression in VectorTypeLocInfo
(used by VectorTypeLoc, DependentVectorTypeLoc, and DependentSizedExtVectorTypeLoc)
and preserve them through template substitution in TreeTransform, including
when a dependent-sized vector becomes fixed-sized. Also preserve them across
AST traversal and PCH serialization/deserialization.

Fixes #229300
---
 clang/docs/ReleaseNotes.md                    |  3 ++
 clang/include/clang/AST/ASTNodeTraverser.h    | 11 ++++-
 clang/include/clang/AST/RecursiveASTVisitor.h | 15 ++++---
 clang/include/clang/AST/TypeLoc.h             | 37 +++++++++++++----
 clang/lib/Sema/SemaTemplate.cpp               |  2 +
 clang/lib/Sema/SemaType.cpp                   | 32 ++++++++++++---
 clang/lib/Sema/TreeTransform.h                | 40 +++++++++++++------
 clang/lib/Serialization/ASTReader.cpp         |  8 +++-
 clang/lib/Serialization/ASTWriter.cpp         |  8 +++-
 .../PCH/cxx-dependent-sized-ext-vector.cpp    | 23 ++++++++++-
 clang/test/SemaCXX/vector.cpp                 |  6 ++-
 clang/test/SemaTemplate/ext-vector-type.cpp   | 30 +++++++++++++-
 clang/unittests/AST/SourceLocationTest.cpp    | 29 ++++++++++++++
 13 files changed, 205 insertions(+), 39 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 44f30666457f94..0bcdefba1aa063 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -458,6 +458,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 ### Improvements to Clang's diagnostics
 
+- Errors for invalid element types during instantiation of an `ext_vector_type`
+  now include a source location, including in alias templates. (#GH229300)
+
 - Fixed missing `-Wuninitialized-explicit-init` warnings for C file-scope
   tentative definitions of structure types which contain a field marked with
   the `require_explicit_initialization` attribute. (#GH232483)
diff --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h
index 8e09c49982e5d6..c287c100fab88e 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -525,7 +525,16 @@ class ASTNodeTraverser
     Visit(TL.getSizeExpr());
   }
   void VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL) {
-    Visit(cast<DependentSizedExtVectorType>(TL.getType())->getSizeExpr());
+    Expr *Size = TL.getSizeExpr();
+    Visit(Size ? Size : TL.getTypePtr()->getSizeExpr());
+  }
+  void VisitVectorTypeLoc(VectorTypeLoc TL) {
+    if (Expr *Size = TL.getSizeExpr())
+      Visit(Size);
+  }
+  void VisitDependentVectorTypeLoc(DependentVectorTypeLoc TL) {
+    Expr *Size = TL.getSizeExpr();
+    Visit(Size ? Size : TL.getTypePtr()->getSizeExpr());
   }
   void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
     Visit(TL.getUnderlyingExpr());
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 13dd5eda84fa65..c7944fd4a10f05 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1381,25 +1381,30 @@ DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, {
 // FIXME: order? why not size expr first?
 // FIXME: base VectorTypeLoc is unfinished
 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
-  if (TL.getTypePtr()->getSizeExpr())
-    TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
+  Expr *Size = TL.getSizeExpr();
+  if (!Size)
+    Size = TL.getTypePtr()->getSizeExpr();
+  TRY_TO(TraverseStmt(Size));
   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
 })
 
 // FIXME: VectorTypeLoc is unfinished
 DEF_TRAVERSE_TYPELOC(VectorType, {
+  TRY_TO(TraverseStmt(TL.getSizeExpr()));
   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
 })
 
 DEF_TRAVERSE_TYPELOC(DependentVectorType, {
-  if (TL.getTypePtr()->getSizeExpr())
-    TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
+  Expr *Size = TL.getSizeExpr();
+  if (!Size)
+    Size = TL.getTypePtr()->getSizeExpr();
+  TRY_TO(TraverseStmt(Size));
   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
 })
 
-// FIXME: size and attributes
 // FIXME: base VectorTypeLoc is unfinished
 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
+  TRY_TO(TraverseStmt(TL.getSizeExpr()));
   TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
 })
 
diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index b3cb4d0f336295..eb3df4389d4993 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -2062,25 +2062,35 @@ class DependentAddressSpaceTypeLoc
 //
 //===----------------------------------------------------------------------===//
 
-// FIXME: size expression and attribute locations (or keyword if we
-// ever fully support altivec syntax).
+// FIXME: keyword location if we ever fully support altivec syntax.
 struct VectorTypeLocInfo {
   SourceLocation NameLoc;
+  SourceLocation AttrLoc;
+  Expr *SizeExpr;
 };
 
+// FIXME: Make VectorTypeLoc a common base for fixed-size and dependent vector
+// type locations, similar to MatrixTypeLoc.
 class VectorTypeLoc : public ConcreteTypeLoc<UnqualTypeLoc, VectorTypeLoc,
                                              VectorType, VectorTypeLocInfo> {
 public:
   SourceLocation getNameLoc() const { return this->getLocalData()->NameLoc; }
-
   void setNameLoc(SourceLocation Loc) { this->getLocalData()->NameLoc = Loc; }
 
+  SourceLocation getAttrNameLoc() const { return getLocalData()->AttrLoc; }
+  void setAttrNameLoc(SourceLocation Loc) { getLocalData()->AttrLoc = Loc; }
+
+  Expr *getSizeExpr() const { return getLocalData()->SizeExpr; }
+  void setSizeExpr(Expr *E) { getLocalData()->SizeExpr = E; }
+
   SourceRange getLocalSourceRange() const {
     return SourceRange(getNameLoc(), getNameLoc());
   }
 
   void initializeLocal(ASTContext &Context, SourceLocation Loc) {
     setNameLoc(Loc);
+    setAttrNameLoc(Loc);
+    setSizeExpr(nullptr);
   }
 
   TypeLoc getElementLoc() const { return getInnerTypeLoc(); }
@@ -2088,22 +2098,27 @@ class VectorTypeLoc : public ConcreteTypeLoc<UnqualTypeLoc, VectorTypeLoc,
   QualType getInnerType() const { return this->getTypePtr()->getElementType(); }
 };
 
-// FIXME: size expression and attribute locations (or keyword if we
-// ever fully support altivec syntax).
 class DependentVectorTypeLoc
     : public ConcreteTypeLoc<UnqualTypeLoc, DependentVectorTypeLoc,
                              DependentVectorType, VectorTypeLocInfo> {
 public:
   SourceLocation getNameLoc() const { return this->getLocalData()->NameLoc; }
-
   void setNameLoc(SourceLocation Loc) { this->getLocalData()->NameLoc = Loc; }
 
+  SourceLocation getAttrNameLoc() const { return getLocalData()->AttrLoc; }
+  void setAttrNameLoc(SourceLocation Loc) { getLocalData()->AttrLoc = Loc; }
+
+  Expr *getSizeExpr() const { return getLocalData()->SizeExpr; }
+  void setSizeExpr(Expr *E) { getLocalData()->SizeExpr = E; }
+
   SourceRange getLocalSourceRange() const {
     return SourceRange(getNameLoc(), getNameLoc());
   }
 
   void initializeLocal(ASTContext &Context, SourceLocation Loc) {
     setNameLoc(Loc);
+    setAttrNameLoc(Loc);
+    setSizeExpr(nullptr);
   }
 
   TypeLoc getElementLoc() const { return getInnerTypeLoc(); }
@@ -2111,12 +2126,10 @@ class DependentVectorTypeLoc
   QualType getInnerType() const { return this->getTypePtr()->getElementType(); }
 };
 
-// FIXME: size expression and attribute locations.
 class ExtVectorTypeLoc
     : public InheritingConcreteTypeLoc<VectorTypeLoc, ExtVectorTypeLoc,
                                        ExtVectorType> {};
 
-// FIXME: attribute locations.
 // For some reason, this isn't a subtype of VectorType.
 class DependentSizedExtVectorTypeLoc
     : public ConcreteTypeLoc<UnqualTypeLoc, DependentSizedExtVectorTypeLoc,
@@ -2126,12 +2139,20 @@ class DependentSizedExtVectorTypeLoc
 
   void setNameLoc(SourceLocation Loc) { this->getLocalData()->NameLoc = Loc; }
 
+  SourceLocation getAttrNameLoc() const { return getLocalData()->AttrLoc; }
+  void setAttrNameLoc(SourceLocation Loc) { getLocalData()->AttrLoc = Loc; }
+
+  Expr *getSizeExpr() const { return getLocalData()->SizeExpr; }
+  void setSizeExpr(Expr *E) { getLocalData()->SizeExpr = E; }
+
   SourceRange getLocalSourceRange() const {
     return SourceRange(getNameLoc(), getNameLoc());
   }
 
   void initializeLocal(ASTContext &Context, SourceLocation Loc) {
     setNameLoc(Loc);
+    setAttrNameLoc(Loc);
+    setSizeExpr(nullptr);
   }
 
   TypeLoc getElementLoc() const { return getInnerTypeLoc(); }
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index ec1f8077893eb3..f8d5c0d2d65813 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -3911,6 +3911,8 @@ QualType Sema::CheckTemplateIdType(ElaboratedTypeKeyword Keyword,
     if (!AliasTemplate->getDeclContext()->isFileContext())
       SavedContext.emplace(*this, AliasTemplate->getDeclContext());
 
+    // FIXME: Preserve the pattern's type source information during substitution
+    // while respecting underlying type changes made by mode attributes.
     CanonType =
         SubstType(Pattern->getUnderlyingType(), TemplateArgLists,
                   AliasTemplate->getLocation(), AliasTemplate->getDeclName());
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index af0403e73480c0..675eeda667b56e 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -5954,6 +5954,26 @@ static void fillMatrixTypeLoc(MatrixTypeLoc MTL,
   llvm_unreachable("no matrix_type attribute found at the expected location!");
 }
 
+// FIXME: Also handle attributes on the decl-specifier, declarator and
+// declaration. Matrix type locations need the same handling.
+static void fillVectorTypeLoc(VectorTypeLoc VTL,
+                              const ParsedAttributesView &Attrs) {
+  auto Kind = isa<ExtVectorType>(VTL.getTypePtr())
+                  ? ParsedAttr::AT_ExtVectorType
+                  : ParsedAttr::AT_VectorSize;
+  for (const ParsedAttr &AL : Attrs) {
+    if (AL.getKind() == Kind && !AL.isInvalid() && AL.getNumArgs() == 1) {
+      VTL.setAttrNameLoc(AL.getLoc());
+      VTL.setSizeExpr(AL.getArgAsExpr(0));
+      return;
+    }
+  }
+
+  // The attribute may be on the declarator rather than this chunk, or the
+  // vector may use a target-specific attribute. Keep the default locations
+  // when no matching attribute is present.
+}
+
 static void fillAtomicQualLoc(AtomicTypeLoc ATL, const DeclaratorChunk &Chunk) {
   SourceLocation Loc;
   switch (Chunk.Kind) {
@@ -6356,19 +6376,19 @@ namespace {
     void VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
       TL.setExpansionLoc(Chunk.Loc);
     }
-    void VisitVectorTypeLoc(VectorTypeLoc TL) { TL.setNameLoc(Chunk.Loc); }
-    void VisitDependentVectorTypeLoc(DependentVectorTypeLoc TL) {
-      TL.setNameLoc(Chunk.Loc);
+    void VisitVectorTypeLoc(VectorTypeLoc TL) {
+      TL.initializeLocal(Context, Chunk.Loc);
+      fillVectorTypeLoc(TL, Chunk.getAttrs());
     }
-    void VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
-      TL.setNameLoc(Chunk.Loc);
+    void VisitDependentVectorTypeLoc(DependentVectorTypeLoc TL) {
+      TL.initializeLocal(Context, Chunk.Loc);
     }
     void VisitAtomicTypeLoc(AtomicTypeLoc TL) {
       fillAtomicQualLoc(TL, Chunk);
     }
     void
     VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL) {
-      TL.setNameLoc(Chunk.Loc);
+      TL.initializeLocal(Context, Chunk.Loc);
     }
     void VisitMatrixTypeLoc(MatrixTypeLoc TL) {
       fillMatrixTypeLoc(TL, Chunk.getAttrs());
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 25e9113aeb7a46..0aa15b5c999e3a 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6213,14 +6213,17 @@ QualType TreeTransform<Derived>::TransformDependentVectorType(
   EnterExpressionEvaluationContext Unevaluated(
       SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
 
-  ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
+  Expr *OrigSize = TL.getSizeExpr();
+  if (!OrigSize)
+    OrigSize = T->getSizeExpr();
+  ExprResult Size = getDerived().TransformExpr(OrigSize);
   Size = SemaRef.ActOnConstantExpression(Size);
   if (Size.isInvalid())
     return QualType();
 
   QualType Result = TL.getType();
   if (getDerived().AlwaysRebuild() || ElementType != T->getElementType() ||
-      Size.get() != T->getSizeExpr()) {
+      Size.get() != OrigSize) {
     Result = getDerived().RebuildDependentVectorType(
         ElementType, Size.get(), T->getAttributeLoc(), T->getVectorKind());
     if (Result.isNull())
@@ -6232,9 +6235,13 @@ QualType TreeTransform<Derived>::TransformDependentVectorType(
     DependentVectorTypeLoc NewTL =
         TLB.push<DependentVectorTypeLoc>(Result);
     NewTL.setNameLoc(TL.getNameLoc());
+    NewTL.setAttrNameLoc(TL.getAttrNameLoc());
+    NewTL.setSizeExpr(Size.get());
   } else {
     VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
     NewTL.setNameLoc(TL.getNameLoc());
+    NewTL.setAttrNameLoc(TL.getAttrNameLoc());
+    NewTL.setSizeExpr(Size.get());
   }
 
   return Result;
@@ -6246,7 +6253,6 @@ QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType(
                                       DependentSizedExtVectorTypeLoc TL) {
   const DependentSizedExtVectorType *T = TL.getTypePtr();
 
-  // FIXME: ext vector locs should be nested
   QualType ElementType = getDerived().TransformType(TLB, TL.getElementLoc());
   if (ElementType.isNull())
     return QualType();
@@ -6255,18 +6261,19 @@ QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType(
   EnterExpressionEvaluationContext Unevaluated(
       SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
 
-  ExprResult Size = getDerived().TransformExpr(T->getSizeExpr());
+  Expr *OrigSize = TL.getSizeExpr();
+  if (!OrigSize)
+    OrigSize = T->getSizeExpr();
+  ExprResult Size = getDerived().TransformExpr(OrigSize);
   Size = SemaRef.ActOnConstantExpression(Size);
   if (Size.isInvalid())
     return QualType();
 
   QualType Result = TL.getType();
-  if (getDerived().AlwaysRebuild() ||
-      ElementType != T->getElementType() ||
-      Size.get() != T->getSizeExpr()) {
-    Result = getDerived().RebuildDependentSizedExtVectorType(ElementType,
-                                                             Size.get(),
-                                                         T->getAttributeLoc());
+  if (getDerived().AlwaysRebuild() || ElementType != T->getElementType() ||
+      Size.get() != OrigSize) {
+    Result = getDerived().RebuildDependentSizedExtVectorType(
+        ElementType, Size.get(), T->getAttributeLoc());
     if (Result.isNull())
       return QualType();
   }
@@ -6276,9 +6283,13 @@ QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType(
     DependentSizedExtVectorTypeLoc NewTL
       = TLB.push<DependentSizedExtVectorTypeLoc>(Result);
     NewTL.setNameLoc(TL.getNameLoc());
+    NewTL.setAttrNameLoc(TL.getAttrNameLoc());
+    NewTL.setSizeExpr(Size.get());
   } else {
     ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
     NewTL.setNameLoc(TL.getNameLoc());
+    NewTL.setAttrNameLoc(TL.getAttrNameLoc());
+    NewTL.setSizeExpr(Size.get());
   }
 
   return Result;
@@ -6428,6 +6439,8 @@ QualType TreeTransform<Derived>::TransformVectorType(TypeLocBuilder &TLB,
 
   VectorTypeLoc NewTL = TLB.push<VectorTypeLoc>(Result);
   NewTL.setNameLoc(TL.getNameLoc());
+  NewTL.setAttrNameLoc(TL.getAttrNameLoc());
+  NewTL.setSizeExpr(TL.getSizeExpr());
 
   return Result;
 }
@@ -6443,15 +6456,16 @@ QualType TreeTransform<Derived>::TransformExtVectorType(TypeLocBuilder &TLB,
   QualType Result = TL.getType();
   if (getDerived().AlwaysRebuild() ||
       ElementType != T->getElementType()) {
-    Result = getDerived().RebuildExtVectorType(ElementType,
-                                               T->getNumElements(),
-                                               /*FIXME*/ SourceLocation());
+    Result = getDerived().RebuildExtVectorType(ElementType, T->getNumElements(),
+                                               TL.getAttrNameLoc());
     if (Result.isNull())
       return QualType();
   }
 
   ExtVectorTypeLoc NewTL = TLB.push<ExtVectorTypeLoc>(Result);
   NewTL.setNameLoc(TL.getNameLoc());
+  NewTL.setAttrNameLoc(TL.getAttrNameLoc());
+  NewTL.setSizeExpr(TL.getSizeExpr());
 
   return Result;
 }
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 56f1aa8a7f932a..0bed289ecbbc67 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -7670,19 +7670,25 @@ void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
                                         DependentSizedExtVectorTypeLoc TL) {
   TL.setNameLoc(readSourceLocation());
+  TL.setAttrNameLoc(readSourceLocation());
+  TL.setSizeExpr(Reader.readExpr());
 }
 
 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
   TL.setNameLoc(readSourceLocation());
+  TL.setAttrNameLoc(readSourceLocation());
+  TL.setSizeExpr(Reader.readExpr());
 }
 
 void TypeLocReader::VisitDependentVectorTypeLoc(
     DependentVectorTypeLoc TL) {
   TL.setNameLoc(readSourceLocation());
+  TL.setAttrNameLoc(readSourceLocation());
+  TL.setSizeExpr(Reader.readExpr());
 }
 
 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
-  TL.setNameLoc(readSourceLocation());
+  VisitVectorTypeLoc(TL);
 }
 
 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index cb89eaffc4bf00..95ff6cdb4d7e5a 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -433,19 +433,25 @@ void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
                                         DependentSizedExtVectorTypeLoc TL) {
   addSourceLocation(TL.getNameLoc());
+  addSourceLocation(TL.getAttrNameLoc());
+  Record.AddStmt(TL.getSizeExpr());
 }
 
 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
   addSourceLocation(TL.getNameLoc());
+  addSourceLocation(TL.getAttrNameLoc());
+  Record.AddStmt(TL.getSizeExpr());
 }
 
 void TypeLocWriter::VisitDependentVectorTypeLoc(
     DependentVectorTypeLoc TL) {
   addSourceLocation(TL.getNameLoc());
+  addSourceLocation(TL.getAttrNameLoc());
+  Record.AddStmt(TL.getSizeExpr());
 }
 
 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
-  addSourceLocation(TL.getNameLoc());
+  VisitVectorTypeLoc(TL);
 }
 
 void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
diff --git a/clang/test/PCH/cxx-dependent-sized-ext-vector.cpp b/clang/test/PCH/cxx-dependent-sized-ext-vector.cpp
index 93fb56161dfc00..23345a03c0663b 100644
--- a/clang/test/PCH/cxx-dependent-sized-ext-vector.cpp
+++ b/clang/test/PCH/cxx-dependent-sized-ext-vector.cpp
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -std=c++11 -emit-pch %s -o %t
 // RUN: %clang_cc1 -std=c++11 -include-pch %t -verify %s
+// RUN: not %clang_cc1 -std=c++11 -include-pch %t -fno-caret-diagnostics %s 2>&1 | FileCheck %s --implicit-check-not='error:'
 
 // RUN: %clang_cc1 -std=c++11 -emit-pch -fpch-instantiate-templates %s -o %t
 // RUN: %clang_cc1 -std=c++11 -include-pch %t -verify %s
@@ -9,13 +10,33 @@
 #define HEADER_INCLUDED
 
 template<typename T, int N>
+// CHECK: :[[#@LINE+1]]:30: error: zero vector size
 using vec = T __attribute__((ext_vector_type(N)));
 
+struct S {};
+
+template <class T>
+// CHECK: :[[#@LINE+1]]:1: error: invalid vector element type 'S'
+using Alias = T __attribute__((ext_vector_type(4)));
+
+template <class T, int N>
+// CHECK: :[[#@LINE+1]]:32: error: invalid vector element type 'S'
+using Sized = T __attribute__((vector_size(N)));
+
+template <class T> struct Attributed {
+  // CHECK: :[[#@LINE+1]]:29: error: invalid vector element type 'S'
+  typedef T (__attribute__((ext_vector_type(4))) Type);
+};
+
 #else
 
 void test() {
-  vec<float, 2> a;  // expected-error at -5 {{zero vector size}}
+  vec<float, 2> a;  // expected-error at -20 {{zero vector size}}
   vec<float, 0> b; // expected-note {{in instantiation of template type alias 'vec' requested here}}
 }
 
+Alias<S> alias; // expected-error at -18 {{invalid vector element type 'S'}} expected-note {{in instantiation of template type alias 'Alias' requested here}}
+Sized<S, 16> invalid_sized; // expected-error at -15 {{invalid vector element type 'S'}} expected-note {{in instantiation of template type alias 'Sized' requested here}}
+Attributed<S> attributed; // expected-error at -12 {{invalid vector element type 'S'}} expected-note {{in instantiation of template class 'Attributed<S>' requested here}}
+
 #endif
diff --git a/clang/test/SemaCXX/vector.cpp b/clang/test/SemaCXX/vector.cpp
index fd6127a33f1ae6..6ae7d3103d3f54 100644
--- a/clang/test/SemaCXX/vector.cpp
+++ b/clang/test/SemaCXX/vector.cpp
@@ -5,6 +5,7 @@
 // RUN: %clang_cc1 -flax-vector-conversions=all -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=c++20 %s
 // RUN: %clang_cc1 -flax-vector-conversions=integer -triple x86_64-apple-darwin10 -fsyntax-only -verify %s -DNO_LAX_FLOAT
 // RUN: %clang_cc1 -flax-vector-conversions=none -triple x86_64-apple-darwin10 -fsyntax-only -verify %s -DNO_LAX_FLOAT -DNO_LAX_INT
+// RUN: not %clang_cc1 -flax-vector-conversions=all -triple x86_64-apple-darwin10 -fsyntax-only -fno-caret-diagnostics %s 2>&1 | FileCheck %s
 
 typedef char char16 __attribute__ ((__vector_size__ (16)));
 typedef long long longlong16 __attribute__ ((__vector_size__ (16)));
@@ -346,8 +347,8 @@ typedef ScopedE ScopedEnumSizeVector __attribute__((vector_size(16))); // expect
 #endif
 
 template <typename T> struct Vector {
-  // Rebuilding a fixed-size extended vector currently loses the attribute location.
-  typedef T type __attribute__((ext_vector_type(4))); // expected-error@*:* {{invalid vector element type 'GH225037::E'}}
+  // CHECK: :[[#@LINE+1]]:11: error: invalid vector element type 'GH225037::E'
+  typedef T type __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type 'GH225037::E'}}
 };
 Vector<E> enum_vector; // expected-note {{in instantiation of template class 'GH225037::Vector<GH225037::E>' requested here}}
 Vector<int> int_vector;
@@ -546,6 +547,7 @@ namespace PR48540 {
 // The below used to cause an OOM error, or an assert, make sure it is still
 //  valid.
 int (__attribute__((vector_size(16))) a);
+int (parenthesized_vector) __attribute__((vector_size(16)));
 
 template <typename T, int I>
 struct S {
diff --git a/clang/test/SemaTemplate/ext-vector-type.cpp b/clang/test/SemaTemplate/ext-vector-type.cpp
index f968c13f10238e..6d4a250361aa7e 100644
--- a/clang/test/SemaTemplate/ext-vector-type.cpp
+++ b/clang/test/SemaTemplate/ext-vector-type.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+// RUN: not %clang_cc1 -std=c++11 -fsyntax-only -fno-caret-diagnostics %s 2>&1 | FileCheck %s
 template<typename T, unsigned Length> 
 struct make1 { 
   typedef T __attribute__((ext_vector_type(Length))) type; 
@@ -92,3 +93,30 @@ namespace Deduction {
   int array2[X0<float2>::value == 2? 1 : -1];
   int array3[X0<float4>::value == 3? 1 : -1];
 }
+
+// The primary error should retain a source location, independently of the
+// template instantiation notes.
+struct S {};
+
+template <class T> struct Parenthesized {
+  typedef T (Type)
+      // CHECK: :[[#@LINE-1]]:13: error: invalid vector element type 'S'
+      __attribute__((ext_vector_type(4))); // expected-error at -2 {{invalid vector element type 'S'}}
+};
+Parenthesized<S> parenthesized; // expected-note {{in instantiation of template class 'Parenthesized<S>' requested here}}
+
+template <class T>
+// CHECK: :[[#@LINE+1]]:1: error: invalid vector element type 'S'
+using Alias = T __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type 'S'}}
+Alias<S> alias; // expected-note {{in instantiation of template type alias 'Alias' requested here}}
+Alias<int> valid_alias;
+
+// The source location must also survive conversion from a dependent-sized
+// vector to a fixed-size vector whose element type is still dependent.
+template <int N> struct TwoStage {
+  template <class T>
+  // CHECK: :[[#@LINE+1]]:3: error: invalid vector element type 'S'
+  using Type = T __attribute__((ext_vector_type(N))); // expected-error {{invalid vector element type 'S'}}
+};
+TwoStage<4>::Type<S> two_stage; // expected-note {{in instantiation of template type alias 'Type' requested here}}
+TwoStage<4>::Type<int> valid_two_stage;
diff --git a/clang/unittests/AST/SourceLocationTest.cpp b/clang/unittests/AST/SourceLocationTest.cpp
index 4cc3c7aae9829d..863cacf756bbf8 100644
--- a/clang/unittests/AST/SourceLocationTest.cpp
+++ b/clang/unittests/AST/SourceLocationTest.cpp
@@ -230,6 +230,35 @@ TEST(TypeLoc, LongRange) {
   EXPECT_TRUE(Verifier.match("long a;", typeLoc()));
 }
 
+TEST(TypeLoc, VectorSourceInfo) {
+  llvm::Annotations Code(R"cpp(
+    typedef int (__attribute__(($Vector^vector_size($VectorSize[[8 * 2]]))) Vector);
+    typedef int (__attribute__(($Extended^ext_vector_type($ExtendedSize[[4]]))) Extended);
+  )cpp");
+  auto AST = tooling::buildASTFromCodeWithArgs(Code.code(), /*Args=*/{});
+  ASSERT_TRUE(AST);
+
+  ASTContext &Ctx = AST->getASTContext();
+  const SourceManager &SM = Ctx.getSourceManager();
+  auto Matches = match(
+      typedefNameDecl(hasAnyName("Vector", "Extended")).bind("decl"), Ctx);
+  ASSERT_EQ(Matches.size(), 2u);
+  for (const auto &Match : Matches) {
+    const auto *Decl = Match.getNodeAs<TypedefNameDecl>("decl");
+    std::string Name = Decl->getNameAsString();
+    auto TL = Decl->getTypeSourceInfo()->getTypeLoc().getAs<VectorTypeLoc>();
+    ASSERT_TRUE(TL) << Name;
+    ASSERT_TRUE(TL.getAttrNameLoc().isValid()) << Name;
+    EXPECT_EQ(SM.getFileOffset(TL.getAttrNameLoc()), Code.point(Name)) << Name;
+    ASSERT_TRUE(TL.getSizeExpr()) << Name;
+    auto Range = Lexer::getAsCharRange(TL.getSizeExpr()->getSourceRange(), SM,
+                                       Ctx.getLangOpts());
+    auto Expected = Code.range(Name + "Size");
+    EXPECT_EQ(SM.getFileOffset(Range.getBegin()), Expected.Begin) << Name;
+    EXPECT_EQ(SM.getFileOffset(Range.getEnd()), Expected.End) << Name;
+  }
+}
+
 TEST(TypeLoc, DecltypeTypeLocRange) {
   llvm::Annotations Code(R"(
     $full1[[decltype(1)]] a;



More information about the cfe-commits mailing list