Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -3551,6 +3551,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: lib/ASTMatchers/Dynamic/Marshallers.h =================================================================== --- lib/ASTMatchers/Dynamic/Marshallers.h +++ lib/ASTMatchers/Dynamic/Marshallers.h @@ -96,6 +96,28 @@ } }; +template <> struct ArgTypeTraits { +private: + static clang::CastKind getCastKind(llvm::StringRef AttrKind) { + return llvm::StringSwitch(AttrKind) +#define CAST_OPERATION(Name) .Case( #Name, CK_##Name) +#include "clang/AST/OperationKinds.def" + .Default(CK_Invalid); + } + +public: + static bool is(const VariantValue &Value) { + return Value.isString() && + getCastKind(Value.getString()) != CK_Invalid; + } + static clang::CastKind get(const VariantValue &Value) { + return getCastKind(Value.getString()); + } + static ArgKind getKind() { + return ArgKind(ArgKind::AK_String); + } +}; + /// \brief Matcher descriptor interface. /// /// Provides a \c create() method that constructs the matcher from the provided Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -210,6 +210,7 @@ REGISTER_MATCHER(hasBody); REGISTER_MATCHER(hasCanonicalType); REGISTER_MATCHER(hasCaseConstant); + REGISTER_MATCHER(hasCastKind); REGISTER_MATCHER(hasCondition); REGISTER_MATCHER(hasConditionVariableStatement); REGISTER_MATCHER(hasDecayedType); Index: unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3485,6 +3485,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(&p);", cxxReinterpretCastExpr()));