Skip to content

Commit 5f5cf44

Browse files
committed
[clang][cas] Move -fdepscan-prefix-map-* options to clang driver
Centralize the handling of prefix mapping options so that the driver is responsible for lowering `-fdepscan-prefix-map-sdk` and `-fdepscan-prefix-map-toolchain` to use `-fdepscan-prefix-map`, and all the tools take their prefix mappings from `FrontendOptions::PathPrefixMappings` instead of each having their own handling. This makes it easier for new code paths to use prefix mapping, since they get configuration "for free". cc1depscan[d] no longer needs special handling of these options at all, and in tests we just move to the driver or cc1 options. For clang-scan-deps it's move convenient to keep the command-line options, but implement them in the argument adjuster by adding the right driver options. rdar://106307646
1 parent 774cde6 commit 5f5cf44

24 files changed

+262
-390
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ class DependencyScanningTool {
127127

128128
/// Collect dependency tree.
129129
llvm::Expected<llvm::cas::ObjectProxy>
130-
getDependencyTree(const std::vector<std::string> &CommandLine, StringRef CWD,
131-
DepscanPrefixMapping PrefixMapping = {});
130+
getDependencyTree(const std::vector<std::string> &CommandLine, StringRef CWD);
132131

133132
/// If \p DiagGenerationAsCompilation is true it will generate error
134133
/// diagnostics same way as the normal compilation, with "N errors generated"
@@ -138,14 +137,12 @@ class DependencyScanningTool {
138137
getDependencyTreeFromCompilerInvocation(
139138
std::shared_ptr<CompilerInvocation> Invocation, StringRef CWD,
140139
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
141-
bool DiagGenerationAsCompilation,
142-
DepscanPrefixMapping PrefixMapping = {});
140+
bool DiagGenerationAsCompilation);
143141

144142
Expected<cas::IncludeTreeRoot>
145143
getIncludeTree(cas::ObjectStore &DB,
146144
const std::vector<std::string> &CommandLine, StringRef CWD,
147-
LookupModuleOutputCallback LookupModuleOutput,
148-
const DepscanPrefixMapping &PrefixMapping);
145+
LookupModuleOutputCallback LookupModuleOutput);
149146

150147
/// If \p DiagGenerationAsCompilation is true it will generate error
151148
/// diagnostics same way as the normal compilation, with "N errors generated"
@@ -154,7 +151,6 @@ class DependencyScanningTool {
154151
Expected<cas::IncludeTreeRoot> getIncludeTreeFromCompilerInvocation(
155152
cas::ObjectStore &DB, std::shared_ptr<CompilerInvocation> Invocation,
156153
StringRef CWD, LookupModuleOutputCallback LookupModuleOutput,
157-
const DepscanPrefixMapping &PrefixMapping,
158154
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
159155
bool DiagGenerationAsCompilation);
160156

@@ -176,8 +172,7 @@ class DependencyScanningTool {
176172
getTranslationUnitDependencies(const std::vector<std::string> &CommandLine,
177173
StringRef CWD,
178174
const llvm::StringSet<> &AlreadySeen,
179-
LookupModuleOutputCallback LookupModuleOutput,
180-
DepscanPrefixMapping PrefixMapping);
175+
LookupModuleOutputCallback LookupModuleOutput);
181176

182177
/// Given a compilation context specified via the Clang driver command-line,
183178
/// gather modular dependencies of module with the given name, and return the
@@ -186,8 +181,7 @@ class DependencyScanningTool {
186181
getModuleDependencies(StringRef ModuleName,
187182
const std::vector<std::string> &CommandLine,
188183
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
189-
LookupModuleOutputCallback LookupModuleOutput,
190-
DepscanPrefixMapping PrefixMapping);
184+
LookupModuleOutputCallback LookupModuleOutput);
191185

192186
ScanningOutputFormat getScanningFormat() const {
193187
return Worker.getScanningFormat();
@@ -208,13 +202,11 @@ class DependencyScanningTool {
208202

209203
static std::unique_ptr<DependencyActionController>
210204
createActionController(DependencyScanningWorker &Worker,
211-
LookupModuleOutputCallback LookupModuleOutput,
212-
DepscanPrefixMapping PrefixMapping);
205+
LookupModuleOutputCallback LookupModuleOutput);
213206

214207
private:
215208
std::unique_ptr<DependencyActionController>
216-
createActionController(LookupModuleOutputCallback LookupModuleOutput,
217-
DepscanPrefixMapping PrefixMapping);
209+
createActionController(LookupModuleOutputCallback LookupModuleOutput);
218210

219211
private:
220212
DependencyScanningWorker Worker;

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ using CachingOnDiskFileSystemPtr =
3232
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem>;
3333

3434
class DependencyScanningWorkerFilesystem;
35-
struct DepscanPrefixMapping;
3635

3736
/// A command-line tool invocation that is part of building a TU.
3837
///
@@ -104,12 +103,6 @@ class DependencyActionController {
104103
const ModuleDeps &MD) {
105104
return llvm::Error::success();
106105
}
107-
108-
/// FIXME: This is temporary until we eliminate the split between consumers in
109-
/// \p DependencyScanningTool and collectors in \p DependencyScanningWorker
110-
/// and have them both in the same file. see FIXME in \p
111-
/// DependencyScanningAction::runInvocation.
112-
virtual const DepscanPrefixMapping *getPrefixMapping() { return nullptr; }
113106
};
114107

115108
/// An individual dependency scanning worker that is able to run on its own

clang/include/clang/Tooling/DependencyScanning/ScanAndUpdateArgs.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ void configureInvocationForCaching(CompilerInvocation &CI, CASOptions CASOpts,
4141
bool ProduceIncludeTree);
4242

4343
struct DepscanPrefixMapping {
44-
std::optional<std::string> NewSDKPath;
45-
std::optional<std::string> NewToolchainPath;
46-
SmallVector<std::string> PrefixMap;
44+
/// Add path mappings to the \p Mapper.
45+
static void configurePrefixMapper(const CompilerInvocation &Invocation,
46+
llvm::PrefixMapper &Mapper);
4747

48-
/// Add path mappings from the current path in \p Invocation to the new path
49-
/// from \c DepscanPrefixMapping to the \p Mapper.
50-
llvm::Error configurePrefixMapper(const CompilerInvocation &Invocation,
51-
llvm::PrefixMapper &Mapper) const;
48+
/// Add path mappings to the \p Mapper.
49+
static void configurePrefixMapper(ArrayRef<std::string> PathPrefixMappings,
50+
llvm::PrefixMapper &Mapper);
5251

5352
/// Apply the mappings from \p Mapper to \p Invocation.
5453
static void remapInvocationPaths(CompilerInvocation &Invocation,
@@ -61,7 +60,6 @@ Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
6160
tooling::dependencies::DependencyScanningTool &Tool,
6261
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
6362
CompilerInvocation &Invocation, StringRef WorkingDirectory,
64-
const tooling::dependencies::DepscanPrefixMapping &PrefixMapping,
6563
llvm::cas::ObjectStore &DB);
6664

6765
} // end namespace clang

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
10521052
const Driver &D, const ArgList &Args,
10531053
ArgStringList &CmdArgs,
10541054
const InputInfo &Output,
1055-
const InputInfoList &Inputs) const {
1055+
const InputInfoList &Inputs,
1056+
std::optional<StringRef> &Sysroot) const {
10561057
const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
10571058

10581059
CheckPreprocessingOptions(D, Args);
@@ -1309,10 +1310,15 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
13091310
// -isysroot to the CC1 invocation.
13101311
StringRef sysroot = C.getSysRoot();
13111312
if (sysroot != "") {
1312-
if (!Args.hasArg(options::OPT_isysroot)) {
1313+
if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1314+
Sysroot = A->getValue();
1315+
} else {
13131316
CmdArgs.push_back("-isysroot");
13141317
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
1318+
Sysroot = sysroot;
13151319
}
1320+
} else if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1321+
Sysroot = A->getValue();
13161322
}
13171323

13181324
// Parse additional include paths from environment variables.
@@ -4490,6 +4496,52 @@ static void ProcessVSRuntimeLibrary(const ArgList &Args,
44904496
}
44914497
}
44924498

4499+
void Clang::AddPrefixMappingOptions(const ArgList &Args, ArgStringList &CmdArgs,
4500+
const Driver &D,
4501+
std::optional<StringRef> Sysroot) const {
4502+
auto IsPathApplicableAsPrefix = [](std::optional<StringRef> Path) {
4503+
return Path && llvm::sys::path::is_absolute(*Path) &&
4504+
*Path != llvm::sys::path::root_path(*Path);
4505+
};
4506+
4507+
if (Arg *A = Args.getLastArg(options::OPT_fdepscan_prefix_map_sdk_EQ)) {
4508+
if (IsPathApplicableAsPrefix(Sysroot)) {
4509+
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
4510+
*Sysroot + "=" + A->getValue()));
4511+
} else {
4512+
// FIXME: warning if we cannot infer sdk
4513+
}
4514+
}
4515+
4516+
if (Arg *A = Args.getLastArg(options::OPT_fdepscan_prefix_map_toolchain_EQ)) {
4517+
StringRef ResourceDir = D.ResourceDir;
4518+
StringRef Guess = llvm::sys::path::parent_path(ResourceDir);
4519+
for (StringRef Dir : {"clang", "lib", "usr"}) {
4520+
if (llvm::sys::path::filename(Guess) != Dir)
4521+
break;
4522+
Guess = llvm::sys::path::parent_path(Guess);
4523+
}
4524+
if (IsPathApplicableAsPrefix(Guess)) {
4525+
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
4526+
Guess + "=" + A->getValue()));
4527+
} else {
4528+
// FIXME: warning if we cannot infer toolchain
4529+
}
4530+
}
4531+
4532+
for (const Arg *A : Args.filtered(options::OPT_fdepscan_prefix_map_EQ)) {
4533+
A->claim();
4534+
StringRef Map = A->getValue();
4535+
StringRef Prefix = Map.split('=').first;
4536+
if (Prefix.size() == Map.size() || !IsPathApplicableAsPrefix(Prefix)) {
4537+
D.Diag(diag::err_drv_invalid_argument_to_option)
4538+
<< A->getValue() << A->getOption().getName();
4539+
} else {
4540+
A->render(Args, CmdArgs);
4541+
}
4542+
}
4543+
}
4544+
44934545
void Clang::ConstructJob(Compilation &C, const JobAction &Job,
44944546
const InputInfo &Output, const InputInfoList &Inputs,
44954547
const ArgList &Args, const char *LinkingOutput) const {
@@ -4582,19 +4634,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
45824634
D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";
45834635

45844636
{
4585-
const OptSpecifier DepScanOpts[] = {
4586-
options::OPT_fdepscan_EQ,
4587-
options::OPT_fdepscan_include_tree,
4588-
options::OPT_fdepscan_share_EQ,
4589-
options::OPT_fdepscan_share_identifier,
4590-
options::OPT_fdepscan_share_parent,
4591-
options::OPT_fdepscan_share_parent_EQ,
4592-
options::OPT_fno_depscan_share,
4593-
options::OPT_fdepscan_share_stop_EQ,
4594-
options::OPT_fdepscan_daemon_EQ,
4595-
options::OPT_fdepscan_prefix_map_EQ,
4596-
options::OPT_fdepscan_prefix_map_sdk_EQ,
4597-
options::OPT_fdepscan_prefix_map_toolchain_EQ};
4637+
const OptSpecifier DepScanOpts[] = {options::OPT_fdepscan_EQ,
4638+
options::OPT_fdepscan_include_tree,
4639+
options::OPT_fdepscan_share_EQ,
4640+
options::OPT_fdepscan_share_identifier,
4641+
options::OPT_fdepscan_share_parent,
4642+
options::OPT_fdepscan_share_parent_EQ,
4643+
options::OPT_fno_depscan_share,
4644+
options::OPT_fdepscan_share_stop_EQ,
4645+
options::OPT_fdepscan_daemon_EQ};
45984646

45994647
// Handle depscan.
46004648
if (Job.getKind() == Action::DepscanJobClass) {
@@ -5836,8 +5884,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
58365884
// preprocessor.
58375885
//
58385886
// FIXME: Support -fpreprocessed
5887+
std::optional<StringRef> Sysroot;
58395888
if (types::getPreprocessedType(InputType) != types::TY_INVALID)
5840-
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
5889+
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs, Sysroot);
5890+
5891+
// Handle -fdepscan-prefix-map-* options
5892+
AddPrefixMappingOptions(Args, CmdArgs, D, Sysroot);
58415893

58425894
// Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
58435895
// that "The compiler can only warn and ignore the option if not recognized".

clang/lib/Driver/ToolChains/Clang.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class LLVM_LIBRARY_VISIBILITY Clang : public Tool {
4343
const Driver &D, const llvm::opt::ArgList &Args,
4444
llvm::opt::ArgStringList &CmdArgs,
4545
const InputInfo &Output,
46-
const InputInfoList &Inputs) const;
46+
const InputInfoList &Inputs,
47+
std::optional<StringRef> &Sysroot) const;
4748

4849
void RenderTargetOptions(const llvm::Triple &EffectiveTriple,
4950
const llvm::opt::ArgList &Args, bool KernelOrKext,
@@ -104,6 +105,11 @@ class LLVM_LIBRARY_VISIBILITY Clang : public Tool {
104105
StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output,
105106
const InputInfo &Input, const llvm::opt::ArgList &Args) const;
106107

108+
void AddPrefixMappingOptions(const llvm::opt::ArgList &Args,
109+
llvm::opt::ArgStringList &CmdArgs,
110+
const Driver &D,
111+
std::optional<StringRef> Sysroot) const;
112+
107113
public:
108114
Clang(const ToolChain &TC, bool HasIntegratedBackend = true);
109115
~Clang() override;

clang/lib/Tooling/DependencyScanning/CASFSActionController.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ namespace {
2222
class CASFSActionController : public CallbackActionController {
2323
public:
2424
CASFSActionController(LookupModuleOutputCallback LookupModuleOutput,
25-
llvm::cas::CachingOnDiskFileSystem &CacheFS,
26-
DepscanPrefixMapping PrefixMapping);
25+
llvm::cas::CachingOnDiskFileSystem &CacheFS);
2726

2827
llvm::Error initialize(CompilerInstance &ScanInstance,
2928
CompilerInvocation &NewInvocation) override;
@@ -38,25 +37,22 @@ class CASFSActionController : public CallbackActionController {
3837

3938
private:
4039
llvm::cas::CachingOnDiskFileSystem &CacheFS;
41-
DepscanPrefixMapping PrefixMapping;
4240
std::optional<llvm::TreePathPrefixMapper> Mapper;
4341
CASOptions CASOpts;
4442
};
4543
} // anonymous namespace
4644

4745
CASFSActionController::CASFSActionController(
4846
LookupModuleOutputCallback LookupModuleOutput,
49-
llvm::cas::CachingOnDiskFileSystem &CacheFS,
50-
DepscanPrefixMapping PrefixMapping)
51-
: CallbackActionController(std::move(LookupModuleOutput)), CacheFS(CacheFS),
52-
PrefixMapping(std::move(PrefixMapping)) {}
47+
llvm::cas::CachingOnDiskFileSystem &CacheFS)
48+
: CallbackActionController(std::move(LookupModuleOutput)),
49+
CacheFS(CacheFS) {}
5350

5451
Error CASFSActionController::initialize(CompilerInstance &ScanInstance,
5552
CompilerInvocation &NewInvocation) {
5653
// Setup prefix mapping.
5754
Mapper.emplace(&CacheFS);
58-
if (Error E = PrefixMapping.configurePrefixMapper(NewInvocation, *Mapper))
59-
return E;
55+
DepscanPrefixMapping::configurePrefixMapper(NewInvocation, *Mapper);
6056

6157
const PreprocessorOptions &PPOpts = ScanInstance.getPreprocessorOpts();
6258
if (!PPOpts.Includes.empty() || !PPOpts.ImplicitPCHInclude.empty())
@@ -197,8 +193,6 @@ Error CASFSActionController::finalizeModuleInvocation(CompilerInvocation &CI,
197193
std::unique_ptr<DependencyActionController>
198194
dependencies::createCASFSActionController(
199195
LookupModuleOutputCallback LookupModuleOutput,
200-
llvm::cas::CachingOnDiskFileSystem &CacheFS,
201-
DepscanPrefixMapping PrefixMapping) {
202-
return std::make_unique<CASFSActionController>(LookupModuleOutput, CacheFS,
203-
std::move(PrefixMapping));
196+
llvm::cas::CachingOnDiskFileSystem &CacheFS) {
197+
return std::make_unique<CASFSActionController>(LookupModuleOutput, CacheFS);
204198
}

clang/lib/Tooling/DependencyScanning/CachingActions.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ namespace clang::tooling::dependencies {
2020

2121
std::unique_ptr<DependencyActionController>
2222
createIncludeTreeActionController(LookupModuleOutputCallback LookupModuleOutput,
23-
cas::ObjectStore &DB,
24-
DepscanPrefixMapping PrefixMapping);
23+
cas::ObjectStore &DB);
2524

2625
std::unique_ptr<DependencyActionController>
2726
createCASFSActionController(LookupModuleOutputCallback LookupModuleOutput,
28-
llvm::cas::CachingOnDiskFileSystem &CacheFS,
29-
DepscanPrefixMapping PrefixMapping);
27+
llvm::cas::CachingOnDiskFileSystem &CacheFS);
3028

3129
/// The PCH recorded file paths with canonical paths, create a VFS that
3230
/// allows remapping back to the non-canonical source paths so that they are

0 commit comments

Comments
 (0)