TextStub.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. //===- TextStub.cpp -------------------------------------------------------===//
  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. //===----------------------------------------------------------------------===//
  8. //
  9. // Implements the text stub file reader/writer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "TextAPIContext.h"
  13. #include "TextStubCommon.h"
  14. #include "llvm/ADT/BitmaskEnum.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Support/Allocator.h"
  18. #include "llvm/Support/SourceMgr.h"
  19. #include "llvm/Support/YAMLTraits.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include "llvm/TextAPI/MachO/Architecture.h"
  22. #include "llvm/TextAPI/MachO/ArchitectureSet.h"
  23. #include "llvm/TextAPI/MachO/InterfaceFile.h"
  24. #include "llvm/TextAPI/MachO/PackedVersion.h"
  25. #include "llvm/TextAPI/MachO/TextAPIReader.h"
  26. #include "llvm/TextAPI/MachO/TextAPIWriter.h"
  27. #include <algorithm>
  28. #include <set>
  29. // clang-format off
  30. /*
  31. YAML Format specification.
  32. The TBD v1 format only support two level address libraries and is per
  33. definition application extension safe.
  34. --- # the tag !tapi-tbd-v1 is optional and
  35. # shouldn't be emitted to support older linker.
  36. archs: [ armv7, armv7s, arm64 ] # the list of architecture slices that are
  37. # supported by this file.
  38. platform: ios # Specifies the platform (macosx, ios, etc)
  39. install-name: /u/l/libfoo.dylib #
  40. current-version: 1.2.3 # Optional: defaults to 1.0
  41. compatibility-version: 1.0 # Optional: defaults to 1.0
  42. swift-version: 0 # Optional: defaults to 0
  43. objc-constraint: none # Optional: defaults to none
  44. exports: # List of export sections
  45. ...
  46. Each export section is defined as following:
  47. - archs: [ arm64 ] # the list of architecture slices
  48. allowed-clients: [ client ] # Optional: List of clients
  49. re-exports: [ ] # Optional: List of re-exports
  50. symbols: [ _sym ] # Optional: List of symbols
  51. objc-classes: [] # Optional: List of Objective-C classes
  52. objc-ivars: [] # Optional: List of Objective C Instance
  53. # Variables
  54. weak-def-symbols: [] # Optional: List of weak defined symbols
  55. thread-local-symbols: [] # Optional: List of thread local symbols
  56. */
  57. /*
  58. YAML Format specification.
  59. --- !tapi-tbd-v2
  60. archs: [ armv7, armv7s, arm64 ] # the list of architecture slices that are
  61. # supported by this file.
  62. uuids: [ armv7:... ] # Optional: List of architecture and UUID pairs.
  63. platform: ios # Specifies the platform (macosx, ios, etc)
  64. flags: [] # Optional:
  65. install-name: /u/l/libfoo.dylib #
  66. current-version: 1.2.3 # Optional: defaults to 1.0
  67. compatibility-version: 1.0 # Optional: defaults to 1.0
  68. swift-version: 0 # Optional: defaults to 0
  69. objc-constraint: retain_release # Optional: defaults to retain_release
  70. parent-umbrella: # Optional:
  71. exports: # List of export sections
  72. ...
  73. undefineds: # List of undefineds sections
  74. ...
  75. Each export section is defined as following:
  76. - archs: [ arm64 ] # the list of architecture slices
  77. allowed-clients: [ client ] # Optional: List of clients
  78. re-exports: [ ] # Optional: List of re-exports
  79. symbols: [ _sym ] # Optional: List of symbols
  80. objc-classes: [] # Optional: List of Objective-C classes
  81. objc-ivars: [] # Optional: List of Objective C Instance
  82. # Variables
  83. weak-def-symbols: [] # Optional: List of weak defined symbols
  84. thread-local-symbols: [] # Optional: List of thread local symbols
  85. Each undefineds section is defined as following:
  86. - archs: [ arm64 ] # the list of architecture slices
  87. symbols: [ _sym ] # Optional: List of symbols
  88. objc-classes: [] # Optional: List of Objective-C classes
  89. objc-ivars: [] # Optional: List of Objective C Instance Variables
  90. weak-ref-symbols: [] # Optional: List of weak defined symbols
  91. */
  92. /*
  93. YAML Format specification.
  94. --- !tapi-tbd-v3
  95. archs: [ armv7, armv7s, arm64 ] # the list of architecture slices that are
  96. # supported by this file.
  97. uuids: [ armv7:... ] # Optional: List of architecture and UUID pairs.
  98. platform: ios # Specifies the platform (macosx, ios, etc)
  99. flags: [] # Optional:
  100. install-name: /u/l/libfoo.dylib #
  101. current-version: 1.2.3 # Optional: defaults to 1.0
  102. compatibility-version: 1.0 # Optional: defaults to 1.0
  103. swift-abi-version: 0 # Optional: defaults to 0
  104. objc-constraint: retain_release # Optional: defaults to retain_release
  105. parent-umbrella: # Optional:
  106. exports: # List of export sections
  107. ...
  108. undefineds: # List of undefineds sections
  109. ...
  110. Each export section is defined as following:
  111. - archs: [ arm64 ] # the list of architecture slices
  112. allowed-clients: [ client ] # Optional: List of clients
  113. re-exports: [ ] # Optional: List of re-exports
  114. symbols: [ _sym ] # Optional: List of symbols
  115. objc-classes: [] # Optional: List of Objective-C classes
  116. objc-eh-types: [] # Optional: List of Objective-C classes
  117. # with EH
  118. objc-ivars: [] # Optional: List of Objective C Instance
  119. # Variables
  120. weak-def-symbols: [] # Optional: List of weak defined symbols
  121. thread-local-symbols: [] # Optional: List of thread local symbols
  122. Each undefineds section is defined as following:
  123. - archs: [ arm64 ] # the list of architecture slices
  124. symbols: [ _sym ] # Optional: List of symbols
  125. objc-classes: [] # Optional: List of Objective-C classes
  126. objc-eh-types: [] # Optional: List of Objective-C classes
  127. # with EH
  128. objc-ivars: [] # Optional: List of Objective C Instance Variables
  129. weak-ref-symbols: [] # Optional: List of weak defined symbols
  130. */
  131. /*
  132. YAML Format specification.
  133. --- !tapi-tbd
  134. tbd-version: 4 # The tbd version for format
  135. targets: [ armv7-ios, x86_64-maccatalyst ] # The list of applicable tapi supported target triples
  136. uuids: # Optional: List of target and UUID pairs.
  137. - target: armv7-ios
  138. value: ...
  139. - target: x86_64-maccatalyst
  140. value: ...
  141. flags: [] # Optional:
  142. install-name: /u/l/libfoo.dylib #
  143. current-version: 1.2.3 # Optional: defaults to 1.0
  144. compatibility-version: 1.0 # Optional: defaults to 1.0
  145. swift-abi-version: 0 # Optional: defaults to 0
  146. parent-umbrella: # Optional:
  147. allowable-clients:
  148. - targets: [ armv7-ios ] # Optional:
  149. clients: [ clientA ]
  150. exports: # List of export sections
  151. ...
  152. re-exports: # List of reexport sections
  153. ...
  154. undefineds: # List of undefineds sections
  155. ...
  156. Each export and reexport section is defined as following:
  157. - targets: [ arm64-macos ] # The list of target triples associated with symbols
  158. symbols: [ _symA ] # Optional: List of symbols
  159. objc-classes: [] # Optional: List of Objective-C classes
  160. objc-eh-types: [] # Optional: List of Objective-C classes
  161. # with EH
  162. objc-ivars: [] # Optional: List of Objective C Instance
  163. # Variables
  164. weak-symbols: [] # Optional: List of weak defined symbols
  165. thread-local-symbols: [] # Optional: List of thread local symbols
  166. - targets: [ arm64-macos, x86_64-maccatalyst ] # Optional: Targets for applicable additional symbols
  167. symbols: [ _symB ] # Optional: List of symbols
  168. Each undefineds section is defined as following:
  169. - targets: [ arm64-macos ] # The list of target triples associated with symbols
  170. symbols: [ _symC ] # Optional: List of symbols
  171. objc-classes: [] # Optional: List of Objective-C classes
  172. objc-eh-types: [] # Optional: List of Objective-C classes
  173. # with EH
  174. objc-ivars: [] # Optional: List of Objective C Instance Variables
  175. weak-symbols: [] # Optional: List of weak defined symbols
  176. */
  177. // clang-format on
  178. using namespace llvm;
  179. using namespace llvm::yaml;
  180. using namespace llvm::MachO;
  181. namespace {
  182. struct ExportSection {
  183. std::vector<Architecture> Architectures;
  184. std::vector<FlowStringRef> AllowableClients;
  185. std::vector<FlowStringRef> ReexportedLibraries;
  186. std::vector<FlowStringRef> Symbols;
  187. std::vector<FlowStringRef> Classes;
  188. std::vector<FlowStringRef> ClassEHs;
  189. std::vector<FlowStringRef> IVars;
  190. std::vector<FlowStringRef> WeakDefSymbols;
  191. std::vector<FlowStringRef> TLVSymbols;
  192. };
  193. struct UndefinedSection {
  194. std::vector<Architecture> Architectures;
  195. std::vector<FlowStringRef> Symbols;
  196. std::vector<FlowStringRef> Classes;
  197. std::vector<FlowStringRef> ClassEHs;
  198. std::vector<FlowStringRef> IVars;
  199. std::vector<FlowStringRef> WeakRefSymbols;
  200. };
  201. // Sections for direct target mapping in TBDv4
  202. struct SymbolSection {
  203. TargetList Targets;
  204. std::vector<FlowStringRef> Symbols;
  205. std::vector<FlowStringRef> Classes;
  206. std::vector<FlowStringRef> ClassEHs;
  207. std::vector<FlowStringRef> Ivars;
  208. std::vector<FlowStringRef> WeakSymbols;
  209. std::vector<FlowStringRef> TlvSymbols;
  210. };
  211. struct MetadataSection {
  212. enum Option { Clients, Libraries };
  213. std::vector<Target> Targets;
  214. std::vector<FlowStringRef> Values;
  215. };
  216. struct UmbrellaSection {
  217. std::vector<Target> Targets;
  218. std::string Umbrella;
  219. };
  220. // UUID's for TBDv4 are mapped to target not arch
  221. struct UUIDv4 {
  222. Target TargetID;
  223. std::string Value;
  224. UUIDv4() = default;
  225. UUIDv4(const Target &TargetID, const std::string &Value)
  226. : TargetID(TargetID), Value(Value) {}
  227. };
  228. // clang-format off
  229. enum TBDFlags : unsigned {
  230. None = 0U,
  231. FlatNamespace = 1U << 0,
  232. NotApplicationExtensionSafe = 1U << 1,
  233. InstallAPI = 1U << 2,
  234. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/InstallAPI),
  235. };
  236. // clang-format on
  237. } // end anonymous namespace.
  238. LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Architecture)
  239. LLVM_YAML_IS_SEQUENCE_VECTOR(ExportSection)
  240. LLVM_YAML_IS_SEQUENCE_VECTOR(UndefinedSection)
  241. // Specific to TBDv4
  242. LLVM_YAML_IS_SEQUENCE_VECTOR(SymbolSection)
  243. LLVM_YAML_IS_SEQUENCE_VECTOR(MetadataSection)
  244. LLVM_YAML_IS_SEQUENCE_VECTOR(UmbrellaSection)
  245. LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Target)
  246. LLVM_YAML_IS_SEQUENCE_VECTOR(UUIDv4)
  247. namespace llvm {
  248. namespace yaml {
  249. template <> struct MappingTraits<ExportSection> {
  250. static void mapping(IO &IO, ExportSection &Section) {
  251. const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  252. assert((!Ctx || (Ctx && Ctx->FileKind != FileType::Invalid)) &&
  253. "File type is not set in YAML context");
  254. IO.mapRequired("archs", Section.Architectures);
  255. if (Ctx->FileKind == FileType::TBD_V1)
  256. IO.mapOptional("allowed-clients", Section.AllowableClients);
  257. else
  258. IO.mapOptional("allowable-clients", Section.AllowableClients);
  259. IO.mapOptional("re-exports", Section.ReexportedLibraries);
  260. IO.mapOptional("symbols", Section.Symbols);
  261. IO.mapOptional("objc-classes", Section.Classes);
  262. if (Ctx->FileKind == FileType::TBD_V3)
  263. IO.mapOptional("objc-eh-types", Section.ClassEHs);
  264. IO.mapOptional("objc-ivars", Section.IVars);
  265. IO.mapOptional("weak-def-symbols", Section.WeakDefSymbols);
  266. IO.mapOptional("thread-local-symbols", Section.TLVSymbols);
  267. }
  268. };
  269. template <> struct MappingTraits<UndefinedSection> {
  270. static void mapping(IO &IO, UndefinedSection &Section) {
  271. const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  272. assert((!Ctx || (Ctx && Ctx->FileKind != FileType::Invalid)) &&
  273. "File type is not set in YAML context");
  274. IO.mapRequired("archs", Section.Architectures);
  275. IO.mapOptional("symbols", Section.Symbols);
  276. IO.mapOptional("objc-classes", Section.Classes);
  277. if (Ctx->FileKind == FileType::TBD_V3)
  278. IO.mapOptional("objc-eh-types", Section.ClassEHs);
  279. IO.mapOptional("objc-ivars", Section.IVars);
  280. IO.mapOptional("weak-ref-symbols", Section.WeakRefSymbols);
  281. }
  282. };
  283. template <> struct MappingTraits<SymbolSection> {
  284. static void mapping(IO &IO, SymbolSection &Section) {
  285. IO.mapRequired("targets", Section.Targets);
  286. IO.mapOptional("symbols", Section.Symbols);
  287. IO.mapOptional("objc-classes", Section.Classes);
  288. IO.mapOptional("objc-eh-types", Section.ClassEHs);
  289. IO.mapOptional("objc-ivars", Section.Ivars);
  290. IO.mapOptional("weak-symbols", Section.WeakSymbols);
  291. IO.mapOptional("thread-local-symbols", Section.TlvSymbols);
  292. }
  293. };
  294. template <> struct MappingTraits<UmbrellaSection> {
  295. static void mapping(IO &IO, UmbrellaSection &Section) {
  296. IO.mapRequired("targets", Section.Targets);
  297. IO.mapRequired("umbrella", Section.Umbrella);
  298. }
  299. };
  300. template <> struct MappingTraits<UUIDv4> {
  301. static void mapping(IO &IO, UUIDv4 &UUID) {
  302. IO.mapRequired("target", UUID.TargetID);
  303. IO.mapRequired("value", UUID.Value);
  304. }
  305. };
  306. template <>
  307. struct MappingContextTraits<MetadataSection, MetadataSection::Option> {
  308. static void mapping(IO &IO, MetadataSection &Section,
  309. MetadataSection::Option &OptionKind) {
  310. IO.mapRequired("targets", Section.Targets);
  311. switch (OptionKind) {
  312. case MetadataSection::Option::Clients:
  313. IO.mapRequired("clients", Section.Values);
  314. return;
  315. case MetadataSection::Option::Libraries:
  316. IO.mapRequired("libraries", Section.Values);
  317. return;
  318. }
  319. llvm_unreachable("unexpected option for metadata");
  320. }
  321. };
  322. template <> struct ScalarBitSetTraits<TBDFlags> {
  323. static void bitset(IO &IO, TBDFlags &Flags) {
  324. IO.bitSetCase(Flags, "flat_namespace", TBDFlags::FlatNamespace);
  325. IO.bitSetCase(Flags, "not_app_extension_safe",
  326. TBDFlags::NotApplicationExtensionSafe);
  327. IO.bitSetCase(Flags, "installapi", TBDFlags::InstallAPI);
  328. }
  329. };
  330. template <> struct ScalarTraits<Target> {
  331. static void output(const Target &Value, void *, raw_ostream &OS) {
  332. OS << Value.Arch << "-";
  333. switch (Value.Platform) {
  334. default:
  335. OS << "unknown";
  336. break;
  337. case PlatformKind::macOS:
  338. OS << "macos";
  339. break;
  340. case PlatformKind::iOS:
  341. OS << "ios";
  342. break;
  343. case PlatformKind::tvOS:
  344. OS << "tvos";
  345. break;
  346. case PlatformKind::watchOS:
  347. OS << "watchos";
  348. break;
  349. case PlatformKind::bridgeOS:
  350. OS << "bridgeos";
  351. break;
  352. case PlatformKind::macCatalyst:
  353. OS << "maccatalyst";
  354. break;
  355. case PlatformKind::iOSSimulator:
  356. OS << "ios-simulator";
  357. break;
  358. case PlatformKind::tvOSSimulator:
  359. OS << "tvos-simulator";
  360. break;
  361. case PlatformKind::watchOSSimulator:
  362. OS << "watchos-simulator";
  363. break;
  364. }
  365. }
  366. static StringRef input(StringRef Scalar, void *, Target &Value) {
  367. auto Result = Target::create(Scalar);
  368. if (!Result)
  369. return toString(Result.takeError());
  370. Value = *Result;
  371. if (Value.Arch == AK_unknown)
  372. return "unknown architecture";
  373. if (Value.Platform == PlatformKind::unknown)
  374. return "unknown platform";
  375. return {};
  376. }
  377. static QuotingType mustQuote(StringRef) { return QuotingType::None; }
  378. };
  379. template <> struct MappingTraits<const InterfaceFile *> {
  380. struct NormalizedTBD {
  381. explicit NormalizedTBD(IO &IO) {}
  382. NormalizedTBD(IO &IO, const InterfaceFile *&File) {
  383. Architectures = File->getArchitectures();
  384. UUIDs = File->uuids();
  385. Platforms = File->getPlatforms();
  386. InstallName = File->getInstallName();
  387. CurrentVersion = PackedVersion(File->getCurrentVersion());
  388. CompatibilityVersion = PackedVersion(File->getCompatibilityVersion());
  389. SwiftABIVersion = File->getSwiftABIVersion();
  390. ObjCConstraint = File->getObjCConstraint();
  391. Flags = TBDFlags::None;
  392. if (!File->isApplicationExtensionSafe())
  393. Flags |= TBDFlags::NotApplicationExtensionSafe;
  394. if (!File->isTwoLevelNamespace())
  395. Flags |= TBDFlags::FlatNamespace;
  396. if (File->isInstallAPI())
  397. Flags |= TBDFlags::InstallAPI;
  398. for (const auto &Iter : File->umbrellas()) {
  399. ParentUmbrella = Iter.second;
  400. break;
  401. }
  402. std::set<ArchitectureSet> ArchSet;
  403. for (const auto &Library : File->allowableClients())
  404. ArchSet.insert(Library.getArchitectures());
  405. for (const auto &Library : File->reexportedLibraries())
  406. ArchSet.insert(Library.getArchitectures());
  407. std::map<const Symbol *, ArchitectureSet> SymbolToArchSet;
  408. for (const auto *Symbol : File->exports()) {
  409. auto Architectures = Symbol->getArchitectures();
  410. SymbolToArchSet[Symbol] = Architectures;
  411. ArchSet.insert(Architectures);
  412. }
  413. for (auto Architectures : ArchSet) {
  414. ExportSection Section;
  415. Section.Architectures = Architectures;
  416. for (const auto &Library : File->allowableClients())
  417. if (Library.getArchitectures() == Architectures)
  418. Section.AllowableClients.emplace_back(Library.getInstallName());
  419. for (const auto &Library : File->reexportedLibraries())
  420. if (Library.getArchitectures() == Architectures)
  421. Section.ReexportedLibraries.emplace_back(Library.getInstallName());
  422. for (const auto &SymArch : SymbolToArchSet) {
  423. if (SymArch.second != Architectures)
  424. continue;
  425. const auto *Symbol = SymArch.first;
  426. switch (Symbol->getKind()) {
  427. case SymbolKind::GlobalSymbol:
  428. if (Symbol->isWeakDefined())
  429. Section.WeakDefSymbols.emplace_back(Symbol->getName());
  430. else if (Symbol->isThreadLocalValue())
  431. Section.TLVSymbols.emplace_back(Symbol->getName());
  432. else
  433. Section.Symbols.emplace_back(Symbol->getName());
  434. break;
  435. case SymbolKind::ObjectiveCClass:
  436. if (File->getFileType() != FileType::TBD_V3)
  437. Section.Classes.emplace_back(
  438. copyString("_" + Symbol->getName().str()));
  439. else
  440. Section.Classes.emplace_back(Symbol->getName());
  441. break;
  442. case SymbolKind::ObjectiveCClassEHType:
  443. if (File->getFileType() != FileType::TBD_V3)
  444. Section.Symbols.emplace_back(
  445. copyString("_OBJC_EHTYPE_$_" + Symbol->getName().str()));
  446. else
  447. Section.ClassEHs.emplace_back(Symbol->getName());
  448. break;
  449. case SymbolKind::ObjectiveCInstanceVariable:
  450. if (File->getFileType() != FileType::TBD_V3)
  451. Section.IVars.emplace_back(
  452. copyString("_" + Symbol->getName().str()));
  453. else
  454. Section.IVars.emplace_back(Symbol->getName());
  455. break;
  456. }
  457. }
  458. llvm::sort(Section.Symbols.begin(), Section.Symbols.end());
  459. llvm::sort(Section.Classes.begin(), Section.Classes.end());
  460. llvm::sort(Section.ClassEHs.begin(), Section.ClassEHs.end());
  461. llvm::sort(Section.IVars.begin(), Section.IVars.end());
  462. llvm::sort(Section.WeakDefSymbols.begin(),
  463. Section.WeakDefSymbols.end());
  464. llvm::sort(Section.TLVSymbols.begin(), Section.TLVSymbols.end());
  465. Exports.emplace_back(std::move(Section));
  466. }
  467. ArchSet.clear();
  468. SymbolToArchSet.clear();
  469. for (const auto *Symbol : File->undefineds()) {
  470. auto Architectures = Symbol->getArchitectures();
  471. SymbolToArchSet[Symbol] = Architectures;
  472. ArchSet.insert(Architectures);
  473. }
  474. for (auto Architectures : ArchSet) {
  475. UndefinedSection Section;
  476. Section.Architectures = Architectures;
  477. for (const auto &SymArch : SymbolToArchSet) {
  478. if (SymArch.second != Architectures)
  479. continue;
  480. const auto *Symbol = SymArch.first;
  481. switch (Symbol->getKind()) {
  482. case SymbolKind::GlobalSymbol:
  483. if (Symbol->isWeakReferenced())
  484. Section.WeakRefSymbols.emplace_back(Symbol->getName());
  485. else
  486. Section.Symbols.emplace_back(Symbol->getName());
  487. break;
  488. case SymbolKind::ObjectiveCClass:
  489. if (File->getFileType() != FileType::TBD_V3)
  490. Section.Classes.emplace_back(
  491. copyString("_" + Symbol->getName().str()));
  492. else
  493. Section.Classes.emplace_back(Symbol->getName());
  494. break;
  495. case SymbolKind::ObjectiveCClassEHType:
  496. if (File->getFileType() != FileType::TBD_V3)
  497. Section.Symbols.emplace_back(
  498. copyString("_OBJC_EHTYPE_$_" + Symbol->getName().str()));
  499. else
  500. Section.ClassEHs.emplace_back(Symbol->getName());
  501. break;
  502. case SymbolKind::ObjectiveCInstanceVariable:
  503. if (File->getFileType() != FileType::TBD_V3)
  504. Section.IVars.emplace_back(
  505. copyString("_" + Symbol->getName().str()));
  506. else
  507. Section.IVars.emplace_back(Symbol->getName());
  508. break;
  509. }
  510. }
  511. llvm::sort(Section.Symbols.begin(), Section.Symbols.end());
  512. llvm::sort(Section.Classes.begin(), Section.Classes.end());
  513. llvm::sort(Section.ClassEHs.begin(), Section.ClassEHs.end());
  514. llvm::sort(Section.IVars.begin(), Section.IVars.end());
  515. llvm::sort(Section.WeakRefSymbols.begin(),
  516. Section.WeakRefSymbols.end());
  517. Undefineds.emplace_back(std::move(Section));
  518. }
  519. }
  520. // TBD v1 - TBD v3 files only support one platform and several
  521. // architectures. It is possible to have more than one platform for TBD v3
  522. // files, but the architectures don't apply to all
  523. // platforms, specifically to filter out the i386 slice from
  524. // platform macCatalyst.
  525. TargetList synthesizeTargets(ArchitectureSet Architectures,
  526. const PlatformSet &Platforms) {
  527. TargetList Targets;
  528. for (auto Platform : Platforms) {
  529. Platform = mapToPlatformKind(Platform, Architectures.hasX86());
  530. for (const auto &&Architecture : Architectures) {
  531. if ((Architecture == AK_i386) &&
  532. (Platform == PlatformKind::macCatalyst))
  533. continue;
  534. Targets.emplace_back(Architecture, Platform);
  535. }
  536. }
  537. return Targets;
  538. }
  539. const InterfaceFile *denormalize(IO &IO) {
  540. auto Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  541. assert(Ctx);
  542. auto *File = new InterfaceFile;
  543. File->setPath(Ctx->Path);
  544. File->setFileType(Ctx->FileKind);
  545. File->addTargets(synthesizeTargets(Architectures, Platforms));
  546. for (auto &ID : UUIDs)
  547. File->addUUID(ID.first, ID.second);
  548. File->setInstallName(InstallName);
  549. File->setCurrentVersion(CurrentVersion);
  550. File->setCompatibilityVersion(CompatibilityVersion);
  551. File->setSwiftABIVersion(SwiftABIVersion);
  552. File->setObjCConstraint(ObjCConstraint);
  553. for (const auto &Target : File->targets())
  554. File->addParentUmbrella(Target, ParentUmbrella);
  555. if (Ctx->FileKind == FileType::TBD_V1) {
  556. File->setTwoLevelNamespace();
  557. File->setApplicationExtensionSafe();
  558. } else {
  559. File->setTwoLevelNamespace(!(Flags & TBDFlags::FlatNamespace));
  560. File->setApplicationExtensionSafe(
  561. !(Flags & TBDFlags::NotApplicationExtensionSafe));
  562. File->setInstallAPI(Flags & TBDFlags::InstallAPI);
  563. }
  564. for (const auto &Section : Exports) {
  565. const auto Targets =
  566. synthesizeTargets(Section.Architectures, Platforms);
  567. for (const auto &Lib : Section.AllowableClients)
  568. for (const auto &Target : Targets)
  569. File->addAllowableClient(Lib, Target);
  570. for (const auto &Lib : Section.ReexportedLibraries)
  571. for (const auto &Target : Targets)
  572. File->addReexportedLibrary(Lib, Target);
  573. for (const auto &Symbol : Section.Symbols) {
  574. if (Ctx->FileKind != FileType::TBD_V3 &&
  575. Symbol.value.startswith("_OBJC_EHTYPE_$_"))
  576. File->addSymbol(SymbolKind::ObjectiveCClassEHType,
  577. Symbol.value.drop_front(15), Targets);
  578. else
  579. File->addSymbol(SymbolKind::GlobalSymbol, Symbol, Targets);
  580. }
  581. for (auto &Symbol : Section.Classes) {
  582. auto Name = Symbol.value;
  583. if (Ctx->FileKind != FileType::TBD_V3)
  584. Name = Name.drop_front();
  585. File->addSymbol(SymbolKind::ObjectiveCClass, Name, Targets);
  586. }
  587. for (auto &Symbol : Section.ClassEHs)
  588. File->addSymbol(SymbolKind::ObjectiveCClassEHType, Symbol, Targets);
  589. for (auto &Symbol : Section.IVars) {
  590. auto Name = Symbol.value;
  591. if (Ctx->FileKind != FileType::TBD_V3)
  592. Name = Name.drop_front();
  593. File->addSymbol(SymbolKind::ObjectiveCInstanceVariable, Name,
  594. Targets);
  595. }
  596. for (auto &Symbol : Section.WeakDefSymbols)
  597. File->addSymbol(SymbolKind::GlobalSymbol, Symbol, Targets,
  598. SymbolFlags::WeakDefined);
  599. for (auto &Symbol : Section.TLVSymbols)
  600. File->addSymbol(SymbolKind::GlobalSymbol, Symbol, Targets,
  601. SymbolFlags::ThreadLocalValue);
  602. }
  603. for (const auto &Section : Undefineds) {
  604. const auto Targets =
  605. synthesizeTargets(Section.Architectures, Platforms);
  606. for (auto &Symbol : Section.Symbols) {
  607. if (Ctx->FileKind != FileType::TBD_V3 &&
  608. Symbol.value.startswith("_OBJC_EHTYPE_$_"))
  609. File->addSymbol(SymbolKind::ObjectiveCClassEHType,
  610. Symbol.value.drop_front(15), Targets,
  611. SymbolFlags::Undefined);
  612. else
  613. File->addSymbol(SymbolKind::GlobalSymbol, Symbol, Targets,
  614. SymbolFlags::Undefined);
  615. }
  616. for (auto &Symbol : Section.Classes) {
  617. auto Name = Symbol.value;
  618. if (Ctx->FileKind != FileType::TBD_V3)
  619. Name = Name.drop_front();
  620. File->addSymbol(SymbolKind::ObjectiveCClass, Name, Targets,
  621. SymbolFlags::Undefined);
  622. }
  623. for (auto &Symbol : Section.ClassEHs)
  624. File->addSymbol(SymbolKind::ObjectiveCClassEHType, Symbol, Targets,
  625. SymbolFlags::Undefined);
  626. for (auto &Symbol : Section.IVars) {
  627. auto Name = Symbol.value;
  628. if (Ctx->FileKind != FileType::TBD_V3)
  629. Name = Name.drop_front();
  630. File->addSymbol(SymbolKind::ObjectiveCInstanceVariable, Name, Targets,
  631. SymbolFlags::Undefined);
  632. }
  633. for (auto &Symbol : Section.WeakRefSymbols)
  634. File->addSymbol(SymbolKind::GlobalSymbol, Symbol, Targets,
  635. SymbolFlags::Undefined | SymbolFlags::WeakReferenced);
  636. }
  637. return File;
  638. }
  639. llvm::BumpPtrAllocator Allocator;
  640. StringRef copyString(StringRef String) {
  641. if (String.empty())
  642. return {};
  643. void *Ptr = Allocator.Allocate(String.size(), 1);
  644. memcpy(Ptr, String.data(), String.size());
  645. return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
  646. }
  647. std::vector<Architecture> Architectures;
  648. std::vector<UUID> UUIDs;
  649. PlatformSet Platforms;
  650. StringRef InstallName;
  651. PackedVersion CurrentVersion;
  652. PackedVersion CompatibilityVersion;
  653. SwiftVersion SwiftABIVersion{0};
  654. ObjCConstraintType ObjCConstraint{ObjCConstraintType::None};
  655. TBDFlags Flags{TBDFlags::None};
  656. StringRef ParentUmbrella;
  657. std::vector<ExportSection> Exports;
  658. std::vector<UndefinedSection> Undefineds;
  659. };
  660. static void setFileTypeForInput(TextAPIContext *Ctx, IO &IO) {
  661. if (IO.mapTag("!tapi-tbd", false))
  662. Ctx->FileKind = FileType::TBD_V4;
  663. else if (IO.mapTag("!tapi-tbd-v3", false))
  664. Ctx->FileKind = FileType::TBD_V3;
  665. else if (IO.mapTag("!tapi-tbd-v2", false))
  666. Ctx->FileKind = FileType::TBD_V2;
  667. else if (IO.mapTag("!tapi-tbd-v1", false) ||
  668. IO.mapTag("tag:yaml.org,2002:map", false))
  669. Ctx->FileKind = FileType::TBD_V1;
  670. else {
  671. Ctx->FileKind = FileType::Invalid;
  672. return;
  673. }
  674. }
  675. static void mapping(IO &IO, const InterfaceFile *&File) {
  676. auto *Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  677. assert((!Ctx || !IO.outputting() ||
  678. (Ctx && Ctx->FileKind != FileType::Invalid)) &&
  679. "File type is not set in YAML context");
  680. if (!IO.outputting()) {
  681. setFileTypeForInput(Ctx, IO);
  682. switch (Ctx->FileKind) {
  683. default:
  684. break;
  685. case FileType::TBD_V4:
  686. mapKeysToValuesV4(IO, File);
  687. return;
  688. case FileType::Invalid:
  689. IO.setError("unsupported file type");
  690. return;
  691. }
  692. } else {
  693. // Set file type when writing.
  694. switch (Ctx->FileKind) {
  695. default:
  696. llvm_unreachable("unexpected file type");
  697. case FileType::TBD_V4:
  698. mapKeysToValuesV4(IO, File);
  699. return;
  700. case FileType::TBD_V3:
  701. IO.mapTag("!tapi-tbd-v3", true);
  702. break;
  703. case FileType::TBD_V2:
  704. IO.mapTag("!tapi-tbd-v2", true);
  705. break;
  706. case FileType::TBD_V1:
  707. // Don't write the tag into the .tbd file for TBD v1
  708. break;
  709. }
  710. }
  711. mapKeysToValues(Ctx->FileKind, IO, File);
  712. }
  713. using SectionList = std::vector<SymbolSection>;
  714. struct NormalizedTBD_V4 {
  715. explicit NormalizedTBD_V4(IO &IO) {}
  716. NormalizedTBD_V4(IO &IO, const InterfaceFile *&File) {
  717. auto Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  718. assert(Ctx);
  719. TBDVersion = Ctx->FileKind >> 1;
  720. Targets.insert(Targets.begin(), File->targets().begin(),
  721. File->targets().end());
  722. for (const auto &IT : File->uuids())
  723. UUIDs.emplace_back(IT.first, IT.second);
  724. InstallName = File->getInstallName();
  725. CurrentVersion = File->getCurrentVersion();
  726. CompatibilityVersion = File->getCompatibilityVersion();
  727. SwiftABIVersion = File->getSwiftABIVersion();
  728. Flags = TBDFlags::None;
  729. if (!File->isApplicationExtensionSafe())
  730. Flags |= TBDFlags::NotApplicationExtensionSafe;
  731. if (!File->isTwoLevelNamespace())
  732. Flags |= TBDFlags::FlatNamespace;
  733. if (File->isInstallAPI())
  734. Flags |= TBDFlags::InstallAPI;
  735. {
  736. std::map<std::string, TargetList> valueToTargetList;
  737. for (const auto &it : File->umbrellas())
  738. valueToTargetList[it.second].emplace_back(it.first);
  739. for (const auto &it : valueToTargetList) {
  740. UmbrellaSection CurrentSection;
  741. CurrentSection.Targets.insert(CurrentSection.Targets.begin(),
  742. it.second.begin(), it.second.end());
  743. CurrentSection.Umbrella = it.first;
  744. ParentUmbrellas.emplace_back(std::move(CurrentSection));
  745. }
  746. }
  747. assignTargetsToLibrary(File->allowableClients(), AllowableClients);
  748. assignTargetsToLibrary(File->reexportedLibraries(), ReexportedLibraries);
  749. auto handleSymbols =
  750. [](SectionList &CurrentSections,
  751. InterfaceFile::const_filtered_symbol_range Symbols,
  752. std::function<bool(const Symbol *)> Pred) {
  753. std::set<TargetList> TargetSet;
  754. std::map<const Symbol *, TargetList> SymbolToTargetList;
  755. for (const auto *Symbol : Symbols) {
  756. if (!Pred(Symbol))
  757. continue;
  758. TargetList Targets(Symbol->targets());
  759. SymbolToTargetList[Symbol] = Targets;
  760. TargetSet.emplace(std::move(Targets));
  761. }
  762. for (const auto &TargetIDs : TargetSet) {
  763. SymbolSection CurrentSection;
  764. CurrentSection.Targets.insert(CurrentSection.Targets.begin(),
  765. TargetIDs.begin(), TargetIDs.end());
  766. for (const auto &IT : SymbolToTargetList) {
  767. if (IT.second != TargetIDs)
  768. continue;
  769. const auto *Symbol = IT.first;
  770. switch (Symbol->getKind()) {
  771. case SymbolKind::GlobalSymbol:
  772. if (Symbol->isWeakDefined())
  773. CurrentSection.WeakSymbols.emplace_back(Symbol->getName());
  774. else if (Symbol->isThreadLocalValue())
  775. CurrentSection.TlvSymbols.emplace_back(Symbol->getName());
  776. else
  777. CurrentSection.Symbols.emplace_back(Symbol->getName());
  778. break;
  779. case SymbolKind::ObjectiveCClass:
  780. CurrentSection.Classes.emplace_back(Symbol->getName());
  781. break;
  782. case SymbolKind::ObjectiveCClassEHType:
  783. CurrentSection.ClassEHs.emplace_back(Symbol->getName());
  784. break;
  785. case SymbolKind::ObjectiveCInstanceVariable:
  786. CurrentSection.Ivars.emplace_back(Symbol->getName());
  787. break;
  788. }
  789. }
  790. sort(CurrentSection.Symbols);
  791. sort(CurrentSection.Classes);
  792. sort(CurrentSection.ClassEHs);
  793. sort(CurrentSection.Ivars);
  794. sort(CurrentSection.WeakSymbols);
  795. sort(CurrentSection.TlvSymbols);
  796. CurrentSections.emplace_back(std::move(CurrentSection));
  797. }
  798. };
  799. handleSymbols(Exports, File->exports(), [](const Symbol *Symbol) {
  800. return !Symbol->isReexported();
  801. });
  802. handleSymbols(Reexports, File->exports(), [](const Symbol *Symbol) {
  803. return Symbol->isReexported();
  804. });
  805. handleSymbols(Undefineds, File->undefineds(),
  806. [](const Symbol *Symbol) { return true; });
  807. }
  808. const InterfaceFile *denormalize(IO &IO) {
  809. auto Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  810. assert(Ctx);
  811. auto *File = new InterfaceFile;
  812. File->setPath(Ctx->Path);
  813. File->setFileType(Ctx->FileKind);
  814. for (auto &id : UUIDs)
  815. File->addUUID(id.TargetID, id.Value);
  816. File->addTargets(Targets);
  817. File->setInstallName(InstallName);
  818. File->setCurrentVersion(CurrentVersion);
  819. File->setCompatibilityVersion(CompatibilityVersion);
  820. File->setSwiftABIVersion(SwiftABIVersion);
  821. for (const auto &CurrentSection : ParentUmbrellas)
  822. for (const auto &target : CurrentSection.Targets)
  823. File->addParentUmbrella(target, CurrentSection.Umbrella);
  824. File->setTwoLevelNamespace(!(Flags & TBDFlags::FlatNamespace));
  825. File->setApplicationExtensionSafe(
  826. !(Flags & TBDFlags::NotApplicationExtensionSafe));
  827. File->setInstallAPI(Flags & TBDFlags::InstallAPI);
  828. for (const auto &CurrentSection : AllowableClients) {
  829. for (const auto &lib : CurrentSection.Values)
  830. for (const auto &Target : CurrentSection.Targets)
  831. File->addAllowableClient(lib, Target);
  832. }
  833. for (const auto &CurrentSection : ReexportedLibraries) {
  834. for (const auto &Lib : CurrentSection.Values)
  835. for (const auto &Target : CurrentSection.Targets)
  836. File->addReexportedLibrary(Lib, Target);
  837. }
  838. auto handleSymbols = [File](const SectionList &CurrentSections,
  839. SymbolFlags Flag = SymbolFlags::None) {
  840. for (const auto &CurrentSection : CurrentSections) {
  841. for (auto &sym : CurrentSection.Symbols)
  842. File->addSymbol(SymbolKind::GlobalSymbol, sym,
  843. CurrentSection.Targets, Flag);
  844. for (auto &sym : CurrentSection.Classes)
  845. File->addSymbol(SymbolKind::ObjectiveCClass, sym,
  846. CurrentSection.Targets);
  847. for (auto &sym : CurrentSection.ClassEHs)
  848. File->addSymbol(SymbolKind::ObjectiveCClassEHType, sym,
  849. CurrentSection.Targets);
  850. for (auto &sym : CurrentSection.Ivars)
  851. File->addSymbol(SymbolKind::ObjectiveCInstanceVariable, sym,
  852. CurrentSection.Targets);
  853. for (auto &sym : CurrentSection.WeakSymbols)
  854. File->addSymbol(SymbolKind::GlobalSymbol, sym,
  855. CurrentSection.Targets);
  856. for (auto &sym : CurrentSection.TlvSymbols)
  857. File->addSymbol(SymbolKind::GlobalSymbol, sym,
  858. CurrentSection.Targets,
  859. SymbolFlags::ThreadLocalValue);
  860. }
  861. };
  862. handleSymbols(Exports);
  863. handleSymbols(Reexports, SymbolFlags::Rexported);
  864. handleSymbols(Undefineds, SymbolFlags::Undefined);
  865. return File;
  866. }
  867. unsigned TBDVersion;
  868. std::vector<UUIDv4> UUIDs;
  869. TargetList Targets;
  870. StringRef InstallName;
  871. PackedVersion CurrentVersion;
  872. PackedVersion CompatibilityVersion;
  873. SwiftVersion SwiftABIVersion{0};
  874. std::vector<MetadataSection> AllowableClients;
  875. std::vector<MetadataSection> ReexportedLibraries;
  876. TBDFlags Flags{TBDFlags::None};
  877. std::vector<UmbrellaSection> ParentUmbrellas;
  878. SectionList Exports;
  879. SectionList Reexports;
  880. SectionList Undefineds;
  881. private:
  882. void assignTargetsToLibrary(const std::vector<InterfaceFileRef> &Libraries,
  883. std::vector<MetadataSection> &Section) {
  884. std::set<TargetList> targetSet;
  885. std::map<const InterfaceFileRef *, TargetList> valueToTargetList;
  886. for (const auto &library : Libraries) {
  887. TargetList targets(library.targets());
  888. valueToTargetList[&library] = targets;
  889. targetSet.emplace(std::move(targets));
  890. }
  891. for (const auto &targets : targetSet) {
  892. MetadataSection CurrentSection;
  893. CurrentSection.Targets.insert(CurrentSection.Targets.begin(),
  894. targets.begin(), targets.end());
  895. for (const auto &it : valueToTargetList) {
  896. if (it.second != targets)
  897. continue;
  898. CurrentSection.Values.emplace_back(it.first->getInstallName());
  899. }
  900. llvm::sort(CurrentSection.Values);
  901. Section.emplace_back(std::move(CurrentSection));
  902. }
  903. }
  904. };
  905. static void mapKeysToValues(FileType FileKind, IO &IO,
  906. const InterfaceFile *&File) {
  907. MappingNormalization<NormalizedTBD, const InterfaceFile *> Keys(IO, File);
  908. IO.mapRequired("archs", Keys->Architectures);
  909. if (FileKind != FileType::TBD_V1)
  910. IO.mapOptional("uuids", Keys->UUIDs);
  911. IO.mapRequired("platform", Keys->Platforms);
  912. if (FileKind != FileType::TBD_V1)
  913. IO.mapOptional("flags", Keys->Flags, TBDFlags::None);
  914. IO.mapRequired("install-name", Keys->InstallName);
  915. IO.mapOptional("current-version", Keys->CurrentVersion,
  916. PackedVersion(1, 0, 0));
  917. IO.mapOptional("compatibility-version", Keys->CompatibilityVersion,
  918. PackedVersion(1, 0, 0));
  919. if (FileKind != FileType::TBD_V3)
  920. IO.mapOptional("swift-version", Keys->SwiftABIVersion, SwiftVersion(0));
  921. else
  922. IO.mapOptional("swift-abi-version", Keys->SwiftABIVersion,
  923. SwiftVersion(0));
  924. IO.mapOptional("objc-constraint", Keys->ObjCConstraint,
  925. (FileKind == FileType::TBD_V1)
  926. ? ObjCConstraintType::None
  927. : ObjCConstraintType::Retain_Release);
  928. if (FileKind != FileType::TBD_V1)
  929. IO.mapOptional("parent-umbrella", Keys->ParentUmbrella, StringRef());
  930. IO.mapOptional("exports", Keys->Exports);
  931. if (FileKind != FileType::TBD_V1)
  932. IO.mapOptional("undefineds", Keys->Undefineds);
  933. }
  934. static void mapKeysToValuesV4(IO &IO, const InterfaceFile *&File) {
  935. MappingNormalization<NormalizedTBD_V4, const InterfaceFile *> Keys(IO,
  936. File);
  937. IO.mapTag("!tapi-tbd", true);
  938. IO.mapRequired("tbd-version", Keys->TBDVersion);
  939. IO.mapRequired("targets", Keys->Targets);
  940. IO.mapOptional("uuids", Keys->UUIDs);
  941. IO.mapOptional("flags", Keys->Flags, TBDFlags::None);
  942. IO.mapRequired("install-name", Keys->InstallName);
  943. IO.mapOptional("current-version", Keys->CurrentVersion,
  944. PackedVersion(1, 0, 0));
  945. IO.mapOptional("compatibility-version", Keys->CompatibilityVersion,
  946. PackedVersion(1, 0, 0));
  947. IO.mapOptional("swift-abi-version", Keys->SwiftABIVersion, SwiftVersion(0));
  948. IO.mapOptional("parent-umbrella", Keys->ParentUmbrellas);
  949. auto OptionKind = MetadataSection::Option::Clients;
  950. IO.mapOptionalWithContext("allowable-clients", Keys->AllowableClients,
  951. OptionKind);
  952. OptionKind = MetadataSection::Option::Libraries;
  953. IO.mapOptionalWithContext("reexported-libraries", Keys->ReexportedLibraries,
  954. OptionKind);
  955. IO.mapOptional("exports", Keys->Exports);
  956. IO.mapOptional("reexports", Keys->Reexports);
  957. IO.mapOptional("undefineds", Keys->Undefineds);
  958. }
  959. };
  960. template <>
  961. struct DocumentListTraits<std::vector<const MachO::InterfaceFile *>> {
  962. static size_t size(IO &IO, std::vector<const MachO::InterfaceFile *> &Seq) {
  963. return Seq.size();
  964. }
  965. static const InterfaceFile *&
  966. element(IO &IO, std::vector<const InterfaceFile *> &Seq, size_t Index) {
  967. if (Index >= Seq.size())
  968. Seq.resize(Index + 1);
  969. return Seq[Index];
  970. }
  971. };
  972. } // end namespace yaml.
  973. namespace MachO {
  974. static void DiagHandler(const SMDiagnostic &Diag, void *Context) {
  975. auto *File = static_cast<TextAPIContext *>(Context);
  976. SmallString<1024> Message;
  977. raw_svector_ostream S(Message);
  978. SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), File->Path,
  979. Diag.getLineNo(), Diag.getColumnNo(), Diag.getKind(),
  980. Diag.getMessage(), Diag.getLineContents(),
  981. Diag.getRanges(), Diag.getFixIts());
  982. NewDiag.print(nullptr, S);
  983. File->ErrorMessage = ("malformed file\n" + Message).str();
  984. }
  985. Expected<std::unique_ptr<InterfaceFile>>
  986. TextAPIReader::get(MemoryBufferRef InputBuffer) {
  987. TextAPIContext Ctx;
  988. Ctx.Path = InputBuffer.getBufferIdentifier();
  989. yaml::Input YAMLIn(InputBuffer.getBuffer(), &Ctx, DiagHandler, &Ctx);
  990. // Fill vector with interface file objects created by parsing the YAML file.
  991. std::vector<const InterfaceFile *> Files;
  992. YAMLIn >> Files;
  993. // YAMLIn dynamically allocates for Interface file and in case of error,
  994. // memory leak will occur unless wrapped around unique_ptr
  995. auto File = std::unique_ptr<InterfaceFile>(
  996. const_cast<InterfaceFile *>(Files.front()));
  997. if (YAMLIn.error())
  998. return make_error<StringError>(Ctx.ErrorMessage, YAMLIn.error());
  999. return std::move(File);
  1000. }
  1001. Error TextAPIWriter::writeToStream(raw_ostream &OS, const InterfaceFile &File) {
  1002. TextAPIContext Ctx;
  1003. Ctx.Path = File.getPath();
  1004. Ctx.FileKind = File.getFileType();
  1005. llvm::yaml::Output YAMLOut(OS, &Ctx, /*WrapColumn=*/80);
  1006. std::vector<const InterfaceFile *> Files;
  1007. Files.emplace_back(&File);
  1008. // Stream out yaml.
  1009. YAMLOut << Files;
  1010. return Error::success();
  1011. }
  1012. } // end namespace MachO.
  1013. } // end namespace llvm.