Skip to content

Commit e0f169c

Browse files
authored
Merge pull request #66633 from bnbarham/cherry-ignore-host-modules
[5.9][Frontend] Ignore adjacent swiftmodule in compiler host modules
2 parents 1bf42db + e279560 commit e0f169c

File tree

6 files changed

+75
-17
lines changed

6 files changed

+75
-17
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ NOTE(sdk_version_pbm_version,none,
440440
NOTE(compiled_module_ignored_reason,none,
441441
"compiled module '%0' was ignored because %select{%error"
442442
"|it belongs to a framework in the SDK"
443-
"|loading from module interfaces is preferred}1",
443+
"|loading from module interfaces is preferred"
444+
"|it's a compiler host module}1",
444445
(StringRef, unsigned))
445446
NOTE(out_of_date_module_here,none,
446447
"%select{compiled|cached|forwarding|prebuilt}0 module is out of date: '%1'",

include/swift/Basic/StringExtras.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ class NullTerminatedStringRef {
519519
/// where escaped Unicode characters lead to malformed/invalid JSON.
520520
void writeEscaped(llvm::StringRef Str, llvm::raw_ostream &OS);
521521

522+
/// Whether the path components of `path` begin with those from `prefix`.
523+
bool pathStartsWith(StringRef prefix, StringRef path);
524+
522525
} // end namespace swift
523526

524527
#endif // SWIFT_BASIC_STRINGEXTRAS_H

lib/AST/Module.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "swift/Basic/Compiler.h"
4444
#include "swift/Basic/SourceManager.h"
4545
#include "swift/Basic/Statistic.h"
46+
#include "swift/Basic/StringExtras.h"
4647
#include "swift/Demangling/ManglingMacros.h"
4748
#include "swift/Parse/Token.h"
4849
#include "swift/Strings.h"
@@ -4223,18 +4224,6 @@ FrontendStatsTracer::getTraceFormatter<const SourceFile *>() {
42234224
return &TF;
42244225
}
42254226

4226-
static bool prefixMatches(StringRef prefix, StringRef path) {
4227-
auto prefixIt = llvm::sys::path::begin(prefix),
4228-
prefixEnd = llvm::sys::path::end(prefix);
4229-
for (auto pathIt = llvm::sys::path::begin(path),
4230-
pathEnd = llvm::sys::path::end(path);
4231-
prefixIt != prefixEnd && pathIt != pathEnd; ++prefixIt, ++pathIt) {
4232-
if (*prefixIt != *pathIt)
4233-
return false;
4234-
}
4235-
return prefixIt == prefixEnd;
4236-
}
4237-
42384227
bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) const {
42394228
// stdlib is non-user by definition
42404229
if (mod->isStdlibModule())
@@ -4262,5 +4251,6 @@ bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) con
42624251
return false;
42634252

42644253
StringRef runtimePath = searchPathOpts.RuntimeResourcePath;
4265-
return (!runtimePath.empty() && prefixMatches(runtimePath, modulePath)) || (!sdkPath.empty() && prefixMatches(sdkPath, modulePath));
4254+
return (!runtimePath.empty() && pathStartsWith(runtimePath, modulePath)) ||
4255+
(!sdkPath.empty() && pathStartsWith(sdkPath, modulePath));
42664256
}

lib/Basic/StringExtras.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/SmallVector.h"
2424
#include "llvm/ADT/StringSwitch.h"
2525
#include "llvm/Support/Compiler.h"
26+
#include "llvm/Support/Path.h"
2627
#include "llvm/Support/raw_ostream.h"
2728
#include <algorithm>
2829

@@ -1417,3 +1418,15 @@ void swift::writeEscaped(llvm::StringRef Str, llvm::raw_ostream &OS) {
14171418
}
14181419
}
14191420
}
1421+
1422+
bool swift::pathStartsWith(StringRef prefix, StringRef path) {
1423+
auto prefixIt = llvm::sys::path::begin(prefix),
1424+
prefixEnd = llvm::sys::path::end(prefix);
1425+
for (auto pathIt = llvm::sys::path::begin(path),
1426+
pathEnd = llvm::sys::path::end(path);
1427+
prefixIt != prefixEnd && pathIt != pathEnd; ++prefixIt, ++pathIt) {
1428+
if (*prefixIt != *pathIt)
1429+
return false;
1430+
}
1431+
return prefixIt == prefixEnd;
1432+
}

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/FileSystem.h"
2121
#include "swift/AST/Module.h"
2222
#include "swift/Basic/Platform.h"
23+
#include "swift/Basic/StringExtras.h"
2324
#include "swift/Frontend/Frontend.h"
2425
#include "swift/Frontend/ModuleInterfaceSupport.h"
2526
#include "swift/Parse/ParseVersion.h"
@@ -227,6 +228,7 @@ struct ModuleRebuildInfo {
227228
NotIgnored,
228229
PublicFramework,
229230
InterfacePreferred,
231+
CompilerHostModule,
230232
};
231233
struct CandidateModule {
232234
std::string path;
@@ -698,7 +700,28 @@ class ModuleInterfaceLoaderImpl {
698700
bool isInResourceDir(StringRef path) {
699701
StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
700702
if (resourceDir.empty()) return false;
701-
return path.startswith(resourceDir);
703+
return pathStartsWith(resourceDir, path);
704+
}
705+
706+
bool isInResourceHostDir(StringRef path) {
707+
StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
708+
if (resourceDir.empty()) return false;
709+
710+
SmallString<128> hostPath;
711+
llvm::sys::path::append(hostPath,
712+
resourceDir, "host");
713+
return pathStartsWith(hostPath, path);
714+
}
715+
716+
bool isInSystemFrameworks(StringRef path) {
717+
StringRef sdkPath = ctx.SearchPathOpts.getSDKPath();
718+
if (sdkPath.empty()) return false;
719+
720+
SmallString<128> publicFrameworksPath;
721+
llvm::sys::path::append(publicFrameworksPath,
722+
sdkPath, "System", "Library", "Frameworks");
723+
724+
return pathStartsWith(publicFrameworksPath, path);
702725
}
703726

704727
std::pair<std::string, std::string> getCompiledModuleCandidates() {
@@ -713,10 +736,12 @@ class ModuleInterfaceLoaderImpl {
713736
llvm::sys::path::append(publicFrameworksPath,
714737
ctx.SearchPathOpts.getSDKPath(),
715738
"System", "Library", "Frameworks");
716-
if (!ctx.SearchPathOpts.getSDKPath().empty() &&
717-
modulePath.startswith(publicFrameworksPath)) {
739+
if (isInSystemFrameworks(modulePath)) {
718740
shouldLoadAdjacentModule = false;
719741
rebuildInfo.addIgnoredModule(modulePath, ReasonIgnored::PublicFramework);
742+
} else if (isInResourceHostDir(modulePath)) {
743+
shouldLoadAdjacentModule = false;
744+
rebuildInfo.addIgnoredModule(modulePath, ReasonIgnored::CompilerHostModule);
720745
}
721746

722747
switch (loadMode) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/build/host)
3+
// RUN: %empty-directory(%t/cache)
4+
// RUN: split-file %s %t
5+
6+
/// Modules loaded from within lib/swift/host should also be rebuilt from
7+
/// their interface (which actually means anything within resource-dir/host).
8+
9+
// RUN: %target-swift-frontend -emit-module %t/Lib.swift \
10+
// RUN: -swift-version 5 -enable-library-evolution \
11+
// RUN: -parse-stdlib -module-cache-path %t/cache \
12+
// RUN: -o %t/build/host -emit-module-interface-path %t/build/host/Lib.swiftinterface
13+
14+
// RUN: %target-swift-frontend -typecheck %t/Client.swift \
15+
// RUN: -resource-dir %t/build -I %t/build/host \
16+
// RUN: -parse-stdlib -module-cache-path %t/cache \
17+
// RUN: -Rmodule-loading 2>&1 | %FileCheck %s
18+
19+
// CHECK: remark: loaded module 'Lib'; source: '{{.*}}{{/|\\}}host{{/|\\}}Lib.swiftinterface', loaded: '{{.*}}{{/|\\}}cache{{/|\\}}Lib-{{.*}}.swiftmodule'
20+
21+
//--- Lib.swift
22+
public func foo() {}
23+
24+
//--- Client.swift
25+
import Lib
26+
foo()

0 commit comments

Comments
 (0)