etienneb created this revision. etienneb added reviewers: alexfh, sbenza, klimek. etienneb added a subscriber: cfe-commits. Herald added a subscriber: klimek.
This AST matcher will match a given CastExpr kind. It's an narrowing matcher on CastExpr. http://reviews.llvm.org/D19871 Files: include/clang/ASTMatchers/ASTMatchers.h lib/ASTMatchers/Dynamic/Registry.cpp unittests/ASTMatchers/ASTMatchersTest.cpp Index: unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3475,6 +3475,13 @@ EXPECT_TRUE(notMatches("int i = 0;", castExpr())); } +TEST(CastExpression, HasCastKind) { + EXPECT_TRUE(matches("char *p = 0;", + castExpr(hasCastKind(CK_NullToPointer)))); + EXPECT_TRUE(notMatches("char *p = 0;", + castExpr(hasCastKind(CK_DerivedToBase)))); +} + TEST(ReinterpretCast, MatchesSimpleCase) { EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", cxxReinterpretCastExpr())); Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -75,6 +75,7 @@ // TODO: Here is the list of the missing matchers, grouped by reason. // // Need Variant/Parser fixes: + // hasCastKind // ofKind // // Polymorphic + argument overload: Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -3548,6 +3548,17 @@ InnerMatcher.matches(*SubExpression, Finder, Builder)); } +/// \brief Matches casts that has a given cast kind. +/// +/// Example: matches the implicit cast around \c 0 +/// (matcher = castExpr(hasCastKind(CK_NullToPointer))) +/// \code +/// int *p = 0; +/// \endcode +AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) { + return Node.getCastKind() == Kind; +} + /// \brief Matches casts whose destination type matches a given matcher. /// /// (Note: Clang's AST refers to other conversions as "casts" too, and calls
Index: unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3475,6 +3475,13 @@ EXPECT_TRUE(notMatches("int i = 0;", castExpr())); } +TEST(CastExpression, HasCastKind) { + EXPECT_TRUE(matches("char *p = 0;", + castExpr(hasCastKind(CK_NullToPointer)))); + EXPECT_TRUE(notMatches("char *p = 0;", + castExpr(hasCastKind(CK_DerivedToBase)))); +} + TEST(ReinterpretCast, MatchesSimpleCase) { EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", cxxReinterpretCastExpr())); Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -75,6 +75,7 @@ // TODO: Here is the list of the missing matchers, grouped by reason. // // Need Variant/Parser fixes: + // hasCastKind // ofKind // // Polymorphic + argument overload: Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -3548,6 +3548,17 @@ InnerMatcher.matches(*SubExpression, Finder, Builder)); } +/// \brief Matches casts that has a given cast kind. +/// +/// Example: matches the implicit cast around \c 0 +/// (matcher = castExpr(hasCastKind(CK_NullToPointer))) +/// \code +/// int *p = 0; +/// \endcode +AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) { + return Node.getCastKind() == Kind; +} + /// \brief Matches casts whose destination type matches a given matcher. /// /// (Note: Clang's AST refers to other conversions as "casts" too, and calls
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits