[PATCH] D24361: hasDeclaration(qualType(...)) matcher should unwrap ElaboratedType and TemplateSpecializationType

Ɓukasz Anforowicz via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 24 17:05:06 PDT 2016


lukasza updated the summary for this revision.
lukasza updated this revision to Diff 75661.
lukasza added a comment.

Reverted changes in the patch that are not related to the issue of hasDeclaration not matching *anything* in some cases.


https://reviews.llvm.org/D24361

Files:
  include/clang/ASTMatchers/ASTMatchers.h
  include/clang/ASTMatchers/ASTMatchersInternal.h
  unittests/ASTMatchers/ASTMatchersTraversalTest.cpp


Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -2106,5 +2106,36 @@
                                        functionDecl(hasName("bar"))))));
 }
 
+TEST(HasDeclaration, ElaboratedTypeAndTemplateSpecializationType) {
+  std::string input =
+      "namespace Namespace {\n"
+      "template<typename T>\n"
+      "class Template {\n"
+      " public:\n"
+      "  void Method() {}\n"
+      "};\n"
+      "}  // namespace Namespace\n"
+      "template <typename U>\n"
+      "void Function(Namespace::Template<U> param) {\n"
+      "  param.Method();\n"
+      "};\n";
+
+  // Matcher for ::Namespace::Template<T> template decl.
+  auto param_type_decl_matcher = classTemplateDecl(
+      hasName("Template"),
+      hasParent(namespaceDecl(hasName("Namespace"))),
+      has(templateTypeParmDecl(hasName("T"))));
+
+  // hasDeclaration / qualType-flavour.
+  EXPECT_TRUE(matches(input, parmVarDecl(
+      hasName("param"),
+      hasType(qualType(hasDeclaration(decl(param_type_decl_matcher)))))));
+
+  // hasDeclaration / elaboratedType-flavour.
+  EXPECT_TRUE(matches(input, parmVarDecl(
+      hasName("param"),
+      hasType(elaboratedType(hasDeclaration(decl(param_type_decl_matcher)))))));
+}
+
 } // namespace ast_matchers
 } // namespace clang
Index: include/clang/ASTMatchers/ASTMatchersInternal.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -748,6 +748,10 @@
       return matchesDecl(TD, Finder, Builder);
     else if (auto *TT = Node->getAs<TypedefType>())
       return matchesDecl(TT->getDecl(), Finder, Builder);
+    else if (auto *ET = Node->getAs<ElaboratedType>())
+      return matchesSpecialized(ET->getNamedType(), Finder, Builder);
+    else if (auto *TST = Node->getAs<TemplateSpecializationType>())
+      return matchesSpecialized(*TST, Finder, Builder);
     // Do not use getAs<TemplateTypeParmType> instead of the direct dyn_cast.
     // Calling getAs will return the canonical type, but that type does not
     // store a TemplateTypeParmDecl. We *need* the uncanonical type, if it is
@@ -763,6 +767,14 @@
     return false;
   }
 
+  /// \brief Gets the QualType from ElaboratedType
+  /// and returns whether the inner matches on it.
+  bool matchesSpecialized(const ElaboratedType &Node,
+                          ASTMatchFinder *Finder,
+                          BoundNodesTreeBuilder *Builder) const {
+    return matchesSpecialized(Node.getNamedType(), Finder, Builder);
+  }
+
   /// \brief Gets the TemplateDecl from a TemplateSpecializationType
   /// and returns whether the inner matches on it.
   bool matchesSpecialized(const TemplateSpecializationType &Node,
@@ -1007,10 +1019,10 @@
                  TypeLoc, QualType> AdaptativeDefaultToTypes;
 
 /// \brief All types that are supported by HasDeclarationMatcher above.
-typedef TypeList<CallExpr, CXXConstructExpr, DeclRefExpr, EnumType,
-                 InjectedClassNameType, LabelStmt, AddrLabelExpr, MemberExpr,
-                 QualType, RecordType, TagType, TemplateSpecializationType,
-                 TemplateTypeParmType, TypedefType,
+typedef TypeList<CallExpr, CXXConstructExpr, DeclRefExpr, ElaboratedType,
+                 EnumType, InjectedClassNameType, LabelStmt, AddrLabelExpr,
+                 MemberExpr, QualType, RecordType, TagType,
+                 TemplateSpecializationType, TemplateTypeParmType, TypedefType,
                  UnresolvedUsingType> HasDeclarationSupportedTypes;
 
 /// \brief Converts a \c Matcher<T> to a matcher of desired type \c To by
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -2454,7 +2454,8 @@
 /// function. e.g. various subtypes of clang::Type and various expressions.
 ///
 /// Usable as: Matcher<CallExpr>, Matcher<CXXConstructExpr>,
-///   Matcher<DeclRefExpr>, Matcher<EnumType>, Matcher<InjectedClassNameType>,
+///   Matcher<DeclRefExpr>, Matcher<ElaboratedType>, Matcher<EnumType>,
+///   Matcher<InjectedClassNameType>,
 ///   Matcher<LabelStmt>, Matcher<AddrLabelExpr>, Matcher<MemberExpr>,
 ///   Matcher<QualType>, Matcher<RecordType>, Matcher<TagType>,
 ///   Matcher<TemplateSpecializationType>, Matcher<TemplateTypeParmType>,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24361.75661.patch
Type: text/x-patch
Size: 4584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161025/c7fbc91e/attachment-0001.bin>


More information about the cfe-commits mailing list