[Alignment][NFC] Allow constexpr Align
[llvm-complete.git] / unittests / TextAPI / TextStubV1Tests.cpp
blob4472b1c599350d3219dbaea28e56a6f82258bebd
1 //===-- TextStubV1Tests.cpp - TBD V1 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 TBDv1Symbols[] = {
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 TBDv1 {
59 TEST(TBDv1, ReadFile) {
60 static const char tbd_v1_file1[] =
61 "---\n"
62 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
63 "platform: ios\n"
64 "install-name: Test.dylib\n"
65 "current-version: 2.3.4\n"
66 "compatibility-version: 1.0\n"
67 "swift-version: 1.1\n"
68 "exports:\n"
69 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
70 " allowed-clients: [ clientA ]\n"
71 " re-exports: [ /usr/lib/libfoo.dylib ]\n"
72 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
73 " objc-classes: [ _class1, _class2 ]\n"
74 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
75 " weak-def-symbols: [ _weak1, _weak2 ]\n"
76 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
77 " - archs: [ armv7, armv7s, armv7k ]\n"
78 " symbols: [ _sym5 ]\n"
79 " objc-classes: [ _class3 ]\n"
80 " objc-ivars: [ _class1._ivar3 ]\n"
81 " weak-def-symbols: [ _weak3 ]\n"
82 " thread-local-symbols: [ _tlv3 ]\n"
83 "...\n";
85 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_file1, "Test.tbd"));
86 EXPECT_TRUE(!!Result);
87 auto File = std::move(Result.get());
88 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
89 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
90 auto Platform = PlatformKind::iOS;
91 TargetList Targets;
92 for (auto &&arch : Archs)
93 Targets.emplace_back(Target(arch, Platform));
94 EXPECT_EQ(Archs, File->getArchitectures());
95 EXPECT_EQ(File->getPlatforms().size(), 1U);
96 EXPECT_EQ(Platform, *File->getPlatforms().begin());
97 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
98 EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
99 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
100 EXPECT_EQ(2U, File->getSwiftABIVersion());
101 EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint());
102 EXPECT_TRUE(File->isTwoLevelNamespace());
103 EXPECT_TRUE(File->isApplicationExtensionSafe());
104 EXPECT_FALSE(File->isInstallAPI());
105 InterfaceFileRef client("clientA", Targets);
106 InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Targets);
107 EXPECT_EQ(1U, File->allowableClients().size());
108 EXPECT_EQ(client, File->allowableClients().front());
109 EXPECT_EQ(1U, File->reexportedLibraries().size());
110 EXPECT_EQ(reexport, File->reexportedLibraries().front());
112 ExportedSymbolSeq Exports;
113 for (const auto *Sym : File->symbols()) {
114 EXPECT_FALSE(Sym->isWeakReferenced());
115 EXPECT_FALSE(Sym->isUndefined());
116 Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(),
117 Sym->isWeakDefined(),
118 Sym->isThreadLocalValue()});
120 llvm::sort(Exports.begin(), Exports.end());
122 EXPECT_EQ(sizeof(TBDv1Symbols) / sizeof(ExportedSymbol), Exports.size());
123 EXPECT_TRUE(
124 std::equal(Exports.begin(), Exports.end(), std::begin(TBDv1Symbols)));
126 File->addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", {Targets[1]});
127 File->addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
128 {Targets[1]});
131 TEST(TBDv1, ReadFile2) {
132 static const char tbd_v1_file2[] = "--- !tapi-tbd-v1\n"
133 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
134 "platform: ios\n"
135 "install-name: Test.dylib\n"
136 "...\n";
138 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_file2, "Test.tbd"));
139 EXPECT_TRUE(!!Result);
140 auto File = std::move(Result.get());
141 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
142 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
143 auto Platform = PlatformKind::iOS;
144 TargetList Targets;
145 for (auto &&arch : Archs)
146 Targets.emplace_back(Target(arch, Platform));
147 EXPECT_EQ(Archs, File->getArchitectures());
148 EXPECT_EQ(File->getPlatforms().size(), 1U);
149 EXPECT_EQ(Platform, *File->getPlatforms().begin());
150 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
151 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
152 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
153 EXPECT_EQ(0U, File->getSwiftABIVersion());
154 EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint());
155 EXPECT_TRUE(File->isTwoLevelNamespace());
156 EXPECT_TRUE(File->isApplicationExtensionSafe());
157 EXPECT_FALSE(File->isInstallAPI());
158 EXPECT_EQ(0U, File->allowableClients().size());
159 EXPECT_EQ(0U, File->reexportedLibraries().size());
162 TEST(TBDv1, WriteFile) {
163 static const char tbd_v1_file3[] =
164 "---\n"
165 "archs: [ i386, x86_64 ]\n"
166 "platform: macosx\n"
167 "install-name: '/usr/lib/libfoo.dylib'\n"
168 "current-version: 1.2.3\n"
169 "compatibility-version: 0\n"
170 "swift-version: 5\n"
171 "objc-constraint: retain_release\n"
172 "exports:\n"
173 " - archs: [ i386 ]\n"
174 " symbols: [ _sym1 ]\n"
175 " weak-def-symbols: [ _sym2 ]\n"
176 " thread-local-symbols: [ _sym3 ]\n"
177 " - archs: [ x86_64 ]\n"
178 " allowed-clients: [ clientA ]\n"
179 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
180 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
181 " objc-classes: [ _Class1 ]\n"
182 " objc-ivars: [ _Class1._ivar1 ]\n"
183 "...\n";
185 InterfaceFile File;
186 TargetList Targets;
187 for (auto &&arch : AK_i386 | AK_x86_64)
188 Targets.emplace_back(Target(arch, PlatformKind::macOS));
189 File.setPath("libfoo.dylib");
190 File.setInstallName("/usr/lib/libfoo.dylib");
191 File.setFileType(FileType::TBD_V1);
192 File.addTargets(Targets);
193 File.setCurrentVersion(PackedVersion(1, 2, 3));
194 File.setSwiftABIVersion(5);
195 File.setObjCConstraint(ObjCConstraintType::Retain_Release);
196 File.addAllowableClient("clientA", Targets[1]);
197 File.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets[1]);
198 File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", {Targets[0]});
199 File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", {Targets[0]},
200 SymbolFlags::WeakDefined);
201 File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", {Targets[0]},
202 SymbolFlags::ThreadLocalValue);
203 File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", {Targets[1]});
204 File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", {Targets[1]});
205 File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
206 {Targets[1]});
208 SmallString<4096> Buffer;
209 raw_svector_ostream OS(Buffer);
210 auto Result = TextAPIWriter::writeToStream(OS, File);
211 EXPECT_FALSE(Result);
212 EXPECT_STREQ(tbd_v1_file3, Buffer.c_str());
215 TEST(TBDv1, Platform_macOS) {
216 static const char tbd_v1_platform_macos[] = "---\n"
217 "archs: [ x86_64 ]\n"
218 "platform: macosx\n"
219 "install-name: Test.dylib\n"
220 "...\n";
222 auto Result =
223 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos, "Test.tbd"));
224 EXPECT_TRUE(!!Result);
225 auto Platform = PlatformKind::macOS;
226 auto File = std::move(Result.get());
227 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
228 EXPECT_EQ(File->getPlatforms().size(), 1U);
229 EXPECT_EQ(Platform, *File->getPlatforms().begin());
232 TEST(TBDv1, Platform_iOS) {
233 static const char tbd_v1_platform_ios[] = "---\n"
234 "archs: [ arm64 ]\n"
235 "platform: ios\n"
236 "install-name: Test.dylib\n"
237 "...\n";
239 auto Result =
240 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios, "Test.tbd"));
241 EXPECT_TRUE(!!Result);
242 auto Platform = PlatformKind::iOS;
243 auto File = std::move(Result.get());
244 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
245 EXPECT_EQ(File->getPlatforms().size(), 1U);
246 EXPECT_EQ(Platform, *File->getPlatforms().begin());
249 TEST(TBDv1, Platform_watchOS) {
250 static const char tbd_v1_platform_watchos[] = "---\n"
251 "archs: [ armv7k ]\n"
252 "platform: watchos\n"
253 "install-name: Test.dylib\n"
254 "...\n";
256 auto Result =
257 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos, "Test.tbd"));
258 EXPECT_TRUE(!!Result);
259 auto Platform = PlatformKind::watchOS;
260 auto File = std::move(Result.get());
261 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
262 EXPECT_EQ(File->getPlatforms().size(), 1U);
263 EXPECT_EQ(Platform, *File->getPlatforms().begin());
266 TEST(TBDv1, Platform_tvOS) {
267 static const char tbd_v1_platform_tvos[] = "---\n"
268 "archs: [ arm64 ]\n"
269 "platform: tvos\n"
270 "install-name: Test.dylib\n"
271 "...\n";
273 auto Result =
274 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos, "Test.tbd"));
275 EXPECT_TRUE(!!Result);
276 auto Platform = PlatformKind::tvOS;
277 auto File = std::move(Result.get());
278 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
279 EXPECT_EQ(File->getPlatforms().size(), 1U);
280 EXPECT_EQ(Platform, *File->getPlatforms().begin());
283 TEST(TBDv1, Platform_bridgeOS) {
284 static const char tbd_v1_platform_bridgeos[] = "---\n"
285 "archs: [ armv7k ]\n"
286 "platform: bridgeos\n"
287 "install-name: Test.dylib\n"
288 "...\n";
290 auto Result =
291 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos, "Test.tbd"));
292 EXPECT_TRUE(!!Result);
293 auto Platform = PlatformKind::bridgeOS;
294 auto File = std::move(Result.get());
295 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
296 EXPECT_EQ(File->getPlatforms().size(), 1U);
297 EXPECT_EQ(Platform, *File->getPlatforms().begin());
300 TEST(TBDv1, Swift_1_0) {
301 static const char tbd_v1_swift_1_0[] = "---\n"
302 "archs: [ arm64 ]\n"
303 "platform: ios\n"
304 "install-name: Test.dylib\n"
305 "swift-version: 1.0\n"
306 "...\n";
308 auto Result =
309 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0, "Test.tbd"));
310 EXPECT_TRUE(!!Result);
311 auto File = std::move(Result.get());
312 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
313 EXPECT_EQ(1U, File->getSwiftABIVersion());
316 TEST(TBDv1, Swift_1_1) {
317 static const char tbd_v1_swift_1_1[] = "---\n"
318 "archs: [ arm64 ]\n"
319 "platform: ios\n"
320 "install-name: Test.dylib\n"
321 "swift-version: 1.1\n"
322 "...\n";
324 auto Result =
325 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1, "Test.tbd"));
326 EXPECT_TRUE(!!Result);
327 auto File = std::move(Result.get());
328 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
329 EXPECT_EQ(2U, File->getSwiftABIVersion());
332 TEST(TBDv1, Swift_2_0) {
333 static const char tbd_v1_swift_2_0[] = "---\n"
334 "archs: [ arm64 ]\n"
335 "platform: ios\n"
336 "install-name: Test.dylib\n"
337 "swift-version: 2.0\n"
338 "...\n";
340 auto Result =
341 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0, "Test.tbd"));
342 EXPECT_TRUE(!!Result);
343 auto File = std::move(Result.get());
344 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
345 EXPECT_EQ(3U, File->getSwiftABIVersion());
348 TEST(TBDv1, Swift_3_0) {
349 static const char tbd_v1_swift_3_0[] = "---\n"
350 "archs: [ arm64 ]\n"
351 "platform: ios\n"
352 "install-name: Test.dylib\n"
353 "swift-version: 3.0\n"
354 "...\n";
356 auto Result =
357 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0, "Test.tbd"));
358 EXPECT_TRUE(!!Result);
359 auto File = std::move(Result.get());
360 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
361 EXPECT_EQ(4U, File->getSwiftABIVersion());
364 TEST(TBDv1, Swift_4_0) {
365 static const char tbd_v1_swift_4_0[] = "---\n"
366 "archs: [ arm64 ]\n"
367 "platform: ios\n"
368 "install-name: Test.dylib\n"
369 "swift-version: 4.0\n"
370 "...\n";
372 auto Result =
373 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0, "Test.tbd"));
374 EXPECT_FALSE(!!Result);
375 auto errorMessage = toString(Result.takeError());
376 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
377 "version.\nswift-version: 4.0\n ^~~\n",
378 errorMessage);
381 TEST(TBDv1, Swift_5) {
382 static const char tbd_v1_swift_5[] = "---\n"
383 "archs: [ arm64 ]\n"
384 "platform: ios\n"
385 "install-name: Test.dylib\n"
386 "swift-version: 5\n"
387 "...\n";
389 auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5, "Test.tbd"));
390 EXPECT_TRUE(!!Result);
391 auto File = std::move(Result.get());
392 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
393 EXPECT_EQ(5U, File->getSwiftABIVersion());
396 TEST(TBDv1, Swift_99) {
397 static const char tbd_v1_swift_99[] = "---\n"
398 "archs: [ arm64 ]\n"
399 "platform: ios\n"
400 "install-name: Test.dylib\n"
401 "swift-version: 99\n"
402 "...\n";
404 auto Result =
405 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99, "Test.tbd"));
406 EXPECT_TRUE(!!Result);
407 auto File = std::move(Result.get());
408 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
409 EXPECT_EQ(99U, File->getSwiftABIVersion());
412 TEST(TBDv1, UnknownArchitecture) {
413 static const char tbd_v1_file_unknown_architecture[] =
414 "---\n"
415 "archs: [ foo ]\n"
416 "platform: macosx\n"
417 "install-name: Test.dylib\n"
418 "...\n";
420 auto Result = TextAPIReader::get(
421 MemoryBufferRef(tbd_v1_file_unknown_architecture, "Test.tbd"));
422 EXPECT_TRUE(!!Result);
425 TEST(TBDv1, UnknownPlatform) {
426 static const char tbd_v1_file_unknown_platform[] = "---\n"
427 "archs: [ i386 ]\n"
428 "platform: newOS\n"
429 "...\n";
431 auto Result = TextAPIReader::get(
432 MemoryBufferRef(tbd_v1_file_unknown_platform, "Test.tbd"));
433 EXPECT_FALSE(!!Result);
434 auto errorMessage = toString(Result.takeError());
435 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
436 "newOS\n ^~~~~\n",
437 errorMessage);
440 TEST(TBDv1, MalformedFile1) {
441 static const char malformed_file1[] = "---\n"
442 "archs: [ arm64 ]\n"
443 "foobar: \"Unsupported key\"\n"
444 "...\n";
446 auto Result =
447 TextAPIReader::get(MemoryBufferRef(malformed_file1, "Test.tbd"));
448 EXPECT_FALSE(!!Result);
449 auto errorMessage = toString(Result.takeError());
450 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
451 "'platform'\narchs: [ arm64 ]\n^\n",
452 errorMessage);
455 TEST(TBDv1, MalformedFile2) {
456 static const char malformed_file2[] = "---\n"
457 "archs: [ arm64 ]\n"
458 "platform: ios\n"
459 "install-name: Test.dylib\n"
460 "foobar: \"Unsupported key\"\n"
461 "...\n";
463 auto Result =
464 TextAPIReader::get(MemoryBufferRef(malformed_file2, "Test.tbd"));
465 EXPECT_FALSE(!!Result);
466 auto errorMessage = toString(Result.takeError());
467 ASSERT_EQ(
468 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
469 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
470 errorMessage);
473 } // end namespace TBDv1.