Skip to content

Commit fc52f2f

Browse files
Merge pull request #76232 from cachemeifyoucan/eng/PR-135221984
[DependencyScanner] Drop macro search path if not needed
2 parents 9ac56f2 + fcdc295 commit fc52f2f

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ class ModuleDependencyInfo {
813813
void addMacroDependency(StringRef macroModuleName, StringRef libraryPath,
814814
StringRef executablePath);
815815

816+
/// For a Source/Textual dependency, if it Has macro dependency.
817+
bool hasMacroDependencies() const;
818+
816819
/// Whether or not a queried module name is a `@Testable` import dependency
817820
/// of this module. Can only return `true` for Swift source modules.
818821
bool isTestableImport(StringRef moduleName) const;

lib/AST/ModuleDependencies.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ void ModuleDependencyInfo::addMacroDependency(StringRef macroModuleName,
120120
llvm_unreachable("Unexpected dependency kind");
121121
}
122122

123+
bool ModuleDependencyInfo::hasMacroDependencies() const {
124+
if (auto sourceModule =
125+
dyn_cast<SwiftSourceModuleDependenciesStorage>(storage.get()))
126+
return !sourceModule->textualModuleDetails.macroDependencies.empty();
127+
128+
if (auto interfaceModule =
129+
dyn_cast<SwiftInterfaceModuleDependenciesStorage>(storage.get()))
130+
return !interfaceModule->textualModuleDetails.macroDependencies.empty();
131+
132+
llvm_unreachable("Unexpected dependency kind");
133+
}
134+
123135
bool ModuleDependencyInfo::isTestableImport(StringRef moduleName) const {
124136
if (auto swiftSourceDepStorage = getAsSwiftSourceModule())
125137
return swiftSourceDepStorage->testableImports.contains(moduleName);

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ parseBatchScanInputFile(ASTContext &ctx, StringRef batchInputPath,
159159
return result;
160160
}
161161

162+
static void removeMacroSearchPaths(std::vector<std::string> &cmd) {
163+
// Macro search path options.
164+
static const llvm::StringSet<> macroSearchOptions = {
165+
"-plugin-path",
166+
"-external-plugin-path",
167+
"-load-plugin-library",
168+
"-load-plugin-executable",
169+
"-in-process-plugin-server-path",
170+
};
171+
172+
// Remove macro search path option and its argument.
173+
for (auto it = cmd.begin(), ie=cmd.end(); it != ie; ++it) {
174+
if (macroSearchOptions.contains(*it) && it + 1 != ie) {
175+
it = cmd.erase(it, it + 2);
176+
}
177+
}
178+
}
179+
162180
static llvm::Expected<llvm::cas::ObjectRef>
163181
updateModuleCacheKey(ModuleDependencyInfo &depInfo,
164182
ModuleDependenciesCache &cache,
@@ -378,9 +396,15 @@ static llvm::Error resolveExplicitModuleInputs(
378396
// Update build command line.
379397
if (resolvingDepInfo.isSwiftInterfaceModule() ||
380398
resolvingDepInfo.isSwiftSourceModule()) {
381-
// Update with casfs option.
382399
std::vector<std::string> newCommandLine =
383400
dependencyInfoCopy.getCommandline();
401+
402+
// If there are no external macro dependencies, drop all plugin search
403+
// paths.
404+
if (!resolvingDepInfo.hasMacroDependencies())
405+
removeMacroSearchPaths(newCommandLine);
406+
407+
// Update with casfs option.
384408
for (auto rootID : rootIDs) {
385409
newCommandLine.push_back("-cas-fs");
386410
newCommandLine.push_back(rootID);
@@ -390,6 +414,7 @@ static llvm::Error resolveExplicitModuleInputs(
390414
newCommandLine.push_back("-clang-include-tree-root");
391415
newCommandLine.push_back(tree);
392416
}
417+
393418
dependencyInfoCopy.updateCommandLine(newCommandLine);
394419
}
395420

test/CAS/macro_deps.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
// RUN: %swift_frontend_plain @%t/SwiftShims.cmd
3030
// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json Foo > %t/Foo.cmd
3131
// RUN: %swift_frontend_plain @%t/Foo.cmd
32+
// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json Bar > %t/Bar.cmd
33+
// RUN: %swift_frontend_plain @%t/Bar.cmd
34+
35+
// RUN: %FileCheck %s --check-prefix=PLUGIN_SEARCH --input-file=%t/Bar.cmd
36+
// PLUGIN_SEARCH-NOT: -external-plugin-path
3237

3338
// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json MyApp > %t/MyApp.cmd
3439
// RUN: %{python} %S/Inputs/GenerateExplicitModuleMap.py %t/deps.json > %t/map.json
@@ -87,6 +92,11 @@ public func assertFalse() {
8792
#assert(false)
8893
}
8994

95+
//--- include/Bar.swiftinterface
96+
// swift-interface-format-version: 1.0
97+
// swift-module-flags: -enable-library-evolution -swift-version 5 -O -module-name Bar -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib
98+
public func bar()
99+
90100
//--- test.swift
91101
import Foo
92102
@inlinable
@@ -96,6 +106,7 @@ public func test() {
96106

97107
//--- main.swift
98108
import Test
109+
import Bar
99110
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroTwo", type: "StringifyMacro")
100111

101112
func appTest() {

0 commit comments

Comments
 (0)