-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathModuleDependencyCacheSerialization.cpp
1950 lines (1705 loc) · 76.8 KB
/
ModuleDependencyCacheSerialization.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=== ModuleDependencyCacheSerialization.cpp - serialized format -*- C++ -*-==//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/FileSystem.h"
#include "swift/AST/ModuleDependencies.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/Version.h"
#include "swift/DependencyScan/SerializedModuleDependencyCacheFormat.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/VirtualOutputBackend.h"
#include <unordered_map>
using namespace swift;
using namespace dependencies;
using namespace module_dependency_cache_serialization;
namespace {
ModuleDependencyKind &operator++(ModuleDependencyKind &e) {
if (e == ModuleDependencyKind::LastKind) {
llvm_unreachable(
"Attempting to increment last enum value on ModuleDependencyKind");
}
e = ModuleDependencyKind(
static_cast<std::underlying_type<ModuleDependencyKind>::type>(e) + 1);
return e;
}
} // namespace
// MARK: Deserialization
namespace swift {
class ModuleDependenciesCacheDeserializer {
std::vector<std::string> Identifiers;
std::vector<std::vector<uint64_t>> ArraysOfIdentifierIDs;
std::vector<LinkLibrary> LinkLibraries;
std::vector<std::vector<uint64_t>> ArraysOfLinkLibraryIDs;
std::vector<std::pair<std::string, MacroPluginDependency>> MacroDependencies;
std::vector<std::vector<uint64_t>> ArraysOfMacroDependenciesIDs;
std::vector<ScannerImportStatementInfo> ImportStatements;
std::vector<std::vector<uint64_t>> ArraysOfImportStatementIDs;
std::vector<std::vector<uint64_t>> ArraysOfOptionalImportStatementIDs;
llvm::BitstreamCursor Cursor;
SmallVector<uint64_t, 64> Scratch;
StringRef BlobData;
// These return true if there was an error.
bool readSignature();
bool enterGraphBlock();
bool readMetadata(StringRef scannerContextHash);
bool readGraph(ModuleDependenciesCache &cache);
std::optional<std::string> getIdentifier(unsigned n);
std::optional<std::vector<std::string>> getStringArray(unsigned n);
std::optional<std::vector<LinkLibrary>> getLinkLibraryArray(unsigned n);
std::optional<std::vector<std::pair<std::string, MacroPluginDependency>>>
getMacroDependenciesArray(unsigned n);
std::optional<std::vector<ScannerImportStatementInfo>>
getImportStatementInfoArray(unsigned n);
std::optional<std::vector<ScannerImportStatementInfo>>
getOptionalImportStatementInfoArray(unsigned n);
std::optional<std::vector<ModuleDependencyID>>
getModuleDependencyIDArray(unsigned n);
public:
ModuleDependenciesCacheDeserializer(llvm::MemoryBufferRef Data)
: Cursor(Data) {}
bool readInterModuleDependenciesCache(ModuleDependenciesCache &cache);
};
} // namespace swift
/// Read in the expected signature: IMDC
bool ModuleDependenciesCacheDeserializer::readSignature() {
for (unsigned char byte : MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE) {
if (Cursor.AtEndOfStream())
return true;
if (auto maybeRead = Cursor.Read(8)) {
if (maybeRead.get() != byte)
return true;
} else {
return true;
}
}
return false;
}
/// Read in the info block and enter the top-level block which represents the
/// graph
bool ModuleDependenciesCacheDeserializer::enterGraphBlock() {
// Read the BLOCKINFO_BLOCK, which contains metadata used when dumping
// the binary data with llvm-bcanalyzer.
{
auto next = Cursor.advance();
if (!next) {
consumeError(next.takeError());
return true;
}
if (next->Kind != llvm::BitstreamEntry::SubBlock)
return true;
if (next->ID != llvm::bitc::BLOCKINFO_BLOCK_ID)
return true;
if (!Cursor.ReadBlockInfoBlock())
return true;
}
// Enters our top-level subblock,
// which contains the actual module dependency information.
{
auto next = Cursor.advance();
if (!next) {
consumeError(next.takeError());
return true;
}
if (next->Kind != llvm::BitstreamEntry::SubBlock)
return true;
if (next->ID != GRAPH_BLOCK_ID)
return true;
if (auto err = Cursor.EnterSubBlock(GRAPH_BLOCK_ID)) {
consumeError(std::move(err));
return true;
}
}
return false;
}
/// Read in the serialized file's format version, error/exit if not matching
/// current version.
bool ModuleDependenciesCacheDeserializer::readMetadata(StringRef scannerContextHash) {
using namespace graph_block;
auto entry = Cursor.advance();
if (!entry) {
consumeError(entry.takeError());
return true;
}
if (entry->Kind != llvm::BitstreamEntry::Record)
return true;
auto recordID = Cursor.readRecord(entry->ID, Scratch, &BlobData);
if (!recordID) {
consumeError(recordID.takeError());
return true;
}
if (*recordID != METADATA)
return true;
unsigned majorVersion, minorVersion;
MetadataLayout::readRecord(Scratch, majorVersion, minorVersion);
if (majorVersion != MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR ||
minorVersion != MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR)
return true;
std::string readScannerContextHash = BlobData.str();
if (readScannerContextHash != scannerContextHash)
return true;
return false;
}
/// Read in the top-level block's graph structure by first reading in
/// all of the file's identifiers and arrays of identifiers, followed by
/// consuming individual module info records and registering them into the
/// cache.
bool ModuleDependenciesCacheDeserializer::readGraph(
ModuleDependenciesCache &cache) {
using namespace graph_block;
bool hasCurrentModule = false;
std::string currentModuleName;
std::vector<ScannerImportStatementInfo> currentModuleImports;
std::vector<ScannerImportStatementInfo> currentOptionalModuleImports;
std::vector<ModuleDependencyID> importedSwiftDependenciesIDs;
std::vector<ModuleDependencyID> importedClangDependenciesIDs;
std::vector<ModuleDependencyID> crossImportOverlayDependenciesIDs;
std::vector<ModuleDependencyID> swiftOverlayDependenciesIDs;
std::vector<LinkLibrary> linkLibraries;
std::vector<std::pair<std::string, MacroPluginDependency>> macroDependencies;
std::vector<ScannerImportStatementInfo> importStatements;
std::vector<ScannerImportStatementInfo> optionalImportStatements;
std::vector<std::string> auxiliaryFiles;
auto addCommonDependencyInfo =
[¤tModuleImports, ¤tOptionalModuleImports,
&importedClangDependenciesIDs, &auxiliaryFiles,
¯oDependencies](ModuleDependencyInfo &moduleDep) {
// Add imports of this module
for (const auto &moduleName : currentModuleImports)
moduleDep.addModuleImport(moduleName.importIdentifier);
// Add optional imports of this module
for (const auto &moduleName : currentOptionalModuleImports)
moduleDep.addOptionalModuleImport(moduleName.importIdentifier);
// Add qualified dependencies of this module
moduleDep.setImportedClangDependencies(importedClangDependenciesIDs);
// Add any auxiliary files
moduleDep.setAuxiliaryFiles(auxiliaryFiles);
// Add macro dependencies
for (const auto &md : macroDependencies)
moduleDep.addMacroDependency(md.first, md.second.LibraryPath,
md.second.ExecutablePath);
moduleDep.setIsFinalized(true);
};
auto addSwiftCommonDependencyInfo =
[&importedSwiftDependenciesIDs, &crossImportOverlayDependenciesIDs,
&swiftOverlayDependenciesIDs](ModuleDependencyInfo &moduleDep) {
moduleDep.setImportedSwiftDependencies(importedSwiftDependenciesIDs);
moduleDep.setCrossImportOverlayDependencies(
crossImportOverlayDependenciesIDs);
moduleDep.setSwiftOverlayDependencies(swiftOverlayDependenciesIDs);
};
auto addSwiftTextualDependencyInfo =
[this](ModuleDependencyInfo &moduleDep, unsigned bridgingHeaderFileID,
unsigned bridgingSourceFilesArrayID,
unsigned bridgingModuleDependenciesArrayID,
unsigned bridgingHeaderIncludeTreeID) {
// Add bridging header file path
if (bridgingHeaderFileID != 0) {
auto bridgingHeaderFile = getIdentifier(bridgingHeaderFileID);
if (!bridgingHeaderFile)
llvm::report_fatal_error("Bad bridging header path");
moduleDep.addBridgingHeader(*bridgingHeaderFile);
}
// Add bridging source files
auto bridgingSourceFiles = getStringArray(bridgingSourceFilesArrayID);
if (!bridgingSourceFiles)
llvm::report_fatal_error("Bad bridging source files");
for (const auto &file : *bridgingSourceFiles)
moduleDep.addHeaderSourceFile(file);
// Add bridging module dependencies
auto bridgingModuleDeps =
getStringArray(bridgingModuleDependenciesArrayID);
if (!bridgingModuleDeps)
llvm::report_fatal_error("Bad bridging module dependencies");
llvm::StringSet<> alreadyAdded;
std::vector<ModuleDependencyID> bridgingModuleDepIDs;
for (const auto &mod : bridgingModuleDeps.value())
bridgingModuleDepIDs.push_back(
ModuleDependencyID{mod, ModuleDependencyKind::Clang});
moduleDep.setHeaderClangDependencies(bridgingModuleDepIDs);
// Add bridging header include tree
auto bridgingHeaderIncludeTree =
getIdentifier(bridgingHeaderIncludeTreeID);
if (!bridgingHeaderIncludeTree)
llvm::report_fatal_error("Bad bridging header include tree");
if (!bridgingHeaderIncludeTree->empty())
moduleDep.addBridgingHeaderIncludeTree(*bridgingHeaderIncludeTree);
};
while (!Cursor.AtEndOfStream()) {
auto entry = cantFail(Cursor.advance(), "Advance bitstream cursor");
if (entry.Kind == llvm::BitstreamEntry::EndBlock) {
Cursor.ReadBlockEnd();
assert(Cursor.GetCurrentBitNo() % CHAR_BIT == 0);
break;
}
if (entry.Kind != llvm::BitstreamEntry::Record)
llvm::report_fatal_error("Bad bitstream entry kind");
Scratch.clear();
unsigned recordID =
cantFail(Cursor.readRecord(entry.ID, Scratch, &BlobData),
"Read bitstream record");
switch (recordID) {
case METADATA: {
// METADATA must appear at the beginning and is read by readMetadata().
llvm::report_fatal_error("Unexpected METADATA record");
break;
}
case IDENTIFIER_NODE: {
// IDENTIFIER_NODE must come before MODULE_NODEs.
if (hasCurrentModule)
llvm::report_fatal_error("Unexpected IDENTIFIER_NODE record");
IdentifierNodeLayout::readRecord(Scratch);
Identifiers.push_back(BlobData.str());
break;
}
case IDENTIFIER_ARRAY_NODE: {
// IDENTIFIER_ARRAY_NODE must come before MODULE_NODEs.
if (hasCurrentModule)
llvm::report_fatal_error("Unexpected IDENTIFIER_NODE record");
ArrayRef<uint64_t> identifierIDs;
IdentifierArrayLayout::readRecord(Scratch, identifierIDs);
ArraysOfIdentifierIDs.push_back(identifierIDs.vec());
break;
}
case LINK_LIBRARY_NODE: {
unsigned libraryIdentifierID;
bool isFramework, shouldForceLoad;
LinkLibraryLayout::readRecord(Scratch, libraryIdentifierID, isFramework,
shouldForceLoad);
auto libraryIdentifier = getIdentifier(libraryIdentifierID);
if (!libraryIdentifier)
llvm::report_fatal_error("Bad link library identifier");
LinkLibraries.push_back(LinkLibrary(libraryIdentifier.value(),
isFramework ? LibraryKind::Framework
: LibraryKind::Library,
shouldForceLoad));
break;
}
case LINK_LIBRARY_ARRAY_NODE: {
ArrayRef<uint64_t> identifierIDs;
LinkLibraryArrayLayout::readRecord(Scratch, identifierIDs);
ArraysOfLinkLibraryIDs.push_back(identifierIDs.vec());
break;
}
case MACRO_DEPENDENCY_NODE: {
unsigned macroModuleNameID, libraryPathID, executablePathID;
MacroDependencyLayout::readRecord(Scratch, macroModuleNameID,
libraryPathID, executablePathID);
auto macroModuleName = getIdentifier(macroModuleNameID);
if (!macroModuleName)
llvm::report_fatal_error("Bad macro dependency: no module name");
auto libraryPath = getIdentifier(libraryPathID);
if (!libraryPath)
llvm::report_fatal_error("Bad macro dependency: no library path");
auto executablePath = getIdentifier(executablePathID);
if (!executablePath)
llvm::report_fatal_error("Bad macro dependency: no executable path");
MacroDependencies.push_back(
{macroModuleName.value(),
MacroPluginDependency{libraryPath.value(), executablePath.value()}});
break;
}
case MACRO_DEPENDENCY_ARRAY_NODE: {
ArrayRef<uint64_t> identifierIDs;
MacroDependencyArrayLayout::readRecord(Scratch, identifierIDs);
ArraysOfMacroDependenciesIDs.push_back(identifierIDs.vec());
break;
}
case IMPORT_STATEMENT_NODE: {
unsigned importIdentifierID, bufferIdentifierID;
unsigned lineNumber, columnNumber;
bool isOptional;
ImportStatementLayout::readRecord(Scratch, importIdentifierID,
bufferIdentifierID, lineNumber,
columnNumber, isOptional);
auto importIdentifier = getIdentifier(importIdentifierID);
if (!importIdentifier)
llvm::report_fatal_error("Bad import statement info: no import name");
auto bufferIdentifier = getIdentifier(bufferIdentifierID);
if (!bufferIdentifier)
llvm::report_fatal_error(
"Bad import statement info: no buffer identifier");
ImportStatements.push_back(ScannerImportStatementInfo(
importIdentifier.value(),
ScannerImportStatementInfo::ImportDiagnosticLocationInfo(
bufferIdentifier.value(), lineNumber, columnNumber)));
break;
}
case IMPORT_STATEMENT_ARRAY_NODE: {
ArrayRef<uint64_t> identifierIDs;
ImportStatementArrayLayout::readRecord(Scratch, identifierIDs);
ArraysOfImportStatementIDs.push_back(identifierIDs.vec());
break;
}
case OPTIONAL_IMPORT_STATEMENT_ARRAY_NODE: {
ArrayRef<uint64_t> identifierIDs;
OptionalImportStatementArrayLayout::readRecord(Scratch, identifierIDs);
ArraysOfOptionalImportStatementIDs.push_back(identifierIDs.vec());
break;
}
case MODULE_NODE: {
hasCurrentModule = true;
unsigned moduleNameID, moduleImportsArrayID, optionalImportsArrayID,
linkLibraryArrayID, macroDependencyArrayID,
importedSwiftDependenciesIDsArrayID,
importedClangDependenciesIDsArrayID,
crossImportOverlayDependenciesIDsArrayID,
swiftOverlayDependenciesIDsArrayID, moduleCacheKeyID,
AuxiliaryFilesArrayID;
ModuleInfoLayout::readRecord(Scratch, moduleNameID, moduleImportsArrayID,
optionalImportsArrayID, linkLibraryArrayID,
macroDependencyArrayID,
importedSwiftDependenciesIDsArrayID,
importedClangDependenciesIDsArrayID,
crossImportOverlayDependenciesIDsArrayID,
swiftOverlayDependenciesIDsArrayID,
moduleCacheKeyID, AuxiliaryFilesArrayID);
auto moduleName = getIdentifier(moduleNameID);
if (!moduleName)
llvm::report_fatal_error("Bad module name");
currentModuleName = *moduleName;
auto optionalImportStatementInfos =
getImportStatementInfoArray(moduleImportsArrayID);
if (!optionalImportStatementInfos)
llvm::report_fatal_error("Bad direct Swift dependencies: no imports");
importStatements = optionalImportStatementInfos.value();
auto optionalOptionalImportStatementInfos =
getOptionalImportStatementInfoArray(optionalImportsArrayID);
if (!optionalOptionalImportStatementInfos)
llvm::report_fatal_error(
"Bad direct Swift dependencies: no optional imports");
optionalImportStatements = optionalOptionalImportStatementInfos.value();
auto optionalAuxiliaryFiles = getStringArray(AuxiliaryFilesArrayID);
if (optionalAuxiliaryFiles.has_value())
for (const auto &af : optionalAuxiliaryFiles.value())
auxiliaryFiles.push_back(af);
auto optionalImportedSwiftDependenciesIDs =
getModuleDependencyIDArray(importedSwiftDependenciesIDsArrayID);
if (!optionalImportedSwiftDependenciesIDs)
llvm::report_fatal_error(
"Bad direct Swift dependencies: no qualified dependencies");
importedSwiftDependenciesIDs =
optionalImportedSwiftDependenciesIDs.value();
auto optionalImportedClangDependenciesIDs =
getModuleDependencyIDArray(importedClangDependenciesIDsArrayID);
if (!optionalImportedClangDependenciesIDs)
llvm::report_fatal_error(
"Bad direct Clang dependencies: no qualified dependencies");
importedClangDependenciesIDs =
optionalImportedClangDependenciesIDs.value();
auto optionalCrossImportOverlayDependenciesIDs =
getModuleDependencyIDArray(crossImportOverlayDependenciesIDsArrayID);
if (!optionalCrossImportOverlayDependenciesIDs)
llvm::report_fatal_error(
"Bad Cross-Import Overlay dependencies: no qualified dependencies");
crossImportOverlayDependenciesIDs =
optionalCrossImportOverlayDependenciesIDs.value();
auto optionalSwiftOverlayDependenciesIDs =
getModuleDependencyIDArray(swiftOverlayDependenciesIDsArrayID);
if (!optionalSwiftOverlayDependenciesIDs)
llvm::report_fatal_error(
"Bad Swift Overlay dependencies: no qualified dependencies");
swiftOverlayDependenciesIDs = optionalSwiftOverlayDependenciesIDs.value();
auto optionalLinkLibraries = getLinkLibraryArray(linkLibraryArrayID);
if (!optionalLinkLibraries)
llvm::report_fatal_error("Bad Link Libraries info");
linkLibraries = *optionalLinkLibraries;
auto optionalMacroDependencies =
getMacroDependenciesArray(macroDependencyArrayID);
if (!optionalMacroDependencies)
llvm::report_fatal_error("Bad Macro Dependencies info");
macroDependencies = *optionalMacroDependencies;
break;
}
case SWIFT_INTERFACE_MODULE_DETAILS_NODE: {
if (!hasCurrentModule)
llvm::report_fatal_error(
"Unexpected SWIFT_TEXTUAL_MODULE_DETAILS_NODE record");
unsigned outputPathFileID, interfaceFileID,
compiledModuleCandidatesArrayID, buildCommandLineArrayID,
extraPCMArgsArrayID, contextHashID, isFramework, isStatic,
bridgingHeaderFileID, sourceFilesArrayID, bridgingSourceFilesArrayID,
bridgingModuleDependenciesArrayID, CASFileSystemRootID,
bridgingHeaderIncludeTreeID, moduleCacheKeyID, userModuleVersionID;
SwiftInterfaceModuleDetailsLayout::readRecord(
Scratch, outputPathFileID, interfaceFileID,
compiledModuleCandidatesArrayID, buildCommandLineArrayID,
extraPCMArgsArrayID, contextHashID, isFramework, isStatic,
bridgingHeaderFileID, sourceFilesArrayID, bridgingSourceFilesArrayID,
bridgingModuleDependenciesArrayID, CASFileSystemRootID,
bridgingHeaderIncludeTreeID, moduleCacheKeyID, userModuleVersionID);
auto outputModulePath = getIdentifier(outputPathFileID);
if (!outputModulePath)
llvm::report_fatal_error("Bad .swiftmodule output path");
std::optional<std::string> optionalSwiftInterfaceFile;
if (interfaceFileID != 0) {
auto swiftInterfaceFile = getIdentifier(interfaceFileID);
if (!swiftInterfaceFile)
llvm::report_fatal_error("Bad swift interface file path");
optionalSwiftInterfaceFile = *swiftInterfaceFile;
}
auto compiledModuleCandidates =
getStringArray(compiledModuleCandidatesArrayID);
if (!compiledModuleCandidates)
llvm::report_fatal_error("Bad compiled module candidates");
auto commandLine = getStringArray(buildCommandLineArrayID);
if (!commandLine)
llvm::report_fatal_error("Bad command line");
auto extraPCMArgs = getStringArray(extraPCMArgsArrayID);
if (!extraPCMArgs)
llvm::report_fatal_error("Bad PCM Args set");
auto contextHash = getIdentifier(contextHashID);
if (!contextHash)
llvm::report_fatal_error("Bad context hash");
// forSwiftInterface API demands references here.
std::vector<StringRef> buildCommandRefs;
for (auto &arg : *commandLine)
buildCommandRefs.push_back(arg);
std::vector<StringRef> extraPCMRefs;
for (auto &arg : *extraPCMArgs)
extraPCMRefs.push_back(arg);
std::vector<StringRef> compiledCandidatesRefs;
for (auto &cc : compiledCandidatesRefs)
compiledCandidatesRefs.push_back(cc);
auto rootFileSystemID = getIdentifier(CASFileSystemRootID);
if (!rootFileSystemID)
llvm::report_fatal_error("Bad CASFileSystem RootID");
auto moduleCacheKey = getIdentifier(moduleCacheKeyID);
if (!moduleCacheKey)
llvm::report_fatal_error("Bad moduleCacheKey");
auto userModuleVersion = getIdentifier(userModuleVersionID);
if (!userModuleVersion)
llvm::report_fatal_error("Bad userModuleVersion");
// Form the dependencies storage object
auto moduleDep = ModuleDependencyInfo::forSwiftInterfaceModule(
outputModulePath.value(), optionalSwiftInterfaceFile.value(),
compiledCandidatesRefs, buildCommandRefs, importStatements,
optionalImportStatements, linkLibraries, extraPCMRefs, *contextHash,
isFramework, isStatic, *rootFileSystemID, *moduleCacheKey,
*userModuleVersion);
addCommonDependencyInfo(moduleDep);
addSwiftCommonDependencyInfo(moduleDep);
addSwiftTextualDependencyInfo(
moduleDep, bridgingHeaderFileID, bridgingSourceFilesArrayID,
bridgingModuleDependenciesArrayID, bridgingHeaderIncludeTreeID);
cache.recordDependency(currentModuleName, std::move(moduleDep));
hasCurrentModule = false;
break;
}
case SWIFT_SOURCE_MODULE_DETAILS_NODE: {
if (!hasCurrentModule)
llvm::report_fatal_error(
"Unexpected SWIFT_SOURCE_MODULE_DETAILS_NODE record");
unsigned extraPCMArgsArrayID, bridgingHeaderFileID, sourceFilesArrayID,
bridgingSourceFilesArrayID, bridgingModuleDependenciesArrayID,
CASFileSystemRootID, bridgingHeaderIncludeTreeID,
buildCommandLineArrayID, bridgingHeaderBuildCommandLineArrayID;
SwiftSourceModuleDetailsLayout::readRecord(
Scratch, extraPCMArgsArrayID, bridgingHeaderFileID,
sourceFilesArrayID, bridgingSourceFilesArrayID,
bridgingModuleDependenciesArrayID, CASFileSystemRootID,
bridgingHeaderIncludeTreeID, buildCommandLineArrayID,
bridgingHeaderBuildCommandLineArrayID);
auto extraPCMArgs = getStringArray(extraPCMArgsArrayID);
if (!extraPCMArgs)
llvm::report_fatal_error("Bad PCM Args set");
std::vector<StringRef> extraPCMRefs;
for (auto &arg : *extraPCMArgs)
extraPCMRefs.push_back(arg);
auto rootFileSystemID = getIdentifier(CASFileSystemRootID);
if (!rootFileSystemID)
llvm::report_fatal_error("Bad CASFileSystem RootID");
auto commandLine = getStringArray(buildCommandLineArrayID);
if (!commandLine)
llvm::report_fatal_error("Bad command line");
std::vector<StringRef> buildCommandRefs;
for (auto &arg : *commandLine)
buildCommandRefs.push_back(arg);
std::vector<StringRef> bridgingHeaderBuildCommandRefs;
auto bridgingHeaderCommandLine =
getStringArray(bridgingHeaderBuildCommandLineArrayID);
if (!bridgingHeaderCommandLine)
llvm::report_fatal_error("Bad bridging header command line");
for (auto &arg : *bridgingHeaderCommandLine)
bridgingHeaderBuildCommandRefs.push_back(arg);
// Form the dependencies storage object
auto moduleDep = ModuleDependencyInfo::forSwiftSourceModule(
*rootFileSystemID, buildCommandRefs, importStatements,
optionalImportStatements, bridgingHeaderBuildCommandRefs,
extraPCMRefs);
// Add source files
auto sourceFiles = getStringArray(sourceFilesArrayID);
if (!sourceFiles)
llvm::report_fatal_error("Bad bridging source files");
for (const auto &file : *sourceFiles)
moduleDep.addSourceFile(file);
addCommonDependencyInfo(moduleDep);
addSwiftCommonDependencyInfo(moduleDep);
addSwiftTextualDependencyInfo(
moduleDep, bridgingHeaderFileID, bridgingSourceFilesArrayID,
bridgingModuleDependenciesArrayID, bridgingHeaderIncludeTreeID);
cache.recordDependency(currentModuleName, std::move(moduleDep));
hasCurrentModule = false;
break;
}
case SWIFT_BINARY_MODULE_DETAILS_NODE: {
if (!hasCurrentModule)
llvm::report_fatal_error(
"Unexpected SWIFT_BINARY_MODULE_DETAILS_NODE record");
unsigned compiledModulePathID, moduleDocPathID, moduleSourceInfoPathID,
headerImportID, definingInterfacePathID,
headerModuleDependenciesArrayID, headerImportsSourceFilesArrayID,
isFramework, isStatic, moduleCacheKeyID, userModuleVersionID;
SwiftBinaryModuleDetailsLayout::readRecord(
Scratch, compiledModulePathID, moduleDocPathID,
moduleSourceInfoPathID, headerImportID, definingInterfacePathID,
headerModuleDependenciesArrayID, headerImportsSourceFilesArrayID,
isFramework, isStatic, moduleCacheKeyID, userModuleVersionID);
auto compiledModulePath = getIdentifier(compiledModulePathID);
if (!compiledModulePath)
llvm::report_fatal_error("Bad compiled module path");
auto moduleDocPath = getIdentifier(moduleDocPathID);
if (!moduleDocPath)
llvm::report_fatal_error("Bad module doc path");
auto moduleSourceInfoPath = getIdentifier(moduleSourceInfoPathID);
if (!moduleSourceInfoPath)
llvm::report_fatal_error("Bad module source info path");
auto moduleCacheKey = getIdentifier(moduleCacheKeyID);
if (!moduleCacheKey)
llvm::report_fatal_error("Bad moduleCacheKey");
auto userModuleVersion = getIdentifier(userModuleVersionID);
if (!userModuleVersion)
llvm::report_fatal_error("Bad userModuleVersion");
auto headerImport = getIdentifier(headerImportID);
if (!headerImport)
llvm::report_fatal_error(
"Bad binary direct dependencies: no header import");
auto definingInterfacePath = getIdentifier(definingInterfacePathID);
if (!definingInterfacePath)
llvm::report_fatal_error(
"Bad binary direct dependencies: no defining interface path");
// Form the dependencies storage object
auto moduleDep = ModuleDependencyInfo::forSwiftBinaryModule(
*compiledModulePath, *moduleDocPath, *moduleSourceInfoPath,
currentModuleImports, currentOptionalModuleImports, linkLibraries,
*headerImport, *definingInterfacePath, isFramework, isStatic,
*moduleCacheKey, *userModuleVersion);
addCommonDependencyInfo(moduleDep);
addSwiftCommonDependencyInfo(moduleDep);
auto headerModuleDependencies =
getStringArray(headerModuleDependenciesArrayID);
if (!headerModuleDependencies)
llvm::report_fatal_error("Bad binary direct dependencies: no header "
"import module dependencies");
llvm::StringSet<> alreadyAdded;
std::vector<ModuleDependencyID> clangHeaderDependencyIDs;
for (const auto &headerDepName : *headerModuleDependencies)
clangHeaderDependencyIDs.push_back(
ModuleDependencyID{headerDepName, ModuleDependencyKind::Clang});
moduleDep.setHeaderClangDependencies(clangHeaderDependencyIDs);
auto headerImportsSourceFiles =
getStringArray(headerImportsSourceFilesArrayID);
if (!headerImportsSourceFiles)
llvm::report_fatal_error(
"Bad binary direct dependencies: no header import source files");
for (const auto &depSource : *headerImportsSourceFiles)
moduleDep.addHeaderSourceFile(depSource);
cache.recordDependency(currentModuleName, std::move(moduleDep));
hasCurrentModule = false;
break;
}
case SWIFT_PLACEHOLDER_MODULE_DETAILS_NODE: {
if (!hasCurrentModule)
llvm::report_fatal_error(
"Unexpected SWIFT_PLACEHOLDER_MODULE_DETAILS_NODE record");
unsigned compiledModulePathID, moduleDocPathID, moduleSourceInfoPathID;
SwiftPlaceholderModuleDetailsLayout::readRecord(
Scratch, compiledModulePathID, moduleDocPathID,
moduleSourceInfoPathID);
auto compiledModulePath = getIdentifier(compiledModulePathID);
if (!compiledModulePath)
llvm::report_fatal_error("Bad compiled module path");
auto moduleDocPath = getIdentifier(moduleDocPathID);
if (!moduleDocPath)
llvm::report_fatal_error("Bad module doc path");
auto moduleSourceInfoPath = getIdentifier(moduleSourceInfoPathID);
if (!moduleSourceInfoPath)
llvm::report_fatal_error("Bad module source info path");
// Form the dependencies storage object
auto moduleDep = ModuleDependencyInfo::forPlaceholderSwiftModuleStub(
*compiledModulePath, *moduleDocPath, *moduleSourceInfoPath);
cache.recordDependency(currentModuleName, std::move(moduleDep));
hasCurrentModule = false;
break;
}
case CLANG_MODULE_DETAILS_NODE: {
if (!hasCurrentModule)
llvm::report_fatal_error("Unexpected CLANG_MODULE_DETAILS_NODE record");
unsigned pcmOutputPathID, mappedPCMPathID, moduleMapPathID, contextHashID,
commandLineArrayID, fileDependenciesArrayID, capturedPCMArgsArrayID,
CASFileSystemRootID, clangIncludeTreeRootID, moduleCacheKeyID,
isSystem;
ClangModuleDetailsLayout::readRecord(
Scratch, pcmOutputPathID, mappedPCMPathID, moduleMapPathID,
contextHashID, commandLineArrayID, fileDependenciesArrayID,
capturedPCMArgsArrayID, CASFileSystemRootID, clangIncludeTreeRootID,
moduleCacheKeyID, isSystem);
auto pcmOutputPath = getIdentifier(pcmOutputPathID);
if (!pcmOutputPath)
llvm::report_fatal_error("Bad pcm output path");
auto mappedPCMPath = getIdentifier(mappedPCMPathID);
if (!mappedPCMPath)
llvm::report_fatal_error("Bad mapped pcm path");
auto moduleMapPath = getIdentifier(moduleMapPathID);
if (!moduleMapPath)
llvm::report_fatal_error("Bad module map path");
auto contextHash = getIdentifier(contextHashID);
if (!contextHash)
llvm::report_fatal_error("Bad context hash");
auto commandLineArgs = getStringArray(commandLineArrayID);
if (!commandLineArgs)
llvm::report_fatal_error("Bad command line");
auto fileDependencies = getStringArray(fileDependenciesArrayID);
if (!fileDependencies)
llvm::report_fatal_error("Bad file dependencies");
auto capturedPCMArgs = getStringArray(capturedPCMArgsArrayID);
if (!capturedPCMArgs)
llvm::report_fatal_error("Bad captured PCM Args");
auto rootFileSystemID = getIdentifier(CASFileSystemRootID);
if (!rootFileSystemID)
llvm::report_fatal_error("Bad CASFileSystem RootID");
auto clangIncludeTreeRoot = getIdentifier(clangIncludeTreeRootID);
if (!clangIncludeTreeRoot)
llvm::report_fatal_error("Bad clang include tree ID");
auto moduleCacheKey = getIdentifier(moduleCacheKeyID);
if (!moduleCacheKey)
llvm::report_fatal_error("Bad moduleCacheKey");
// Form the dependencies storage object
auto moduleDep = ModuleDependencyInfo::forClangModule(
*pcmOutputPath, *mappedPCMPath, *moduleMapPath, *contextHash,
*commandLineArgs, *fileDependencies, *capturedPCMArgs, linkLibraries,
*rootFileSystemID, *clangIncludeTreeRoot, *moduleCacheKey, isSystem);
addCommonDependencyInfo(moduleDep);
cache.recordDependency(currentModuleName, std::move(moduleDep));
hasCurrentModule = false;
break;
}
default: {
llvm::report_fatal_error("Unknown record ID");
}
}
}
return false;
}
bool ModuleDependenciesCacheDeserializer::readInterModuleDependenciesCache(
ModuleDependenciesCache &cache) {
using namespace graph_block;
if (readSignature())
return true;
if (enterGraphBlock())
return true;
if (readMetadata(cache.scannerContextHash))
return true;
if (readGraph(cache))
return true;
return false;
}
std::optional<std::string>
ModuleDependenciesCacheDeserializer::getIdentifier(unsigned n) {
if (n == 0)
return std::string();
--n;
if (n >= Identifiers.size())
return std::nullopt;
return Identifiers[n];
}
std::optional<std::vector<std::string>>
ModuleDependenciesCacheDeserializer::getStringArray(unsigned n) {
if (n == 0)
return std::vector<std::string>();
--n;
if (n >= ArraysOfIdentifierIDs.size())
return std::nullopt;
auto &identifierIDs = ArraysOfIdentifierIDs[n];
auto IDtoStringMap = [this](unsigned id) {
auto identifier = getIdentifier(id);
if (!identifier)
llvm::report_fatal_error("Bad identifier array element");
return *identifier;
};
std::vector<std::string> result;
result.reserve(identifierIDs.size());
std::transform(identifierIDs.begin(), identifierIDs.end(),
std::back_inserter(result), IDtoStringMap);
return result;
}
std::optional<std::vector<LinkLibrary>>
ModuleDependenciesCacheDeserializer::getLinkLibraryArray(unsigned n) {
if (n == 0)
return std::vector<LinkLibrary>();
--n;
if (n >= ArraysOfLinkLibraryIDs.size())
return std::nullopt;
auto &llIDs = ArraysOfLinkLibraryIDs[n];
auto IDtoLLMap = [this](unsigned index) { return LinkLibraries[index]; };
std::vector<LinkLibrary> result;
result.reserve(llIDs.size());
std::transform(llIDs.begin(), llIDs.end(), std::back_inserter(result),
IDtoLLMap);
return result;
}
std::optional<std::vector<std::pair<std::string, MacroPluginDependency>>>
ModuleDependenciesCacheDeserializer::getMacroDependenciesArray(unsigned n) {
if (n == 0)
return std::vector<std::pair<std::string, MacroPluginDependency>>();
--n;
if (n >= ArraysOfMacroDependenciesIDs.size())
return std::nullopt;
auto &llIDs = ArraysOfMacroDependenciesIDs[n];
auto IDtoLLMap = [this](unsigned index) { return MacroDependencies[index]; };
std::vector<std::pair<std::string, MacroPluginDependency>> result;
result.reserve(llIDs.size());
std::transform(llIDs.begin(), llIDs.end(), std::back_inserter(result),
IDtoLLMap);
return result;
}
std::optional<std::vector<ScannerImportStatementInfo>>
ModuleDependenciesCacheDeserializer::getImportStatementInfoArray(unsigned n) {
if (n == 0)
return std::vector<ScannerImportStatementInfo>();
--n;
if (n >= ArraysOfImportStatementIDs.size())
return std::nullopt;
auto &ISIDs = ArraysOfImportStatementIDs[n];
llvm::StringMap<ScannerImportStatementInfo> addedImports;
for (const auto &importID : ISIDs) {
const auto &entry = ImportStatements[importID];
if (addedImports.contains(entry.importIdentifier)) {
auto entryIt = addedImports.find(entry.importIdentifier);
for (const auto &importLoc : entry.importLocations)
entryIt->second.addImportLocation(importLoc);
} else
addedImports.insert({entry.importIdentifier, entry});
}
std::vector<ScannerImportStatementInfo> result;
result.reserve(addedImports.size());
for (const auto &keyValPair : addedImports) {
result.push_back(keyValPair.second);
}
return result;
}
std::optional<std::vector<ScannerImportStatementInfo>>
ModuleDependenciesCacheDeserializer::getOptionalImportStatementInfoArray(
unsigned n) {
if (n == 0)
return std::vector<ScannerImportStatementInfo>();
--n;
if (n >= ArraysOfOptionalImportStatementIDs.size())
return std::nullopt;
auto &ISIDs = ArraysOfOptionalImportStatementIDs[n];
auto IDtoISMap = [this](unsigned index) { return ImportStatements[index]; };
std::vector<ScannerImportStatementInfo> result;
result.reserve(ISIDs.size());
std::transform(ISIDs.begin(), ISIDs.end(), std::back_inserter(result),
IDtoISMap);
return result;
}
std::optional<std::vector<ModuleDependencyID>>
ModuleDependenciesCacheDeserializer::getModuleDependencyIDArray(unsigned n) {
auto encodedIdentifierStringArray = getStringArray(n);
if (encodedIdentifierStringArray.has_value()) {
static const std::string textualPrefix("swiftTextual");
static const std::string binaryPrefix("swiftBinary");
static const std::string placeholderPrefix("swiftPlaceholder");
static const std::string clangPrefix("clang");
std::vector<ModuleDependencyID> result;
for (const auto &encodedIdentifierString : *encodedIdentifierStringArray) {
ModuleDependencyID id;
if (!encodedIdentifierString.compare(0, textualPrefix.size(),
textualPrefix)) {
auto moduleName =
encodedIdentifierString.substr(textualPrefix.size() + 1);
id = {moduleName, ModuleDependencyKind::SwiftInterface};
} else if (!encodedIdentifierString.compare(0, binaryPrefix.size(),
binaryPrefix)) {
auto moduleName =
encodedIdentifierString.substr(binaryPrefix.size() + 1);
id = {moduleName, ModuleDependencyKind::SwiftBinary};
} else if (!encodedIdentifierString.compare(0, placeholderPrefix.size(),
placeholderPrefix)) {
auto moduleName =
encodedIdentifierString.substr(placeholderPrefix.size() + 1);
id = {moduleName, ModuleDependencyKind::SwiftPlaceholder};
} else {
auto moduleName =
encodedIdentifierString.substr(clangPrefix.size() + 1);
id = {moduleName, ModuleDependencyKind::Clang};
}
result.push_back(id);
}
return result;
}
return std::nullopt;
}
bool swift::dependencies::module_dependency_cache_serialization::
readInterModuleDependenciesCache(llvm::MemoryBuffer &buffer,
ModuleDependenciesCache &cache) {
ModuleDependenciesCacheDeserializer deserializer(buffer.getMemBufferRef());
return deserializer.readInterModuleDependenciesCache(cache);
}
bool swift::dependencies::module_dependency_cache_serialization::