From c8546f850ba0829d49ea8b49dc4d47c2d2bddb48 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 20 Feb 2014 19:18:03 +0000 Subject: [PATCH] Add TemplateSpecializationType polymorphism for hasTemplateArgument and hasAnyTemplateArgument, and (out of necessity) an isExpr matcher. Also updates the TemplateArgument doxygen to reflect reality for non-canonical template arguments. Differential Revision: http://llvm-reviews.chandlerc.com/D2810 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201804 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/TemplateBase.h | 16 ++++--- include/clang/ASTMatchers/ASTMatchers.h | 53 +++++++++++++++++++---- lib/ASTMatchers/Dynamic/Registry.cpp | 1 + unittests/ASTMatchers/ASTMatchersTest.cpp | 24 ++++++++++ 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/include/clang/AST/TemplateBase.h b/include/clang/AST/TemplateBase.h index 6c40eb1168..7810c306dd 100644 --- a/include/clang/AST/TemplateBase.h +++ b/include/clang/AST/TemplateBase.h @@ -34,8 +34,7 @@ struct PrintingPolicy; class TypeSourceInfo; class ValueDecl; -/// \brief Represents a template argument within a class template -/// specialization. +/// \brief Represents a template argument. class TemplateArgument { public: /// \brief The kind of template argument we're storing. @@ -52,16 +51,19 @@ public: /// was provided for a non-type template parameter. NullPtr, /// The template argument is an integral value stored in an llvm::APSInt - /// that was provided for an integral non-type template parameter. + /// that was provided for an integral non-type template parameter. Integral, - /// The template argument is a template name that was provided for a + /// The template argument is a template name that was provided for a /// template template parameter. Template, - /// The template argument is a pack expansion of a template name that was + /// The template argument is a pack expansion of a template name that was /// provided for a template template parameter. TemplateExpansion, - /// The template argument is a value- or type-dependent expression or a - /// non-dependent __uuidof expression stored in an Expr*. + /// The template argument is an expression, and we've not resolved it to one + /// of the other forms yet, either because it's dependent or because we're + /// representing a non-canonical template argument (for instance, in a + /// TemplateSpecializationType). Also used to represent a non-dependent + /// __uuidof expression (a Microsoft extension). Expression, /// The template argument is actually a parameter pack. Arguments are stored /// in the Args struct. diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 5355a9a857..e010133ca2 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -311,6 +311,18 @@ AST_MATCHER(Decl, isPrivate) { return Node.getAccess() == AS_private; } +// FIXME: unify ClassTemplateSpecializationDecl and TemplateSpecializationType's +// APIs for accessing the template argument list. +inline llvm::ArrayRef +getTemplateSpecializationArgs(const ClassTemplateSpecializationDecl &D) { + return D.getTemplateArgs().asArray(); +} + +inline llvm::ArrayRef +getTemplateSpecializationArgs(const TemplateSpecializationType &T) { + return llvm::ArrayRef(T.getArgs(), T.getNumArgs()); +} + /// \brief Matches classTemplateSpecializations that have at least one /// TemplateArgument matching the given InnerMatcher. /// @@ -323,9 +335,12 @@ AST_MATCHER(Decl, isPrivate) { /// classTemplateSpecializationDecl(hasAnyTemplateArgument( /// refersToType(asString("int")))) /// matches the specialization \c A -AST_MATCHER_P(ClassTemplateSpecializationDecl, hasAnyTemplateArgument, - internal::Matcher, InnerMatcher) { - llvm::ArrayRef List = Node.getTemplateArgs().asArray(); +AST_POLYMORPHIC_MATCHER_P( + hasAnyTemplateArgument, + AST_POLYMORPHIC_SUPPORTED_TYPES_2(ClassTemplateSpecializationDecl, + TemplateSpecializationType), + internal::Matcher, InnerMatcher) { + llvm::ArrayRef List = getTemplateSpecializationArgs(Node); return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder, Builder); } @@ -419,12 +434,15 @@ AST_MATCHER_P(Expr, ignoringParenImpCasts, /// classTemplateSpecializationDecl(hasTemplateArgument( /// 1, refersToType(asString("int")))) /// matches the specialization \c A -AST_MATCHER_P2(ClassTemplateSpecializationDecl, hasTemplateArgument, - unsigned, N, internal::Matcher, InnerMatcher) { - const TemplateArgumentList &List = Node.getTemplateArgs(); +AST_POLYMORPHIC_MATCHER_P2( + hasTemplateArgument, + AST_POLYMORPHIC_SUPPORTED_TYPES_2(ClassTemplateSpecializationDecl, + TemplateSpecializationType), + unsigned, N, internal::Matcher, InnerMatcher) { + llvm::ArrayRef List = getTemplateSpecializationArgs(Node); if (List.size() <= N) return false; - return InnerMatcher.matches(List.get(N), Finder, Builder); + return InnerMatcher.matches(List[N], Finder, Builder); } /// \brief Matches a TemplateArgument that refers to a certain type. @@ -445,7 +463,8 @@ AST_MATCHER_P(TemplateArgument, refersToType, return InnerMatcher.matches(Node.getAsType(), Finder, Builder); } -/// \brief Matches a TemplateArgument that refers to a certain declaration. +/// \brief Matches a canonical TemplateArgument that refers to a certain +/// declaration. /// /// Given /// \code @@ -464,6 +483,24 @@ AST_MATCHER_P(TemplateArgument, refersToDeclaration, return false; } +/// \brief Matches a sugar TemplateArgument that refers to a certain expression. +/// +/// Given +/// \code +/// template struct A {}; +/// struct B { B* next; }; +/// A<&B::next> a; +/// \endcode +/// templateSpecializationType(hasAnyTemplateArgument( +/// isExpr(hasDescendant(declRefExpr(to(fieldDecl(hasName("next")))))))) +/// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching +/// \c B::next +AST_MATCHER_P(TemplateArgument, isExpr, internal::Matcher, InnerMatcher) { + if (Node.getKind() == TemplateArgument::Expression) + return InnerMatcher.matches(*Node.getAsExpr(), Finder, Builder); + return false; +} + /// \brief Matches C++ constructor declarations. /// /// Example matches Foo::Foo() and Foo::Foo(int) diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp index 11230ad049..d4b68bf0d0 100644 --- a/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/lib/ASTMatchers/Dynamic/Registry.cpp @@ -228,6 +228,7 @@ RegistryMaps::RegistryMaps() { REGISTER_MATCHER(isConstQualified); REGISTER_MATCHER(isDefinition); REGISTER_MATCHER(isExplicitTemplateSpecialization); + REGISTER_MATCHER(isExpr); REGISTER_MATCHER(isExternC); REGISTER_MATCHER(isImplicit); REGISTER_MATCHER(isInteger); diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index d84e6a6ef9..cf77f2f4dd 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -1530,6 +1530,19 @@ TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) { "A a;", classTemplateSpecializationDecl(hasAnyTemplateArgument( refersToDeclaration(decl()))))); + + EXPECT_TRUE(matches( + "struct B { int next; };" + "template struct A {};" + "A<&B::next> a;", + templateSpecializationType(hasAnyTemplateArgument(isExpr( + hasDescendant(declRefExpr(to(fieldDecl(hasName("next")))))))))); + + EXPECT_TRUE(notMatches( + "template struct A {};" + "A a;", + templateSpecializationType(hasAnyTemplateArgument( + refersToDeclaration(decl()))))); } TEST(Matcher, MatchesSpecificArgument) { @@ -1543,6 +1556,17 @@ TEST(Matcher, MatchesSpecificArgument) { "A a;", classTemplateSpecializationDecl(hasTemplateArgument( 1, refersToType(asString("int")))))); + + EXPECT_TRUE(matches( + "template class A {};" + "A a;", + templateSpecializationType(hasTemplateArgument( + 1, refersToType(asString("int")))))); + EXPECT_TRUE(notMatches( + "template class A {};" + "A a;", + templateSpecializationType(hasTemplateArgument( + 1, refersToType(asString("int")))))); } TEST(Matcher, MatchesAccessSpecDecls) { -- 2.40.0