upsj updated this revision to Diff 467698.
upsj marked 6 inline comments as done.
upsj added a comment.

I incorporated the `cast`, `ASTContext` and `StringRef` suggestions. About 
JSONDumper, I was surprised to see that it doesn't seem to use `AddChild` for 
the AST hierarchy at all, so I'm not quite clear how (if?) it builds a nested 
output. What's the easiest way to invoke it?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135518/new/

https://reviews.llvm.org/D135518

Files:
  clang/include/clang/AST/ASTNodeTraverser.h
  clang/include/clang/AST/TextNodeDumper.h
  clang/include/clang/AST/TypeLoc.h
  clang/lib/AST/TextNodeDumper.cpp
  clang/lib/AST/TypeLoc.cpp

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<TypeNamer, llvm::StringRef> {
+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<TypeAligner, unsigned> {
 public:
 #define ABSTRACT_TYPELOC(CLASS, PARENT)
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<TextNodeDumper>::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<ReferenceType>(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<DependentSizedExtVectorType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitVectorTypeLoc(VectorTypeLoc TL) {
+  VisitVectorType(cast<VectorType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
+  VisitFunctionType(cast<FunctionType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
+  VisitFunctionProtoType(cast<FunctionProtoType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
+  VisitUnresolvedUsingType(
+      cast<UnresolvedUsingType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitUsingTypeLoc(UsingTypeLoc TL) {
+  VisitUsingType(cast<UsingType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
+  VisitTypedefType(cast<TypedefType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
+  VisitUnaryTransformType(cast<UnaryTransformType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTagTypeLoc(TagTypeLoc TL) {
+  VisitTagType(cast<TagType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
+  VisitTemplateTypeParmType(
+      cast<TemplateTypeParmType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitSubstTemplateTypeParmTypeLoc(
+    SubstTemplateTypeParmTypeLoc TL) {
+  VisitSubstTemplateTypeParmType(
+      cast<SubstTemplateTypeParmType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitAutoTypeLoc(AutoTypeLoc TL) {
+  VisitAutoType(cast<AutoType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitDeducedTemplateSpecializationTypeLoc(
+    DeducedTemplateSpecializationTypeLoc TL) {
+  VisitDeducedTemplateSpecializationType(
+      cast<DeducedTemplateSpecializationType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTemplateSpecializationTypeLoc(
+    TemplateSpecializationTypeLoc TL) {
+  VisitTemplateSpecializationType(
+      cast<TemplateSpecializationType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitInjectedClassNameTypeLoc(
+    InjectedClassNameTypeLoc TL) {
+  VisitInjectedClassNameType(
+      cast<InjectedClassNameType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
+  VisitObjCInterfaceType(cast<ObjCInterfaceType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
+  VisitPackExpansionType(cast<PackExpansionType>(TL.getType().getTypePtr()));
+}
+
 void TextNodeDumper::VisitLabelDecl(const LabelDecl *D) { dumpName(D); }
 
 void TextNodeDumper::VisitTypedefDecl(const TypedefDecl *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/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<TextNodeDumper>,
       public ConstStmtVisitor<TextNodeDumper>,
       public TypeVisitor<TextNodeDumper>,
+      public TypeLocVisitor<TextNodeDumper>,
       public ConstDeclVisitor<TextNodeDumper> {
   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/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<Derived, void,
                                            const comments::FullComment *>,
       public TypeVisitor<Derived>,
+      public TypeLocVisitor<Derived>,
       public ConstAttrVisitor<Derived>,
       public ConstTemplateArgumentVisitor<Derived> {
 
@@ -181,6 +184,13 @@
     });
   }
 
+  void Visit(TypeLoc TL) {
+    getNodeDelegate().AddChild([=] {
+      getNodeDelegate().Visit(TL);
+      TypeLocVisitor<Derived>::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<Type>())
       Visit(T);
+    else if (const auto *TL = N.get<TypeLoc>())
+      Visit(*TL);
     else if (const auto *C = N.get<CXXCtorInitializer>())
       Visit(C);
     else if (const auto *C = N.get<OMPClause>())
@@ -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) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to