Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -3813,7 +3813,7 @@ /// true /// \endcode /// -/// Usable as: Matcher, Matcher, +/// Usable as: Matcher, Matcher, /// Matcher, Matcher template internal::PolymorphicMatcherWithParam1 @@ -3823,6 +3823,13 @@ ValueT>(Value); } +/// \brief Instantiations for the \c equals matcher. +/// TODO add support for FloatingLiteral and CharacterLiteral +/// @{ +AST_CONCRETE_EQUALS_MATCHER(CXXBoolLiteralExpr, bool, 0); +AST_CONCRETE_EQUALS_MATCHER(IntegerLiteral, unsigned, 1); +/// @} + /// \brief Matches the operator Name of operator expressions (binary or /// unary). /// Index: include/clang/ASTMatchers/ASTMatchersMacros.h =================================================================== --- include/clang/ASTMatchers/ASTMatchersMacros.h +++ include/clang/ASTMatchers/ASTMatchersMacros.h @@ -359,6 +359,17 @@ ::clang::ast_matchers::internal::BoundNodesTreeBuilder *Builder) \ const +/// \brief Defines functions and a type for the \c equals matcher, suitable for +/// comparing a \c ReturnType node against the a \c ParamType value. +#define AST_CONCRETE_EQUALS_MATCHER(ReturnType, ParamType, OverloadId) \ + inline ::clang::ast_matchers::internal::Matcher equals( \ + ParamType const &Param) { \ + return ::clang::ast_matchers::internal::makeMatcher( \ + new internal::ValueEqualsMatcher(Param)); \ + } \ + typedef ::clang::ast_matchers::internal::Matcher( \ + &equals_Type##OverloadId)(ParamType const &Param) + /// \brief Creates a variadic matcher for both a specific \c Type as well as /// the corresponding \c TypeLoc. #define AST_TYPE_MATCHER(NodeType, MatcherName) \ Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -96,6 +96,9 @@ REGISTER_OVERLOADED_2(references); REGISTER_OVERLOADED_2(thisPointerType); + // TODO add support for floats and char literals + REGISTER_OVERLOADED_2(equals); + REGISTER_MATCHER(accessSpecDecl); REGISTER_MATCHER(addrLabelExpr); REGISTER_MATCHER(alignOfExpr); Index: unittests/ASTMatchers/Dynamic/RegistryTest.cpp =================================================================== --- unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -511,6 +511,21 @@ EXPECT_FALSE(matches("int i = 1;", Value)); } +TEST_F(RegistryTest, EqualsMatcher) { + Matcher IntegerStmt = constructMatcher( + "integerLiteral", constructMatcher("equals", VariantValue(42))) + .getTypedMatcher(); + EXPECT_TRUE(matches("int x = 42;", IntegerStmt)); + EXPECT_FALSE(matches("int x = 1;", IntegerStmt)); + + Matcher BooleanStmt = constructMatcher( + "cxxBoolLiteral", constructMatcher("equals", VariantValue(true))) + .getTypedMatcher(); + EXPECT_TRUE(matches("bool x = true;", BooleanStmt)); + EXPECT_FALSE(matches("bool x = false;", BooleanStmt)); + EXPECT_FALSE(matches("bool x = 0;", BooleanStmt)); +} + } // end anonymous namespace } // end namespace dynamic } // end namespace ast_matchers