Skip to content

Applying the new prefix mapping option to use for cross-repo testing with swift #10751

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

Open
wants to merge 1 commit into
base: stable/20240723
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -8409,11 +8409,15 @@ def fdepscan_include_tree : Flag<["-"], "fdepscan-include-tree">,
//
// FIXME: Add DepscanOption flag.
def fdepscan_prefix_map_EQ : Joined<["-"], "fdepscan-prefix-map=">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
Group<f_Group>, Visibility<[ClangOption]>,
MetaVarName<"<old>=<new>">,
HelpText<"With -fdepscan, seamlessly filter the CAS filesystem to"
" apply the given prefix, updating the command-line to match.">,
MarshallingInfoStringVector<FrontendOpts<"PathPrefixMappings">>;
" apply the given prefix, updating the command-line to match.">;
def fdepscan_prefix_map : MultiArg<["-"], "fdepscan-prefix-map", 2>,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
MetaVarName<"<old> <new>">,
HelpText<"With -fdepscan, seamlessly filter the CAS filesystem to"
" apply the given prefix, updating the command-line to match.">;
def fdepscan_prefix_map_sdk_EQ :
Joined<["-"], "fdepscan-prefix-map-sdk=">,
Group<f_Group>, MetaVarName<"<new>">,
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Frontend/CompileJobCacheKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct CompileJobCachingOptions {
/// See \c FrontendOptions::WriteOutputAsCASID.
bool WriteOutputAsCASID;
/// See \c FrontendOptions::PathPrefixMappings.
std::vector<std::string> PathPrefixMappings;
std::vector<std::pair<std::string, std::string>> PathPrefixMappings;
};

/// Create a cache key for the given \c CompilerInvocation as a \c CASID. If \p
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ class FrontendOptions {
/// When caching is enabled, represents remappings for all the file paths that
/// the compilation may access. This is useful for canonicalizing the
/// compilation for caching purposes.
std::vector<std::string> PathPrefixMappings;
std::vector<std::pair<std::string, std::string>> PathPrefixMappings;

// Currently this is only used as part of the `-extract-api` action.
// A comma separated list of files providing a list of APIs to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct DepscanPrefixMapping {
llvm::PrefixMapper &Mapper);

/// Add path mappings to the \p Mapper.
static void configurePrefixMapper(ArrayRef<std::string> PathPrefixMappings,
static void configurePrefixMapper(ArrayRef<std::pair<std::string, std::string>> PathPrefixMappings,
llvm::PrefixMapper &Mapper);

/// Apply the mappings from \p Mapper to \p Invocation.
Expand Down
28 changes: 19 additions & 9 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4910,8 +4910,9 @@ void Clang::AddPrefixMappingOptions(const ArgList &Args, ArgStringList &CmdArgs,

if (Arg *A = Args.getLastArg(options::OPT_fdepscan_prefix_map_sdk_EQ)) {
if (IsPathApplicableAsPrefix(Sysroot)) {
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
*Sysroot + "=" + A->getValue()));
CmdArgs.push_back("-fdepscan-prefix-map");
CmdArgs.push_back(Args.MakeArgString(*Sysroot));
CmdArgs.push_back(Args.MakeArgString(A->getValue()));
} else {
// FIXME: warning if we cannot infer sdk
}
Expand All @@ -4926,22 +4927,31 @@ void Clang::AddPrefixMappingOptions(const ArgList &Args, ArgStringList &CmdArgs,
Guess = llvm::sys::path::parent_path(Guess);
}
if (IsPathApplicableAsPrefix(Guess)) {
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
Guess + "=" + A->getValue()));
CmdArgs.push_back("-fdepscan-prefix-map");
CmdArgs.push_back(Args.MakeArgString(Guess));
CmdArgs.push_back(Args.MakeArgString(A->getValue()));
} else {
// FIXME: warning if we cannot infer toolchain
}
}

for (const Arg *A : Args.filtered(options::OPT_fdepscan_prefix_map_EQ)) {
for (const Arg *A : Args.filtered(options::OPT_fdepscan_prefix_map, options::OPT_fdepscan_prefix_map_EQ)) {
A->claim();
StringRef Map = A->getValue();
StringRef Prefix = Map.split('=').first;
if (Prefix.size() == Map.size() || !IsPathApplicableAsPrefix(Prefix)) {
StringRef Prefix, MapTarget;
if (A->getOption().matches(options::OPT_fdepscan_prefix_map_EQ)) {
StringRef Map = A->getValue();
std::tie(Prefix, MapTarget) = Map.split('=');
} else {
Prefix = A->getValue(0);
MapTarget = A->getValue(1);
}
if (MapTarget.size() == 0 || !IsPathApplicableAsPrefix(Prefix)) {
D.Diag(diag::err_drv_invalid_argument_to_option)
<< A->getValue() << A->getOption().getName();
} else {
A->render(Args, CmdArgs);
CmdArgs.push_back("-fdepscan-prefix-map");
CmdArgs.push_back(Args.MakeArgString(Prefix));
CmdArgs.push_back(Args.MakeArgString(MapTarget));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Frontend/CompileJobCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ std::optional<int> CompileJobCache::initialize(CompilerInstance &Clang) {

llvm::PrefixMapper PrefixMapper;
llvm::SmallVector<llvm::MappedPrefix> Split;
llvm::MappedPrefix::transformJoinedIfValid(CacheOpts.PathPrefixMappings,
Split);
llvm::MappedPrefix::transformPairs(CacheOpts.PathPrefixMappings, Split);
for (const auto &MappedPrefix : Split) {
// We use the inverse mapping because the \p PrefixMapper will be used for
// de-canonicalization of paths.
Expand Down Expand Up @@ -607,7 +606,7 @@ Expected<std::optional<int>> CompileJobCache::replayCachedResult(

llvm::PrefixMapper PrefixMapper;
llvm::SmallVector<llvm::MappedPrefix> Split;
llvm::MappedPrefix::transformJoinedIfValid(
llvm::MappedPrefix::transformPairs(
Clang.getFrontendOpts().PathPrefixMappings, Split);
for (const auto &MappedPrefix : Split) {
// We use the inverse mapping because the \p PrefixMapper will be used for
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/PrefixMapper.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/VersionTuple.h"
Expand Down Expand Up @@ -3022,6 +3023,9 @@ static void GenerateFrontendArgs(const FrontendOptions &Opts,
for (const auto &A : Opts.ModuleCacheKeys)
GenerateMultiArg(Consumer, OPT_fmodule_file_cache_key, {A.first, A.second});

for (const auto &A : Opts.PathPrefixMappings)
GenerateMultiArg(Consumer, OPT_fdepscan_prefix_map, {A.first, A.second});

if (Opts.AuxTargetCPU)
GenerateArg(Consumer, OPT_aux_target_cpu, *Opts.AuxTargetCPU);

Expand Down Expand Up @@ -3249,6 +3253,13 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
Opts.ModuleCacheKeys.emplace_back(Values[0], Values[1]);
}

// Handle path prefix mappings
for (const Arg *A : Args.filtered(OPT_fdepscan_prefix_map)) {
ArrayRef<const char *> Values = A->getValues();
assert(Values.size() == 2);
Opts.PathPrefixMappings.emplace_back(Values[0], Values[1]);
}

if (Opts.ProgramAction != frontend::GenerateModule && Opts.IsSystemModule)
Diags.Report(diag::err_drv_argument_only_allowed_with) << "-fsystem-module"
<< "-emit-module";
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void DepscanPrefixMapping::remapInvocationPaths(CompilerInvocation &Invocation,
// Pass the remappings so that we can map cached diagnostics to the local
// paths during diagnostic rendering.
for (const llvm::MappedPrefix &Map : Mapper.getMappings()) {
FrontendOpts.PathPrefixMappings.push_back(Map.Old + "=" + Map.New);
FrontendOpts.PathPrefixMappings.emplace_back(Map.Old, Map.New);
}

auto mapInPlaceAll = [&](std::vector<std::string> &Vector) {
Expand Down Expand Up @@ -229,12 +229,12 @@ void DepscanPrefixMapping::configurePrefixMapper(const CompilerInvocation &CI,
}

void DepscanPrefixMapping::configurePrefixMapper(
ArrayRef<std::string> PathPrefixMappings, llvm::PrefixMapper &Mapper) {
ArrayRef<std::pair<std::string, std::string>> PathPrefixMappings, llvm::PrefixMapper &Mapper) {
if (PathPrefixMappings.empty())
return;

llvm::SmallVector<llvm::MappedPrefix> Split;
llvm::MappedPrefix::transformJoinedIfValid(PathPrefixMappings, Split);
llvm::MappedPrefix::transformPairs(PathPrefixMappings, Split);
for (auto &MappedPrefix : Split)
Mapper.add(MappedPrefix);

Expand Down
6 changes: 3 additions & 3 deletions clang/test/CAS/cached-diagnostics.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// RUN: %clang_cc1 -triple x86_64-apple-macos12 -fsyntax-only %t/src/main.c -I %t/src/inc -Wunknown-pragmas 2> %t/regular-diags1.txt

// RUN: %clang -cc1depscan -fdepscan=inline -fdepscan-include-tree -o %t/t1.rsp -cc1-args \
// RUN: -cc1 -triple x86_64-apple-macos12 -fcas-path %t/cas -fdepscan-prefix-map=%t/src=/^src \
// RUN: -cc1 -triple x86_64-apple-macos12 -fcas-path %t/cas -fdepscan-prefix-map %t/src /^src \
// RUN: -emit-obj %t/src/main.c -o %t/out/output.o -I %t/src/inc -Wunknown-pragmas

// Compare diagnostics after a miss.
Expand Down Expand Up @@ -42,7 +42,7 @@
// RUN: %clang_cc1 -triple x86_64-apple-macos12 -fsyntax-only %t/src2/main.c -I %t/src2/inc -Wunknown-pragmas 2> %t/regular-diags2.txt

// RUN: %clang -cc1depscan -fdepscan=inline -fdepscan-include-tree -o %t/t2.rsp -cc1-args \
// RUN: -cc1 -triple x86_64-apple-macos12 -fcas-path %t/cas -fdepscan-prefix-map=%t/src2=/^src \
// RUN: -cc1 -triple x86_64-apple-macos12 -fcas-path %t/cas -fdepscan-prefix-map %t/src2 /^src \
// RUN: -emit-obj %t/src2/main.c -o %t/out2/output.o -I %t/src2/inc -Wunknown-pragmas
// RUN: %clang @%t/t2.rsp -Rcompile-job-cache 2> %t/diags-hit2.txt

Expand All @@ -55,7 +55,7 @@
// RUN: diff -u %t/regular-diags2.txt %t/cached-diags2.txt

// RUN: %clang -cc1depscan -fdepscan=inline -fdepscan-include-tree -o %t/terr.rsp -cc1-args \
// RUN: -cc1 -triple x86_64-apple-macos12 -fcas-path %t/cas -fdepscan-prefix-map=%t/src=/^src \
// RUN: -cc1 -triple x86_64-apple-macos12 -fcas-path %t/cas -fdepscan-prefix-map %t/src /^src \
// RUN: -emit-obj %t/src/main.c -o %t/out/output.o -I %t/src/inc -Rcompile-job-cache -DERROR

// RUN: not %clang @%t/terr.rsp -ferror-limit 1 2> %t/diags_error1.txt
Expand Down
30 changes: 15 additions & 15 deletions clang/test/CAS/depscan-prefix-map.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
// RUN: -internal-isystem %S/Inputs/toolchain_dir/usr/lib/clang/1000/include \
// RUN: -working-directory %t.d \
// RUN: -fcas-path %t.d/cas \
// RUN: -fdepscan-prefix-map=%S=/^source \
// RUN: -fdepscan-prefix-map=%t.d=/^testdir \
// RUN: -fdepscan-prefix-map=%{objroot}=/^objroot \
// RUN: -fdepscan-prefix-map=%S/Inputs/toolchain_dir=/^toolchain \
// RUN: -fdepscan-prefix-map=%S/Inputs/SDK=/^sdk \
// RUN: -fdepscan-prefix-map %S /^source \
// RUN: -fdepscan-prefix-map %t.d /^testdir \
// RUN: -fdepscan-prefix-map %{objroot} /^objroot \
// RUN: -fdepscan-prefix-map %S/Inputs/toolchain_dir /^toolchain \
// RUN: -fdepscan-prefix-map %S/Inputs/SDK /^sdk \
// RUN: -fdepfile-entry=%t.d/extra \
// RUN: | FileCheck %s -DPREFIX=%t.d
// RUN: %clang -cc1depscan -dump-depscan-tree=%t.root -fdepscan=inline \
Expand All @@ -25,11 +25,11 @@
// RUN: -internal-isystem %S/Inputs/toolchain_dir/lib/clang/1000/include \
// RUN: -working-directory %t.d \
// RUN: -fcas-path %t.d/cas \
// RUN: -fdepscan-prefix-map=%S=/^source \
// RUN: -fdepscan-prefix-map=%t.d=/^testdir \
// RUN: -fdepscan-prefix-map=%{objroot}=/^objroot \
// RUN: -fdepscan-prefix-map=%S/Inputs/toolchain_dir=/^toolchain \
// RUN: -fdepscan-prefix-map=%S/Inputs/SDK=/^sdk \
// RUN: -fdepscan-prefix-map %S /^source \
// RUN: -fdepscan-prefix-map %t.d /^testdir \
// RUN: -fdepscan-prefix-map %{objroot} /^objroot \
// RUN: -fdepscan-prefix-map %S/Inputs/toolchain_dir /^toolchain \
// RUN: -fdepscan-prefix-map %S/Inputs/SDK /^sdk \
// RUN: -fdepfile-entry=%t.d/extra \
// RUN: | FileCheck %s -DPREFIX=%t.d
// RUN: %clang -cc1depscand -execute %{clang-daemon-dir}/%basename_t \
Expand All @@ -42,11 +42,11 @@
// RUN: -internal-isystem %S/Inputs/toolchain_dir/usr/lib/clang/1000/include \
// RUN: -working-directory %t.d \
// RUN: -fcas-path %t.d/cas \
// RUN: -fdepscan-prefix-map=%S=/^source \
// RUN: -fdepscan-prefix-map=%t.d=/^testdir \
// RUN: -fdepscan-prefix-map=%{objroot}=/^objroot \
// RUN: -fdepscan-prefix-map=%S/Inputs/toolchain_dir=/^toolchain \
// RUN: -fdepscan-prefix-map=%S/Inputs/SDK=/^sdk \
// RUN: -fdepscan-prefix-map %S /^source \
// RUN: -fdepscan-prefix-map %t.d /^testdir \
// RUN: -fdepscan-prefix-map %{objroot} /^objroot \
// RUN: -fdepscan-prefix-map %S/Inputs/toolchain_dir /^toolchain \
// RUN: -fdepscan-prefix-map %S/Inputs/SDK /^sdk \
// RUN: -fdepfile-entry=%t.d/extra \
// RUN: | FileCheck %s -DPREFIX=%t.d
//
Expand Down
18 changes: 9 additions & 9 deletions clang/test/CAS/driver-cache-launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@
// SESSION-CMAKE-PREFIX: note: setting LLVM_CACHE_BUILD_SESSION_ID=

// CLANG-CMAKE-PREFIX: "-cc1depscan" "-fdepscan=daemon" "-fdepscan-share-identifier"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=[[INPUTS]]/SDK=/^sdk"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=[[INPUTS]]/toolchain_dir=/^toolchain"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=/llvm/build=/^build"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=/llvm/llvm-project/llvm=/^src"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=/llvm/llvm-project/clang=/^src-clang"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=/llvm/llvm-project/clang-tools-extra=/^src-clang-tools-extra"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=/llvm/llvm-project/third-party/benchmark=/^src-benchmark"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=/llvm/llvm-project/other/benchmark=/^src-benchmark-1"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map=/llvm/llvm-project/another/benchmark=/^src-benchmark-2"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "[[INPUTS]]/SDK" "/^sdk"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "[[INPUTS]]/toolchain_dir" "/^toolchain"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "/llvm/build" "/^build"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "/llvm/llvm-project/llvm" "/^src"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "/llvm/llvm-project/clang" "/^src-clang"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "/llvm/llvm-project/clang-tools-extra" "/^src-clang-tools-extra"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "/llvm/llvm-project/third-party/benchmark" "/^src-benchmark"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "/llvm/llvm-project/other/benchmark" "/^src-benchmark-1"
// CLANG-CMAKE-PREFIX: "-fdepscan-prefix-map" "/llvm/llvm-project/another/benchmark" "/^src-benchmark-2"

// Make sure `cache-build-session` can invoke an executable script.
// RUN: cache-build-session %t/clang -c %s -o %t.o 2>&1 | FileCheck %s -check-prefix=SESSION-SCRIPT -DSRC=%s -DPREFIX=%t
Expand Down
Loading