Author: Chen Miao Date: 2026-11-15T10:09:53+08:00 New Revision: 7075e15fe997deecdbe064b7e9cb8742d6fc7595
URL: https://github.com/llvm/llvm-project/commit/7075e15fe997deecdbe064b7e9cb8742d6fc7595 DIFF: https://github.com/llvm/llvm-project/commit/7075e15fe997deecdbe064b7e9cb8742d6fc7595.diff LOG: [clang] Preserve ext_vector_type locations during template substitution (#229328) Instantiating a fixed-length `ext_vector_type` with a dependent element type could emit an invalid-element diagnostic without a source location. For example: ```cc struct S {}; template <typename T> struct Vector { typedef T type __attribute__((ext_vector_type(4))); }; Vector<S> v; ``` Before: ```txt error: invalid vector element type 'S' repro.cc:7:11: note: in instantiation of template class 'Vector<S>' requested here 7 | Vector<S> v; | ^ 1 error generated. ``` After: ```txt repro.cc:4:33: error: invalid vector element type 'S' 4 | typedef T type __attribute__((ext_vector_type(4))); | ^ repro.cc:7:11: note: in instantiation of template class 'Vector<S>' requested here 7 | Vector<S> v; | ^ 1 error generated. ``` Follow the `MatrixTypeLoc` approach to store attribute locations and size expressions in `VictorTypeLoc`, fixing missing source locations in template instantiation diagnostics. Fixes #229300 Added: Modified: clang/docs/ReleaseNotes.md clang/include/clang/AST/ASTNodeTraverser.h clang/include/clang/AST/RecursiveASTVisitor.h clang/include/clang/AST/TypeLoc.h clang/lib/Sema/SemaTemplate.cpp clang/lib/Sema/SemaType.cpp clang/lib/Sema/TreeTransform.h clang/lib/Serialization/ASTReader.cpp clang/lib/Serialization/ASTWriter.cpp clang/test/PCH/cxx-dependent-sized-ext-vector.cpp clang/test/SemaCXX/vector.cpp clang/test/SemaTemplate/ext-vector-type.cpp clang/unittests/AST/SourceLocationTest.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 563b711fb89db0..a72577e6b33d71 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -511,6 +511,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 `-Wheader-guard` warnings pointing at `if` instead of the macro name in `#if !defined(...)` header guards. diff --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h index 8e09c49982e5d6..e3314e99d9ad69 100644 --- a/clang/include/clang/AST/ASTNodeTraverser.h +++ b/clang/include/clang/AST/ASTNodeTraverser.h @@ -525,7 +525,11 @@ class ASTNodeTraverser Visit(TL.getSizeExpr()); } void VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL) { - Visit(cast<DependentSizedExtVectorType>(TL.getType())->getSizeExpr()); + Visit(TL.getSizeExpr()); + } + void VisitVectorTypeLoc(VectorTypeLoc TL) { Visit(TL.getSizeExpr()); } + void VisitDependentVectorTypeLoc(DependentVectorTypeLoc TL) { + Visit(TL.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 56aa9b772b1e75..70373178cddb8b 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1381,25 +1381,24 @@ 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())); + TRY_TO(TraverseStmt(TL.getSizeExpr())); 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())); + TRY_TO(TraverseStmt(TL.getSizeExpr())); 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..9a16e0b43937da 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(getTypePtr()->getSizeExpr()); } 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(getTypePtr()->getSizeExpr()); } TypeLoc getElementLoc() const { return getInnerTypeLoc(); } diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 0caf7593ca9d5b..411ff948012a22 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 e16630ce9cd844..19d39b58614859 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -5980,6 +5980,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) { @@ -6382,19 +6402,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 2912ed99df4617..2b6348b7ec8a6b 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6224,14 +6224,15 @@ QualType TreeTransform<Derived>::TransformDependentVectorType( EnterExpressionEvaluationContext Unevaluated( SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); - ExprResult Size = getDerived().TransformExpr(T->getSizeExpr()); + Expr *OrigSize = TL.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()) @@ -6243,9 +6244,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; @@ -6257,7 +6262,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(); @@ -6266,18 +6270,17 @@ QualType TreeTransform<Derived>::TransformDependentSizedExtVectorType( EnterExpressionEvaluationContext Unevaluated( SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); - ExprResult Size = getDerived().TransformExpr(T->getSizeExpr()); + Expr *OrigSize = TL.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(); } @@ -6287,9 +6290,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; @@ -6439,6 +6446,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; } @@ -6454,15 +6463,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 cd7d9f24924f82..db04c06f3cee73 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -7674,19 +7674,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 ad218305039f67..3ae7ac0e8c9fa0 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..09e8d96bf24a84 100644 --- a/clang/test/PCH/cxx-dependent-sized-ext-vector.cpp +++ b/clang/test/PCH/cxx-dependent-sized-ext-vector.cpp @@ -11,11 +11,27 @@ template<typename T, int N> using vec = T __attribute__((ext_vector_type(N))); +struct S {}; + +template <class T> +using Alias = T __attribute__((ext_vector_type(4))); + +template <class T, int N> +using Sized = T __attribute__((vector_size(N))); + +template <class T> struct Attributed { + typedef T (__attribute__((ext_vector_type(4))) Type); +}; + #else void test() { - vec<float, 2> a; // expected-error@-5 {{zero vector size}} + vec<float, 2> a; // expected-error@-17 {{zero vector size}} vec<float, 0> b; // expected-note {{in instantiation of template type alias 'vec' requested here}} } +Alias<S> alias; // expected-error@-16 {{invalid vector element type 'S'}} expected-note {{in instantiation of template type alias 'Alias' requested here}} +Sized<S, 16> invalid_sized; // expected-error@-14 {{invalid vector element type 'S'}} expected-note {{in instantiation of template type alias 'Sized' requested here}} +Attributed<S> attributed; // expected-error@-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..8b71e1157ec2ce 100644 --- a/clang/test/SemaCXX/vector.cpp +++ b/clang/test/SemaCXX/vector.cpp @@ -346,8 +346,7 @@ 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'}} + 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 +545,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..766c0c531439de 100644 --- a/clang/test/SemaTemplate/ext-vector-type.cpp +++ b/clang/test/SemaTemplate/ext-vector-type.cpp @@ -92,3 +92,26 @@ 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) __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type 'S'}} +}; +Parenthesized<S> parenthesized; // expected-note {{in instantiation of template class 'Parenthesized<S>' requested here}} + +template <class T> +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> + 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..6a5ce634c0f890 100644 --- a/clang/unittests/AST/SourceLocationTest.cpp +++ b/clang/unittests/AST/SourceLocationTest.cpp @@ -230,6 +230,53 @@ 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); + template <int N> struct V { + typedef int __attribute__((vector_size(N))) DependentVector; + typedef int __attribute__((ext_vector_type(N))) DependentExtended; + }; + )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; + } + + auto DependentMatches = + match(typedefNameDecl(hasAnyName("DependentVector", "DependentExtended")) + .bind("decl"), + Ctx); + ASSERT_EQ(DependentMatches.size(), 2u); + for (const auto &Match : DependentMatches) { + const auto *Decl = Match.getNodeAs<TypedefNameDecl>("decl"); + TypeLoc TL = Decl->getTypeSourceInfo()->getTypeLoc(); + if (auto VTL = TL.getAs<DependentVectorTypeLoc>()) + EXPECT_TRUE(VTL.getSizeExpr()); + else + EXPECT_TRUE(TL.castAs<DependentSizedExtVectorTypeLoc>().getSizeExpr()); + } +} + TEST(TypeLoc, DecltypeTypeLocRange) { llvm::Annotations Code(R"( $full1[[decltype(1)]] a; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
