Index: clang/include/clang/AST/ASTNodeTraverser.h =================================================================== --- clang/include/clang/AST/ASTNodeTraverser.h +++ clang/include/clang/AST/ASTNodeTraverser.h @@ -23,6 +23,7 @@ #include "clang/AST/StmtVisitor.h" #include "clang/AST/TemplateArgumentVisitor.h" #include "clang/AST/Type.h" +#include "clang/AST/TypeLocVisitor.h" #include "clang/AST/TypeVisitor.h" namespace clang { @@ -48,6 +49,7 @@ void Visit(const Stmt *Node); void Visit(const Type *T); void Visit(QualType T); + void Visit(TypeLoc TL); void Visit(const Decl *D); void Visit(const CXXCtorInitializer *Init); void Visit(const OMPClause *C); @@ -64,6 +66,7 @@ public comments::ConstCommentVisitor, public TypeVisitor, + public TypeLocVisitor, public ConstAttrVisitor, public ConstTemplateArgumentVisitor { @@ -181,6 +184,13 @@ }); } + void Visit(TypeLoc TL) { + getNodeDelegate().AddChild([=] { + getNodeDelegate().Visit(TL); + TypeLocVisitor::Visit(TL); + }); + } + void Visit(const Attr *A) { getNodeDelegate().AddChild([=] { getNodeDelegate().Visit(A); @@ -282,6 +292,8 @@ Visit(*QT); else if (const auto *T = N.get()) Visit(T); + else if (const auto *TL = N.get()) + Visit(*TL); else if (const auto *C = N.get()) Visit(C); else if (const auto *C = N.get()) @@ -414,6 +426,76 @@ // FIXME: ElaboratedType, DependentNameType, // DependentTemplateSpecializationType, ObjCObjectType + void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { + Visit(TL.getUnqualifiedLoc()); + } + void VisitComplexTypeLoc(ComplexTypeLoc TL) { Visit(TL.getType()); } + void VisitPointerTypeLoc(PointerTypeLoc TL) { Visit(TL.getPointeeLoc()); } + void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { + Visit(TL.getPointeeLoc()); + } + void VisitReferenceTypeLoc(ReferenceTypeLoc TL) { Visit(TL.getPointeeLoc()); } + void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { + Visit(TL.getClass()); + Visit(TL.getPointeeLoc()); + } + void VisitArrayTypeLoc(ArrayTypeLoc TL) { Visit(TL.getElementLoc()); } + void VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { + VisitArrayTypeLoc(TL); + } + void VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc TL) { + VisitArrayTypeLoc(TL); + } + void VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL) { + Visit(TL.getElementLoc()); + } + void VisitVectorTypeLoc(VectorTypeLoc TL) { Visit(TL.getElementLoc()); } + void VisitFunctionTypeLoc(FunctionTypeLoc TL) { Visit(TL.getReturnLoc()); } + void VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { + VisitFunctionTypeLoc(TL); + // FIXME: Parameter TypeLocs + } + void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { + Visit(TL.getUnderlyingExpr()); + } + void VisitDecltypeTypeLoc(const DecltypeTypeLoc TL) { + Visit(TL.getUnderlyingExpr()); + } + void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { + // FIXME: Transformed TypeLoc + } + void VisitAttributedTypeLoc(AttributedTypeLoc TL) { + // FIXME: AttrKind + Visit(TL.getModifiedLoc()); + } + void VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) { + Visit(TL.getWrappedLoc()); + } + void VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL) { + // FIXME: Replaced Parameter + } + void + VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc TL) { + // FIXME: Replaced Parameter, Argument Pack + } + void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { + // FIXME: Arguments + } + void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { + Visit(TL.getPointeeLoc()); + } + void VisitAtomicTypeLoc(AtomicTypeLoc TL) { Visit(TL.getValueLoc()); } + void VisitPipeTypeLoc(PipeTypeLoc TL) { Visit(TL.getValueLoc()); } + void VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { Visit(TL.getOriginalLoc()); } + void VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { + Visit(TL.getPatternLoc()); + } + void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { + Visit(TL.getNamedTypeLoc()); + } + // FIXME: DependentNameTypeLoc, DependentTemplateSpecializationTypeLoc, + // ObjCObjectTypeLoc + void VisitTypedefDecl(const TypedefDecl *D) { Visit(D->getUnderlyingType()); } void VisitEnumConstantDecl(const EnumConstantDecl *D) { Index: clang/include/clang/AST/TextNodeDumper.h =================================================================== --- clang/include/clang/AST/TextNodeDumper.h +++ clang/include/clang/AST/TextNodeDumper.h @@ -19,11 +19,12 @@ #include "clang/AST/CommentCommandTraits.h" #include "clang/AST/CommentVisitor.h" #include "clang/AST/DeclVisitor.h" -#include "clang/AST/ExprConcepts.h" #include "clang/AST/ExprCXX.h" +#include "clang/AST/ExprConcepts.h" #include "clang/AST/StmtVisitor.h" #include "clang/AST/TemplateArgumentVisitor.h" #include "clang/AST/Type.h" +#include "clang/AST/TypeLocVisitor.h" #include "clang/AST/TypeVisitor.h" namespace clang { @@ -132,6 +133,7 @@ public ConstTemplateArgumentVisitor, public ConstStmtVisitor, public TypeVisitor, + public TypeLocVisitor, public ConstDeclVisitor { raw_ostream &OS; const bool ShowColors; @@ -179,6 +181,8 @@ void Visit(QualType T); + void Visit(TypeLoc TL); + void Visit(const Decl *D); void Visit(const CXXCtorInitializer *Init); @@ -326,6 +330,31 @@ void VisitObjCInterfaceType(const ObjCInterfaceType *T); void VisitPackExpansionType(const PackExpansionType *T); + void VisitQualifiedTypeLoc(QualifiedTypeLoc TL); + void VisitRValueReferenceTypeLoc(ReferenceTypeLoc TL); + void VisitArrayTypeLoc(ArrayTypeLoc TL); + void VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL); + void VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL); + void VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc TL); + void VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL); + void VisitVectorTypeLoc(VectorTypeLoc TL); + void VisitFunctionTypeLoc(FunctionTypeLoc TL); + void VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL); + void VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL); + void VisitUsingTypeLoc(UsingTypeLoc TL); + void VisitTypedefTypeLoc(TypedefTypeLoc TL); + void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL); + void VisitTagTypeLoc(TagTypeLoc TL); + void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL); + void VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL); + void VisitAutoTypeLoc(AutoTypeLoc TL); + void VisitDeducedTemplateSpecializationTypeLoc( + DeducedTemplateSpecializationTypeLoc TL); + void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL); + void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL); + void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); + void VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL); + void VisitLabelDecl(const LabelDecl *D); void VisitTypedefDecl(const TypedefDecl *D); void VisitEnumDecl(const EnumDecl *D); Index: clang/include/clang/AST/TypeLoc.h =================================================================== --- clang/include/clang/AST/TypeLoc.h +++ clang/include/clang/AST/TypeLoc.h @@ -117,6 +117,8 @@ return (TypeLocClass) getType()->getTypeClass(); } + llvm::StringRef getTypeLocClassName() const; + bool isNull() const { return !Ty; } explicit operator bool() const { return Ty; } Index: clang/lib/AST/TextNodeDumper.cpp =================================================================== --- clang/lib/AST/TextNodeDumper.cpp +++ clang/lib/AST/TextNodeDumper.cpp @@ -17,6 +17,7 @@ #include "clang/AST/DeclTemplate.h" #include "clang/AST/LocInfoType.h" #include "clang/AST/Type.h" +#include "clang/AST/TypeLocVisitor.h" #include "clang/Basic/Module.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/Specifiers.h" @@ -239,6 +240,18 @@ OS << " " << T.split().Quals.getAsString(); } +void TextNodeDumper::Visit(TypeLoc TL) { + { + ColorScope Color(OS, ShowColors, TypeColor); + OS << TL.getTypeLocClassName() << "TypeLoc"; + } + dumpSourceRange(TL.getSourceRange()); + OS << " "; + dumpBareType(TL.getType(), false); + + TypeLocVisitor::Visit(TL); +} + void TextNodeDumper::Visit(const Decl *D) { if (!D) { ColorScope Color(OS, ShowColors, NullColor); @@ -1622,6 +1635,116 @@ OS << " expansions " << *N; } +void TextNodeDumper::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { + OS << " " << TL.getType().split().Quals.getAsString(); +} + +void TextNodeDumper::VisitRValueReferenceTypeLoc(ReferenceTypeLoc TL) { + VisitReferenceType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitArrayTypeLoc(ArrayTypeLoc TL) { + if (Context) + VisitArrayType(Context->getAsArrayType(TL.getType())); +} + +void TextNodeDumper::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { + if (Context) + VisitConstantArrayType(Context->getAsConstantArrayType(TL.getType())); +} + +void TextNodeDumper::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { + if (Context) + VisitVariableArrayType(Context->getAsVariableArrayType(TL.getType())); +} + +void TextNodeDumper::VisitDependentSizedArrayTypeLoc( + DependentSizedArrayTypeLoc TL) { + if (Context) + VisitDependentSizedArrayType( + Context->getAsDependentSizedArrayType(TL.getType())); +} + +void TextNodeDumper::VisitDependentSizedExtVectorTypeLoc( + DependentSizedExtVectorTypeLoc TL) { + VisitDependentSizedExtVectorType( + cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitVectorTypeLoc(VectorTypeLoc TL) { + VisitVectorType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitFunctionTypeLoc(FunctionTypeLoc TL) { + VisitFunctionType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { + VisitFunctionProtoType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { + VisitUnresolvedUsingType( + cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitUsingTypeLoc(UsingTypeLoc TL) { + VisitUsingType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitTypedefTypeLoc(TypedefTypeLoc TL) { + VisitTypedefType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { + VisitUnaryTransformType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitTagTypeLoc(TagTypeLoc TL) { + VisitTagType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { + VisitTemplateTypeParmType( + cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitSubstTemplateTypeParmTypeLoc( + SubstTemplateTypeParmTypeLoc TL) { + VisitSubstTemplateTypeParmType( + cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitAutoTypeLoc(AutoTypeLoc TL) { + VisitAutoType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitDeducedTemplateSpecializationTypeLoc( + DeducedTemplateSpecializationTypeLoc TL) { + VisitDeducedTemplateSpecializationType( + cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitTemplateSpecializationTypeLoc( + TemplateSpecializationTypeLoc TL) { + VisitTemplateSpecializationType( + cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitInjectedClassNameTypeLoc( + InjectedClassNameTypeLoc TL) { + VisitInjectedClassNameType( + cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { + VisitObjCInterfaceType(cast(TL.getType().getTypePtr())); +} + +void TextNodeDumper::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { + VisitPackExpansionType(cast(TL.getType().getTypePtr())); +} + void TextNodeDumper::VisitLabelDecl(const LabelDecl *D) { dumpName(D); } void TextNodeDumper::VisitTypedefDecl(const TypedefDecl *D) { Index: clang/lib/AST/TypeLoc.cpp =================================================================== --- clang/lib/AST/TypeLoc.cpp +++ clang/lib/AST/TypeLoc.cpp @@ -57,6 +57,22 @@ namespace { +class TypeNamer : public TypeLocVisitor { +public: +#define ABSTRACT_TYPELOC(CLASS, PARENT) +#define TYPELOC(CLASS, PARENT) \ + llvm::StringRef Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { return #CLASS; } +#include "clang/AST/TypeLocNodes.def" +}; + +} // namespace + +llvm::StringRef TypeLoc::getTypeLocClassName() const { + return TypeNamer().Visit(*this); +} + +namespace { + class TypeAligner : public TypeLocVisitor { public: #define ABSTRACT_TYPELOC(CLASS, PARENT)