Skip to content

[DependencyScanner] Drop macro search path if not needed #76232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/ModuleDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ class ModuleDependencyInfo {
void addMacroDependency(StringRef macroModuleName, StringRef libraryPath,
StringRef executablePath);

/// For a Source/Textual dependency, if it Has macro dependency.
bool hasMacroDependencies() const;

/// Whether or not a queried module name is a `@Testable` import dependency
/// of this module. Can only return `true` for Swift source modules.
bool isTestableImport(StringRef moduleName) const;
Expand Down
12 changes: 12 additions & 0 deletions lib/AST/ModuleDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ void ModuleDependencyInfo::addMacroDependency(StringRef macroModuleName,
llvm_unreachable("Unexpected dependency kind");
}

bool ModuleDependencyInfo::hasMacroDependencies() const {
if (auto sourceModule =
dyn_cast<SwiftSourceModuleDependenciesStorage>(storage.get()))
return !sourceModule->textualModuleDetails.macroDependencies.empty();

if (auto interfaceModule =
dyn_cast<SwiftInterfaceModuleDependenciesStorage>(storage.get()))
return !interfaceModule->textualModuleDetails.macroDependencies.empty();

llvm_unreachable("Unexpected dependency kind");
}

bool ModuleDependencyInfo::isTestableImport(StringRef moduleName) const {
if (auto swiftSourceDepStorage = getAsSwiftSourceModule())
return swiftSourceDepStorage->testableImports.contains(moduleName);
Expand Down
27 changes: 26 additions & 1 deletion lib/DependencyScan/ScanDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ parseBatchScanInputFile(ASTContext &ctx, StringRef batchInputPath,
return result;
}

static void removeMacroSearchPaths(std::vector<std::string> &cmd) {
// Macro search path options.
static const llvm::StringSet<> macroSearchOptions = {
"-plugin-path",
"-external-plugin-path",
"-load-plugin-library",
"-load-plugin-executable",
"-in-process-plugin-server-path",
};

// Remove macro search path option and its argument.
for (auto it = cmd.begin(), ie=cmd.end(); it != ie; ++it) {
if (macroSearchOptions.contains(*it) && it + 1 != ie) {
it = cmd.erase(it, it + 2);
}
}
}

static llvm::Expected<llvm::cas::ObjectRef>
updateModuleCacheKey(ModuleDependencyInfo &depInfo,
ModuleDependenciesCache &cache,
Expand Down Expand Up @@ -378,9 +396,15 @@ static llvm::Error resolveExplicitModuleInputs(
// Update build command line.
if (resolvingDepInfo.isSwiftInterfaceModule() ||
resolvingDepInfo.isSwiftSourceModule()) {
// Update with casfs option.
std::vector<std::string> newCommandLine =
dependencyInfoCopy.getCommandline();

// If there are no external macro dependencies, drop all plugin search
// paths.
if (!resolvingDepInfo.hasMacroDependencies())
removeMacroSearchPaths(newCommandLine);

// Update with casfs option.
for (auto rootID : rootIDs) {
newCommandLine.push_back("-cas-fs");
newCommandLine.push_back(rootID);
Expand All @@ -390,6 +414,7 @@ static llvm::Error resolveExplicitModuleInputs(
newCommandLine.push_back("-clang-include-tree-root");
newCommandLine.push_back(tree);
}

dependencyInfoCopy.updateCommandLine(newCommandLine);
}

Expand Down
11 changes: 11 additions & 0 deletions test/CAS/macro_deps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
// RUN: %swift_frontend_plain @%t/SwiftShims.cmd
// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json Foo > %t/Foo.cmd
// RUN: %swift_frontend_plain @%t/Foo.cmd
// RUN: %S/Inputs/BuildCommandExtractor.py %t/deps.json Bar > %t/Bar.cmd
// RUN: %swift_frontend_plain @%t/Bar.cmd

// RUN: %FileCheck %s --check-prefix=PLUGIN_SEARCH --input-file=%t/Bar.cmd
// PLUGIN_SEARCH-NOT: -external-plugin-path

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

//--- include/Bar.swiftinterface
// swift-interface-format-version: 1.0
// 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
public func bar()

//--- test.swift
import Foo
@inlinable
Expand All @@ -96,6 +106,7 @@ public func test() {

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

func appTest() {
Expand Down