Skip to content

Commit 274907c

Browse files
committed
[ASTImporter] Split out Objective-C related unit tests
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
1 parent 3c8473b commit 274907c

File tree

3 files changed

+90
-53
lines changed

3 files changed

+90
-53
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===- unittest/AST/ASTImporterObjCTest.cpp -============================--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Tests for the correct import of AST nodes related to Objective-C and
10+
// Objective-C++.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "clang/AST/DeclContextInternals.h"
15+
#include "clang/ASTMatchers/ASTMatchers.h"
16+
#include "gtest/gtest.h"
17+
18+
#include "ASTImporterFixtures.h"
19+
20+
using namespace clang::ast_matchers;
21+
using namespace clang;
22+
23+
namespace {
24+
struct ImportObjCDecl : ASTImporterOptionSpecificTestBase {};
25+
} // namespace
26+
27+
TEST_P(ImportObjCDecl, ImplicitlyDeclareSelf) {
28+
Decl *FromTU = getTuDecl(R"(
29+
__attribute__((objc_root_class))
30+
@interface Root
31+
@end
32+
@interface C : Root
33+
-(void)method;
34+
@end
35+
@implementation C
36+
-(void)method {}
37+
@end
38+
)",
39+
Lang_OBJCXX, "input.mm");
40+
auto *FromMethod = LastDeclMatcher<ObjCMethodDecl>().match(
41+
FromTU, namedDecl(hasName("method")));
42+
ASSERT_TRUE(FromMethod);
43+
auto ToMethod = Import(FromMethod, Lang_OBJCXX);
44+
ASSERT_TRUE(ToMethod);
45+
46+
// Both methods should have their implicit parameters.
47+
EXPECT_TRUE(FromMethod->getSelfDecl() != nullptr);
48+
EXPECT_TRUE(ToMethod->getSelfDecl() != nullptr);
49+
}
50+
51+
TEST_P(ImportObjCDecl, ObjPropertyNameConflict) {
52+
// Tests that properties that share the same name are correctly imported.
53+
// This is only possible with one instance and one class property.
54+
Decl *FromTU = getTuDecl(R"(
55+
@interface DupProp{}
56+
@property (class) int prop;
57+
@property int prop;
58+
@end
59+
)",
60+
Lang_OBJCXX, "input.mm");
61+
auto *FromClass = FirstDeclMatcher<ObjCInterfaceDecl>().match(
62+
FromTU, namedDecl(hasName("DupProp")));
63+
auto ToClass = Import(FromClass, Lang_OBJCXX);
64+
ASSERT_TRUE(ToClass);
65+
// We should have one class and one instance property.
66+
ASSERT_EQ(
67+
1, std::distance(ToClass->classprop_begin(), ToClass->classprop_end()));
68+
ASSERT_EQ(1,
69+
std::distance(ToClass->instprop_begin(), ToClass->instprop_end()));
70+
for (clang::ObjCPropertyDecl *prop : ToClass->properties()) {
71+
// All properties should have a getter and a setter.
72+
ASSERT_TRUE(prop->getGetterMethodDecl());
73+
ASSERT_TRUE(prop->getSetterMethodDecl());
74+
// The getters/setters should be able to find the right associated property.
75+
ASSERT_EQ(prop->getGetterMethodDecl()->findPropertyDecl(), prop);
76+
ASSERT_EQ(prop->getSetterMethodDecl()->findPropertyDecl(), prop);
77+
}
78+
}
79+
80+
static const auto ObjCTestArrayForRunOptions =
81+
std::array<std::vector<std::string>, 2>{
82+
{std::vector<std::string>{"-fno-objc-arc"},
83+
std::vector<std::string>{"-fobjc-arc"}}};
84+
85+
const auto ObjCTestValuesForRunOptions =
86+
::testing::ValuesIn(ObjCTestArrayForRunOptions);
87+
88+
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportObjCDecl,
89+
ObjCTestValuesForRunOptions, );

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5615,59 +5615,6 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportDefaultConstructibleLambdas) {
56155615
2u);
56165616
}
56175617

5618-
TEST_P(ASTImporterOptionSpecificTestBase, ImplicitlyDeclareSelf) {
5619-
Decl *FromTU = getTuDecl(R"(
5620-
__attribute__((objc_root_class))
5621-
@interface Root
5622-
@end
5623-
@interface C : Root
5624-
-(void)method;
5625-
@end
5626-
@implementation C
5627-
-(void)method {}
5628-
@end
5629-
)",
5630-
Lang_OBJCXX, "input.mm");
5631-
auto *FromMethod = LastDeclMatcher<ObjCMethodDecl>().match(
5632-
FromTU, namedDecl(hasName("method")));
5633-
ASSERT_TRUE(FromMethod);
5634-
auto ToMethod = Import(FromMethod, Lang_OBJCXX);
5635-
ASSERT_TRUE(ToMethod);
5636-
5637-
// Both methods should have their implicit parameters.
5638-
EXPECT_TRUE(FromMethod->getSelfDecl() != nullptr);
5639-
EXPECT_TRUE(ToMethod->getSelfDecl() != nullptr);
5640-
}
5641-
5642-
TEST_P(ASTImporterOptionSpecificTestBase, ObjPropertyNameConflict) {
5643-
// Tests that properties that share the same name are correctly imported.
5644-
// This is only possible with one instance and one class property.
5645-
Decl *FromTU = getTuDecl(R"(
5646-
@interface DupProp{}
5647-
@property (class) int prop;
5648-
@property int prop;
5649-
@end
5650-
)",
5651-
Lang_OBJCXX, "input.mm");
5652-
auto *FromClass = FirstDeclMatcher<ObjCInterfaceDecl>().match(
5653-
FromTU, namedDecl(hasName("DupProp")));
5654-
auto ToClass = Import(FromClass, Lang_OBJCXX);
5655-
ASSERT_TRUE(ToClass);
5656-
// We should have one class and one instance property.
5657-
ASSERT_EQ(
5658-
1, std::distance(ToClass->classprop_begin(), ToClass->classprop_end()));
5659-
ASSERT_EQ(1,
5660-
std::distance(ToClass->instprop_begin(), ToClass->instprop_end()));
5661-
for (clang::ObjCPropertyDecl *prop : ToClass->properties()) {
5662-
// All properties should have a getter and a setter.
5663-
ASSERT_TRUE(prop->getGetterMethodDecl());
5664-
ASSERT_TRUE(prop->getSetterMethodDecl());
5665-
// The getters/setters should be able to find the right associated property.
5666-
ASSERT_EQ(prop->getGetterMethodDecl()->findPropertyDecl(), prop);
5667-
ASSERT_EQ(prop->getSetterMethodDecl()->findPropertyDecl(), prop);
5668-
}
5669-
}
5670-
56715618
struct ImportAutoFunctions : ASTImporterOptionSpecificTestBase {};
56725619

56735620
TEST_P(ImportAutoFunctions, ReturnWithTypedefDeclaredInside) {

clang/unittests/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_clang_unittest(ASTTests
88
ASTContextParentMapTest.cpp
99
ASTImporterFixtures.cpp
1010
ASTImporterTest.cpp
11+
ASTImporterObjCTest.cpp
1112
ASTImporterGenericRedeclTest.cpp
1213
ASTImporterODRStrategiesTest.cpp
1314
ASTImporterVisibilityTest.cpp

0 commit comments

Comments
 (0)