Author: angelgarcia Date: Fri Oct 30 04:35:51 2015 New Revision: 251693 URL: http://llvm.org/viewvc/llvm-project?rev=251693&view=rev Log: Add "equalsNode" for types and "isCopyAssignmentOperator" matchers.
Summary: This matchers are going to be used in modernize-use-default, but are generic enough to be placed in ASTMatchers.h. Reviewers: klimek Subscribers: alexfh, cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D14152 Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h?rev=251693&r1=251692&r2=251693&view=diff ============================================================================== --- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h (original) +++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h Fri Oct 30 04:35:51 2015 @@ -3385,6 +3385,23 @@ AST_MATCHER(CXXMethodDecl, isConst) { return Node.isConst(); } +/// \brief Matches if the given method declaration declares a copy assignment +/// operator. +/// +/// Given +/// \code +/// struct A { +/// A &operator=(const A &); +/// A &operator=(A &&); +/// }; +/// \endcode +/// +/// cxxMethodDecl(isCopyAssignmentOperator()) matches the first method but not +/// the second one. +AST_MATCHER(CXXMethodDecl, isCopyAssignmentOperator) { + return Node.isCopyAssignmentOperator(); +} + /// \brief Matches if the given method declaration overrides another method. /// /// Given @@ -4307,10 +4324,15 @@ AST_MATCHER_P_OVERLOAD(Decl, equalsNode, /// \brief Matches if a node equals another node. /// /// \c Stmt has pointer identity in the AST. -/// AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, const Stmt*, Other, 1) { return &Node == Other; } +/// \brief Matches if a node equals another node. +/// +/// \c Type has pointer identity in the AST. +AST_MATCHER_P_OVERLOAD(Type, equalsNode, const Type*, Other, 2) { + return &Node == Other; +} /// @} Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp?rev=251693&r1=251692&r2=251693&view=diff ============================================================================== --- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp (original) +++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp Fri Oct 30 04:35:51 2015 @@ -1890,6 +1890,21 @@ TEST(Matcher, MatchesPureMethod) { EXPECT_TRUE(notMatches("class X { int f(); };", cxxMethodDecl(isPure()))); } +TEST(Matcher, MatchesCopyAssignmentOperator) { + EXPECT_TRUE(matches("class X { X &operator=(X); };", + cxxMethodDecl(isCopyAssignmentOperator()))); + EXPECT_TRUE(matches("class X { X &operator=(X &); };", + cxxMethodDecl(isCopyAssignmentOperator()))); + EXPECT_TRUE(matches("class X { X &operator=(const X &); };", + cxxMethodDecl(isCopyAssignmentOperator()))); + EXPECT_TRUE(matches("class X { X &operator=(volatile X &); };", + cxxMethodDecl(isCopyAssignmentOperator()))); + EXPECT_TRUE(matches("class X { X &operator=(const volatile X &); };", + cxxMethodDecl(isCopyAssignmentOperator()))); + EXPECT_TRUE(notMatches("class X { X &operator=(X &&); };", + cxxMethodDecl(isCopyAssignmentOperator()))); +} + TEST(Matcher, MatchesConstMethod) { EXPECT_TRUE( matches("struct A { void foo() const; };", cxxMethodDecl(isConst()))); @@ -4671,6 +4686,16 @@ public: decl(has(decl(equalsNode(TypedNode)))).bind(""))), *Node, Context)) != nullptr; } + bool verify(const BoundNodes &Nodes, ASTContext &Context, const Type *Node) { + // Use the original typed pointer to verify we can pass pointers to subtypes + // to equalsNode. + const T *TypedNode = cast<T>(Node); + const auto *Dec = Nodes.getNodeAs<FieldDecl>("decl"); + return selectFirst<T>( + "", match(fieldDecl(hasParent(decl(has(fieldDecl( + hasType(type(equalsNode(TypedNode)).bind(""))))))), + *Dec, Context)) != nullptr; + } }; TEST(IsEqualTo, MatchesNodesByIdentity) { @@ -4680,6 +4705,10 @@ TEST(IsEqualTo, MatchesNodesByIdentity) EXPECT_TRUE(matchAndVerifyResultTrue( "void f() { if (true) if(true) {} }", ifStmt().bind(""), new VerifyAncestorHasChildIsEqual<IfStmt>())); + EXPECT_TRUE(matchAndVerifyResultTrue( + "class X { class Y {} y; };", + fieldDecl(hasName("y"), hasType(type().bind(""))).bind("decl"), + new VerifyAncestorHasChildIsEqual<Type>())); } TEST(MatchFinder, CheckProfiling) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits