[yaml2obj] - Allow overriding sh_entsize for SHT_GNU_versym sections.
[llvm-complete.git] / unittests / TextAPI / TextStubV1Tests.cpp
blob165e58f09d5893e3d02b69fd0e2ebb7786f9baab
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 Buffer = MemoryBuffer::getMemBuffer(tbd_v1_file1, "Test.tbd");
86 auto Result = TextAPIReader::get(std::move(Buffer));
87 EXPECT_TRUE(!!Result);
88 auto File = std::move(Result.get());
89 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
90 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
91 EXPECT_EQ(Archs, File->getArchitectures());
92 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
93 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
94 EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
95 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
96 EXPECT_EQ(2U, File->getSwiftABIVersion());
97 EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint());
98 EXPECT_TRUE(File->isTwoLevelNamespace());
99 EXPECT_TRUE(File->isApplicationExtensionSafe());
100 EXPECT_FALSE(File->isInstallAPI());
101 InterfaceFileRef client("clientA", Archs);
102 InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Archs);
103 EXPECT_EQ(1U, File->allowableClients().size());
104 EXPECT_EQ(client, File->allowableClients().front());
105 EXPECT_EQ(1U, File->reexportedLibraries().size());
106 EXPECT_EQ(reexport, File->reexportedLibraries().front());
108 ExportedSymbolSeq Exports;
109 for (const auto *Sym : File->symbols()) {
110 EXPECT_FALSE(Sym->isWeakReferenced());
111 EXPECT_FALSE(Sym->isUndefined());
112 Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(),
113 Sym->isWeakDefined(),
114 Sym->isThreadLocalValue()});
116 llvm::sort(Exports.begin(), Exports.end());
118 EXPECT_EQ(sizeof(TBDv1Symbols) / sizeof(ExportedSymbol), Exports.size());
119 EXPECT_TRUE(
120 std::equal(Exports.begin(), Exports.end(), std::begin(TBDv1Symbols)));
123 TEST(TBDv1, ReadFile2) {
124 static const char tbd_v1_file2[] = "--- !tapi-tbd-v1\n"
125 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
126 "platform: ios\n"
127 "install-name: Test.dylib\n"
128 "...\n";
130 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_file2, "Test.tbd");
131 auto Result = TextAPIReader::get(std::move(Buffer));
132 EXPECT_TRUE(!!Result);
133 auto File = std::move(Result.get());
134 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
135 auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
136 EXPECT_EQ(Archs, File->getArchitectures());
137 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
138 EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
139 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
140 EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
141 EXPECT_EQ(0U, File->getSwiftABIVersion());
142 EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint());
143 EXPECT_TRUE(File->isTwoLevelNamespace());
144 EXPECT_TRUE(File->isApplicationExtensionSafe());
145 EXPECT_FALSE(File->isInstallAPI());
146 EXPECT_EQ(0U, File->allowableClients().size());
147 EXPECT_EQ(0U, File->reexportedLibraries().size());
150 // Disable test for windows.
151 #ifndef _WIN32
152 TEST(TBDv1, WriteFile) {
153 static const char tbd_v1_file3[] =
154 "---\n"
155 "archs: [ i386, x86_64 ]\n"
156 "platform: macosx\n"
157 "install-name: '/usr/lib/libfoo.dylib'\n"
158 "current-version: 1.2.3\n"
159 "compatibility-version: 0\n"
160 "swift-version: 5\n"
161 "objc-constraint: retain_release\n"
162 "exports:\n"
163 " - archs: [ i386 ]\n"
164 " symbols: [ _sym1 ]\n"
165 " weak-def-symbols: [ _sym2 ]\n"
166 " thread-local-symbols: [ _sym3 ]\n"
167 " - archs: [ x86_64 ]\n"
168 " allowed-clients: [ clientA ]\n"
169 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
170 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
171 " objc-classes: [ _Class1 ]\n"
172 " objc-ivars: [ _Class1._ivar1 ]\n"
173 "...\n";
175 InterfaceFile File;
176 File.setPath("libfoo.dylib");
177 File.setInstallName("/usr/lib/libfoo.dylib");
178 File.setFileType(FileType::TBD_V1);
179 File.setArchitectures(AK_i386 | AK_x86_64);
180 File.setPlatform(PlatformKind::macOS);
181 File.setCurrentVersion(PackedVersion(1, 2, 3));
182 File.setSwiftABIVersion(5);
183 File.setObjCConstraint(ObjCConstraintType::Retain_Release);
184 File.addAllowableClient("clientA", AK_x86_64);
185 File.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64);
186 File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", AK_i386);
187 File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", AK_i386,
188 SymbolFlags::WeakDefined);
189 File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", AK_i386,
190 SymbolFlags::ThreadLocalValue);
191 File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", AK_x86_64);
192 File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", AK_x86_64);
193 File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
194 AK_x86_64);
196 SmallString<4096> Buffer;
197 raw_svector_ostream OS(Buffer);
198 auto Result = TextAPIWriter::writeToStream(OS, File);
199 EXPECT_FALSE(Result);
200 EXPECT_STREQ(tbd_v1_file3, Buffer.c_str());
203 TEST(TBDv1, Platform_macOS) {
204 static const char tbd_v1_platform_macos[] = "---\n"
205 "archs: [ x86_64 ]\n"
206 "platform: macosx\n"
207 "install-name: Test.dylib\n"
208 "...\n";
210 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_macos, "Test.tbd");
211 auto Result = TextAPIReader::get(std::move(Buffer));
212 EXPECT_TRUE(!!Result);
213 auto File = std::move(Result.get());
214 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
215 EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
217 #endif // _WIN32
219 TEST(TBDv1, Platform_iOS) {
220 static const char tbd_v1_platform_ios[] = "---\n"
221 "archs: [ arm64 ]\n"
222 "platform: ios\n"
223 "install-name: Test.dylib\n"
224 "...\n";
226 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_ios, "Test.tbd");
227 auto Result = TextAPIReader::get(std::move(Buffer));
228 EXPECT_TRUE(!!Result);
229 auto File = std::move(Result.get());
230 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
231 EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
234 TEST(TBDv1, Platform_watchOS) {
235 static const char tbd_v1_platform_watchos[] = "---\n"
236 "archs: [ armv7k ]\n"
237 "platform: watchos\n"
238 "install-name: Test.dylib\n"
239 "...\n";
241 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_watchos, "Test.tbd");
242 auto Result = TextAPIReader::get(std::move(Buffer));
243 EXPECT_TRUE(!!Result);
244 auto File = std::move(Result.get());
245 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
246 EXPECT_EQ(PlatformKind::watchOS, File->getPlatform());
249 TEST(TBDv1, Platform_tvOS) {
250 static const char tbd_v1_platform_tvos[] = "---\n"
251 "archs: [ arm64 ]\n"
252 "platform: tvos\n"
253 "install-name: Test.dylib\n"
254 "...\n";
256 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_tvos, "Test.tbd");
257 auto Result = TextAPIReader::get(std::move(Buffer));
258 EXPECT_TRUE(!!Result);
259 auto File = std::move(Result.get());
260 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
261 EXPECT_EQ(PlatformKind::tvOS, File->getPlatform());
264 TEST(TBDv1, Platform_bridgeOS) {
265 static const char tbd_v1_platform_bridgeos[] = "---\n"
266 "archs: [ armv7k ]\n"
267 "platform: bridgeos\n"
268 "install-name: Test.dylib\n"
269 "...\n";
271 auto Buffer =
272 MemoryBuffer::getMemBuffer(tbd_v1_platform_bridgeos, "Test.tbd");
273 auto Result = TextAPIReader::get(std::move(Buffer));
274 EXPECT_TRUE(!!Result);
275 auto File = std::move(Result.get());
276 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
277 EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
280 TEST(TBDv1, Swift_1_0) {
281 static const char tbd_v1_swift_1_0[] = "---\n"
282 "archs: [ arm64 ]\n"
283 "platform: ios\n"
284 "install-name: Test.dylib\n"
285 "swift-version: 1.0\n"
286 "...\n";
288 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_1_0, "Test.tbd");
289 auto Result = TextAPIReader::get(std::move(Buffer));
290 EXPECT_TRUE(!!Result);
291 auto File = std::move(Result.get());
292 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
293 EXPECT_EQ(1U, File->getSwiftABIVersion());
296 TEST(TBDv1, Swift_1_1) {
297 static const char tbd_v1_swift_1_1[] = "---\n"
298 "archs: [ arm64 ]\n"
299 "platform: ios\n"
300 "install-name: Test.dylib\n"
301 "swift-version: 1.1\n"
302 "...\n";
304 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_1_1, "Test.tbd");
305 auto Result = TextAPIReader::get(std::move(Buffer));
306 EXPECT_TRUE(!!Result);
307 auto File = std::move(Result.get());
308 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
309 EXPECT_EQ(2U, File->getSwiftABIVersion());
312 TEST(TBDv1, Swift_2_0) {
313 static const char tbd_v1_swift_2_0[] = "---\n"
314 "archs: [ arm64 ]\n"
315 "platform: ios\n"
316 "install-name: Test.dylib\n"
317 "swift-version: 2.0\n"
318 "...\n";
320 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_2_0, "Test.tbd");
321 auto Result = TextAPIReader::get(std::move(Buffer));
322 EXPECT_TRUE(!!Result);
323 auto File = std::move(Result.get());
324 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
325 EXPECT_EQ(3U, File->getSwiftABIVersion());
328 TEST(TBDv1, Swift_3_0) {
329 static const char tbd_v1_swift_3_0[] = "---\n"
330 "archs: [ arm64 ]\n"
331 "platform: ios\n"
332 "install-name: Test.dylib\n"
333 "swift-version: 3.0\n"
334 "...\n";
336 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_3_0, "Test.tbd");
337 auto Result = TextAPIReader::get(std::move(Buffer));
338 EXPECT_TRUE(!!Result);
339 auto File = std::move(Result.get());
340 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
341 EXPECT_EQ(4U, File->getSwiftABIVersion());
344 TEST(TBDv1, Swift_4_0) {
345 static const char tbd_v1_swift_4_0[] = "---\n"
346 "archs: [ arm64 ]\n"
347 "platform: ios\n"
348 "install-name: Test.dylib\n"
349 "swift-version: 4.0\n"
350 "...\n";
352 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_4_0, "Test.tbd");
353 auto Result = TextAPIReader::get(std::move(Buffer));
354 EXPECT_FALSE(!!Result);
355 auto errorMessage = toString(Result.takeError());
356 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
357 "version.\nswift-version: 4.0\n ^~~\n",
358 errorMessage);
361 TEST(TBDv1, Swift_5) {
362 static const char tbd_v1_swift_5[] = "---\n"
363 "archs: [ arm64 ]\n"
364 "platform: ios\n"
365 "install-name: Test.dylib\n"
366 "swift-version: 5\n"
367 "...\n";
369 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_5, "Test.tbd");
370 auto Result = TextAPIReader::get(std::move(Buffer));
371 EXPECT_TRUE(!!Result);
372 auto File = std::move(Result.get());
373 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
374 EXPECT_EQ(5U, File->getSwiftABIVersion());
377 TEST(TBDv1, Swift_99) {
378 static const char tbd_v1_swift_99[] = "---\n"
379 "archs: [ arm64 ]\n"
380 "platform: ios\n"
381 "install-name: Test.dylib\n"
382 "swift-version: 99\n"
383 "...\n";
385 auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_99, "Test.tbd");
386 auto Result = TextAPIReader::get(std::move(Buffer));
387 EXPECT_TRUE(!!Result);
388 auto File = std::move(Result.get());
389 EXPECT_EQ(FileType::TBD_V1, File->getFileType());
390 EXPECT_EQ(99U, File->getSwiftABIVersion());
393 TEST(TBDv1, UnknownArchitecture) {
394 static const char tbd_v1_file_unknown_architecture[] =
395 "---\n"
396 "archs: [ foo ]\n"
397 "platform: macosx\n"
398 "install-name: Test.dylib\n"
399 "...\n";
401 auto Buffer =
402 MemoryBuffer::getMemBuffer(tbd_v1_file_unknown_architecture, "Test.tbd");
403 auto Result = TextAPIReader::get(std::move(Buffer));
404 EXPECT_TRUE(!!Result);
407 TEST(TBDv1, UnknownPlatform) {
408 static const char tbd_v1_file_unknown_platform[] = "---\n"
409 "archs: [ i386 ]\n"
410 "platform: newOS\n"
411 "...\n";
413 auto Buffer =
414 MemoryBuffer::getMemBuffer(tbd_v1_file_unknown_platform, "Test.tbd");
415 auto Result = TextAPIReader::get(std::move(Buffer));
416 EXPECT_FALSE(!!Result);
417 auto errorMessage = toString(Result.takeError());
418 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
419 "newOS\n ^~~~~\n",
420 errorMessage);
423 TEST(TBDv1, MalformedFile1) {
424 static const char malformed_file1[] = "---\n"
425 "archs: [ arm64 ]\n"
426 "foobar: \"Unsupported key\"\n"
427 "...\n";
429 auto Buffer = MemoryBuffer::getMemBuffer(malformed_file1, "Test.tbd");
430 auto Result = TextAPIReader::get(std::move(Buffer));
431 EXPECT_FALSE(!!Result);
432 auto errorMessage = toString(Result.takeError());
433 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
434 "'platform'\narchs: [ arm64 ]\n^\n",
435 errorMessage);
438 TEST(TBDv1, MalformedFile2) {
439 static const char malformed_file2[] = "---\n"
440 "archs: [ arm64 ]\n"
441 "platform: ios\n"
442 "install-name: Test.dylib\n"
443 "foobar: \"Unsupported key\"\n"
444 "...\n";
446 auto Buffer = MemoryBuffer::getMemBuffer(malformed_file2, "Test.tbd");
447 auto Result = TextAPIReader::get(std::move(Buffer));
448 EXPECT_FALSE(!!Result);
449 auto errorMessage = toString(Result.takeError());
450 ASSERT_EQ(
451 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
452 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
453 errorMessage);
456 } // end namespace TBDv1.