upsj created this revision.
upsj added a reviewer: aaron.ballman.
Herald added a project: All.
upsj requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

TextNodeDumper currently doesn't support printing the `TypeLoc` hierarchy, 
which makes navigating `clang-query` output involving `TypeLoc` hard to 
understand.
This diff replicates the `Type` hierarchy as well as possible, relying on 
`Type` visitors for printing type-specific info.
The only exception where this doesn't work is most sugared types and any kinds 
of parameters (function or template), for which the `TypeLoc` is not easily 
accessible.

Example `clang-query` output:

  clang-query> set output dump
  clang-query> match typeLoc(loc(type().bind("type")))
  ...
  Binding for "root":
  QualifiedTypeLoc </home/tribizel/llvm-project/build/test.cpp:23:10, col:60> 
'matrix::Dense<typename remove_complex_t<T>::type> *const' const
  `-PointerTypeLoc <col:10, col:60> 'matrix::Dense<typename 
remove_complex_t<T>::type> *'
    `-ElaboratedTypeLoc <col:10, col:58> 'matrix::Dense<typename 
remove_complex_t<T>::type>'
      `-TemplateSpecializationTypeLoc <col:18, col:58> 'Dense<typename 
remove_complex_t<T>::type>' Dense
  
  Binding for "type":
  PointerType 0x261f2b0 'matrix::Dense<typename remove_complex_t<T>::type> *' 
dependent
  `-ElaboratedType 0x261f250 'matrix::Dense<typename 
remove_complex_t<T>::type>' sugar dependent
    `-TemplateSpecializationType 0x261f210 'Dense<typename 
remove_complex_t<T>::type>' dependent Dense
      `-TemplateArgument type 'typename remove_complex_t<T>::type'
        `-DependentNameType 0x261f190 'typename remove_complex_t<T>::type' 
dependent


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135518

Files:
  clang/include/clang/AST/ASTDumperUtils.h
  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, const char *> {
+public:
+#define ABSTRACT_TYPELOC(CLASS, PARENT)
+#define TYPELOC(CLASS, PARENT)                                                 \
+  const char *Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { return #CLASS; }
+#include "clang/AST/TypeLocNodes.def"
+};
+
+} // namespace
+
+const char *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, TypeLocColor);
+    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,118 @@
     OS << " expansions " << *N;
 }
 
+void TextNodeDumper::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
+  OS << " " << TL.getType().split().Quals.getAsString();
+}
+
+void TextNodeDumper::VisitRValueReferenceTypeLoc(ReferenceTypeLoc TL) {
+  VisitReferenceType(dyn_cast<ReferenceType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitArrayTypeLoc(ArrayTypeLoc TL) {
+  VisitArrayType(dyn_cast<ArrayType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
+  VisitConstantArrayType(
+      dyn_cast<ConstantArrayType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
+  VisitVariableArrayType(
+      dyn_cast<VariableArrayType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitDependentSizedArrayTypeLoc(
+    DependentSizedArrayTypeLoc TL) {
+  VisitDependentSizedArrayType(
+      dyn_cast<DependentSizedArrayType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitDependentSizedExtVectorTypeLoc(
+    DependentSizedExtVectorTypeLoc TL) {
+  VisitDependentSizedExtVectorType(
+      dyn_cast<DependentSizedExtVectorType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitVectorTypeLoc(VectorTypeLoc TL) {
+  VisitVectorType(dyn_cast<VectorType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
+  VisitFunctionType(dyn_cast<FunctionType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
+  VisitFunctionProtoType(
+      dyn_cast<FunctionProtoType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
+  VisitUnresolvedUsingType(
+      dyn_cast<UnresolvedUsingType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitUsingTypeLoc(UsingTypeLoc TL) {
+  VisitUsingType(dyn_cast<UsingType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
+  VisitTypedefType(dyn_cast<TypedefType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
+  VisitUnaryTransformType(
+      dyn_cast<UnaryTransformType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTagTypeLoc(TagTypeLoc TL) {
+  VisitTagType(dyn_cast<TagType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
+  VisitTemplateTypeParmType(
+      dyn_cast<TemplateTypeParmType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitSubstTemplateTypeParmTypeLoc(
+    SubstTemplateTypeParmTypeLoc TL) {
+  VisitSubstTemplateTypeParmType(
+      dyn_cast<SubstTemplateTypeParmType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitAutoTypeLoc(AutoTypeLoc TL) {
+  VisitAutoType(dyn_cast<AutoType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitDeducedTemplateSpecializationTypeLoc(
+    DeducedTemplateSpecializationTypeLoc TL) {
+  VisitDeducedTemplateSpecializationType(
+      dyn_cast<DeducedTemplateSpecializationType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitTemplateSpecializationTypeLoc(
+    TemplateSpecializationTypeLoc TL) {
+  VisitTemplateSpecializationType(
+      dyn_cast<TemplateSpecializationType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitInjectedClassNameTypeLoc(
+    InjectedClassNameTypeLoc TL) {
+  VisitInjectedClassNameType(
+      dyn_cast<InjectedClassNameType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
+  VisitObjCInterfaceType(
+      dyn_cast<ObjCInterfaceType>(TL.getType().getTypePtr()));
+}
+
+void TextNodeDumper::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
+  VisitPackExpansionType(
+      dyn_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();
   }
 
+  const char *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) {
Index: clang/include/clang/AST/ASTDumperUtils.h
===================================================================
--- clang/include/clang/AST/ASTDumperUtils.h
+++ clang/include/clang/AST/ASTDumperUtils.h
@@ -52,6 +52,8 @@
 
 // Type names (int, float, etc, plus user defined types)
 static const TerminalColor TypeColor = {llvm::raw_ostream::GREEN, false};
+// Type location names (int, float, etc, plus user defined types)
+static const TerminalColor TypeLocColor = {llvm::raw_ostream::GREEN, false};
 
 // Pointer address
 static const TerminalColor AddressColor = {llvm::raw_ostream::YELLOW, false};
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to