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 {EncodeKind::GlobalSymbol
, "$ld$hide$os9.0$_sym1", false, false},
21 {EncodeKind::GlobalSymbol
, "_sym1", false, false},
22 {EncodeKind::GlobalSymbol
, "_sym2", false, false},
23 {EncodeKind::GlobalSymbol
, "_sym3", false, false},
24 {EncodeKind::GlobalSymbol
, "_sym4", false, false},
25 {EncodeKind::GlobalSymbol
, "_sym5", false, false},
26 {EncodeKind::GlobalSymbol
, "_tlv1", false, true},
27 {EncodeKind::GlobalSymbol
, "_tlv2", false, true},
28 {EncodeKind::GlobalSymbol
, "_tlv3", false, true},
29 {EncodeKind::GlobalSymbol
, "_weak1", true, false},
30 {EncodeKind::GlobalSymbol
, "_weak2", true, false},
31 {EncodeKind::GlobalSymbol
, "_weak3", true, false},
32 {EncodeKind::ObjectiveCClass
, "class1", false, false},
33 {EncodeKind::ObjectiveCClass
, "class2", false, false},
34 {EncodeKind::ObjectiveCClass
, "class3", false, false},
35 {EncodeKind::ObjectiveCInstanceVariable
, "class1._ivar1", false, false},
36 {EncodeKind::ObjectiveCInstanceVariable
, "class1._ivar2", false, false},
37 {EncodeKind::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
= PLATFORM_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 InterfaceFileRef
client("clientA", Targets
);
91 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Targets
);
92 EXPECT_EQ(1U, File
->allowableClients().size());
93 EXPECT_EQ(client
, File
->allowableClients().front());
94 EXPECT_EQ(1U, File
->reexportedLibraries().size());
95 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
97 ExportedSymbolSeq Exports
;
98 for (const auto *Sym
: File
->symbols()) {
99 EXPECT_FALSE(Sym
->isWeakReferenced());
100 EXPECT_FALSE(Sym
->isUndefined());
101 Exports
.emplace_back(
102 ExportedSymbol
{Sym
->getKind(), std::string(Sym
->getName()),
103 Sym
->isWeakDefined(), Sym
->isThreadLocalValue()});
107 EXPECT_EQ(std::size(TBDv2Symbols
), Exports
.size());
109 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv2Symbols
)));
112 TEST(TBDv2
, ReadFile2
) {
113 static const char TBDv2File2
[] =
115 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
117 "flags: [ flat_namespace, not_app_extension_safe ]\n"
118 "install-name: Test.dylib\n"
119 "swift-version: 1.1\n"
121 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
122 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
123 " objc-classes: [ _class1, _class2 ]\n"
124 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
125 " weak-def-symbols: [ _weak1, _weak2 ]\n"
126 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
127 " - archs: [ armv7, armv7s, armv7k ]\n"
128 " symbols: [ _sym5 ]\n"
129 " objc-classes: [ _class3 ]\n"
130 " objc-ivars: [ _class1._ivar3 ]\n"
131 " weak-def-symbols: [ _weak3 ]\n"
132 " thread-local-symbols: [ _tlv3 ]\n"
134 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
135 " symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
136 " objc-classes: [ _undefClass1, _undefClass2 ]\n"
137 " objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
138 " weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
141 Expected
<TBDFile
> Result
=
142 TextAPIReader::get(MemoryBufferRef(TBDv2File2
, "Test.tbd"));
143 EXPECT_TRUE(!!Result
);
144 TBDFile File
= std::move(Result
.get());
145 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
146 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
147 auto Platform
= PLATFORM_IOS
;
149 for (auto &&arch
: Archs
)
150 Targets
.emplace_back(Target(arch
, Platform
));
151 EXPECT_EQ(Archs
, File
->getArchitectures());
152 EXPECT_EQ(File
->getPlatforms().size(), 1U);
153 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
154 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
155 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCurrentVersion());
156 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
157 EXPECT_EQ(2U, File
->getSwiftABIVersion());
158 EXPECT_EQ(ObjCConstraintType::Retain_Release
, File
->getObjCConstraint());
159 EXPECT_FALSE(File
->isTwoLevelNamespace());
160 EXPECT_FALSE(File
->isApplicationExtensionSafe());
161 EXPECT_EQ(0U, File
->allowableClients().size());
162 EXPECT_EQ(0U, File
->reexportedLibraries().size());
165 TEST(TBDv2
, WriteFile
) {
166 static const char TBDv2File3
[] =
168 "archs: [ i386, x86_64 ]\n"
170 "install-name: '/usr/lib/libfoo.dylib'\n"
171 "current-version: 1.2.3\n"
172 "compatibility-version: 0\n"
175 " - archs: [ i386 ]\n"
176 " symbols: [ _sym1 ]\n"
177 " weak-def-symbols: [ _sym2 ]\n"
178 " thread-local-symbols: [ _sym3 ]\n"
179 " - archs: [ x86_64 ]\n"
180 " allowable-clients: [ clientA ]\n"
181 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
182 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
183 " objc-classes: [ _Class1 ]\n"
184 " objc-ivars: [ _Class1._ivar1 ]\n"
189 for (auto &&arch
: AK_i386
| AK_x86_64
)
190 Targets
.emplace_back(Target(arch
, PLATFORM_MACOS
));
191 File
.setPath("libfoo.dylib");
192 File
.setInstallName("/usr/lib/libfoo.dylib");
193 File
.setFileType(FileType::TBD_V2
);
194 File
.addTargets(Targets
);
195 File
.setCurrentVersion(PackedVersion(1, 2, 3));
196 File
.setTwoLevelNamespace();
197 File
.setApplicationExtensionSafe();
198 File
.setSwiftABIVersion(5);
199 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
200 File
.addAllowableClient("clientA", Targets
[1]);
201 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets
[1]);
202 File
.addSymbol(EncodeKind::GlobalSymbol
, "_sym1", {Targets
[0]});
203 File
.addSymbol(EncodeKind::GlobalSymbol
, "_sym2", {Targets
[0]},
204 SymbolFlags::WeakDefined
);
205 File
.addSymbol(EncodeKind::GlobalSymbol
, "_sym3", {Targets
[0]},
206 SymbolFlags::ThreadLocalValue
);
207 File
.addSymbol(EncodeKind::ObjectiveCClass
, "Class1", {Targets
[1]});
208 File
.addSymbol(EncodeKind::ObjectiveCClassEHType
, "Class1", {Targets
[1]});
209 File
.addSymbol(EncodeKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
212 SmallString
<4096> Buffer
;
213 raw_svector_ostream
OS(Buffer
);
214 Error Result
= TextAPIWriter::writeToStream(OS
, File
);
215 EXPECT_FALSE(Result
);
216 EXPECT_STREQ(TBDv2File3
, Buffer
.c_str());
219 TEST(TBDv2
, Platform_macOS
) {
220 static const char TBDv2PlatformMacOS
[] = "--- !tapi-tbd-v2\n"
221 "archs: [ x86_64 ]\n"
223 "install-name: Test.dylib\n"
226 Expected
<TBDFile
> Result
=
227 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformMacOS
, "Test.tbd"));
228 EXPECT_TRUE(!!Result
);
229 TBDFile File
= std::move(Result
.get());
230 auto Platform
= PLATFORM_MACOS
;
231 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
232 EXPECT_EQ(File
->getPlatforms().size(), 1U);
233 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
236 TEST(TBDv2
, Platform_iOS
) {
237 static const char TBDv2PlatformiOS
[] = "--- !tapi-tbd-v2\n"
240 "install-name: Test.dylib\n"
243 Expected
<TBDFile
> Result
=
244 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformiOS
, "Test.tbd"));
245 EXPECT_TRUE(!!Result
);
246 auto Platform
= PLATFORM_IOS
;
247 TBDFile File
= std::move(Result
.get());
248 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
249 EXPECT_EQ(File
->getPlatforms().size(), 1U);
250 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
253 TEST(TBDv2
, Platform_watchOS
) {
254 static const char TBDv2PlatformWatchOS
[] = "--- !tapi-tbd-v2\n"
255 "archs: [ armv7k ]\n"
256 "platform: watchos\n"
257 "install-name: Test.dylib\n"
260 Expected
<TBDFile
> Result
=
261 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformWatchOS
, "Test.tbd"));
262 EXPECT_TRUE(!!Result
);
263 auto Platform
= PLATFORM_WATCHOS
;
264 TBDFile File
= std::move(Result
.get());
265 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
266 EXPECT_EQ(File
->getPlatforms().size(), 1U);
267 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
270 TEST(TBDv2
, Platform_tvOS
) {
271 static const char TBDv2PlatformtvOS
[] = "--- !tapi-tbd-v2\n"
274 "install-name: Test.dylib\n"
277 Expected
<TBDFile
> Result
=
278 TextAPIReader::get(MemoryBufferRef(TBDv2PlatformtvOS
, "Test.tbd"));
279 EXPECT_TRUE(!!Result
);
280 auto Platform
= PLATFORM_TVOS
;
281 TBDFile File
= std::move(Result
.get());
282 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
283 EXPECT_EQ(File
->getPlatforms().size(), 1U);
284 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
287 TEST(TBDv2
, Platform_bridgeOS
) {
288 static const char TBDv2BridgeOS
[] = "--- !tapi-tbd-v2\n"
289 "archs: [ armv7k ]\n"
290 "platform: bridgeos\n"
291 "install-name: Test.dylib\n"
294 Expected
<TBDFile
> Result
=
295 TextAPIReader::get(MemoryBufferRef(TBDv2BridgeOS
, "Test.tbd"));
296 EXPECT_TRUE(!!Result
);
297 auto Platform
= PLATFORM_BRIDGEOS
;
298 TBDFile File
= std::move(Result
.get());
299 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
300 EXPECT_EQ(File
->getPlatforms().size(), 1U);
301 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
304 TEST(TBDv2
, Swift_1_0
) {
305 static const char TBDv2Swift1
[] = "--- !tapi-tbd-v2\n"
308 "install-name: Test.dylib\n"
309 "swift-version: 1.0\n"
312 Expected
<TBDFile
> Result
=
313 TextAPIReader::get(MemoryBufferRef(TBDv2Swift1
, "Test.tbd"));
314 EXPECT_TRUE(!!Result
);
315 TBDFile File
= std::move(Result
.get());
316 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
317 EXPECT_EQ(1U, File
->getSwiftABIVersion());
320 TEST(TBDv2
, Swift_1_1
) {
321 static const char TBDv2Swift1dot
[] = "--- !tapi-tbd-v2\n"
324 "install-name: Test.dylib\n"
325 "swift-version: 1.1\n"
328 Expected
<TBDFile
> Result
=
329 TextAPIReader::get(MemoryBufferRef(TBDv2Swift1dot
, "Test.tbd"));
330 EXPECT_TRUE(!!Result
);
331 TBDFile File
= std::move(Result
.get());
332 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
333 EXPECT_EQ(2U, File
->getSwiftABIVersion());
336 TEST(TBDv2
, Swift_2_0
) {
337 static const char tbd_v2_swift_2_0
[] = "--- !tapi-tbd-v2\n"
340 "install-name: Test.dylib\n"
341 "swift-version: 2.0\n"
344 Expected
<TBDFile
> Result
=
345 TextAPIReader::get(MemoryBufferRef(tbd_v2_swift_2_0
, "Test.tbd"));
346 EXPECT_TRUE(!!Result
);
347 TBDFile File
= std::move(Result
.get());
348 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
349 EXPECT_EQ(3U, File
->getSwiftABIVersion());
352 TEST(TBDv2
, Swift_3_0
) {
353 static const char TBDv2Swift3
[] = "--- !tapi-tbd-v2\n"
356 "install-name: Test.dylib\n"
357 "swift-version: 3.0\n"
360 Expected
<TBDFile
> Result
=
361 TextAPIReader::get(MemoryBufferRef(TBDv2Swift3
, "Test.tbd"));
362 EXPECT_TRUE(!!Result
);
363 TBDFile File
= std::move(Result
.get());
364 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
365 EXPECT_EQ(4U, File
->getSwiftABIVersion());
368 TEST(TBDv2
, Swift_4_0
) {
369 static const char TBDv2Swift4
[] = "--- !tapi-tbd-v2\n"
372 "install-name: Test.dylib\n"
373 "swift-version: 4.0\n"
376 Expected
<TBDFile
> Result
=
377 TextAPIReader::get(MemoryBufferRef(TBDv2Swift4
, "Test.tbd"));
378 EXPECT_FALSE(!!Result
);
379 std::string ErrorMessage
= toString(Result
.takeError());
380 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
381 "version.\nswift-version: 4.0\n ^~~\n",
385 TEST(TBDv2
, Swift_5
) {
386 static const char TBDv2Swift5
[] = "--- !tapi-tbd-v2\n"
389 "install-name: Test.dylib\n"
393 Expected
<TBDFile
> Result
=
394 TextAPIReader::get(MemoryBufferRef(TBDv2Swift5
, "Test.tbd"));
395 EXPECT_TRUE(!!Result
);
396 TBDFile File
= std::move(Result
.get());
397 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
398 EXPECT_EQ(5U, File
->getSwiftABIVersion());
401 TEST(TBDv2
, Swift_99
) {
402 static const char TBDv2Swift99
[] = "--- !tapi-tbd-v2\n"
405 "install-name: Test.dylib\n"
406 "swift-version: 99\n"
409 Expected
<TBDFile
> Result
=
410 TextAPIReader::get(MemoryBufferRef(TBDv2Swift99
, "Test.tbd"));
411 EXPECT_TRUE(!!Result
);
412 TBDFile File
= std::move(Result
.get());
413 EXPECT_EQ(FileType::TBD_V2
, File
->getFileType());
414 EXPECT_EQ(99U, File
->getSwiftABIVersion());
417 TEST(TBDv2
, UnknownArchitecture
) {
418 static const char TBDv2FileUnknownArch
[] = "--- !tapi-tbd-v2\n"
421 "install-name: Test.dylib\n"
423 Expected
<TBDFile
> Result
=
424 TextAPIReader::get(MemoryBufferRef(TBDv2FileUnknownArch
, "Test.tbd"));
425 EXPECT_TRUE(!!Result
);
428 TEST(TBDv2
, UnknownPlatform
) {
429 static const char TBDv2FileUnknownPlatform
[] = "--- !tapi-tbd-v2\n"
434 Expected
<TBDFile
> Result
=
435 TextAPIReader::get(MemoryBufferRef(TBDv2FileUnknownPlatform
, "Test.tbd"));
436 EXPECT_FALSE(!!Result
);
437 std::string ErrorMessage
= toString(Result
.takeError());
438 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
443 TEST(TBDv2
, InvalidPlatform
) {
444 static const char TBDv2FileInvalidPlatform
[] = "--- !tapi-tbd-v2\n"
447 "install-name: Test.dylib\n"
450 Expected
<TBDFile
> Result
=
451 TextAPIReader::get(MemoryBufferRef(TBDv2FileInvalidPlatform
, "Test.tbd"));
452 EXPECT_FALSE(!!Result
);
453 std::string ErrorMessage
= toString(Result
.takeError());
454 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: invalid platform\nplatform: "
459 TEST(TBDv2
, MalformedFile1
) {
460 static const char TBDv2FileMalformed1
[] = "--- !tapi-tbd-v2\n"
462 "foobar: \"Unsupported key\"\n"
465 Expected
<TBDFile
> Result
=
466 TextAPIReader::get(MemoryBufferRef(TBDv2FileMalformed1
, "Test.tbd"));
467 EXPECT_FALSE(!!Result
);
468 std::string ErrorMessage
= toString(Result
.takeError());
469 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
470 "'platform'\narchs: [ arm64 ]\n^\n",
474 TEST(TBDv2
, MalformedFile2
) {
475 static const char TBDv2FileMalformed2
[] = "--- !tapi-tbd-v2\n"
478 "install-name: Test.dylib\n"
479 "foobar: \"Unsupported key\"\n"
482 Expected
<TBDFile
> Result
=
483 TextAPIReader::get(MemoryBufferRef(TBDv2FileMalformed2
, "Test.tbd"));
484 EXPECT_FALSE(!!Result
);
485 std::string ErrorMessage
= toString(Result
.takeError());
487 "malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
488 "\"Unsupported key\"\n^~~~~~\n",