Skip to content

Commit 2d51786

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 (cherry picked from commit 5f5cf44)
1 parent 8510dd0 commit 2d51786

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
@@ -128,8 +128,7 @@ class DependencyScanningTool {
128128

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

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

145143
Expected<cas::IncludeTreeRoot>
146144
getIncludeTree(cas::ObjectStore &DB,
147145
const std::vector<std::string> &CommandLine, StringRef CWD,
148-
LookupModuleOutputCallback LookupModuleOutput,
149-
const DepscanPrefixMapping &PrefixMapping);
146+
LookupModuleOutputCallback LookupModuleOutput);
150147

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

@@ -177,8 +173,7 @@ class DependencyScanningTool {
177173
getTranslationUnitDependencies(const std::vector<std::string> &CommandLine,
178174
StringRef CWD,
179175
const llvm::StringSet<> &AlreadySeen,
180-
LookupModuleOutputCallback LookupModuleOutput,
181-
DepscanPrefixMapping PrefixMapping);
176+
LookupModuleOutputCallback LookupModuleOutput);
182177

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

193187
ScanningOutputFormat getScanningFormat() const {
194188
return Worker.getScanningFormat();
@@ -209,13 +203,11 @@ class DependencyScanningTool {
209203

210204
static std::unique_ptr<DependencyActionController>
211205
createActionController(DependencyScanningWorker &Worker,
212-
LookupModuleOutputCallback LookupModuleOutput,
213-
DepscanPrefixMapping PrefixMapping);
206+
LookupModuleOutputCallback LookupModuleOutput);
214207

215208
private:
216209
std::unique_ptr<DependencyActionController>
217-
createActionController(LookupModuleOutputCallback LookupModuleOutput,
218-
DepscanPrefixMapping PrefixMapping);
210+
createActionController(LookupModuleOutputCallback LookupModuleOutput);
219211

220212
private:
221213
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);
@@ -1310,10 +1311,15 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
13101311
// -isysroot to the CC1 invocation.
13111312
StringRef sysroot = C.getSysRoot();
13121313
if (sysroot != "") {
1313-
if (!Args.hasArg(options::OPT_isysroot)) {
1314+
if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1315+
Sysroot = A->getValue();
1316+
} else {
13141317
CmdArgs.push_back("-isysroot");
13151318
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
1319+
Sysroot = sysroot;
13161320
}
1321+
} else if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1322+
Sysroot = A->getValue();
13171323
}
13181324

13191325
// Parse additional include paths from environment variables.
@@ -4491,6 +4497,52 @@ static void ProcessVSRuntimeLibrary(const ArgList &Args,
44914497
}
44924498
}
44934499

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

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

46004648
// Handle depscan.
46014649
if (Job.getKind() == Action::DepscanJobClass) {
@@ -5860,8 +5908,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
58605908
// preprocessor.
58615909
//
58625910
// FIXME: Support -fpreprocessed
5911+
std::optional<StringRef> Sysroot;
58635912
if (types::getPreprocessedType(InputType) != types::TY_INVALID)
5864-
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
5913+
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs, Sysroot);
5914+
5915+
// Handle -fdepscan-prefix-map-* options
5916+
AddPrefixMappingOptions(Args, CmdArgs, D, Sysroot);
58655917

58665918
// Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
58675919
// 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)