Index: cfe/trunk/include/clang/AST/ASTTypeTraits.h =================================================================== --- cfe/trunk/include/clang/AST/ASTTypeTraits.h +++ cfe/trunk/include/clang/AST/ASTTypeTraits.h @@ -63,6 +63,9 @@ /// \brief Returns \c true if \c this and \c Other represent the same kind. bool isSame(ASTNodeKind Other) const; + /// \brief Returns \c true only for the default \c ASTNodeKind() + bool isNone() const { return KindId == NKI_None; } + /// \brief Returns \c true if \c this is a base kind of (or same as) \c Other. /// \param Distance If non-null, used to return the distance between \c this /// and \c Other in the class hierarchy. @@ -76,6 +79,17 @@ return KindId < Other.KindId; } + /// \brief Return the most derived type between \p Kind1 and \p Kind2. + /// + /// Return ASTNodeKind() if they are not related. + static ASTNodeKind getMostDerivedType(ASTNodeKind Kind1, ASTNodeKind Kind2); + + /// \brief Return the most derived common ancestor between Kind1 and Kind2. + /// + /// Return ASTNodeKind() if they are not related. + static ASTNodeKind getMostDerivedCommonAncestor(ASTNodeKind Kind1, + ASTNodeKind Kind2); + private: /// \brief Kind ids. /// Index: cfe/trunk/lib/AST/ASTTypeTraits.cpp =================================================================== --- cfe/trunk/lib/AST/ASTTypeTraits.cpp +++ cfe/trunk/lib/AST/ASTTypeTraits.cpp @@ -62,6 +62,22 @@ StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; } +ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1, + ASTNodeKind Kind2) { + if (Kind1.isBaseOf(Kind2)) return Kind2; + if (Kind2.isBaseOf(Kind1)) return Kind1; + return ASTNodeKind(); +} + +ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1, + ASTNodeKind Kind2) { + NodeKindId Parent = Kind1.KindId; + while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) { + Parent = AllKindInfo[Parent].ParentId; + } + return ASTNodeKind(Parent); +} + ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) { switch (D.getKind()) { #define DECL(DERIVED, BASE) \ Index: cfe/trunk/lib/ASTMatchers/ASTMatchersInternal.cpp =================================================================== --- cfe/trunk/lib/ASTMatchers/ASTMatchersInternal.cpp +++ cfe/trunk/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -64,28 +64,6 @@ const IntrusiveRefCntPtr InnerMatcher; }; -/// \brief Return the most derived type between \p Kind1 and \p Kind2. -/// -/// Return the null type if they are not related. -ast_type_traits::ASTNodeKind getMostDerivedType( - const ast_type_traits::ASTNodeKind Kind1, - const ast_type_traits::ASTNodeKind Kind2) { - if (Kind1.isBaseOf(Kind2)) return Kind2; - if (Kind2.isBaseOf(Kind1)) return Kind1; - return ast_type_traits::ASTNodeKind(); -} - -/// \brief Return the least derived type between \p Kind1 and \p Kind2. -/// -/// Return the null type if they are not related. -static ast_type_traits::ASTNodeKind getLeastDerivedType( - const ast_type_traits::ASTNodeKind Kind1, - const ast_type_traits::ASTNodeKind Kind2) { - if (Kind1.isBaseOf(Kind2)) return Kind1; - if (Kind2.isBaseOf(Kind1)) return Kind2; - return ast_type_traits::ASTNodeKind(); -} - } // namespace DynTypedMatcher DynTypedMatcher::constructVariadic( @@ -98,7 +76,8 @@ assert(Result.SupportedKind.isSame(M.SupportedKind) && "SupportedKind must match!"); Result.RestrictKind = - getLeastDerivedType(Result.RestrictKind, M.RestrictKind); + ast_type_traits::ASTNodeKind::getMostDerivedCommonAncestor( + Result.RestrictKind, M.RestrictKind); } Result.Implementation = new VariadicMatcher(Func, std::move(InnerMatchers)); return Result; @@ -108,7 +87,8 @@ const ast_type_traits::ASTNodeKind Kind) const { auto Copy = *this; Copy.SupportedKind = Kind; - Copy.RestrictKind = getMostDerivedType(Kind, RestrictKind); + Copy.RestrictKind = + ast_type_traits::ASTNodeKind::getMostDerivedType(Kind, RestrictKind); return Copy; } Index: cfe/trunk/unittests/AST/ASTTypeTraitsTest.cpp =================================================================== --- cfe/trunk/unittests/AST/ASTTypeTraitsTest.cpp +++ cfe/trunk/unittests/AST/ASTTypeTraitsTest.cpp @@ -26,6 +26,12 @@ return ASTNodeKind::getFromNodeKind(); } +TEST(ASTNodeKind, IsNone) { + EXPECT_TRUE(ASTNodeKind().isNone()); + EXPECT_FALSE(DNT().isNone()); + EXPECT_FALSE(DNT().isNone()); +} + TEST(ASTNodeKind, Bases) { EXPECT_TRUE(DNT().isBaseOf(DNT())); EXPECT_FALSE(DNT().isSame(DNT())); @@ -60,6 +66,39 @@ EXPECT_FALSE(DNT().isSame(DNT())); } +TEST(ASTNodeKind, MostDerivedType) { + EXPECT_TRUE(DNT().isSame( + ASTNodeKind::getMostDerivedType(DNT(), DNT()))); + EXPECT_TRUE(DNT().isSame( + ASTNodeKind::getMostDerivedType(DNT(), DNT()))); + EXPECT_TRUE(DNT().isSame( + ASTNodeKind::getMostDerivedType(DNT(), DNT()))); + + // Not related. Returns nothing. + EXPECT_TRUE( + ASTNodeKind::getMostDerivedType(DNT(), DNT()).isNone()); + EXPECT_TRUE(ASTNodeKind::getMostDerivedType(DNT(), + DNT()).isNone()); +} + +TEST(ASTNodeKind, MostDerivedCommonAncestor) { + EXPECT_TRUE(DNT().isSame(ASTNodeKind::getMostDerivedCommonAncestor( + DNT(), DNT()))); + EXPECT_TRUE(DNT().isSame(ASTNodeKind::getMostDerivedCommonAncestor( + DNT(), DNT()))); + EXPECT_TRUE(DNT().isSame(ASTNodeKind::getMostDerivedCommonAncestor( + DNT(), DNT()))); + + // A little related. Returns the ancestor. + EXPECT_TRUE( + DNT().isSame(ASTNodeKind::getMostDerivedCommonAncestor( + DNT(), DNT()))); + + // Not related. Returns nothing. + EXPECT_TRUE(ASTNodeKind::getMostDerivedCommonAncestor( + DNT(), DNT()).isNone()); +} + struct Foo {}; TEST(ASTNodeKind, UnknownKind) { Index: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp +++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -460,6 +460,11 @@ EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV)); EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV)); EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV)); + + StatementMatcher MixedTypes = stmt(anyOf(ifStmt(), binaryOperator())); + EXPECT_TRUE(matches("int F() { return 1 + 2; }", MixedTypes)); + EXPECT_TRUE(matches("int F() { if (true) return 1; }", MixedTypes)); + EXPECT_TRUE(notMatches("int F() { return 1; }", MixedTypes)); } TEST(DeclarationMatcher, MatchHas) { Index: cfe/trunk/unittests/ASTMatchers/Dynamic/RegistryTest.cpp =================================================================== --- cfe/trunk/unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ cfe/trunk/unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -347,7 +347,7 @@ "anyOf", constructMatcher("recordDecl", constructMatcher("hasName", std::string("Foo"))), - constructMatcher("namedDecl", + constructMatcher("functionDecl", constructMatcher("hasName", std::string("foo")))) .getTypedMatcher();