Skip to content

Commit e9691e4

Browse files
authored
Merge pull request #32450 from nkcsgexi/codable-json-format
ExplictModuleLoader: update acceptable JSON format to be more Codable friendly
2 parents 1cd33f9 + 23ef061 commit e9691e4

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,6 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
14281428
}
14291429

14301430
struct ExplicitSwiftModuleLoader::Implementation {
1431-
14321431
// Information about explicitly specified Swift module files.
14331432
struct ExplicitModuleInfo {
14341433
// Path of the .swiftmodule file.
@@ -1451,44 +1450,48 @@ struct ExplicitSwiftModuleLoader::Implementation {
14511450
return Saver.save(cast<llvm::yaml::ScalarNode>(N)->getValue(Buffer));
14521451
}
14531452

1454-
bool parseSingleModuleEntry(llvm::yaml::KeyValueNode &node) {
1453+
bool parseSingleModuleEntry(llvm::yaml::Node &node) {
14551454
using namespace llvm::yaml;
1456-
auto moduleName = getScalaNodeText(node.getKey());
1457-
auto insertRes = ExplicitModuleMap.insert({moduleName,
1458-
ExplicitModuleInfo()});
1459-
if (!insertRes.second) {
1460-
return true;
1461-
}
1462-
auto moduleDetails = dyn_cast<MappingNode>(node.getValue());
1463-
if (!moduleDetails)
1455+
auto *mapNode = dyn_cast<MappingNode>(&node);
1456+
if (!mapNode)
14641457
return true;
1465-
for (auto &entry: *moduleDetails) {
1458+
StringRef moduleName;
1459+
ExplicitModuleInfo result;
1460+
for (auto &entry: *mapNode) {
14661461
auto key = getScalaNodeText(entry.getKey());
14671462
auto val = getScalaNodeText(entry.getValue());
1468-
if (key == "SwiftModulePath") {
1469-
insertRes.first->second.modulePath = val;
1463+
if (key == "SwiftModule") {
1464+
moduleName = val;
1465+
} else if (key == "SwiftModulePath") {
1466+
result.modulePath = val;
14701467
} else if (key == "SwiftDocPath") {
1471-
insertRes.first->second.moduleDocPath = val;
1468+
result.moduleDocPath = val;
14721469
} else if (key == "SwiftSourceInfoPath") {
1473-
insertRes.first->second.moduleSourceInfoPath = val;
1470+
result.moduleSourceInfoPath = val;
14741471
} else {
1475-
return true;
1472+
// Being forgiving for future fields.
1473+
continue;
14761474
}
14771475
}
1476+
if (moduleName.empty())
1477+
return true;
1478+
ExplicitModuleMap[moduleName] = std::move(result);
14781479
return false;
14791480
}
1480-
// {
1481-
// "A": {
1481+
// [
1482+
// {
1483+
// "SwiftModule": "A",
14821484
// "SwiftModulePath": "A.swiftmodule",
14831485
// "SwiftDocPath": "A.swiftdoc",
14841486
// "SwiftSourceInfoPath": "A.swiftsourceinfo"
14851487
// },
1486-
// "B": {
1488+
// {
1489+
// "SwiftModule": "B",
14871490
// "SwiftModulePath": "B.swiftmodule",
14881491
// "SwiftDocPath": "B.swiftdoc",
14891492
// "SwiftSourceInfoPath": "B.swiftsourceinfo"
14901493
// }
1491-
// }
1494+
// ]
14921495
void parseSwiftExplicitModuleMap(StringRef fileName) {
14931496
using namespace llvm::yaml;
14941497
// Load the input file.
@@ -1504,7 +1507,7 @@ struct ExplicitSwiftModuleLoader::Implementation {
15041507
Ctx.SourceMgr.getLLVMSourceMgr());
15051508
for (auto DI = Stream.begin(); DI != Stream.end(); ++ DI) {
15061509
assert(DI != Stream.end() && "Failed to read a document");
1507-
if (auto *MN = dyn_cast_or_null<MappingNode>(DI->getRoot())) {
1510+
if (auto *MN = dyn_cast_or_null<SequenceNode>(DI->getRoot())) {
15081511
for (auto &entry: *MN) {
15091512
if (parseSingleModuleEntry(entry)) {
15101513
Ctx.Diags.diagnose(SourceLoc(),
@@ -1559,8 +1562,12 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
15591562
auto &fs = *Ctx.SourceMgr.getFileSystem();
15601563
// Open .swiftmodule file
15611564
auto moduleBuf = fs.getBufferForFile(moduleInfo.modulePath);
1562-
if (!moduleBuf)
1565+
if (!moduleBuf) {
1566+
// We cannot read the module content, diagnose.
1567+
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
1568+
moduleInfo.modulePath);
15631569
return moduleBuf.getError();
1570+
}
15641571
*ModuleBuffer = std::move(moduleBuf.get());
15651572

15661573
// Open .swiftdoc file

test/ScanDependencies/explicit-module-map.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
// RUN: echo "public func foo() {}" >> %t/foo.swift
66
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/Foo.swiftmodule -emit-module-doc-path %t/inputs/Foo.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/Foo.swiftsourceinfo -module-cache-path %t.module-cache %t/foo.swift -module-name Foo
77

8-
// RUN: echo "{" > %t/inputs/map.json
9-
// RUN: echo "\"Foo\": {" >> %t/inputs/map.json
8+
// RUN: echo "[{" > %t/inputs/map.json
9+
// RUN: echo "\"SwiftModule\": \"Foo\"," >> %t/inputs/map.json
1010
// RUN: echo "\"SwiftModulePath\": \"%t/inputs/Foo.swiftmodule\"," >> %t/inputs/map.json
1111
// RUN: echo "\"SwiftDocPath\": \"%t/inputs/Foo.swiftdoc\"," >> %t/inputs/map.json
1212
// RUN: echo "\"SwiftSourceInfoPath\": \"%t/inputs/Foo.swiftsourceinfo\"" >> %t/inputs/map.json
13-
// RUN: echo "}" >> %t/inputs/map.json
14-
// RUN: echo "}" >> %t/inputs/map.json
13+
// RUN: echo "}]" >> %t/inputs/map.json
1514

1615
// RUN: %target-swift-ide-test -print-module-comments -module-to-print=Foo -enable-swiftsourceinfo -source-filename %s -explicit-swift-module-map-file %t/inputs/map.json | %FileCheck %s
1716

0 commit comments

Comments
 (0)