Skip to content

Commit 183902d

Browse files
authored
Merge pull request #66576 from bnbarham/ignore-host-modules
[Frontend] Ignore adjacent swiftmodule in compiler host modules
2 parents 0c4a59b + ee3a47b commit 183902d

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
@@ -437,7 +437,8 @@ NOTE(sdk_version_pbm_version,none,
437437
NOTE(compiled_module_ignored_reason,none,
438438
"compiled module '%0' was ignored because %select{%error"
439439
"|it belongs to a framework in the SDK"
440-
"|loading from module interfaces is preferred}1",
440+
"|loading from module interfaces is preferred"
441+
"|it's a compiler host module}1",
441442
(StringRef, unsigned))
442443
NOTE(out_of_date_module_here,none,
443444
"%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"
@@ -4235,18 +4236,6 @@ FrontendStatsTracer::getTraceFormatter<const SourceFile *>() {
42354236
return &TF;
42364237
}
42374238

4238-
static bool prefixMatches(StringRef prefix, StringRef path) {
4239-
auto prefixIt = llvm::sys::path::begin(prefix),
4240-
prefixEnd = llvm::sys::path::end(prefix);
4241-
for (auto pathIt = llvm::sys::path::begin(path),
4242-
pathEnd = llvm::sys::path::end(path);
4243-
prefixIt != prefixEnd && pathIt != pathEnd; ++prefixIt, ++pathIt) {
4244-
if (*prefixIt != *pathIt)
4245-
return false;
4246-
}
4247-
return prefixIt == prefixEnd;
4248-
}
4249-
42504239
bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) const {
42514240
// stdlib is non-user by definition
42524241
if (mod->isStdlibModule())
@@ -4274,5 +4263,6 @@ bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) con
42744263
return false;
42754264

42764265
StringRef runtimePath = searchPathOpts.RuntimeResourcePath;
4277-
return (!runtimePath.empty() && prefixMatches(runtimePath, modulePath)) || (!sdkPath.empty() && prefixMatches(sdkPath, modulePath));
4266+
return (!runtimePath.empty() && pathStartsWith(runtimePath, modulePath)) ||
4267+
(!sdkPath.empty() && pathStartsWith(sdkPath, modulePath));
42784268
}

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/CachingUtils.h"
2425
#include "swift/Frontend/Frontend.h"
2526
#include "swift/Frontend/ModuleInterfaceSupport.h"
@@ -233,6 +234,7 @@ struct ModuleRebuildInfo {
233234
NotIgnored,
234235
PublicFramework,
235236
InterfacePreferred,
237+
CompilerHostModule,
236238
};
237239
struct CandidateModule {
238240
std::string path;
@@ -704,7 +706,28 @@ class ModuleInterfaceLoaderImpl {
704706
bool isInResourceDir(StringRef path) {
705707
StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
706708
if (resourceDir.empty()) return false;
707-
return path.startswith(resourceDir);
709+
return pathStartsWith(resourceDir, path);
710+
}
711+
712+
bool isInResourceHostDir(StringRef path) {
713+
StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
714+
if (resourceDir.empty()) return false;
715+
716+
SmallString<128> hostPath;
717+
llvm::sys::path::append(hostPath,
718+
resourceDir, "host");
719+
return pathStartsWith(hostPath, path);
720+
}
721+
722+
bool isInSystemFrameworks(StringRef path) {
723+
StringRef sdkPath = ctx.SearchPathOpts.getSDKPath();
724+
if (sdkPath.empty()) return false;
725+
726+
SmallString<128> publicFrameworksPath;
727+
llvm::sys::path::append(publicFrameworksPath,
728+
sdkPath, "System", "Library", "Frameworks");
729+
730+
return pathStartsWith(publicFrameworksPath, path);
708731
}
709732

710733
std::pair<std::string, std::string> getCompiledModuleCandidates() {
@@ -719,10 +742,12 @@ class ModuleInterfaceLoaderImpl {
719742
llvm::sys::path::append(publicFrameworksPath,
720743
ctx.SearchPathOpts.getSDKPath(),
721744
"System", "Library", "Frameworks");
722-
if (!ctx.SearchPathOpts.getSDKPath().empty() &&
723-
modulePath.startswith(publicFrameworksPath)) {
745+
if (isInSystemFrameworks(modulePath)) {
724746
shouldLoadAdjacentModule = false;
725747
rebuildInfo.addIgnoredModule(modulePath, ReasonIgnored::PublicFramework);
748+
} else if (isInResourceHostDir(modulePath)) {
749+
shouldLoadAdjacentModule = false;
750+
rebuildInfo.addIgnoredModule(modulePath, ReasonIgnored::CompilerHostModule);
726751
}
727752

728753
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)