Skip to content

Commit 9a5a83a

Browse files
committed
Modify ModuleDependencyInfo so we can set swift interface module output paths consistently and avoid const_casts.
1 parent 1ce6949 commit 9a5a83a

File tree

5 files changed

+51
-35
lines changed

5 files changed

+51
-35
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class SwiftInterfaceModuleDependenciesStorage
266266
: public ModuleDependencyInfoStorageBase {
267267
public:
268268
/// Destination output path
269-
const std::string moduleOutputPath;
269+
std::string moduleOutputPath;
270270

271271
/// The Swift interface file to be used to generate the module file.
272272
const std::string swiftInterfaceFile;
@@ -275,7 +275,7 @@ class SwiftInterfaceModuleDependenciesStorage
275275
const std::vector<std::string> compiledModuleCandidates;
276276

277277
/// The hash value that will be used for the generated module
278-
const std::string contextHash;
278+
std::string contextHash;
279279

280280
/// A flag that indicates this dependency is a framework
281281
const bool isFramework;
@@ -290,22 +290,20 @@ class SwiftInterfaceModuleDependenciesStorage
290290
const std::string userModuleVersion;
291291

292292
SwiftInterfaceModuleDependenciesStorage(
293-
StringRef moduleOutputPath, StringRef swiftInterfaceFile,
293+
StringRef swiftInterfaceFile,
294294
ArrayRef<StringRef> compiledModuleCandidates,
295295
ArrayRef<ScannerImportStatementInfo> moduleImports,
296296
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
297297
ArrayRef<StringRef> buildCommandLine, ArrayRef<LinkLibrary> linkLibraries,
298-
StringRef contextHash, bool isFramework,
299-
bool isStatic, StringRef RootID, StringRef moduleCacheKey,
300-
StringRef userModuleVersion)
298+
bool isFramework, bool isStatic, StringRef RootID,
299+
StringRef moduleCacheKey, StringRef userModuleVersion)
301300
: ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftInterface,
302301
moduleImports, optionalModuleImports,
303302
linkLibraries, moduleCacheKey),
304-
moduleOutputPath(moduleOutputPath),
305303
swiftInterfaceFile(swiftInterfaceFile),
306304
compiledModuleCandidates(compiledModuleCandidates.begin(),
307305
compiledModuleCandidates.end()),
308-
contextHash(contextHash), isFramework(isFramework), isStatic(isStatic),
306+
isFramework(isFramework), isStatic(isStatic),
309307
textualModuleDetails(buildCommandLine, RootID),
310308
userModuleVersion(userModuleVersion) {}
311309

@@ -593,21 +591,18 @@ class ModuleDependencyInfo {
593591
/// Describe the module dependencies for a Swift module that can be
594592
/// built from a Swift interface file (\c .swiftinterface).
595593
static ModuleDependencyInfo forSwiftInterfaceModule(
596-
StringRef moduleOutputPath, StringRef swiftInterfaceFile,
597-
ArrayRef<StringRef> compiledCandidates, ArrayRef<StringRef> buildCommands,
594+
StringRef swiftInterfaceFile, ArrayRef<StringRef> compiledCandidates,
595+
ArrayRef<StringRef> buildCommands,
598596
ArrayRef<ScannerImportStatementInfo> moduleImports,
599597
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
600-
ArrayRef<LinkLibrary> linkLibraries,
601-
StringRef contextHash, bool isFramework, bool isStatic,
598+
ArrayRef<LinkLibrary> linkLibraries, bool isFramework, bool isStatic,
602599
StringRef CASFileSystemRootID, StringRef moduleCacheKey,
603600
StringRef userModuleVersion) {
604601
return ModuleDependencyInfo(
605602
std::make_unique<SwiftInterfaceModuleDependenciesStorage>(
606-
moduleOutputPath, swiftInterfaceFile, compiledCandidates,
607-
moduleImports, optionalModuleImports,
608-
buildCommands, linkLibraries, contextHash,
609-
isFramework, isStatic, CASFileSystemRootID, moduleCacheKey,
610-
userModuleVersion));
603+
swiftInterfaceFile, compiledCandidates, moduleImports,
604+
optionalModuleImports, buildCommands, linkLibraries, isFramework,
605+
isStatic, CASFileSystemRootID, moduleCacheKey, userModuleVersion));
611606
}
612607

613608
/// Describe the module dependencies for a serialized or parsed Swift module.
@@ -1005,6 +1000,9 @@ class ModuleDependencyInfo {
10051000
/// Set the chained bridging header buffer.
10061001
void setChainedBridgingHeaderBuffer(StringRef path, StringRef buffer);
10071002

1003+
/// Set the output path and the context hash.
1004+
void setOutputPathAndHash(StringRef outputPath, StringRef hash);
1005+
10081006
/// Collect a map from a secondary module name to a list of cross-import
10091007
/// overlays, when this current module serves as the primary module.
10101008
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>>

lib/AST/ModuleDependencies.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,21 @@ void ModuleDependencyInfo::addSourceFile(StringRef sourceFile) {
465465
}
466466
}
467467

468+
void ModuleDependencyInfo::setOutputPathAndHash(StringRef outputPath,
469+
StringRef hash) {
470+
switch (getKind()) {
471+
case swift::ModuleDependencyKind::SwiftInterface: {
472+
auto swiftInterfaceStorage =
473+
cast<SwiftInterfaceModuleDependenciesStorage>(storage.get());
474+
swiftInterfaceStorage->moduleOutputPath = outputPath.str();
475+
swiftInterfaceStorage->contextHash = hash.str();
476+
break;
477+
}
478+
default:
479+
llvm_unreachable("Unexpected dependency kind");
480+
}
481+
}
482+
468483
SwiftDependencyScanningService::SwiftDependencyScanningService() {
469484
ClangScanningService.emplace(
470485
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,

lib/DependencyScan/ModuleDependencyCacheSerialization.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,12 @@ bool ModuleDependenciesCacheDeserializer::readGraph(
585585

586586
// Form the dependencies storage object
587587
auto moduleDep = ModuleDependencyInfo::forSwiftInterfaceModule(
588-
outputModulePath.value(), optionalSwiftInterfaceFile.value(),
589-
compiledCandidatesRefs, buildCommandRefs, importStatements,
590-
optionalImportStatements, linkLibraries, *contextHash,
591-
isFramework, isStatic, *rootFileSystemID, *moduleCacheKey,
592-
*userModuleVersion);
588+
optionalSwiftInterfaceFile.value(), compiledCandidatesRefs,
589+
buildCommandRefs, importStatements, optionalImportStatements,
590+
linkLibraries, isFramework, isStatic, *rootFileSystemID,
591+
*moduleCacheKey, *userModuleVersion);
593592

593+
moduleDep.setOutputPathAndHash(*outputModulePath, *contextHash);
594594
addCommonDependencyInfo(moduleDep);
595595
addSwiftCommonDependencyInfo(moduleDep);
596596
addSwiftTextualDependencyInfo(

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,16 @@ class ExplicitModuleDependencyResolver {
211211
if (resolvingDepInfo.isSwiftPlaceholderModule())
212212
return llvm::Error::success();
213213

214+
if (outputPathResolver) {
215+
auto resolvedPath = outputPathResolver->getOutputPath();
216+
depInfo.setOutputPathAndHash(resolvedPath.outputPath.str().str(),
217+
resolvedPath.hash.str());
218+
}
219+
214220
// Add macros.
215221
for (auto &macro : macros)
216-
depInfo.addMacroDependency(
217-
macro.first(), macro.second.LibraryPath, macro.second.ExecutablePath);
222+
depInfo.addMacroDependency(macro.first(), macro.second.LibraryPath,
223+
macro.second.ExecutablePath);
218224

219225
for (auto &macro : depInfo.getMacroDependencies()) {
220226
std::string arg = macro.second.LibraryPath + "#" +
@@ -228,20 +234,13 @@ class ExplicitModuleDependencyResolver {
228234
return err;
229235

230236
if (!bridgingHeaderBuildCmd.empty())
231-
depInfo.updateBridgingHeaderCommandLine(
232-
bridgingHeaderBuildCmd);
237+
depInfo.updateBridgingHeaderCommandLine(bridgingHeaderBuildCmd);
233238
if (!resolvingDepInfo.isSwiftBinaryModule()) {
234239
depInfo.updateCommandLine(commandline);
235240
if (auto err = updateModuleCacheKey(depInfo))
236241
return err;
237242
}
238243

239-
if (outputPathResolver) {
240-
auto expandedName = outputPathResolver->getOutputPath();
241-
depInfo.updateModuleOutputPath(expandedName.outputPath.c_str());
242-
depInfo.updateContextHash(expandedName.hash.str());
243-
}
244-
245244
return llvm::Error::success();
246245
}
247246

lib/Serialization/ScanningLoaders.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,13 @@ SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath,
248248
bool isStatic = llvm::find(ArgsRefs, "-static") != ArgsRefs.end();
249249

250250
Result = ModuleDependencyInfo::forSwiftInterfaceModule(
251-
outputPathBase.str().str(), InPath, compiledCandidatesRefs,
252-
ArgsRefs, {}, {}, linkLibraries, Hash, isFramework,
253-
isStatic, {}, /*module-cache-key*/ "", UserModVer);
251+
InPath, compiledCandidatesRefs, ArgsRefs, {}, {}, linkLibraries,
252+
isFramework, isStatic, {}, /*module-cache-key*/ "", UserModVer);
253+
254+
// We do NOT need the code below to set output path in the dependency
255+
// info because it will be calculated again later. We do not want to
256+
// create output paths that do not exist in the end.
257+
// Result->setOutputPathAndHash(outputPathBase.str().str(), Hash);
254258

255259
if (Ctx.CASOpts.EnableCaching) {
256260
std::vector<std::string> clangDependencyFiles;

0 commit comments

Comments
 (0)