Skip to content

Commit 290cf83

Browse files
authored
Merge pull request swiftlang#83767 from artemcm/FixIncScanImportSerialization
[Dependency Scanning] Fix optional import statement serialization logic
2 parents 4becc5d + b14cbbb commit 290cf83

File tree

6 files changed

+95
-42
lines changed

6 files changed

+95
-42
lines changed

include/swift/DependencyScan/DependencyScanImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ struct swiftscan_dependency_info_s {
6565
/// The list of source import infos.
6666
swiftscan_import_info_set_t *imports;
6767

68+
/// The list of source optional import infos.
69+
swiftscan_import_info_set_t *optional_imports;
70+
6871
/// Specific details of a particular kind of module.
6972
swiftscan_module_details_t details;
7073
};

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ using llvm::BCVBR;
4141
const unsigned char MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE[] = {'I', 'M', 'D','C'};
4242
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 10;
4343
/// Increment this on every change.
44-
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 3;
44+
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 4;
4545

4646
/// Various identifiers in this format will rely on having their strings mapped
4747
/// using this ID.

lib/DependencyScan/DependencyScanJSON.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,13 @@ void writeLinkLibraries(llvm::raw_ostream &out,
228228

229229
void writeImportInfos(llvm::raw_ostream &out,
230230
const swiftscan_import_info_set_t *imports,
231-
unsigned indentLevel, bool trailingComma) {
231+
bool optional, unsigned indentLevel,
232+
bool trailingComma) {
232233
out.indent(indentLevel * 2);
233-
out << "\"imports\": ";
234+
if (optional)
235+
out << "\"optionalImports\": ";
236+
else
237+
out << "\"imports\": ";
234238
out << "[\n";
235239

236240
for (size_t i = 0; i < imports->count; ++i) {
@@ -441,7 +445,9 @@ void writeJSON(llvm::raw_ostream &out,
441445
/*trailingComma=*/true);
442446
writeLinkLibraries(out, moduleInfo.link_libraries,
443447
3, /*trailingComma=*/true);
444-
writeImportInfos(out, moduleInfo.imports,
448+
writeImportInfos(out, moduleInfo.imports, /*optional*/ false,
449+
3, /*trailingComma=*/true);
450+
writeImportInfos(out, moduleInfo.optional_imports, /*optional*/ true,
445451
3, /*trailingComma=*/true);
446452
}
447453
// Swift and Clang-specific details.

lib/DependencyScan/ModuleDependencyCacheSerialization.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class ModuleDependenciesCacheDeserializer {
4949
std::vector<std::vector<uint64_t>> ArraysOfMacroDependenciesIDs;
5050
std::vector<ScannerImportStatementInfo> ImportStatements;
5151
std::vector<std::vector<uint64_t>> ArraysOfImportStatementIDs;
52-
std::vector<std::vector<uint64_t>> ArraysOfSearchPathIDs;
5352
std::vector<std::vector<uint64_t>> ArraysOfOptionalImportStatementIDs;
53+
std::vector<std::vector<uint64_t>> ArraysOfSearchPathIDs;
5454

5555
llvm::BitstreamCursor Cursor;
5656
SmallVector<uint64_t, 64> Scratch;
@@ -1170,6 +1170,7 @@ class ModuleDependenciesCacheSerializer {
11701170
unsigned writeImportStatementInfos(const ModuleDependencyInfo &dependencyInfo,
11711171
bool optional);
11721172
void writeImportStatementInfosArray(unsigned startIndex, unsigned count);
1173+
void writeOptionalImportStatementInfosArray(unsigned startIndex, unsigned count);
11731174

11741175
void writeModuleInfo(ModuleDependencyID moduleID,
11751176
const ModuleDependencyInfo &dependencyInfo);
@@ -1478,7 +1479,7 @@ void ModuleDependenciesCacheSerializer::writeImportStatementInfos(
14781479
}
14791480
auto optionalEntries = optionalImportInfoArrayMap.at(moduleID);
14801481
if (optionalEntries.second != 0) {
1481-
writeImportStatementInfosArray(optionalEntries.first, optionalEntries.second);
1482+
writeOptionalImportStatementInfosArray(optionalEntries.first, optionalEntries.second);
14821483
OptionalImportInfosArrayIDsMap.insert({moduleID, lastOptionalImportInfoArrayIndex++});
14831484
}
14841485
}
@@ -1530,6 +1531,15 @@ void ModuleDependenciesCacheSerializer::writeImportStatementInfosArray(
15301531
Out, ScratchRecord, AbbrCodes[ImportStatementArrayLayout::Code], vec);
15311532
}
15321533

1534+
void ModuleDependenciesCacheSerializer::writeOptionalImportStatementInfosArray(
1535+
unsigned startIndex, unsigned count) {
1536+
using namespace graph_block;
1537+
std::vector<unsigned> vec(count);
1538+
std::iota(vec.begin(), vec.end(), startIndex);
1539+
OptionalImportStatementArrayLayout::emitRecord(
1540+
Out, ScratchRecord, AbbrCodes[OptionalImportStatementArrayLayout::Code], vec);
1541+
}
1542+
15331543
void ModuleDependenciesCacheSerializer::writeModuleInfo(
15341544
ModuleDependencyID moduleID, const ModuleDependencyInfo &dependencyInfo) {
15351545
using namespace graph_block;
@@ -1965,6 +1975,7 @@ void ModuleDependenciesCacheSerializer::writeInterModuleDependenciesCache(
19651975
registerRecordAbbr<SearchPathArrayLayout>();
19661976
registerRecordAbbr<ImportStatementLayout>();
19671977
registerRecordAbbr<ImportStatementArrayLayout>();
1978+
registerRecordAbbr<OptionalImportStatementArrayLayout>();
19681979
registerRecordAbbr<ModuleInfoLayout>();
19691980
registerRecordAbbr<SwiftSourceModuleDetailsLayout>();
19701981
registerRecordAbbr<SwiftInterfaceModuleDetailsLayout>();

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -922,37 +922,44 @@ static swiftscan_dependency_graph_t generateFullDependencyGraph(
922922
}
923923
moduleInfo->link_libraries = linkLibrarySet;
924924

925-
// Create source import infos set for this module
926-
auto imports = moduleDependencyInfo.getModuleImports();
927-
swiftscan_import_info_set_t *importInfoSet =
928-
new swiftscan_import_info_set_t;
929-
importInfoSet->count = imports.size();
930-
importInfoSet->imports = new swiftscan_import_info_t[importInfoSet->count];
931-
for (size_t i = 0; i < imports.size(); ++i) {
932-
const auto &ii = imports[i];
933-
swiftscan_import_info_s *iInfo = new swiftscan_import_info_s;
934-
iInfo->import_identifier = create_clone(ii.importIdentifier.c_str());
935-
iInfo->access_level =
936-
static_cast<swiftscan_access_level_t>(ii.accessLevel);
937-
938-
const auto &sourceLocations = ii.importLocations;
939-
swiftscan_source_location_set_t *sourceLocSet =
940-
new swiftscan_source_location_set_t;
941-
sourceLocSet->count = sourceLocations.size();
942-
sourceLocSet->source_locations =
943-
new swiftscan_source_location_t[sourceLocSet->count];
944-
for (size_t j = 0; j < sourceLocations.size(); ++j) {
945-
const auto &sl = sourceLocations[j];
946-
swiftscan_source_location_s *slInfo = new swiftscan_source_location_s;
947-
slInfo->buffer_identifier = create_clone(sl.bufferIdentifier.c_str());
948-
slInfo->line_number = sl.lineNumber;
949-
slInfo->column_number = sl.columnNumber;
950-
sourceLocSet->source_locations[j] = slInfo;
925+
auto createImportSetInfo = [&](ArrayRef<ScannerImportStatementInfo> imports)
926+
-> swiftscan_import_info_set_t * {
927+
swiftscan_import_info_set_t *importInfoSet =
928+
new swiftscan_import_info_set_t;
929+
importInfoSet->count = imports.size();
930+
importInfoSet->imports =
931+
new swiftscan_import_info_t[importInfoSet->count];
932+
for (size_t i = 0; i < imports.size(); ++i) {
933+
const auto &ii = imports[i];
934+
swiftscan_import_info_s *iInfo = new swiftscan_import_info_s;
935+
iInfo->import_identifier = create_clone(ii.importIdentifier.c_str());
936+
iInfo->access_level =
937+
static_cast<swiftscan_access_level_t>(ii.accessLevel);
938+
939+
const auto &sourceLocations = ii.importLocations;
940+
swiftscan_source_location_set_t *sourceLocSet =
941+
new swiftscan_source_location_set_t;
942+
sourceLocSet->count = sourceLocations.size();
943+
sourceLocSet->source_locations =
944+
new swiftscan_source_location_t[sourceLocSet->count];
945+
for (size_t j = 0; j < sourceLocations.size(); ++j) {
946+
const auto &sl = sourceLocations[j];
947+
swiftscan_source_location_s *slInfo = new swiftscan_source_location_s;
948+
slInfo->buffer_identifier = create_clone(sl.bufferIdentifier.c_str());
949+
slInfo->line_number = sl.lineNumber;
950+
slInfo->column_number = sl.columnNumber;
951+
sourceLocSet->source_locations[j] = slInfo;
952+
}
953+
iInfo->source_locations = sourceLocSet;
954+
importInfoSet->imports[i] = iInfo;
951955
}
952-
iInfo->source_locations = sourceLocSet;
953-
importInfoSet->imports[i] = iInfo;
954-
}
955-
moduleInfo->imports = importInfoSet;
956+
return importInfoSet;
957+
};
958+
// Create source import infos set for this module
959+
moduleInfo->imports =
960+
createImportSetInfo(moduleDependencyInfo.getModuleImports());
961+
moduleInfo->optional_imports =
962+
createImportSetInfo(moduleDependencyInfo.getOptionalModuleImports());
956963
}
957964

958965
swiftscan_dependency_graph_t result = new swiftscan_dependency_graph_s;

test/ScanDependencies/serialized_imports.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// RUN: %empty-directory(%t/module-cache)
44

55
// Run the scanner once, emitting the serialized scanner cache
6-
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -Rdependency-scan-cache -serialize-dependency-scan-cache -load-dependency-scan-cache -dependency-scan-cache-path %t/cache.moddepcache -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -enable-cross-import-overlays 2>&1 | %FileCheck %s -check-prefix CHECK-REMARK-SAVE
6+
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -Rdependency-scan-cache -serialize-dependency-scan-cache -load-dependency-scan-cache -dependency-scan-cache-path %t/cache.moddepcache -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -enable-cross-import-overlays -module-name deps 2>&1 | %FileCheck %s -check-prefix CHECK-REMARK-SAVE
77
// RUN: llvm-bcanalyzer --dump %t/cache.moddepcache > %t/cache.moddepcache.initial.dump.txt
8+
// RUN: %validate-json %t/deps.json | %FileCheck %s -check-prefix CHECK-IMPORTS
89

910
// Run the scanner again, but now re-using previously-serialized cache
10-
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -Rdependency-scan-cache -serialize-dependency-scan-cache -load-dependency-scan-cache -dependency-scan-cache-path %t/cache.moddepcache -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -enable-cross-import-overlays 2>&1 | %FileCheck %s -check-prefix CHECK-REMARK-LOAD
11+
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -Rdependency-scan-cache -serialize-dependency-scan-cache -load-dependency-scan-cache -dependency-scan-cache-path %t/cache.moddepcache -module-cache-path %t/module-cache %s -o %t/deps_incremental.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -enable-cross-import-overlays -module-name deps 2>&1 | %FileCheck %s -check-prefix CHECK-REMARK-LOAD
1112
// RUN: llvm-bcanalyzer --dump %t/cache.moddepcache > %t/cache.moddepcache.dump.txt
13+
// RUN: %validate-json %t/deps_incremental.json | %FileCheck %s -check-prefix CHECK-IMPORTS
1214

1315
// Ensure that the initial scan, and the secondary scan which just re-used the initial scan's results report
1416
// the same number of import statement nodes, ensuring that serialization-deserialization did not affect
@@ -22,8 +24,32 @@
2224
// CHECK-REMARK-SAVE: remark: Incremental module scan: Serializing module scanning dependency cache to:
2325
// CHECK-REMARK-LOAD: remark: Incremental module scan: Re-using serialized module scanning dependency cache from:
2426

25-
import E
26-
27-
28-
27+
// CHECK-IMPORTS: "modulePath": "deps.swiftmodule",
28+
// CHECK-IMPORTS: "imports": [
29+
// CHECK-IMPORTS-NEXT: {
30+
// CHECK-IMPORTS-NEXT: "identifier": "Swift",
31+
// CHECK-IMPORTS-NEXT: "accessLevel": "public"
32+
// CHECK-IMPORTS-NEXT: },
33+
// CHECK-IMPORTS-NEXT: {
34+
// CHECK-IMPORTS-NEXT: "identifier": "SwiftOnoneSupport",
35+
// CHECK-IMPORTS-NEXT: "accessLevel": "public"
36+
// CHECK-IMPORTS-NEXT: },
37+
// CHECK-IMPORTS-NEXT: {
38+
// CHECK-IMPORTS-NEXT: "identifier": "E",
39+
// CHECK-IMPORTS-NEXT: "accessLevel": "public",
40+
// CHECK-IMPORTS-NEXT: "importLocations": [
41+
// CHECK-IMPORTS-NEXT: {
42+
// CHECK-IMPORTS-NEXT: "bufferIdentifier": "{{.*}}serialized_imports.swift",
43+
// CHECK-IMPORTS-NEXT: "linuNumber": 54,
44+
// CHECK-IMPORTS-NEXT: "columnNumber": 8
45+
// CHECK-IMPORTS-NEXT: }
46+
47+
// CHECK-IMPORTS: "optionalImports": [
48+
// CHECK-IMPORTS-NEXT: {
49+
// CHECK-IMPORTS-NEXT: "identifier": "E_Private",
50+
// CHECK-IMPORTS-NEXT: "accessLevel": "public"
51+
// CHECK-IMPORTS-NEXT: }
52+
// CHECK-IMPORTS-NEXT: ],
2953

54+
import E
55+
import E.Private

0 commit comments

Comments
 (0)