1 //===-- TextStubV2Tests.cpp - TBD V2 File Test ----------------------------===//
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
7 //===-----------------------------------------------------------------------===/
8 #include "TextStubHelpers.h"
9 #include "llvm/TextAPI/InterfaceFile.h"
10 #include "llvm/TextAPI/TextAPIReader.h"
11 #include "llvm/TextAPI/TextAPIWriter.h"
12 #include "gtest/gtest.h"
17 using namespace llvm::MachO
;
19 static ExportedSymbol TBDv2Symbols
[] = {
20 {SymbolKind::GlobalSymbol
, "$ld$hide$os9.0$_sym1", false, false},
21 {SymbolKind::GlobalSymbol
, "_sym1", false, false},
22 {SymbolKind::GlobalSymbol
, "_sym2", false, false},
23 {SymbolKind::GlobalSymbol
, "_sym3", false, false},
24 {SymbolKind::GlobalSymbol
, "_sym4", false, false},
25 {SymbolKind::GlobalSymbol
, "_sym5", false, false},
26 {SymbolKind::GlobalSymbol
, "_tlv1", false, true},
27 {SymbolKind::GlobalSymbol
, "_tlv2", false, true},
28 {SymbolKind::GlobalSymbol
, "_tlv3", false, true},
29 {SymbolKind::GlobalSymbol
, "_weak1", true, false},
30 {SymbolKind::GlobalSymbol
, "_weak2", true, false},
31 {SymbolKind::GlobalSymbol
, "_weak3", true, false},
32 {SymbolKind::ObjectiveCClass
, "class1", false, false},
33 {SymbolKind::ObjectiveCClass
, "class2", false, false},
34 {SymbolKind::ObjectiveCClass
, "class3", false, false},
35 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar1", false, false},
36 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar2", false, false},
37 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar3", false, false},
42 TEST(TBDv2
, ReadFile
) {
43 static const char TBDv2File1
[] =
45 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
47 "flags: [ installapi ]\n"
48 "install-name: Test.dylib\n"
49 "current-version: 2.3.4\n"
50 "compatibility-version: 1.0\n"
51 "swift-version: 1.1\n"
52 "parent-umbrella: Umbrella.dylib\n"
54 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
55 " allowable-clients: [ clientA ]\n"
56 " re-exports: [ /usr/lib/libfoo.dylib ]\n"
57 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
58 " objc-classes: [ _class1, _class2 ]\n"
59 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
60 " weak-def-symbols: [ _weak1, _weak2 ]\n"
61 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
62 " - archs: [ armv7, armv7s, armv7k ]\n"
63 " symbols: [ _sym5 ]\n"
64 " objc-classes: [ _class3 ]\n"
65 " objc-ivars: [ _class1._ivar3 ]\n"
66 " weak-def-symbols: [ _weak3 ]\n"
67 " thread-local-symbols: [ _tlv3 ]\n"
70 Expected
<TBDFile
> Result
=
71 TextAPIReader::get(MemoryBufferRef(TBDv2File1
, "Test.tbd"));
72 EXPECT_TRUE(!!Result
);
73 TBDFile File
= std::move(Result
.get());
74 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
75 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
76 auto Platform
= PlatformKind::iOS
;
78 for (auto &&arch
: Archs
)
79 Targets
.emplace_back(Target(arch
, Platform
));
80 EXPECT_EQ(Archs
, File
->getArchitectures());
81 EXPECT_EQ(File
->getPlatforms().size(), 1U);
82 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
83 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
84 EXPECT_EQ(PackedVersion(2, 3, 4), File
->getCurrentVersion());
85 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
86 EXPECT_EQ(2U, File
->getSwiftABIVersion());
87 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
88 EXPECT_TRUE(File
->isTwoLevelNamespace());
89 EXPECT_TRUE(File
->isApplicationExtensionSafe());
90 EXPECT_TRUE(File
->isInstallAPI());
91 InterfaceFileRef
client("clientA", Targets
);
92 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Targets
);
93 EXPECT_EQ(1U, File
->allowableClients().size());
94 EXPECT_EQ(client
, File
->allowableClients().front());
95 EXPECT_EQ(1U, File
->reexportedLibraries().size());
96 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
98 ExportedSymbolSeq Exports
;
99 for (const auto *Sym
: File
->symbols()) {
100 EXPECT_FALSE(Sym
->isWeakReferenced());
101 EXPECT_FALSE(Sym
->isUndefined());
102 Exports
.emplace_back(
103 ExportedSymbol
{Sym
->getKind(), std::string(Sym
->getName()),
104 Sym
->isWeakDefined(), Sym
->isThreadLocalValue()});
106 llvm::sort(Exports
.begin(), Exports
.end());
108 EXPECT_EQ(sizeof(TBDv2Symbols
) / sizeof(ExportedSymbol
), Exports
.size());
110 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv2Symbols
)));
113 TEST(TBDv2
, ReadFile2
) {
114 static const char TBDv2File2
[] =
116 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
118 "flags: [ flat_namespace, not_app_extension_safe ]\n"
119 "install-name: Test.dylib\n"
120 "swift-version: 1.1\n"
122 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
123 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
124 " objc-classes: [ _class1, _class2 ]\n"
125 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
126 " weak-def-symbols: [ _weak1, _weak2 ]\n"
127 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
128 " - archs: [ armv7, armv7s, armv7k ]\n"
129 " symbols: [ _sym5 ]\n"
130 " objc-classes: [ _class3 ]\n"
131 " objc-ivars: [ _class1._ivar3 ]\n"
132 " weak-def-symbols: [ _weak3 ]\n"
133 " thread-local-symbols: [ _tlv3 ]\n"
135 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
136 " symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
137 " objc-classes: [ _undefClass1, _undefClass2 ]\n"
138 " objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
139 " weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
142 Expected
<TBDFile
> Result
=
143 TextAPIReader::get(MemoryBufferRef(TBDv2File2
, "Test.tbd"));
144 EXPECT_TRUE(!!Result
);
145 TBDFile File
= std::move(Result
.get());
146 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
147 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
148 auto Platform
= PlatformKind::iOS
;
150 for (auto &&arch
: Archs
)
151 Targets
.emplace_back(Target(arch
, Platform
));
152 EXPECT_EQ(Archs
, File
->getArchitectures());
153 EXPECT_EQ(File
->getPlatforms().size(), 1U);
154 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
155 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
156 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCurrentVersion());
157 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
158 EXPECT_EQ(2U, File
->getSwiftABIVersion());
159 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
160 EXPECT_FALSE(File
->isTwoLevelNamespace());
161 EXPECT_FALSE(File
->isApplicationExtensionSafe());
162 EXPECT_FALSE(File
->isInstallAPI());
163 EXPECT_EQ(0U, File
->allowableClients().size());
164 EXPECT_EQ(0U, File
->reexportedLibraries().size());
167 TEST(TBDv2
, WriteFile
) {
168 static const char TBDv2File3
[] =
170 "archs: [ i386, x86_64 ]\n"
172 "install-name: '/usr/lib/libfoo.dylib'\n"
173 "current-version: 1.2.3\n"
174 "compatibility-version: 0\n"
177 " - archs: [ i386 ]\n"
178 " symbols: [ _sym1 ]\n"
179 " weak-def-symbols: [ _sym2 ]\n"
180 " thread-local-symbols: [ _sym3 ]\n"
181 " - archs: [ x86_64 ]\n"
182 " allowable-clients: [ clientA ]\n"
183 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
184 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
185 " objc-classes: [ _Class1 ]\n"
186 " objc-ivars: [ _Class1._ivar1 ]\n"
191 for (auto &&arch
: AK_i386
| AK_x86_64
)
192 Targets
.emplace_back(Target(arch
, PlatformKind::macOS
));
193 File
.setPath("libfoo.dylib");
194 File
.setInstallName("/usr/lib/libfoo.dylib");
195 File
.setFileType(FileType::TBD_V2
);
196 File
.addTargets(Targets
);
197 File
.setCurrentVersion(PackedVersion(1, 2, 3));
198 File
.setTwoLevelNamespace();
199 File
.setApplicationExtensionSafe();
200 File
.setSwiftABIVersion(5);
201 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
202 File
.addAllowableClient("clientA", Targets
[1]);
203 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets
[1]);
204 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym1", {Targets
[0]});
205 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym2", {Targets
[0]},
206 SymbolFlags::WeakDefined
);
207 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym3", {Targets
[0]},
208 SymbolFlags::ThreadLocalValue
);
209 File
.addSymbol(SymbolKind::ObjectiveCClass
, "Class1", {Targets
[1]});
210 File
.addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", {Targets
[1]});
211 File
.addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
214 SmallString
<4096> Buffer
;
215 raw_svector_ostream
OS(Buffer
);
216 Error Result
= TextAPIWriter::writeToStream(OS
, File
);
217 EXPECT_FALSE(Result
);
218 EXPECT_STREQ(TBDv2File3
, Buffer
.c_str());
221 TEST(TBDv2
, Platform_macOS
) {
222 static const char TBDv2PlatformMacOS
[] = "--- !tapi-tbd-v2\n"
223 "archs: [ x86_64 ]\n"
225 "install-name: Test.dylib\n"
228 Expected
<TBDFile
> Result
=
229 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformMacOS
, "Test.tbd"));
230 EXPECT_TRUE(!!Result
);
231 TBDFile File
= std::move(Result
.get());
232 auto Platform
= PlatformKind::macOS
;
233 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
234 EXPECT_EQ(File
->getPlatforms().size(), 1U);
235 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
238 TEST(TBDv2
, Platform_iOS
) {
239 static const char TBDv2PlatformiOS
[] = "--- !tapi-tbd-v2\n"
242 "install-name: Test.dylib\n"
245 Expected
<TBDFile
> Result
=
246 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformiOS
, "Test.tbd"));
247 EXPECT_TRUE(!!Result
);
248 auto Platform
= PlatformKind::iOS
;
249 TBDFile File
= std::move(Result
.get());
250 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
251 EXPECT_EQ(File
->getPlatforms().size(), 1U);
252 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
255 TEST(TBDv2
, Platform_watchOS
) {
256 static const char TBDv2PlatformWatchOS
[] = "--- !tapi-tbd-v2\n"
257 "archs: [ armv7k ]\n"
258 "platform: watchos\n"
259 "install-name: Test.dylib\n"
262 Expected
<TBDFile
> Result
=
263 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformWatchOS
, "Test.tbd"));
264 EXPECT_TRUE(!!Result
);
265 auto Platform
= PlatformKind::watchOS
;
266 TBDFile File
= std::move(Result
.get());
267 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
268 EXPECT_EQ(File
->getPlatforms().size(), 1U);
269 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
272 TEST(TBDv2
, Platform_tvOS
) {
273 static const char TBDv2PlatformtvOS
[] = "--- !tapi-tbd-v2\n"
276 "install-name: Test.dylib\n"
279 Expected
<TBDFile
> Result
=
280 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformtvOS
, "Test.tbd"));
281 EXPECT_TRUE(!!Result
);
282 auto Platform
= PlatformKind::tvOS
;
283 TBDFile File
= std::move(Result
.get());
284 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
285 EXPECT_EQ(File
->getPlatforms().size(), 1U);
286 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
289 TEST(TBDv2
, Platform_bridgeOS
) {
290 static const char TBDv2BridgeOS
[] = "--- !tapi-tbd-v2\n"
291 "archs: [ armv7k ]\n"
292 "platform: bridgeos\n"
293 "install-name: Test.dylib\n"
296 Expected
<TBDFile
> Result
=
297 TextAPIReader::get(MemoryBufferRef(TBDv2BridgeOS
, "Test.tbd"));
298 EXPECT_TRUE(!!Result
);
299 auto Platform
= PlatformKind::bridgeOS
;
300 TBDFile File
= std::move(Result
.get());
301 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
302 EXPECT_EQ(File
->getPlatforms().size(), 1U);
303 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
306 TEST(TBDv2
, Swift_1_0
) {
307 static const char TBDv2Swift1
[] = "--- !tapi-tbd-v2\n"
310 "install-name: Test.dylib\n"
311 "swift-version: 1.0\n"
314 Expected
<TBDFile
> Result
=
315 TextAPIReader::get(MemoryBufferRef(TBDv2Swift1
, "Test.tbd"));
316 EXPECT_TRUE(!!Result
);
317 TBDFile File
= std::move(Result
.get());
318 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
319 EXPECT_EQ(1U, File
->getSwiftABIVersion());
322 TEST(TBDv2
, Swift_1_1
) {
323 static const char TBDv2Swift1dot
[] = "--- !tapi-tbd-v2\n"
326 "install-name: Test.dylib\n"
327 "swift-version: 1.1\n"
330 Expected
<TBDFile
> Result
=
331 TextAPIReader::get(MemoryBufferRef(TBDv2Swift1dot
, "Test.tbd"));
332 EXPECT_TRUE(!!Result
);
333 TBDFile File
= std::move(Result
.get());
334 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
335 EXPECT_EQ(2U, File
->getSwiftABIVersion());
338 TEST(TBDv2
, Swift_2_0
) {
339 static const char tbd_v2_swift_2_0
[] = "--- !tapi-tbd-v2\n"
342 "install-name: Test.dylib\n"
343 "swift-version: 2.0\n"
346 Expected
<TBDFile
> Result
=
347 TextAPIReader::get(MemoryBufferRef(tbd_v2_swift_2_0
, "Test.tbd"));
348 EXPECT_TRUE(!!Result
);
349 TBDFile File
= std::move(Result
.get());
350 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
351 EXPECT_EQ(3U, File
->getSwiftABIVersion());
354 TEST(TBDv2
, Swift_3_0
) {
355 static const char TBDv2Swift3
[] = "--- !tapi-tbd-v2\n"
358 "install-name: Test.dylib\n"
359 "swift-version: 3.0\n"
362 Expected
<TBDFile
> Result
=
363 TextAPIReader::get(MemoryBufferRef(TBDv2Swift3
, "Test.tbd"));
364 EXPECT_TRUE(!!Result
);
365 TBDFile File
= std::move(Result
.get());
366 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
367 EXPECT_EQ(4U, File
->getSwiftABIVersion());
370 TEST(TBDv2
, Swift_4_0
) {
371 static const char TBDv2Swift4
[] = "--- !tapi-tbd-v2\n"
374 "install-name: Test.dylib\n"
375 "swift-version: 4.0\n"
378 Expected
<TBDFile
> Result
=
379 TextAPIReader::get(MemoryBufferRef(TBDv2Swift4
, "Test.tbd"));
380 EXPECT_FALSE(!!Result
);
381 std::string ErrorMessage
= toString(Result
.takeError());
382 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
383 "version.\nswift-version: 4.0\n ^~~\n",
387 TEST(TBDv2
, Swift_5
) {
388 static const char TBDv2Swift5
[] = "--- !tapi-tbd-v2\n"
391 "install-name: Test.dylib\n"
395 Expected
<TBDFile
> Result
=
396 TextAPIReader::get(MemoryBufferRef(TBDv2Swift5
, "Test.tbd"));
397 EXPECT_TRUE(!!Result
);
398 TBDFile File
= std::move(Result
.get());
399 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
400 EXPECT_EQ(5U, File
->getSwiftABIVersion());
403 TEST(TBDv2
, Swift_99
) {
404 static const char TBDv2Swift99
[] = "--- !tapi-tbd-v2\n"
407 "install-name: Test.dylib\n"
408 "swift-version: 99\n"
411 Expected
<TBDFile
> Result
=
412 TextAPIReader::get(MemoryBufferRef(TBDv2Swift99
, "Test.tbd"));
413 EXPECT_TRUE(!!Result
);
414 TBDFile File
= std::move(Result
.get());
415 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
416 EXPECT_EQ(99U, File
->getSwiftABIVersion());
419 TEST(TBDv2
, UnknownArchitecture
) {
420 static const char TBDv2FileUnknownArch
[] = "--- !tapi-tbd-v2\n"
423 "install-name: Test.dylib\n"
425 Expected
<TBDFile
> Result
=
426 TextAPIReader::get(MemoryBufferRef(TBDv2FileUnknownArch
, "Test.tbd"));
427 EXPECT_TRUE(!!Result
);
430 TEST(TBDv2
, UnknownPlatform
) {
431 static const char TBDv2FileUnknownPlatform
[] = "--- !tapi-tbd-v2\n"
436 Expected
<TBDFile
> Result
=
437 TextAPIReader::get(MemoryBufferRef(TBDv2FileUnknownPlatform
, "Test.tbd"));
438 EXPECT_FALSE(!!Result
);
439 std::string ErrorMessage
= toString(Result
.takeError());
440 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
445 TEST(TBDv2
, InvalidPlatform
) {
446 static const char TBDv2FileInvalidPlatform
[] = "--- !tapi-tbd-v2\n"
449 "install-name: Test.dylib\n"
452 Expected
<TBDFile
> Result
=
453 TextAPIReader::get(MemoryBufferRef(TBDv2FileInvalidPlatform
, "Test.tbd"));
454 EXPECT_FALSE(!!Result
);
455 std::string ErrorMessage
= toString(Result
.takeError());
456 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: invalid platform\nplatform: "
461 TEST(TBDv2
, MalformedFile1
) {
462 static const char TBDv2FileMalformed1
[] = "--- !tapi-tbd-v2\n"
464 "foobar: \"Unsupported key\"\n"
467 Expected
<TBDFile
> Result
=
468 TextAPIReader::get(MemoryBufferRef(TBDv2FileMalformed1
, "Test.tbd"));
469 EXPECT_FALSE(!!Result
);
470 std::string ErrorMessage
= toString(Result
.takeError());
471 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
472 "'platform'\narchs: [ arm64 ]\n^\n",
476 TEST(TBDv2
, MalformedFile2
) {
477 static const char TBDv2FileMalformed2
[] = "--- !tapi-tbd-v2\n"
480 "install-name: Test.dylib\n"
481 "foobar: \"Unsupported key\"\n"
484 Expected
<TBDFile
> Result
=
485 TextAPIReader::get(MemoryBufferRef(TBDv2FileMalformed2
, "Test.tbd"));
486 EXPECT_FALSE(!!Result
);
487 std::string ErrorMessage
= toString(Result
.takeError());
489 "malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
490 "\"Unsupported key\"\n^~~~~~\n",