Skip to content

Commit b14cbbb

Browse files
committed
[Dependency Scanning] Add serialization of optional imports to the JSON output
And to the corresponding graph data structure the output gets generated from
1 parent bc30953 commit b14cbbb

File tree

5 files changed

+58
-36
lines changed

5 files changed

+58
-36
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/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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@
4040
// CHECK-IMPORTS-NEXT: "importLocations": [
4141
// CHECK-IMPORTS-NEXT: {
4242
// CHECK-IMPORTS-NEXT: "bufferIdentifier": "{{.*}}serialized_imports.swift",
43-
// CHECK-IMPORTS-NEXT: "linuNumber": 49,
43+
// CHECK-IMPORTS-NEXT: "linuNumber": 54,
4444
// CHECK-IMPORTS-NEXT: "columnNumber": 8
4545
// CHECK-IMPORTS-NEXT: }
46-
// 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"
4751
// CHECK-IMPORTS-NEXT: }
52+
// CHECK-IMPORTS-NEXT: ],
4853

4954
import E
55+
import E.Private

0 commit comments

Comments
 (0)