Skip to content

Commit 5d78d48

Browse files
authored
Merge pull request #6593 from benlangmuir/cp-stable-44
[clang][cas] Cherry-pick recent cas include-tree improvements
2 parents 2d2bb9c + 24e2e4c commit 5d78d48

34 files changed

+369
-402
lines changed

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

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

107107
/// Collect dependency tree.
108108
llvm::Expected<llvm::cas::ObjectProxy>
109-
getDependencyTree(const std::vector<std::string> &CommandLine, StringRef CWD,
110-
DepscanPrefixMapping PrefixMapping = {});
109+
getDependencyTree(const std::vector<std::string> &CommandLine, StringRef CWD);
111110

112111
/// If \p DiagGenerationAsCompilation is true it will generate error
113112
/// diagnostics same way as the normal compilation, with "N errors generated"
@@ -117,14 +116,12 @@ class DependencyScanningTool {
117116
getDependencyTreeFromCompilerInvocation(
118117
std::shared_ptr<CompilerInvocation> Invocation, StringRef CWD,
119118
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
120-
bool DiagGenerationAsCompilation,
121-
DepscanPrefixMapping PrefixMapping = {});
119+
bool DiagGenerationAsCompilation);
122120

123121
Expected<cas::IncludeTreeRoot>
124122
getIncludeTree(cas::ObjectStore &DB,
125123
const std::vector<std::string> &CommandLine, StringRef CWD,
126-
LookupModuleOutputCallback LookupModuleOutput,
127-
const DepscanPrefixMapping &PrefixMapping);
124+
LookupModuleOutputCallback LookupModuleOutput);
128125

129126
/// If \p DiagGenerationAsCompilation is true it will generate error
130127
/// diagnostics same way as the normal compilation, with "N errors generated"
@@ -133,7 +130,6 @@ class DependencyScanningTool {
133130
Expected<cas::IncludeTreeRoot> getIncludeTreeFromCompilerInvocation(
134131
cas::ObjectStore &DB, std::shared_ptr<CompilerInvocation> Invocation,
135132
StringRef CWD, LookupModuleOutputCallback LookupModuleOutput,
136-
const DepscanPrefixMapping &PrefixMapping,
137133
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
138134
bool DiagGenerationAsCompilation);
139135

@@ -155,8 +151,7 @@ class DependencyScanningTool {
155151
getTranslationUnitDependencies(const std::vector<std::string> &CommandLine,
156152
StringRef CWD,
157153
const llvm::StringSet<> &AlreadySeen,
158-
LookupModuleOutputCallback LookupModuleOutput,
159-
DepscanPrefixMapping PrefixMapping);
154+
LookupModuleOutputCallback LookupModuleOutput);
160155

161156
/// Given a compilation context specified via the Clang driver command-line,
162157
/// gather modular dependencies of module with the given name, and return the
@@ -165,8 +160,7 @@ class DependencyScanningTool {
165160
getModuleDependencies(StringRef ModuleName,
166161
const std::vector<std::string> &CommandLine,
167162
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
168-
LookupModuleOutputCallback LookupModuleOutput,
169-
DepscanPrefixMapping PrefixMapping);
163+
LookupModuleOutputCallback LookupModuleOutput);
170164

171165
ScanningOutputFormat getScanningFormat() const {
172166
return Worker.getScanningFormat();
@@ -187,13 +181,11 @@ class DependencyScanningTool {
187181

188182
static std::unique_ptr<DependencyActionController>
189183
createActionController(DependencyScanningWorker &Worker,
190-
LookupModuleOutputCallback LookupModuleOutput,
191-
DepscanPrefixMapping PrefixMapping);
184+
LookupModuleOutputCallback LookupModuleOutput);
192185

193186
private:
194187
std::unique_ptr<DependencyActionController>
195-
createActionController(LookupModuleOutputCallback LookupModuleOutput,
196-
DepscanPrefixMapping PrefixMapping);
188+
createActionController(LookupModuleOutputCallback LookupModuleOutput);
197189

198190
private:
199191
DependencyScanningWorker Worker;

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

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

3333
class DependencyScanningWorkerFilesystem;
34-
struct DepscanPrefixMapping;
3534

3635
/// A command-line tool invocation that is part of building a TU.
3736
///
@@ -99,12 +98,6 @@ class DependencyActionController {
9998
const ModuleDeps &MD) {
10099
return llvm::Error::success();
101100
}
102-
103-
/// FIXME: This is temporary until we eliminate the split between consumers in
104-
/// \p DependencyScanningTool and collectors in \p DependencyScanningWorker
105-
/// and have them both in the same file. see FIXME in \p
106-
/// DependencyScanningAction::runInvocation.
107-
virtual const DepscanPrefixMapping *getPrefixMapping() { return nullptr; }
108101
};
109102

110103
/// 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-
Optional<std::string> NewSDKPath;
45-
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
@@ -1190,7 +1190,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11901190
const Driver &D, const ArgList &Args,
11911191
ArgStringList &CmdArgs,
11921192
const InputInfo &Output,
1193-
const InputInfoList &Inputs) const {
1193+
const InputInfoList &Inputs,
1194+
std::optional<StringRef> &Sysroot) const {
11941195
const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
11951196

11961197
CheckPreprocessingOptions(D, Args);
@@ -1428,10 +1429,15 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
14281429
// -isysroot to the CC1 invocation.
14291430
StringRef sysroot = C.getSysRoot();
14301431
if (sysroot != "") {
1431-
if (!Args.hasArg(options::OPT_isysroot)) {
1432+
if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1433+
Sysroot = A->getValue();
1434+
} else {
14321435
CmdArgs.push_back("-isysroot");
14331436
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
1437+
Sysroot = sysroot;
14341438
}
1439+
} else if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1440+
Sysroot = A->getValue();
14351441
}
14361442

14371443
// Parse additional include paths from environment variables.
@@ -4524,6 +4530,52 @@ static void ProcessVSRuntimeLibrary(const ArgList &Args,
45244530
}
45254531
}
45264532

4533+
void Clang::AddPrefixMappingOptions(const ArgList &Args, ArgStringList &CmdArgs,
4534+
const Driver &D,
4535+
std::optional<StringRef> Sysroot) const {
4536+
auto IsPathApplicableAsPrefix = [](std::optional<StringRef> Path) {
4537+
return Path && llvm::sys::path::is_absolute(*Path) &&
4538+
*Path != llvm::sys::path::root_path(*Path);
4539+
};
4540+
4541+
if (Arg *A = Args.getLastArg(options::OPT_fdepscan_prefix_map_sdk_EQ)) {
4542+
if (IsPathApplicableAsPrefix(Sysroot)) {
4543+
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
4544+
*Sysroot + "=" + A->getValue()));
4545+
} else {
4546+
// FIXME: warning if we cannot infer sdk
4547+
}
4548+
}
4549+
4550+
if (Arg *A = Args.getLastArg(options::OPT_fdepscan_prefix_map_toolchain_EQ)) {
4551+
StringRef ResourceDir = D.ResourceDir;
4552+
StringRef Guess = llvm::sys::path::parent_path(ResourceDir);
4553+
for (StringRef Dir : {"clang", "lib", "usr"}) {
4554+
if (llvm::sys::path::filename(Guess) != Dir)
4555+
break;
4556+
Guess = llvm::sys::path::parent_path(Guess);
4557+
}
4558+
if (IsPathApplicableAsPrefix(Guess)) {
4559+
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
4560+
Guess + "=" + A->getValue()));
4561+
} else {
4562+
// FIXME: warning if we cannot infer toolchain
4563+
}
4564+
}
4565+
4566+
for (const Arg *A : Args.filtered(options::OPT_fdepscan_prefix_map_EQ)) {
4567+
A->claim();
4568+
StringRef Map = A->getValue();
4569+
StringRef Prefix = Map.split('=').first;
4570+
if (Prefix.size() == Map.size() || !IsPathApplicableAsPrefix(Prefix)) {
4571+
D.Diag(diag::err_drv_invalid_argument_to_option)
4572+
<< A->getValue() << A->getOption().getName();
4573+
} else {
4574+
A->render(Args, CmdArgs);
4575+
}
4576+
}
4577+
}
4578+
45274579
void Clang::ConstructJob(Compilation &C, const JobAction &Job,
45284580
const InputInfo &Output, const InputInfoList &Inputs,
45294581
const ArgList &Args, const char *LinkingOutput) const {
@@ -4640,19 +4692,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
46404692
D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";
46414693

46424694
{
4643-
const OptSpecifier DepScanOpts[] = {
4644-
options::OPT_fdepscan_EQ,
4645-
options::OPT_fdepscan_include_tree,
4646-
options::OPT_fdepscan_share_EQ,
4647-
options::OPT_fdepscan_share_identifier,
4648-
options::OPT_fdepscan_share_parent,
4649-
options::OPT_fdepscan_share_parent_EQ,
4650-
options::OPT_fno_depscan_share,
4651-
options::OPT_fdepscan_share_stop_EQ,
4652-
options::OPT_fdepscan_daemon_EQ,
4653-
options::OPT_fdepscan_prefix_map_EQ,
4654-
options::OPT_fdepscan_prefix_map_sdk_EQ,
4655-
options::OPT_fdepscan_prefix_map_toolchain_EQ};
4695+
const OptSpecifier DepScanOpts[] = {options::OPT_fdepscan_EQ,
4696+
options::OPT_fdepscan_include_tree,
4697+
options::OPT_fdepscan_share_EQ,
4698+
options::OPT_fdepscan_share_identifier,
4699+
options::OPT_fdepscan_share_parent,
4700+
options::OPT_fdepscan_share_parent_EQ,
4701+
options::OPT_fno_depscan_share,
4702+
options::OPT_fdepscan_share_stop_EQ,
4703+
options::OPT_fdepscan_daemon_EQ};
46564704

46574705
// Handle depscan.
46584706
if (Job.getKind() == Action::DepscanJobClass) {
@@ -5890,8 +5938,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
58905938
// preprocessor.
58915939
//
58925940
// FIXME: Support -fpreprocessed
5941+
std::optional<StringRef> Sysroot;
58935942
if (types::getPreprocessedType(InputType) != types::TY_INVALID)
5894-
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
5943+
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs, Sysroot);
5944+
5945+
// Handle -fdepscan-prefix-map-* options
5946+
AddPrefixMappingOptions(Args, CmdArgs, D, Sysroot);
58955947

58965948
// Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
58975949
// 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/Frontend/CompilerInstance.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2081,8 +2081,12 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
20812081
PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
20822082
PrivPath.push_back(std::make_pair(&II, Path[0].second));
20832083

2084+
std::string FileName;
2085+
// If there is a modulemap module or prebuilt module, load it.
20842086
if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true,
2085-
!IsInclusionDirective))
2087+
!IsInclusionDirective) ||
2088+
selectModuleSource(nullptr, PrivateModule, FileName, BuiltModules,
2089+
PP->getHeaderSearchInfo()) != MS_ModuleNotFound)
20862090
Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
20872091
if (Sub) {
20882092
MapPrivateSubModToTopLevel = true;

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)