OSDN Git Service

[CallSiteSplitting] Report edge deletion to DomTreeUpdater
[android-x86/external-llvm.git] / unittests / TextAPI / TextStubV1Tests.cpp
1 //===-- TextStubV1Tests.cpp - TBD V1 File Test ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
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>
15
16 using namespace llvm;
17 using namespace llvm::MachO;
18
19 struct ExportedSymbol {
20   SymbolKind Kind;
21   std::string Name;
22   bool WeakDefined;
23   bool ThreadLocalValue;
24 };
25 using ExportedSymbolSeq = std::vector<ExportedSymbol>;
26
27 inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
28   return std::tie(lhs.Kind, lhs.Name) < std::tie(rhs.Kind, rhs.Name);
29 }
30
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);
34 }
35
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},
55 };
56
57 namespace TBDv1 {
58
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";
84
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 = Architecture::armv7 | Architecture::armv7s |
91                Architecture::armv7k | Architecture::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::None, File->getObjCConstraint());
99   EXPECT_TRUE(File->isTwoLevelNamespace());
100   EXPECT_TRUE(File->isApplicationExtensionSafe());
101   EXPECT_FALSE(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());
108
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()});
116   }
117   llvm::sort(Exports.begin(), Exports.end());
118
119   EXPECT_EQ(sizeof(TBDv1Symbols) / sizeof(ExportedSymbol), Exports.size());
120   EXPECT_TRUE(
121       std::equal(Exports.begin(), Exports.end(), std::begin(TBDv1Symbols)));
122 }
123
124 TEST(TBDv1, ReadFile2) {
125   static const char tbd_v1_file2[] = "--- !tapi-tbd-v1\n"
126                                      "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
127                                      "platform: ios\n"
128                                      "install-name: Test.dylib\n"
129                                      "...\n";
130
131   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_file2, "Test.tbd");
132   auto Result = TextAPIReader::get(std::move(Buffer));
133   EXPECT_TRUE(!!Result);
134   auto File = std::move(Result.get());
135   EXPECT_EQ(FileType::TBD_V1, File->getFileType());
136   auto Archs = Architecture::armv7 | Architecture::armv7s |
137                Architecture::armv7k | Architecture::arm64;
138   EXPECT_EQ(Archs, File->getArchitectures());
139   EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
140   EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
141   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
142   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
143   EXPECT_EQ(0U, File->getSwiftABIVersion());
144   EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint());
145   EXPECT_TRUE(File->isTwoLevelNamespace());
146   EXPECT_TRUE(File->isApplicationExtensionSafe());
147   EXPECT_FALSE(File->isInstallAPI());
148   EXPECT_EQ(0U, File->allowableClients().size());
149   EXPECT_EQ(0U, File->reexportedLibraries().size());
150 }
151
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";
174
175   InterfaceFile File;
176   File.setPath("libfoo.dylib");
177   File.setInstallName("/usr/lib/libfoo.dylib");
178   File.setFileType(FileType::TBD_V1);
179   File.setArchitectures(Architecture::i386 | Architecture::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", Architecture::x86_64);
185   File.addReexportedLibrary("/usr/lib/libfoo.dylib", Architecture::x86_64);
186   File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", Architecture::i386);
187   File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", Architecture::i386,
188                  SymbolFlags::WeakDefined);
189   File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", Architecture::i386,
190                  SymbolFlags::ThreadLocalValue);
191   File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", Architecture::x86_64);
192   File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1",
193                  Architecture::x86_64);
194   File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
195                  Architecture::x86_64);
196
197   SmallString<4096> Buffer;
198   raw_svector_ostream OS(Buffer);
199   auto Result = TextAPIWriter::writeToStream(OS, File);
200   EXPECT_FALSE(Result);
201   EXPECT_STREQ(tbd_v1_file3, Buffer.c_str());
202 }
203
204 TEST(TBDv1, Platform_macOS) {
205   static const char tbd_v1_platform_macos[] = "---\n"
206                                               "archs: [ x86_64 ]\n"
207                                               "platform: macosx\n"
208                                               "install-name: Test.dylib\n"
209                                               "...\n";
210
211   auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_macos, "Test.tbd");
212   auto Result = TextAPIReader::get(std::move(Buffer));
213   EXPECT_TRUE(!!Result);
214   auto File = std::move(Result.get());
215   EXPECT_EQ(FileType::TBD_V1, File->getFileType());
216   EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
217 }
218
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";
225
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());
232 }
233
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";
240
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());
247 }
248
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";
255
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());
262 }
263
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";
270
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());
278 }
279
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";
287
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());
294 }
295
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";
303
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());
310 }
311
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";
319
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());
326 }
327
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";
335
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());
342 }
343
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";
351
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);
359 }
360
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";
368
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());
375 }
376
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";
384
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());
391 }
392
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";
400
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);
405 }
406
407 TEST(TBDv1, UnknownPlatform) {
408   static const char tbd_v1_file_unknown_platform[] = "---\n"
409                                                      "archs: [ i386 ]\n"
410                                                      "platform: newOS\n"
411                                                      "...\n";
412
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);
421 }
422
423 TEST(TBDv1, MalformedFile1) {
424   static const char malformed_file1[] = "---\n"
425                                         "archs: [ arm64 ]\n"
426                                         "foobar: \"Unsupported key\"\n"
427                                         "...\n";
428
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);
436 }
437
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";
445
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);
454 }
455
456 } // end namespace TBDv1.