summaryrefslogtreecommitdiffstats
path: root/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
diff options
context:
space:
mode:
authorStephane Moore <mog@google.com>2019-08-12 23:23:35 +0000
committerStephane Moore <mog@google.com>2019-08-12 23:23:35 +0000
commita0a47d8ac113b1e959288698deef847f562921d3 (patch)
tree7389a9dabcd21a82c8143ec15db6fd955a0973a9 /clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
parente9865b9b31bb2e6bc742dc6fca8f9f9517c3c43e (diff)
downloadbcm5719-llvm-a0a47d8ac113b1e959288698deef847f562921d3.tar.gz
bcm5719-llvm-a0a47d8ac113b1e959288698deef847f562921d3.zip
[clang] Update isDerivedFrom to support Objective-C classes 🔍
Summary: This change updates `isDerivedFrom` to support Objective-C classes by converting it to a polymorphic matcher. Notes: The matching behavior for Objective-C classes is modeled to match the behavior of `isDerivedFrom` with C++ classes. To that effect, `isDerivedFrom` matches aliased types of derived Objective-C classes, including compatibility aliases. To achieve this, the AST visitor has been updated to map compatibility aliases to their underlying Objective-C class. `isSameOrDerivedFrom` also provides similar behaviors for C++ and Objective-C classes. The behavior that `cxxRecordDecl(isSameOrDerivedFrom("X"))` does not match `class Y {}; typedef Y X;` is mirrored for Objective-C in that `objcInterfaceDecl(isSameOrDerivedFrom("X"))` does not match either `@interface Y @end typedef Y X;` or `@interface Y @end @compatibility_alias X Y;`. Test Notes: Ran clang unit tests. Reviewers: aaron.ballman, jordan_rose, rjmccall, klimek, alexfh, gribozavr Reviewed By: aaron.ballman, gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60543 llvm-svn: 368632
Diffstat (limited to 'clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp')
-rw-r--r--clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 2038e9b6c00..52cd042c965 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -573,6 +573,111 @@ TEST(DeclarationMatcher, IsDerivedFromEmptyName) {
EXPECT_TRUE(notMatches(Code, cxxRecordDecl(isSameOrDerivedFrom(""))));
}
+TEST(DeclarationMatcher, ObjCClassIsDerived) {
+ DeclarationMatcher IsDerivedFromX = objcInterfaceDecl(isDerivedFrom("X"));
+ EXPECT_TRUE(
+ matchesObjC("@interface X @end @interface Y : X @end", IsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end @interface Y<__covariant ObjectType> : X @end",
+ IsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end @compatibility_alias Y X; @interface Z : Y @end",
+ IsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end typedef X Y; @interface Z : Y @end", IsDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@interface X @end", IsDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@class X;", IsDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@class Y;", IsDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@interface X @end @compatibility_alias Y X;",
+ IsDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@interface X @end typedef X Y;", IsDerivedFromX));
+
+ DeclarationMatcher IsDirectlyDerivedFromX =
+ objcInterfaceDecl(isDirectlyDerivedFrom("X"));
+ EXPECT_TRUE(
+ matchesObjC("@interface X @end @interface Y : X @end", IsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end @interface Y<__covariant ObjectType> : X @end",
+ IsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end @compatibility_alias Y X; @interface Z : Y @end",
+ IsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end typedef X Y; @interface Z : Y @end",
+ IsDirectlyDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@interface X @end", IsDirectlyDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@class X;", IsDirectlyDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@class Y;", IsDirectlyDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@interface X @end @compatibility_alias Y X;",
+ IsDirectlyDerivedFromX));
+ EXPECT_TRUE(notMatchesObjC("@interface X @end typedef X Y;",
+ IsDirectlyDerivedFromX));
+
+ DeclarationMatcher IsAX = objcInterfaceDecl(isSameOrDerivedFrom("X"));
+ EXPECT_TRUE(matchesObjC("@interface X @end @interface Y : X @end", IsAX));
+ EXPECT_TRUE(matchesObjC("@interface X @end", IsAX));
+ EXPECT_TRUE(matchesObjC("@class X;", IsAX));
+ EXPECT_TRUE(notMatchesObjC("@interface Y @end", IsAX));
+ EXPECT_TRUE(notMatchesObjC("@class Y;", IsAX));
+
+ DeclarationMatcher ZIsDerivedFromX =
+ objcInterfaceDecl(hasName("Z"), isDerivedFrom("X"));
+ DeclarationMatcher ZIsDirectlyDerivedFromX =
+ objcInterfaceDecl(hasName("Z"), isDirectlyDerivedFrom("X"));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end @interface Y : X @end @interface Z : Y @end",
+ ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC("@interface X @end @interface Y : X @end typedef Y "
+ "V; typedef V W; @interface Z : W @end",
+ ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end typedef X Y; @interface Z : Y @end", ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end typedef X Y; @interface Z : Y @end",
+ ZIsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end typedef A X; typedef A Y; @interface Z : Y @end",
+ ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end typedef A X; typedef A Y; @interface Z : Y @end",
+ ZIsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end @compatibility_alias Y X; @interface Z : Y @end",
+ ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface X @end @compatibility_alias Y X; @interface Z : Y @end",
+ ZIsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface Y @end @compatibility_alias X Y; @interface Z : Y @end",
+ ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface Y @end @compatibility_alias X Y; @interface Z : Y @end",
+ ZIsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end @compatibility_alias X A; @compatibility_alias Y A;"
+ "@interface Z : Y @end", ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end @compatibility_alias X A; @compatibility_alias Y A;"
+ "@interface Z : Y @end", ZIsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface Y @end typedef Y X; @interface Z : X @end", ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface Y @end typedef Y X; @interface Z : X @end",
+ ZIsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end @compatibility_alias Y A; typedef Y X;"
+ "@interface Z : A @end", ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end @compatibility_alias Y A; typedef Y X;"
+ "@interface Z : A @end", ZIsDirectlyDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end typedef A Y; @compatibility_alias X Y;"
+ "@interface Z : A @end", ZIsDerivedFromX));
+ EXPECT_TRUE(matchesObjC(
+ "@interface A @end typedef A Y; @compatibility_alias X Y;"
+ "@interface Z : A @end", ZIsDirectlyDerivedFromX));
+}
+
TEST(DeclarationMatcher, IsLambda) {
const auto IsLambda = cxxMethodDecl(ofClass(cxxRecordDecl(isLambda())));
EXPECT_TRUE(matches("auto x = []{};", IsLambda));
OpenPOWER on IntegriCloud