Fix dumping of explicit template specializations
authorStephen Kelly <steveire@gmail.com>
Sat, 7 Nov 2020 22:25:00 +0000 (22:25 +0000)
committerStephen Kelly <steveire@gmail.com>
Sat, 7 Nov 2020 22:34:16 +0000 (22:34 +0000)
This was missing from commit 7efe07a1 (Traverse-ignore explicit template
instantiations, 2020-11-06).

clang/include/clang/AST/ASTNodeTraverser.h
clang/unittests/AST/ASTTraverserTest.cpp

index c3c06bf..78b2ec5 100644 (file)
@@ -101,8 +101,14 @@ public:
 
       // Decls within functions are visited by the body.
       if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D)) {
-        if (isa<ClassTemplateSpecializationDecl>(*D) && Traversal != TK_AsIs)
-          return;
+        if (Traversal != TK_AsIs) {
+          if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
+            auto SK = CTSD->getSpecializationKind();
+            if (SK == TSK_ExplicitInstantiationDeclaration ||
+                SK == TSK_ExplicitInstantiationDefinition)
+              return;
+          }
+        }
         if (const auto *DC = dyn_cast<DeclContext>(D))
           dumpDeclContext(DC);
       }
index 727a1ff..5e167e4 100644 (file)
@@ -1187,6 +1187,48 @@ ClassTemplateDecl 'TemplStruct'
   }
   {
     auto BN = ast_matchers::match(
+        classTemplateSpecializationDecl(
+            hasTemplateArgument(
+                0, templateArgument(refersToType(asString("_Bool")))))
+            .bind("templSpec"),
+        AST->getASTContext());
+    EXPECT_EQ(BN.size(), 1u);
+
+    EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("templSpec")),
+              R"cpp(
+ClassTemplateSpecializationDecl 'TemplStruct'
+|-TemplateArgument type _Bool
+| `-BuiltinType
+|-CXXRecordDecl 'TemplStruct'
+|-CXXConstructorDecl 'TemplStruct'
+| `-CompoundStmt
+|-CXXDestructorDecl '~TemplStruct'
+| `-CompoundStmt
+|-CXXMethodDecl 'foo'
+| `-CompoundStmt
+|-AccessSpecDecl
+`-FieldDecl 'm_t'
+)cpp");
+
+    EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
+                            BN[0].getNodeAs<Decl>("templSpec")),
+              R"cpp(
+ClassTemplateSpecializationDecl 'TemplStruct'
+|-TemplateArgument type _Bool
+| `-BuiltinType
+|-CXXRecordDecl 'TemplStruct'
+|-CXXConstructorDecl 'TemplStruct'
+| `-CompoundStmt
+|-CXXDestructorDecl '~TemplStruct'
+| `-CompoundStmt
+|-CXXMethodDecl 'foo'
+| `-CompoundStmt
+|-AccessSpecDecl
+`-FieldDecl 'm_t'
+)cpp");
+  }
+  {
+    auto BN = ast_matchers::match(
         functionTemplateDecl(hasName("timesTwo")).bind("fn"),
         AST->getASTContext());
     EXPECT_EQ(BN.size(), 1u);