[ASTImporter] Split out Objective-C related unit tests
authorRaphael Isemann <teemperor@gmail.com>
Tue, 23 Mar 2021 12:55:44 +0000 (13:55 +0100)
committerRaphael Isemann <teemperor@gmail.com>
Tue, 23 Mar 2021 12:58:45 +0000 (13:58 +0100)
This moves the two tests we have for importing Objective-C nodes to their own
file. The motivation is that this means I can add more Objective-C tests without
making the compilation time of ASTImporterTest even longer. Also it seems nice
to separate the Apple-specific stuff from the ASTImporter test.

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D99162

clang/unittests/AST/ASTImporterObjCTest.cpp [new file with mode: 0644]
clang/unittests/AST/ASTImporterTest.cpp
clang/unittests/AST/CMakeLists.txt

diff --git a/clang/unittests/AST/ASTImporterObjCTest.cpp b/clang/unittests/AST/ASTImporterObjCTest.cpp
new file mode 100644 (file)
index 0000000..2d848dc
--- /dev/null
@@ -0,0 +1,89 @@
+//===- unittest/AST/ASTImporterObjCTest.cpp -============================--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Tests for the correct import of AST nodes related to Objective-C and
+// Objective-C++.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/DeclContextInternals.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "gtest/gtest.h"
+
+#include "ASTImporterFixtures.h"
+
+using namespace clang::ast_matchers;
+using namespace clang;
+
+namespace {
+struct ImportObjCDecl : ASTImporterOptionSpecificTestBase {};
+} // namespace
+
+TEST_P(ImportObjCDecl, ImplicitlyDeclareSelf) {
+  Decl *FromTU = getTuDecl(R"(
+                           __attribute__((objc_root_class))
+                           @interface Root
+                           @end
+                           @interface C : Root
+                             -(void)method;
+                           @end
+                           @implementation C
+                             -(void)method {}
+                           @end
+                           )",
+                           Lang_OBJCXX, "input.mm");
+  auto *FromMethod = LastDeclMatcher<ObjCMethodDecl>().match(
+      FromTU, namedDecl(hasName("method")));
+  ASSERT_TRUE(FromMethod);
+  auto ToMethod = Import(FromMethod, Lang_OBJCXX);
+  ASSERT_TRUE(ToMethod);
+
+  // Both methods should have their implicit parameters.
+  EXPECT_TRUE(FromMethod->getSelfDecl() != nullptr);
+  EXPECT_TRUE(ToMethod->getSelfDecl() != nullptr);
+}
+
+TEST_P(ImportObjCDecl, ObjPropertyNameConflict) {
+  // Tests that properties that share the same name are correctly imported.
+  // This is only possible with one instance and one class property.
+  Decl *FromTU = getTuDecl(R"(
+                           @interface DupProp{}
+                           @property (class) int prop;
+                           @property int prop;
+                           @end
+                           )",
+                           Lang_OBJCXX, "input.mm");
+  auto *FromClass = FirstDeclMatcher<ObjCInterfaceDecl>().match(
+      FromTU, namedDecl(hasName("DupProp")));
+  auto ToClass = Import(FromClass, Lang_OBJCXX);
+  ASSERT_TRUE(ToClass);
+  // We should have one class and one instance property.
+  ASSERT_EQ(
+      1, std::distance(ToClass->classprop_begin(), ToClass->classprop_end()));
+  ASSERT_EQ(1,
+            std::distance(ToClass->instprop_begin(), ToClass->instprop_end()));
+  for (clang::ObjCPropertyDecl *prop : ToClass->properties()) {
+    // All properties should have a getter and a setter.
+    ASSERT_TRUE(prop->getGetterMethodDecl());
+    ASSERT_TRUE(prop->getSetterMethodDecl());
+    // The getters/setters should be able to find the right associated property.
+    ASSERT_EQ(prop->getGetterMethodDecl()->findPropertyDecl(), prop);
+    ASSERT_EQ(prop->getSetterMethodDecl()->findPropertyDecl(), prop);
+  }
+}
+
+static const auto ObjCTestArrayForRunOptions =
+    std::array<std::vector<std::string>, 2>{
+        {std::vector<std::string>{"-fno-objc-arc"},
+         std::vector<std::string>{"-fobjc-arc"}}};
+
+const auto ObjCTestValuesForRunOptions =
+    ::testing::ValuesIn(ObjCTestArrayForRunOptions);
+
+INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportObjCDecl,
+                        ObjCTestValuesForRunOptions, );
index 94cec2c..40383bc 100644 (file)
@@ -5615,59 +5615,6 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportDefaultConstructibleLambdas) {
             2u);
 }
 
-TEST_P(ASTImporterOptionSpecificTestBase, ImplicitlyDeclareSelf) {
-  Decl *FromTU = getTuDecl(R"(
-                           __attribute__((objc_root_class))
-                           @interface Root
-                           @end
-                           @interface C : Root
-                             -(void)method;
-                           @end
-                           @implementation C
-                             -(void)method {}
-                           @end
-                           )",
-                           Lang_OBJCXX, "input.mm");
-  auto *FromMethod = LastDeclMatcher<ObjCMethodDecl>().match(
-      FromTU, namedDecl(hasName("method")));
-  ASSERT_TRUE(FromMethod);
-  auto ToMethod = Import(FromMethod, Lang_OBJCXX);
-  ASSERT_TRUE(ToMethod);
-
-  // Both methods should have their implicit parameters.
-  EXPECT_TRUE(FromMethod->getSelfDecl() != nullptr);
-  EXPECT_TRUE(ToMethod->getSelfDecl() != nullptr);
-}
-
-TEST_P(ASTImporterOptionSpecificTestBase, ObjPropertyNameConflict) {
-  // Tests that properties that share the same name are correctly imported.
-  // This is only possible with one instance and one class property.
-  Decl *FromTU = getTuDecl(R"(
-                           @interface DupProp{}
-                           @property (class) int prop;
-                           @property int prop;
-                           @end
-                           )",
-                           Lang_OBJCXX, "input.mm");
-  auto *FromClass = FirstDeclMatcher<ObjCInterfaceDecl>().match(
-      FromTU, namedDecl(hasName("DupProp")));
-  auto ToClass = Import(FromClass, Lang_OBJCXX);
-  ASSERT_TRUE(ToClass);
-  // We should have one class and one instance property.
-  ASSERT_EQ(
-      1, std::distance(ToClass->classprop_begin(), ToClass->classprop_end()));
-  ASSERT_EQ(1,
-            std::distance(ToClass->instprop_begin(), ToClass->instprop_end()));
-  for (clang::ObjCPropertyDecl *prop : ToClass->properties()) {
-    // All properties should have a getter and a setter.
-    ASSERT_TRUE(prop->getGetterMethodDecl());
-    ASSERT_TRUE(prop->getSetterMethodDecl());
-    // The getters/setters should be able to find the right associated property.
-    ASSERT_EQ(prop->getGetterMethodDecl()->findPropertyDecl(), prop);
-    ASSERT_EQ(prop->getSetterMethodDecl()->findPropertyDecl(), prop);
-  }
-}
-
 struct ImportAutoFunctions : ASTImporterOptionSpecificTestBase {};
 
 TEST_P(ImportAutoFunctions, ReturnWithTypedefDeclaredInside) {
index 979d59b..105bfd7 100644 (file)
@@ -8,6 +8,7 @@ add_clang_unittest(ASTTests
   ASTContextParentMapTest.cpp
   ASTImporterFixtures.cpp
   ASTImporterTest.cpp
+  ASTImporterObjCTest.cpp
   ASTImporterGenericRedeclTest.cpp
   ASTImporterODRStrategiesTest.cpp
   ASTImporterVisibilityTest.cpp