[yaml2obj/obj2yaml] - Do not trigger llvm_unreachable when dumping/parsing relocation...
[llvm-complete.git] / unittests / TextAPI / TextStubV2Tests.cpp
blob6350b61db5d75413d9c16ea1aa322cc9457c1deb
1 //===-- TextStubV2Tests.cpp - TBD V2 File Test ----------------------------===//
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 //===-----------------------------------------------------------------------===/
9 #include "llvm/TextAPI/MachO/InterfaceFile.h"
10 #include "llvm/TextAPI/MachO/TextAPIReader.h"
11 #include "llvm/TextAPI/MachO/TextAPIWriter.h"
12 #include "gtest/gtest.h"
13 #include <string>
14 #include <vector>
16 using namespace llvm;
17 using namespace llvm::MachO;
19 struct ExportedSymbol {
20 SymbolKind Kind;
21 std::string Name;
22 bool WeakDefined;
23 bool ThreadLocalValue;
25 using ExportedSymbolSeq = std::vector<ExportedSymbol>;
27 inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
28 return std::tie(lhs.Kind, lhs.Name) < std::tie(rhs.Kind, rhs.Name);
31 inline bool operator==(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
32 return std::tie(lhs.Kind, lhs.Name, lhs.WeakDefined, lhs.ThreadLocalValue) ==
33 std::tie(rhs.Kind, rhs.Name, rhs.WeakDefined, rhs.ThreadLocalValue);
36 static ExportedSymbol TBDv2Symbols[] = {
37 {SymbolKind::GlobalSymbol, "$ld$hide$os9.0$_sym1", false, false},
38 {SymbolKind::GlobalSymbol, "_sym1", false, false},
39 {SymbolKind::GlobalSymbol, "_sym2", false, false},
40 {SymbolKind::GlobalSymbol, "_sym3", false, false},
41 {SymbolKind::GlobalSymbol, "_sym4", false, false},
42 {SymbolKind::GlobalSymbol, "_sym5", false, false},
43 {SymbolKind::GlobalSymbol, "_tlv1", false, true},
44 {SymbolKind::GlobalSymbol, "_tlv2", false, true},
45 {SymbolKind::GlobalSymbol, "_tlv3", false, true},
46 {SymbolKind::GlobalSymbol, "_weak1", true, false},
47 {SymbolKind::GlobalSymbol, "_weak2", true, false},
48 {SymbolKind::GlobalSymbol, "_weak3", true, false},
49 {SymbolKind::ObjectiveCClass, "class1", false, false},
50 {SymbolKind::ObjectiveCClass, "class2", false, false},
51 {SymbolKind::ObjectiveCClass, "class3", false, false},
52 {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar1", false, false},
53 {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar2", false, false},
54 {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar3", false, false},
57 namespace TBDv2 {
59 TEST(TBDv2, ReadFile) {
60 static const char tbd_v2_file1[] =
61 "--- !tapi-tbd-v2\n"
62 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
63 "platform: ios\n"
64 "flags: [ installapi ]\n"
65 "install-name: Test.dylib\n"
66 "current-version: 2.3.4\n"
67 "compatibility-version: 1.0\n"
68 "swift-version: 1.1\n"
69 "parent-umbrella: Umbrella.dylib\n"
70 "exports:\n"
71 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
72 " allowable-clients: [ clientA ]\n"
73 " re-exports: [ /usr/lib/libfoo.dylib ]\n"
74 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
75 " objc-classes: [ _class1, _class2 ]\n"
76 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
77 " weak-def-symbols: [ _weak1, _weak2 ]\n"
78 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
79 " - archs: [ armv7, armv7s, armv7k ]\n"
80 " symbols: [ _sym5 ]\n"
81 " objc-classes: [ _class3 ]\n"
82 " objc-ivars: [ _class1._ivar3 ]\n"
83 " weak-def-symbols: [ _weak3 ]\n"
84 " thread-local-symbols: [ _tlv3 ]\n"
85 "...\n";
87 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v2_file1, "Test.tbd"));
88 EXPECT_TRUE(!!Result);
89 auto File = std::move(Result.get());
90 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
91 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
92 EXPECT_EQ(Archs, File->getArchitectures());
93 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
94 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
95 EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
96 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
97 EXPECT_EQ(2U, File->getSwiftABIVersion());
98 EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
99 EXPECT_TRUE(File->isTwoLevelNamespace());
100 EXPECT_TRUE(File->isApplicationExtensionSafe());
101 EXPECT_TRUE(File->isInstallAPI());
102 InterfaceFileRef client("clientA", Archs);
103 InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Archs);
104 EXPECT_EQ(1U, File->allowableClients().size());
105 EXPECT_EQ(client, File->allowableClients().front());
106 EXPECT_EQ(1U, File->reexportedLibraries().size());
107 EXPECT_EQ(reexport, File->reexportedLibraries().front());
109 ExportedSymbolSeq Exports;
110 for (const auto *Sym : File->symbols()) {
111 EXPECT_FALSE(Sym->isWeakReferenced());
112 EXPECT_FALSE(Sym->isUndefined());
113 Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(),
114 Sym->isWeakDefined(),
115 Sym->isThreadLocalValue()});
117 llvm::sort(Exports.begin(), Exports.end());
119 EXPECT_EQ(sizeof(TBDv2Symbols) / sizeof(ExportedSymbol), Exports.size());
120 EXPECT_TRUE(
121 std::equal(Exports.begin(), Exports.end(), std::begin(TBDv2Symbols)));
124 TEST(TBDv2, ReadFile2) {
125 static const char tbd_v2_file2[] =
126 "--- !tapi-tbd-v2\n"
127 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
128 "platform: ios\n"
129 "flags: [ flat_namespace, not_app_extension_safe ]\n"
130 "install-name: Test.dylib\n"
131 "swift-version: 1.1\n"
132 "exports:\n"
133 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
134 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
135 " objc-classes: [ _class1, _class2 ]\n"
136 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
137 " weak-def-symbols: [ _weak1, _weak2 ]\n"
138 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
139 " - archs: [ armv7, armv7s, armv7k ]\n"
140 " symbols: [ _sym5 ]\n"
141 " objc-classes: [ _class3 ]\n"
142 " objc-ivars: [ _class1._ivar3 ]\n"
143 " weak-def-symbols: [ _weak3 ]\n"
144 " thread-local-symbols: [ _tlv3 ]\n"
145 "undefineds:\n"
146 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
147 " symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
148 " objc-classes: [ _undefClass1, _undefClass2 ]\n"
149 " objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
150 " weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
151 "...\n";
153 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v2_file2, "Test.tbd"));
154 EXPECT_TRUE(!!Result);
155 auto File = std::move(Result.get());
156 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
157 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
158 EXPECT_EQ(Archs, File->getArchitectures());
159 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
160 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
161 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
162 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
163 EXPECT_EQ(2U, File->getSwiftABIVersion());
164 EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
165 EXPECT_FALSE(File->isTwoLevelNamespace());
166 EXPECT_FALSE(File->isApplicationExtensionSafe());
167 EXPECT_FALSE(File->isInstallAPI());
168 EXPECT_EQ(0U, File->allowableClients().size());
169 EXPECT_EQ(0U, File->reexportedLibraries().size());
172 TEST(TBDv2, WriteFile) {
173 static const char tbd_v2_file3[] =
174 "--- !tapi-tbd-v2\n"
175 "archs: [ i386, x86_64 ]\n"
176 "platform: macosx\n"
177 "install-name: '/usr/lib/libfoo.dylib'\n"
178 "current-version: 1.2.3\n"
179 "compatibility-version: 0\n"
180 "swift-version: 5\n"
181 "exports:\n"
182 " - archs: [ i386 ]\n"
183 " symbols: [ _sym1 ]\n"
184 " weak-def-symbols: [ _sym2 ]\n"
185 " thread-local-symbols: [ _sym3 ]\n"
186 " - archs: [ x86_64 ]\n"
187 " allowable-clients: [ clientA ]\n"
188 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
189 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
190 " objc-classes: [ _Class1 ]\n"
191 " objc-ivars: [ _Class1._ivar1 ]\n"
192 "...\n";
194 InterfaceFile File;
195 File.setPath("libfoo.dylib");
196 File.setInstallName("/usr/lib/libfoo.dylib");
197 File.setFileType(FileType::TBD_V2);
198 File.setArchitectures(AK_i386 | AK_x86_64);
199 File.setPlatform(PlatformKind::macOS);
200 File.setCurrentVersion(PackedVersion(1, 2, 3));
201 File.setTwoLevelNamespace();
202 File.setApplicationExtensionSafe();
203 File.setSwiftABIVersion(5);
204 File.setObjCConstraint(ObjCConstraintType::Retain_Release);
205 File.addAllowableClient("clientA", AK_x86_64);
206 File.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64);
207 File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", AK_i386);
208 File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", AK_i386,
209 SymbolFlags::WeakDefined);
210 File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", AK_i386,
211 SymbolFlags::ThreadLocalValue);
212 File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", AK_x86_64);
213 File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", AK_x86_64);
214 File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
215 AK_x86_64);
217 SmallString<4096> Buffer;
218 raw_svector_ostream OS(Buffer);
219 auto Result = TextAPIWriter::writeToStream(OS, File);
220 EXPECT_FALSE(Result);
221 EXPECT_STREQ(tbd_v2_file3, Buffer.c_str());
224 TEST(TBDv2, Platform_macOS) {
225 static const char tbd_v1_platform_macos[] = "--- !tapi-tbd-v2\n"
226 "archs: [ x86_64 ]\n"
227 "platform: macosx\n"
228 "install-name: Test.dylib\n"
229 "...\n";
231 auto Result =
232 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos, "Test.tbd"));
233 EXPECT_TRUE(!!Result);
234 auto File = std::move(Result.get());
235 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
236 EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
239 TEST(TBDv2, Platform_iOS) {
240 static const char tbd_v1_platform_ios[] = "--- !tapi-tbd-v2\n"
241 "archs: [ arm64 ]\n"
242 "platform: ios\n"
243 "install-name: Test.dylib\n"
244 "...\n";
246 auto Result =
247 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios, "Test.tbd"));
248 EXPECT_TRUE(!!Result);
249 auto File = std::move(Result.get());
250 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
251 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
254 TEST(TBDv2, Platform_watchOS) {
255 static const char tbd_v1_platform_watchos[] = "--- !tapi-tbd-v2\n"
256 "archs: [ armv7k ]\n"
257 "platform: watchos\n"
258 "install-name: Test.dylib\n"
259 "...\n";
261 auto Result =
262 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos, "Test.tbd"));
263 EXPECT_TRUE(!!Result);
264 auto File = std::move(Result.get());
265 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
266 EXPECT_EQ(PlatformKind::watchOS, File->getPlatform());
269 TEST(TBDv2, Platform_tvOS) {
270 static const char tbd_v1_platform_tvos[] = "--- !tapi-tbd-v2\n"
271 "archs: [ arm64 ]\n"
272 "platform: tvos\n"
273 "install-name: Test.dylib\n"
274 "...\n";
276 auto Result =
277 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos, "Test.tbd"));
278 EXPECT_TRUE(!!Result);
279 auto File = std::move(Result.get());
280 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
281 EXPECT_EQ(PlatformKind::tvOS, File->getPlatform());
284 TEST(TBDv2, Platform_bridgeOS) {
285 static const char tbd_v1_platform_bridgeos[] = "--- !tapi-tbd-v2\n"
286 "archs: [ armv7k ]\n"
287 "platform: bridgeos\n"
288 "install-name: Test.dylib\n"
289 "...\n";
291 auto Result =
292 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos, "Test.tbd"));
293 EXPECT_TRUE(!!Result);
294 auto File = std::move(Result.get());
295 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
296 EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
299 TEST(TBDv2, Swift_1_0) {
300 static const char tbd_v1_swift_1_0[] = "--- !tapi-tbd-v2\n"
301 "archs: [ arm64 ]\n"
302 "platform: ios\n"
303 "install-name: Test.dylib\n"
304 "swift-version: 1.0\n"
305 "...\n";
307 auto Result =
308 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0, "Test.tbd"));
309 EXPECT_TRUE(!!Result);
310 auto File = std::move(Result.get());
311 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
312 EXPECT_EQ(1U, File->getSwiftABIVersion());
315 TEST(TBDv2, Swift_1_1) {
316 static const char tbd_v1_swift_1_1[] = "--- !tapi-tbd-v2\n"
317 "archs: [ arm64 ]\n"
318 "platform: ios\n"
319 "install-name: Test.dylib\n"
320 "swift-version: 1.1\n"
321 "...\n";
323 auto Result =
324 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1, "Test.tbd"));
325 EXPECT_TRUE(!!Result);
326 auto File = std::move(Result.get());
327 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
328 EXPECT_EQ(2U, File->getSwiftABIVersion());
331 TEST(TBDv2, Swift_2_0) {
332 static const char tbd_v1_swift_2_0[] = "--- !tapi-tbd-v2\n"
333 "archs: [ arm64 ]\n"
334 "platform: ios\n"
335 "install-name: Test.dylib\n"
336 "swift-version: 2.0\n"
337 "...\n";
339 auto Result =
340 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0, "Test.tbd"));
341 EXPECT_TRUE(!!Result);
342 auto File = std::move(Result.get());
343 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
344 EXPECT_EQ(3U, File->getSwiftABIVersion());
347 TEST(TBDv2, Swift_3_0) {
348 static const char tbd_v1_swift_3_0[] = "--- !tapi-tbd-v2\n"
349 "archs: [ arm64 ]\n"
350 "platform: ios\n"
351 "install-name: Test.dylib\n"
352 "swift-version: 3.0\n"
353 "...\n";
355 auto Result =
356 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0, "Test.tbd"));
357 EXPECT_TRUE(!!Result);
358 auto File = std::move(Result.get());
359 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
360 EXPECT_EQ(4U, File->getSwiftABIVersion());
363 TEST(TBDv2, Swift_4_0) {
364 static const char tbd_v1_swift_4_0[] = "--- !tapi-tbd-v2\n"
365 "archs: [ arm64 ]\n"
366 "platform: ios\n"
367 "install-name: Test.dylib\n"
368 "swift-version: 4.0\n"
369 "...\n";
371 auto Result =
372 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0, "Test.tbd"));
373 EXPECT_FALSE(!!Result);
374 auto errorMessage = toString(Result.takeError());
375 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
376 "version.\nswift-version: 4.0\n ^~~\n",
377 errorMessage);
380 TEST(TBDv2, Swift_5) {
381 static const char tbd_v1_swift_5[] = "--- !tapi-tbd-v2\n"
382 "archs: [ arm64 ]\n"
383 "platform: ios\n"
384 "install-name: Test.dylib\n"
385 "swift-version: 5\n"
386 "...\n";
388 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5, "Test.tbd"));
389 EXPECT_TRUE(!!Result);
390 auto File = std::move(Result.get());
391 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
392 EXPECT_EQ(5U, File->getSwiftABIVersion());
395 TEST(TBDv2, Swift_99) {
396 static const char tbd_v1_swift_99[] = "--- !tapi-tbd-v2\n"
397 "archs: [ arm64 ]\n"
398 "platform: ios\n"
399 "install-name: Test.dylib\n"
400 "swift-version: 99\n"
401 "...\n";
403 auto Result =
404 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99, "Test.tbd"));
405 EXPECT_TRUE(!!Result);
406 auto File = std::move(Result.get());
407 EXPECT_EQ(FileType::TBD_V2, File->getFileType());
408 EXPECT_EQ(99U, File->getSwiftABIVersion());
411 TEST(TBDv2, UnknownArchitecture) {
412 static const char tbd_v2_file_unknown_architecture[] =
413 "--- !tapi-tbd-v2\n"
414 "archs: [ foo ]\n"
415 "platform: macosx\n"
416 "install-name: Test.dylib\n"
417 "...\n";
419 auto Result = TextAPIReader::get(
420 MemoryBufferRef(tbd_v2_file_unknown_architecture, "Test.tbd"));
421 EXPECT_TRUE(!!Result);
424 TEST(TBDv2, UnknownPlatform) {
425 static const char tbd_v2_file_unknown_platform[] = "--- !tapi-tbd-v2\n"
426 "archs: [ i386 ]\n"
427 "platform: newOS\n"
428 "...\n";
430 auto Result = TextAPIReader::get(
431 MemoryBufferRef(tbd_v2_file_unknown_platform, "Test.tbd"));
432 EXPECT_FALSE(!!Result);
433 auto errorMessage = toString(Result.takeError());
434 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
435 "newOS\n ^~~~~\n",
436 errorMessage);
439 TEST(TBDv2, MalformedFile1) {
440 static const char malformed_file1[] = "--- !tapi-tbd-v2\n"
441 "archs: [ arm64 ]\n"
442 "foobar: \"Unsupported key\"\n"
443 "...\n";
445 auto Result =
446 TextAPIReader::get(MemoryBufferRef(malformed_file1, "Test.tbd"));
447 EXPECT_FALSE(!!Result);
448 auto errorMessage = toString(Result.takeError());
449 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
450 "'platform'\narchs: [ arm64 ]\n^\n",
451 errorMessage);
454 TEST(TBDv2, MalformedFile2) {
455 static const char malformed_file2[] = "--- !tapi-tbd-v2\n"
456 "archs: [ arm64 ]\n"
457 "platform: ios\n"
458 "install-name: Test.dylib\n"
459 "foobar: \"Unsupported key\"\n"
460 "...\n";
462 auto Result =
463 TextAPIReader::get(MemoryBufferRef(malformed_file2, "Test.tbd"));
464 EXPECT_FALSE(!!Result);
465 auto errorMessage = toString(Result.takeError());
466 ASSERT_EQ(
467 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
468 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
469 errorMessage);
472 } // namespace TBDv2