From a831621951753fa4d57cb613e142466f12d8207a Mon Sep 17 00:00:00 2001 From: Sina Mahdavi Date: Wed, 2 Jul 2025 14:27:04 -0700 Subject: [PATCH] Add -scanner-prefix-map-paths to the frontend --- include/swift/AST/PluginLoader.h | 4 ++-- include/swift/Frontend/FrontendOptions.h | 2 +- include/swift/Option/Options.td | 10 +++++++--- lib/AST/PluginLoader.cpp | 2 +- lib/Frontend/ArgsToFrontendOptionsConverter.cpp | 5 ++++- lib/Frontend/CachedDiagnostics.cpp | 2 +- lib/Frontend/CompilerInvocation.cpp | 9 +++------ lib/Frontend/MakeStyleDependencies.cpp | 4 ++-- 8 files changed, 21 insertions(+), 17 deletions(-) diff --git a/include/swift/AST/PluginLoader.h b/include/swift/AST/PluginLoader.h index 576d88e5b36b1..83c854ac0869d 100644 --- a/include/swift/AST/PluginLoader.h +++ b/include/swift/AST/PluginLoader.h @@ -53,11 +53,11 @@ class PluginLoader { llvm::DenseMap &getPluginMap(); /// Resolved plugin path remappings. - std::vector PathRemap; + std::vector> PathRemap; public: PluginLoader(ASTContext &Ctx, DependencyTracker *DepTracker, - std::optional> Remap = std::nullopt, + std::optional>> Remap = std::nullopt, bool disableSandbox = false) : Ctx(Ctx), DepTracker(DepTracker), disableSandbox(disableSandbox) { if (Remap) diff --git a/include/swift/Frontend/FrontendOptions.h b/include/swift/Frontend/FrontendOptions.h index 339368386cc88..3548839f614b6 100644 --- a/include/swift/Frontend/FrontendOptions.h +++ b/include/swift/Frontend/FrontendOptions.h @@ -145,7 +145,7 @@ class FrontendOptions { std::string VerifyGenericSignaturesInModule; /// CacheReplay PrefixMap. - std::vector CacheReplayPrefixMap; + std::vector> CacheReplayPrefixMap; /// Number of retry opening an input file if the previous opening returns /// bad file descriptor error. diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td index 614590999b7e5..a391eb7637d08 100644 --- a/include/swift/Option/Options.td +++ b/include/swift/Option/Options.td @@ -2224,8 +2224,12 @@ def sdk_module_cache_path : Separate<["-"], "sdk-module-cache-path">, Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>, HelpText<"Specifies the module cache path for explicitly-built SDK modules">; -def scanner_prefix_map : Separate<["-"], "scanner-prefix-map">, +def scanner_prefix_map_paths : MultiArg<["-"], "scanner-prefix-map-paths", 2>, Flags<[FrontendOption, NewDriverOnlyOption]>, + HelpText<"Remap paths reported by dependency scanner">, MetaVarName<" ">; + +def scanner_prefix_map : Separate<["-"], "scanner-prefix-map">, + Flags<[NewDriverOnlyOption]>, HelpText<"Remap paths reported by dependency scanner">, MetaVarName<"">; def scanner_prefix_map_sdk : Separate<["-"], "scanner-prefix-map-sdk">, @@ -2236,9 +2240,9 @@ def scanner_prefix_map_toolchain : Separate<["-"], "scanner-prefix-map-toolchain Flags<[NewDriverOnlyOption]>, HelpText<"Remap paths within toolchain directory reported by dependency scanner">, MetaVarName<"">; -def cache_replay_prefix_map: Separate<["-"], "cache-replay-prefix-map">, +def cache_replay_prefix_map: MultiArg<["-"], "cache-replay-prefix-map", 2>, Flags<[FrontendOption, NoDriverOption, CacheInvariant]>, - HelpText<"Remap paths when replaying outputs from cache">, MetaVarName<"">; + HelpText<"Remap paths when replaying outputs from cache">, MetaVarName<" ">; // END ONLY SUPPORTED IN NEW DRIVER diff --git a/lib/AST/PluginLoader.cpp b/lib/AST/PluginLoader.cpp index 3fb2f6a5ec04d..988543a2ee487 100644 --- a/lib/AST/PluginLoader.cpp +++ b/lib/AST/PluginLoader.cpp @@ -100,7 +100,7 @@ PluginLoader::getPluginMap() { std::optional mapper; if (!PathRemap.empty()) { SmallVector prefixes; - llvm::MappedPrefix::transformJoinedIfValid(PathRemap, prefixes); + llvm::MappedPrefix::transformPairs(PathRemap, prefixes); mapper.emplace(); mapper->addRange(prefixes); mapper->sort(); diff --git a/lib/Frontend/ArgsToFrontendOptionsConverter.cpp b/lib/Frontend/ArgsToFrontendOptionsConverter.cpp index 46cf7e72a2096..16f086ede827f 100644 --- a/lib/Frontend/ArgsToFrontendOptionsConverter.cpp +++ b/lib/Frontend/ArgsToFrontendOptionsConverter.cpp @@ -31,6 +31,7 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/Compression.h" +#include "llvm/Support/PrefixMapper.h" #include "llvm/Support/Process.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/LineIterator.h" @@ -315,7 +316,9 @@ bool ArgsToFrontendOptionsConverter::convert( return true; Opts.DeterministicCheck |= Args.hasArg(OPT_enable_deterministic_check); - Opts.CacheReplayPrefixMap = Args.getAllArgValues(OPT_cache_replay_prefix_map); + for (const Arg *A : Args.filtered(OPT_cache_replay_prefix_map)) { + Opts.CacheReplayPrefixMap.push_back({A->getValue(0), A->getValue(1)}); + } if (FrontendOptions::doesActionGenerateIR(Opts.RequestedAction)) { if (Args.hasArg(OPT_experimental_skip_non_inlinable_function_bodies) || diff --git a/lib/Frontend/CachedDiagnostics.cpp b/lib/Frontend/CachedDiagnostics.cpp index 69634641568a7..941a8e0f23c5d 100644 --- a/lib/Frontend/CachedDiagnostics.cpp +++ b/lib/Frontend/CachedDiagnostics.cpp @@ -721,7 +721,7 @@ class CachingDiagnosticsProcessor::Implementation Instance.getInvocation().getFrontendOptions().InputsAndOutputs), Diags(Instance.getDiags()), CAS(*Instance.getSharedCASInstance()) { SmallVector Prefixes; - llvm::MappedPrefix::transformJoinedIfValid( + llvm::MappedPrefix::transformPairs( Instance.getInvocation().getFrontendOptions().CacheReplayPrefixMap, Prefixes); Mapper.addRange(Prefixes); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index b658685952fb9..459d93b53ec67 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -2473,12 +2473,9 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args, auto SplitMap = StringRef(A).split('='); Opts.DeserializedPathRecoverer.addMapping(SplitMap.first, SplitMap.second); } - for (StringRef Opt : Args.getAllArgValues(OPT_scanner_prefix_map)) { - if (auto Mapping = llvm::MappedPrefix::getFromJoined(Opt)) { - Opts.ScannerPrefixMapper.push_back({Mapping->Old, Mapping->New}); - } else { - Diags.diagnose(SourceLoc(), diag::error_prefix_mapping, Opt); - } + + for (const Arg *A : Args.filtered(OPT_scanner_prefix_map_paths)) { + Opts.ScannerPrefixMapper.push_back({A->getValue(0), A->getValue(1)}); } Opts.ResolvedPluginVerification |= diff --git a/lib/Frontend/MakeStyleDependencies.cpp b/lib/Frontend/MakeStyleDependencies.cpp index 5a6baf7dd3b88..c28270a360c00 100644 --- a/lib/Frontend/MakeStyleDependencies.cpp +++ b/lib/Frontend/MakeStyleDependencies.cpp @@ -95,12 +95,12 @@ reversePathSortedFilenames(const Container &elts) { static void emitMakeDependenciesFile(std::vector &dependencies, const FrontendOptions &opts, const InputFile &input, - const std::vector &prefixMap, + const std::vector> &prefixMap, llvm::raw_ostream &os) { // Prefix map all the path if needed. if (!prefixMap.empty()) { SmallVector prefixes; - llvm::MappedPrefix::transformJoinedIfValid(prefixMap, prefixes); + llvm::MappedPrefix::transformPairs(prefixMap, prefixes); llvm::PrefixMapper mapper; mapper.addRange(prefixes); mapper.sort();