Skip to content

Commit

Permalink
[clang][AST] TextNodeDumper learned to dump qualifiers (NestedNameSpe…
Browse files Browse the repository at this point in the history
…cifier)

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D157681
  • Loading branch information
strimo378 committed Aug 15, 2023
1 parent 6c36a70 commit cad3130
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/AST/TextNodeDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class TextNodeDumper
void dumpAccessSpecifier(AccessSpecifier AS);
void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C);
void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK);
void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS);

void dumpDeclRef(const Decl *D, StringRef Label = {});

Expand Down Expand Up @@ -255,6 +256,7 @@ class TextNodeDumper
void VisitCastExpr(const CastExpr *Node);
void VisitImplicitCastExpr(const ImplicitCastExpr *Node);
void VisitDeclRefExpr(const DeclRefExpr *Node);
void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *Node);
void VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *Node);
void VisitPredefinedExpr(const PredefinedExpr *Node);
void VisitCharacterLiteral(const CharacterLiteral *Node);
Expand Down
52 changes: 52 additions & 0 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,46 @@ void clang::TextNodeDumper::dumpTemplateSpecializationKind(
}
}

void clang::TextNodeDumper::dumpNestedNameSpecifier(const NestedNameSpecifier *NNS) {
if (!NNS)
return;

AddChild([=] {
OS << "NestedNameSpecifier";

switch (NNS->getKind()) {
case NestedNameSpecifier::Identifier:
OS << " Identifier";
OS << " '" << NNS->getAsIdentifier()->getName() << "'";
break;
case NestedNameSpecifier::Namespace:
OS << " Namespace";
dumpBareDeclRef(NNS->getAsNamespace());
break;
case NestedNameSpecifier::NamespaceAlias:
OS << " NamespaceAlias";
dumpBareDeclRef(NNS->getAsNamespaceAlias());
break;
case NestedNameSpecifier::TypeSpec:
OS << " TypeSpec";
dumpType(QualType(NNS->getAsType(), 0));
break;
case NestedNameSpecifier::TypeSpecWithTemplate:
OS << " TypeSpecWithTemplate";
dumpType(QualType(NNS->getAsType(), 0));
break;
case NestedNameSpecifier::Global:
OS << " Global";
break;
case NestedNameSpecifier::Super:
OS << " Super";
break;
}

dumpNestedNameSpecifier(NNS->getPrefix());
});
}

void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) {
if (!D)
return;
Expand Down Expand Up @@ -1065,6 +1105,7 @@ void TextNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *Node) {
void TextNodeDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
OS << " ";
dumpBareDeclRef(Node->getDecl());
dumpNestedNameSpecifier(Node->getQualifier());
if (Node->getDecl() != Node->getFoundDecl()) {
OS << " (";
dumpBareDeclRef(Node->getFoundDecl());
Expand All @@ -1080,6 +1121,12 @@ void TextNodeDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
OS << " immediate-escalating";
}

void clang::TextNodeDumper::VisitDependentScopeDeclRefExpr(
const DependentScopeDeclRefExpr *Node) {

dumpNestedNameSpecifier(Node->getQualifier());
}

void TextNodeDumper::VisitUnresolvedLookupExpr(
const UnresolvedLookupExpr *Node) {
OS << " (";
Expand Down Expand Up @@ -1174,6 +1221,7 @@ void TextNodeDumper::VisitUnaryExprOrTypeTraitExpr(
void TextNodeDumper::VisitMemberExpr(const MemberExpr *Node) {
OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
dumpPointer(Node->getMemberDecl());
dumpNestedNameSpecifier(Node->getQualifier());
switch (Node->isNonOdrUse()) {
case NOUR_None: break;
case NOUR_Unevaluated: OS << " non_odr_use_unevaluated"; break;
Expand Down Expand Up @@ -1867,6 +1915,7 @@ void TextNodeDumper::VisitFieldDecl(const FieldDecl *D) {
}

void TextNodeDumper::VisitVarDecl(const VarDecl *D) {
dumpNestedNameSpecifier(D->getQualifier());
dumpName(D);
dumpType(D->getType());
dumpTemplateSpecializationKind(D->getTemplateSpecializationKind());
Expand Down Expand Up @@ -2061,6 +2110,8 @@ void TextNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
dumpTemplateSpecializationKind(CTSD->getSpecializationKind());

dumpNestedNameSpecifier(D->getQualifier());

if (!D->isCompleteDefinition())
return;

Expand Down Expand Up @@ -2260,6 +2311,7 @@ void TextNodeDumper::VisitUsingDecl(const UsingDecl *D) {
if (D->getQualifier())
D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
OS << D->getDeclName();
dumpNestedNameSpecifier(D->getQualifier());
}

void TextNodeDumper::VisitUsingEnumDecl(const UsingEnumDecl *D) {
Expand Down
16 changes: 10 additions & 6 deletions clang/test/AST/ast-dump-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,24 +614,27 @@ namespace testCanonicalTemplate {
// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{.+}} <col:16, col:25> col:25 referenced typename depth 0 index 0 T
// CHECK-NEXT: |-VarDecl 0x{{.+}} <col:28, col:43> col:43 TestVarTemplate 'const T' static
// CHECK-NEXT: |-VarTemplateSpecializationDecl 0x{{.+}} parent 0x{{.+}} prev 0x{{.+}} <line:[[@LINE-11]]:3, col:34> col:14 referenced TestVarTemplate 'const int':'const int' implicit_instantiation cinit
// CHECK-NEXT: | |-NestedNameSpecifier TypeSpec 'testCanonicalTemplate::S'
// CHECK-NEXT: | |-TemplateArgument type 'int'
// CHECK-NEXT: | | `-BuiltinType 0x{{.+}} 'int'
// CHECK-NEXT: | `-InitListExpr 0x{{.+}} <col:32, col:34> 'int':'int'
// CHECK-NEXT: `-VarTemplateSpecializationDecl 0x{{.+}} <line:[[@LINE-18]]:28, col:43> col:43 referenced TestVarTemplate 'const int':'const int' implicit_instantiation static
// CHECK-NEXT: `-VarTemplateSpecializationDecl 0x{{.+}} <line:[[@LINE-19]]:28, col:43> col:43 referenced TestVarTemplate 'const int':'const int' implicit_instantiation static
// CHECK-NEXT: `-TemplateArgument type 'int'

// CHECK: VarTemplateSpecializationDecl 0x{{.+}} <{{.+}}:[[@LINE-21]]:28, col:43> col:43 referenced TestVarTemplate 'const int':'const int' implicit_instantiation static
// CHECK: VarTemplateSpecializationDecl 0x{{.+}} <{{.+}}:[[@LINE-22]]:28, col:43> col:43 referenced TestVarTemplate 'const int':'const int' implicit_instantiation static
// CHECK-NEXT:`-TemplateArgument type 'int'
// CHECK-NEXT: `-BuiltinType 0x{{.+}} 'int'

// CHECK: VarTemplateDecl 0x{{.+}} parent 0x{{.+}} prev 0x{{.+}} <{{.+}}:[[@LINE-23]]:3, line:[[@LINE-22]]:34> col:14 TestVarTemplate
// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{.+}} <line:[[@LINE-24]]:12, col:21> col:21 referenced typename depth 0 index 0 T
// CHECK-NEXT: |-VarDecl 0x{{.+}} parent 0x{{.+}} prev 0x{{.+}} <line:[[@LINE-24]]:3, col:34> col:14 TestVarTemplate 'const T' cinit
// CHECK: VarTemplateDecl 0x{{.+}} parent 0x{{.+}} prev 0x{{.+}} <{{.+}}:[[@LINE-24]]:3, line:[[@LINE-23]]:34> col:14 TestVarTemplate
// CHECK-NEXT: |-TemplateTypeParmDecl 0x{{.+}} <line:[[@LINE-25]]:12, col:21> col:21 referenced typename depth 0 index 0 T
// CHECK-NEXT: |-VarDecl 0x{{.+}} parent 0x{{.+}} prev 0x{{.+}} <line:[[@LINE-25]]:3, col:34> col:14 TestVarTemplate 'const T' cinit
// CHECK-NEXT: | |-NestedNameSpecifier TypeSpec 'testCanonicalTemplate::S'
// CHECK-NEXT: | `-InitListExpr 0x{{.+}} <col:32, col:34> 'void'
// CHECK-NEXT: |-VarTemplateSpecialization 0x{{.+}} 'TestVarTemplate' 'const int':'const int'
// CHECK-NEXT: `-VarTemplateSpecialization 0x{{.+}} 'TestVarTemplate' 'const int':'const int'

// CHECK: VarTemplateSpecializationDecl 0x{{.+}} parent 0x{{.+}} prev 0x{{.+}} <{{.+}}:[[@LINE-29]]:3, col:34> col:14 referenced TestVarTemplate 'const int':'const int' implicit_instantiation cinit
// CHECK: VarTemplateSpecializationDecl 0x{{.+}} parent 0x{{.+}} prev 0x{{.+}} <{{.+}}:[[@LINE-31]]:3, col:34> col:14 referenced TestVarTemplate 'const int':'const int' implicit_instantiation cinit
// CHECK-NEXT: |-NestedNameSpecifier TypeSpec 'testCanonicalTemplate::S'
// CHECK-NEXT: |-TemplateArgument type 'int'
// CHECK-NEXT: | `-BuiltinType 0x{{.+}} 'int'
// CHECK-NEXT: `-InitListExpr 0x{{.+}} <col:32, col:34> 'int':'int'
Expand Down Expand Up @@ -737,6 +740,7 @@ namespace TestUsingDecl {
}
// CHECK: NamespaceDecl{{.*}} TestUsingDecl
// CHECK-NEXT: UsingDecl{{.*}} testUsingDecl::i
// CHECK-NEXT: | `-NestedNameSpecifier NamespaceNamespace 0x{{.*}} 'testUsingDecl
// CHECK-NEXT: UsingShadowDecl{{.*}} Var{{.*}} 'i' 'int'

namespace testUnresolvedUsing {
Expand Down
3 changes: 3 additions & 0 deletions clang/test/AST/ast-dump-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,16 @@ void PostfixExpressions(S a, S *p, U<int> *r) {
p->::S::~S();
// CHECK: CXXMemberCallExpr 0x{{[^ ]*}} <line:[[@LINE-1]]:3, col:14> 'void'
// CHECK-NEXT: MemberExpr 0x{{[^ ]*}} <col:3, col:12> '<bound member function type>' ->~S 0x{{[^ ]*}}
// CHECK-NEXT: NestedNameSpecifier TypeSpec 'S'
// CHECK-NEXT: NestedNameSpecifier Global
// CHECK-NEXT: ImplicitCastExpr
// CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:3> 'S *' lvalue ParmVar 0x{{[^ ]*}} 'p' 'S *'

// FIXME: there is no mention that this used the template keyword.
r->template U<int>::~U();
// CHECK: CXXMemberCallExpr 0x{{[^ ]*}} <line:[[@LINE-1]]:3, col:26> 'void'
// CHECK-NEXT: MemberExpr 0x{{[^ ]*}} <col:3, col:24> '<bound member function type>' ->~U 0x{{[^ ]*}}
// CHECK-NEXT: NestedNameSpecifier TypeSpecWithTemplate 'U<int>':'U<int>'
// CHECK-NEXT: ImplicitCastExpr
// CHECK-NEXT: DeclRefExpr 0x{{[^ ]*}} <col:3> 'U<int> *' lvalue ParmVar 0x{{[^ ]*}} 'r' 'U<int> *'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,20 @@ int main() {
// CHECK-NEXT: | | |-CallExpr [[ADDR_47:0x[a-z0-9]*]] <col:10, col:17> 'int'
// CHECK-NEXT: | | | `-ImplicitCastExpr [[ADDR_48:0x[a-z0-9]*]] <col:10, col:13> 'int (*)({{.*}})' <FunctionToPointerDecay>
// CHECK-NEXT: | | | `-DeclRefExpr [[ADDR_49:0x[a-z0-9]*]] <col:10, col:13> 'int ({{.*}})' {{.*}}Function [[ADDR_1]] 'foo' 'int ({{.*}})'
// CHECK-NEXT: | | | `-NestedNameSpecifier NamespaceNamespace [[ADDR_0]] 'A'
// CHECK-NEXT: | | `-PseudoObjectExpr [[ADDR_50:0x[a-z0-9]*]] <col:21, col:28> 'int'
// CHECK-NEXT: | | |-CallExpr [[ADDR_51:0x[a-z0-9]*]] <col:21, col:28> 'int'
// CHECK-NEXT: | | | `-ImplicitCastExpr [[ADDR_52:0x[a-z0-9]*]] <col:21, col:24> 'int (*)({{.*}})' <FunctionToPointerDecay>
// CHECK-NEXT: | | | `-DeclRefExpr [[ADDR_53:0x[a-z0-9]*]] <col:21, col:24> 'int ({{.*}})' {{.*}}Function [[ADDR_6]] 'bar' 'int ({{.*}})'
// CHECK-NEXT: | | | `-NestedNameSpecifier NamespaceNamespace [[ADDR_31]] 'B'
// CHECK-NEXT: | | `-CallExpr [[ADDR_54:0x[a-z0-9]*]] <line:34:1, line:50:28> 'int'
// CHECK-NEXT: | | `-ImplicitCastExpr [[ADDR_55:0x[a-z0-9]*]] <line:34:1> 'int (*)({{.*}})' <FunctionToPointerDecay>
// CHECK-NEXT: | | `-DeclRefExpr [[ADDR_11]] <col:1> 'int ({{.*}})' Function [[ADDR_12]] 'bar[implementation={vendor(llvm)}]' 'int ({{.*}})'
// CHECK-NEXT: | `-PseudoObjectExpr [[ADDR_56:0x[a-z0-9]*]] <line:50:32, col:39> 'int'
// CHECK-NEXT: | |-CallExpr [[ADDR_57:0x[a-z0-9]*]] <col:32, col:39> 'int'
// CHECK-NEXT: | | `-ImplicitCastExpr [[ADDR_58:0x[a-z0-9]*]] <col:32, col:35> 'int (*)({{.*}})' <FunctionToPointerDecay>
// CHECK-NEXT: | | `-DeclRefExpr [[ADDR_59:0x[a-z0-9]*]] <col:32, col:35> 'int ({{.*}})' {{.*}}Function [[ADDR_14]] 'baz' 'int ({{.*}})'
// CHECK-NEXT: | | `-NestedNameSpecifier NamespaceNamespace [[ADDR_13]] 'C'
// CHECK-NEXT: | `-CallExpr [[ADDR_60:0x[a-z0-9]*]] <line:42:1, line:50:39> 'int'
// CHECK-NEXT: | `-ImplicitCastExpr [[ADDR_61:0x[a-z0-9]*]] <line:42:1> 'int (*)({{.*}})' <FunctionToPointerDecay>
// CHECK-NEXT: | `-DeclRefExpr [[ADDR_19]] <col:1> 'int ({{.*}})' Function [[ADDR_20]] 'baz[implementation={vendor(llvm)}]' 'int ({{.*}})'
Expand Down
1 change: 1 addition & 0 deletions clang/test/AST/ast-dump-using.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct S;
namespace b {
using a::S;
// CHECK: UsingDecl {{.*}} a::S
// CHECK-NEXT: | `-NestedNameSpecifier NamespaceNamespace {{.*}} 'a'
// CHECK-NEXT: UsingShadowDecl {{.*}} implicit CXXRecord {{.*}} 'S'
// CHECK-NEXT: `-RecordType {{.*}} 'a::S'
typedef S f; // to dump the introduced type
Expand Down

0 comments on commit cad3130

Please sign in to comment.