Skip to content

Commit 592e039

Browse files
committed
[clang][deps] Call llvm::cas::createCASProvidingFileSystem()
automatically
1 parent 66ba679 commit 592e039

File tree

12 files changed

+53
-102
lines changed

12 files changed

+53
-102
lines changed

clang-tools-extra/clangd/ScanningProjectModules.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ModuleDependencyScanner {
3838
: CDB(CDB), TFS(TFS),
3939
Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
4040
tooling::dependencies::ScanningOutputFormat::P1689,
41-
CASOptions(), nullptr, nullptr, nullptr) {}
41+
CASOptions(), nullptr, nullptr) {}
4242

4343
/// The scanned modules dependency information for a specific source file.
4444
struct ModuleDependencyInfo {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class DependencyScanningTool {
144144
}
145145

146146
Expected<cas::IncludeTreeRoot>
147-
getIncludeTree(cas::ObjectStore &DB,
147+
getIncludeTree(std::shared_ptr<cas::ObjectStore> DB,
148148
const std::vector<std::string> &CommandLine, StringRef CWD,
149149
LookupModuleOutputCallback LookupModuleOutput);
150150

@@ -153,7 +153,7 @@ class DependencyScanningTool {
153153
/// message and the serialized diagnostics file emitted if the
154154
/// \p DiagOpts.DiagnosticSerializationFile setting is set for the invocation.
155155
Expected<cas::IncludeTreeRoot> getIncludeTreeFromCompilerInvocation(
156-
cas::ObjectStore &DB, std::shared_ptr<CompilerInvocation> Invocation,
156+
std::shared_ptr<cas::ObjectStore> DB, std::shared_ptr<CompilerInvocation> Invocation,
157157
StringRef CWD, LookupModuleOutputCallback LookupModuleOutput,
158158
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
159159
bool DiagGenerationAsCompilation);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
6262
tooling::dependencies::DependencyScanningTool &Tool,
6363
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
6464
CompilerInvocation &Invocation, StringRef WorkingDirectory,
65-
llvm::cas::ObjectStore &DB);
65+
std::shared_ptr<llvm::cas::ObjectStore> DB);
6666

6767
} // end namespace clang
6868

clang/lib/Tooling/DependencyScanning/CachingActions.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@
1010
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_CACHINGACTIONS_H
1111

1212
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
13-
#include "clang/Tooling/DependencyScanning/ScanAndUpdateArgs.h"
1413

1514
namespace clang::tooling::dependencies {
1615

1716
std::unique_ptr<DependencyActionController>
1817
createIncludeTreeActionController(LookupModuleOutputCallback LookupModuleOutput,
19-
cas::ObjectStore &DB);
20-
21-
/// The PCH recorded file paths with canonical paths, create a VFS that
22-
/// allows remapping back to the non-canonical source paths so that they are
23-
/// found during dep-scanning.
24-
void addReversePrefixMappingFileSystem(const llvm::PrefixMapper &PrefixMapper,
25-
CompilerInstance &ScanInstance);
18+
std::shared_ptr<cas::ObjectStore> DB);
2619

2720
} // namespace clang::tooling::dependencies
2821
#endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_CACHINGACTIONS_H

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -564,15 +564,7 @@ initVFSForTUBuferScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
564564
auto InputPath = TUBuffer.getBufferIdentifier();
565565
InMemoryFS->addFile(
566566
InputPath, 0, llvm::MemoryBuffer::getMemBufferCopy(TUBuffer.getBuffer()));
567-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
568-
569-
// If we are using a CAS, we need to provide the fake input file in a
570-
// CASProvidingFS for include-tree.
571-
if (CAS)
572-
InMemoryOverlay =
573-
llvm::cas::createCASProvidingFileSystem(CAS, std::move(InMemoryFS));
574-
575-
OverlayFS->pushOverlay(InMemoryOverlay);
567+
OverlayFS->pushOverlay(std::move(InMemoryFS));
576568
ModifiedFS = OverlayFS;
577569
std::vector<std::string> ModifiedCommandLine(CommandLine);
578570
ModifiedCommandLine.emplace_back(InputPath);
@@ -601,15 +593,7 @@ initVFSForByNameScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
601593
llvm::sys::fs::createUniquePath(ModuleName + "-%%%%%%%%.input", FakeInputPath,
602594
/*MakeAbsolute=*/false);
603595
InMemoryFS->addFile(FakeInputPath, 0, llvm::MemoryBuffer::getMemBuffer(""));
604-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS;
605-
606-
// If we are using a CAS, we need to provide the fake input file in a
607-
// CASProvidingFS for include-tree.
608-
if (CAS)
609-
InMemoryOverlay =
610-
llvm::cas::createCASProvidingFileSystem(CAS, std::move(InMemoryFS));
611-
612-
OverlayFS->pushOverlay(InMemoryOverlay);
596+
OverlayFS->pushOverlay(std::move(InMemoryFS));
613597

614598
std::vector<std::string> ModifiedCommandLine(CommandLine);
615599
ModifiedCommandLine.emplace_back(FakeInputPath);

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ class GetIncludeTree : public EmptyDependencyConsumer {
137137
}
138138

139139
Expected<cas::IncludeTreeRoot> DependencyScanningTool::getIncludeTree(
140-
cas::ObjectStore &DB, const std::vector<std::string> &CommandLine,
140+
std::shared_ptr<cas::ObjectStore> DB, const std::vector<std::string> &CommandLine,
141141
StringRef CWD, LookupModuleOutputCallback LookupModuleOutput) {
142-
GetIncludeTree Consumer(DB);
142+
GetIncludeTree Consumer(*DB);
143143
auto Controller = createIncludeTreeActionController(LookupModuleOutput, DB);
144144
llvm::Error Result =
145145
Worker.computeDependencies(CWD, CommandLine, Consumer, *Controller);
@@ -150,11 +150,11 @@ Expected<cas::IncludeTreeRoot> DependencyScanningTool::getIncludeTree(
150150

151151
Expected<cas::IncludeTreeRoot>
152152
DependencyScanningTool::getIncludeTreeFromCompilerInvocation(
153-
cas::ObjectStore &DB, std::shared_ptr<CompilerInvocation> Invocation,
153+
std::shared_ptr<cas::ObjectStore> DB, std::shared_ptr<CompilerInvocation> Invocation,
154154
StringRef CWD, LookupModuleOutputCallback LookupModuleOutput,
155155
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
156156
bool DiagGenerationAsCompilation) {
157-
GetIncludeTree Consumer(DB);
157+
GetIncludeTree Consumer(*DB);
158158
auto Controller = createIncludeTreeActionController(LookupModuleOutput, DB);
159159
Worker.computeDependenciesFromCompilerInvocation(
160160
std::move(Invocation), CWD, Consumer, *Controller, DiagsConsumer,
@@ -311,7 +311,7 @@ DependencyScanningTool::createActionController(
311311
LookupModuleOutputCallback LookupModuleOutput) {
312312
if (Worker.getScanningFormat() == ScanningOutputFormat::FullIncludeTree)
313313
return createIncludeTreeActionController(LookupModuleOutput,
314-
*Worker.getCAS());
314+
Worker.getCAS());
315315
return std::make_unique<CallbackActionController>(LookupModuleOutput);
316316
}
317317

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/CAS/IncludeTree.h"
1313
#include "clang/Frontend/CompilerInstance.h"
1414
#include "clang/Lex/Preprocessor.h"
15+
#include "llvm/CAS/CASProvidingFileSystem.h"
1516
#include "llvm/CAS/ObjectStore.h"
1617
#include "llvm/Support/PrefixMapper.h"
1718
#include "llvm/Support/PrefixMappingFileSystem.h"
@@ -26,9 +27,9 @@ class IncludeTreeBuilder;
2627

2728
class IncludeTreeActionController : public CallbackActionController {
2829
public:
29-
IncludeTreeActionController(cas::ObjectStore &DB,
30+
IncludeTreeActionController(std::shared_ptr<cas::ObjectStore> DB,
3031
LookupModuleOutputCallback LookupOutput)
31-
: CallbackActionController(LookupOutput), DB(DB) {}
32+
: CallbackActionController(LookupOutput), DB(std::move(DB)) {}
3233

3334
Expected<cas::IncludeTreeRoot> getIncludeTree();
3435

@@ -52,7 +53,7 @@ class IncludeTreeActionController : public CallbackActionController {
5253
}
5354

5455
private:
55-
cas::ObjectStore &DB;
56+
std::shared_ptr<cas::ObjectStore> DB;
5657
CASOptions CASOpts;
5758
llvm::PrefixMapper PrefixMapper;
5859
// IncludeTreePPCallbacks keeps a pointer to the current builder, so use a
@@ -274,22 +275,6 @@ class LookupPCHModulesListener : public ASTReaderListener {
274275
};
275276
} // namespace
276277

277-
/// The PCH recorded file paths with canonical paths, create a VFS that
278-
/// allows remapping back to the non-canonical source paths so that they are
279-
/// found during dep-scanning.
280-
void dependencies::addReversePrefixMappingFileSystem(
281-
const llvm::PrefixMapper &PrefixMapper, CompilerInstance &ScanInstance) {
282-
llvm::PrefixMapper ReverseMapper;
283-
ReverseMapper.addInverseRange(PrefixMapper.getMappings());
284-
ReverseMapper.sort();
285-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
286-
llvm::vfs::createPrefixMappingFileSystem(
287-
std::move(ReverseMapper), &ScanInstance.getVirtualFileSystem());
288-
289-
ScanInstance.setVirtualFileSystem(FS);
290-
ScanInstance.getFileManager().setVirtualFileSystem(std::move(FS));
291-
}
292-
293278
Expected<cas::IncludeTreeRoot> IncludeTreeActionController::getIncludeTree() {
294279
if (IncludeTreeResult)
295280
return *IncludeTreeResult;
@@ -301,25 +286,35 @@ Error IncludeTreeActionController::initialize(
301286
CompilerInstance &ScanInstance, CompilerInvocation &NewInvocation) {
302287
DepscanPrefixMapping::configurePrefixMapper(NewInvocation, PrefixMapper);
303288

304-
auto ensurePathRemapping = [&]() {
305-
if (PrefixMapper.empty())
306-
return;
307-
289+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
290+
ScanInstance.getVirtualFileSystemPtr();
291+
FS = llvm::cas::createCASProvidingFileSystem(DB, std::move(FS));
292+
if (!PrefixMapper.empty()) {
308293
PreprocessorOptions &PPOpts = ScanInstance.getPreprocessorOpts();
309-
if (PPOpts.Includes.empty() && PPOpts.ImplicitPCHInclude.empty() &&
310-
!ScanInstance.getLangOpts().Modules)
311-
return;
312-
313-
addReversePrefixMappingFileSystem(PrefixMapper, ScanInstance);
294+
if (!PPOpts.Includes.empty() || !PPOpts.ImplicitPCHInclude.empty() ||
295+
ScanInstance.getLangOpts().Modules) {
296+
297+
/// The PCH recorded file paths with canonical paths, create a VFS that
298+
/// allows remapping back to the non-canonical source paths so that they
299+
/// are found during dep-scanning.
300+
llvm::PrefixMapper ReverseMapper;
301+
ReverseMapper.addInverseRange(PrefixMapper.getMappings());
302+
ReverseMapper.sort();
303+
304+
FS = llvm::vfs::createPrefixMappingFileSystem(std::move(ReverseMapper),
305+
std::move(FS));
306+
}
314307

315308
// These are written in the predefines buffer, so we need to remap them.
316309
for (std::string &Include : PPOpts.Includes)
317310
PrefixMapper.mapInPlace(Include);
318-
};
319-
ensurePathRemapping();
311+
}
312+
313+
ScanInstance.setVirtualFileSystem(FS);
314+
ScanInstance.getFileManager().setVirtualFileSystem(std::move(FS));
320315

321316
BuilderStack.push_back(
322-
std::make_unique<IncludeTreeBuilder>(DB, PrefixMapper));
317+
std::make_unique<IncludeTreeBuilder>(*DB, PrefixMapper));
323318

324319
// Attach callbacks for the IncludeTree of the TU. The preprocessor
325320
// does not exist yet, so we need to indirect this via DependencyCollector.
@@ -400,7 +395,7 @@ std::optional<std::string> IncludeTreeActionController::getCacheKey(
400395
Error IncludeTreeActionController::initializeModuleBuild(
401396
CompilerInstance &ModuleScanInstance) {
402397
BuilderStack.push_back(
403-
std::make_unique<IncludeTreeBuilder>(DB, PrefixMapper));
398+
std::make_unique<IncludeTreeBuilder>(*DB, PrefixMapper));
404399

405400
// Attach callbacks for the IncludeTree of the module. The preprocessor
406401
// does not exist yet, so we need to indirect this via DependencyCollector.
@@ -942,6 +937,6 @@ IncludeTreeBuilder::createIncludeFile(StringRef Filename,
942937

943938
std::unique_ptr<DependencyActionController>
944939
dependencies::createIncludeTreeActionController(
945-
LookupModuleOutputCallback LookupModuleOutput, cas::ObjectStore &DB) {
946-
return std::make_unique<IncludeTreeActionController>(DB, LookupModuleOutput);
940+
LookupModuleOutputCallback LookupModuleOutput, std::shared_ptr<cas::ObjectStore> DB) {
941+
return std::make_unique<IncludeTreeActionController>(std::move(DB), LookupModuleOutput);
947942
}

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void DepscanPrefixMapping::configurePrefixMapper(
249249
Expected<llvm::cas::CASID> clang::scanAndUpdateCC1InlineWithTool(
250250
DependencyScanningTool &Tool, DiagnosticConsumer &DiagsConsumer,
251251
raw_ostream *VerboseOS, CompilerInvocation &Invocation,
252-
StringRef WorkingDirectory, llvm::cas::ObjectStore &DB) {
252+
StringRef WorkingDirectory, std::shared_ptr<llvm::cas::ObjectStore> DB) {
253253
// Override the CASOptions. They may match (the caller having sniffed them
254254
// out of InputArgs) but if they have been overridden we want the new ones.
255255
Invocation.getCASOpts() = Tool.getCASOpts();

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,10 +1177,7 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
11771177
std::atomic<size_t> NumIsLocalCalls = 0;
11781178

11791179
auto ScanningTask = [&](DependencyScanningService &Service) {
1180-
std::unique_ptr<llvm::vfs::FileSystem> FS = llvm::vfs::createPhysicalFileSystem();
1181-
if (CAS)
1182-
FS = llvm::cas::createCASProvidingFileSystem(CAS, std::move(FS));
1183-
DependencyScanningTool WorkerTool(Service, std::move(FS));
1180+
DependencyScanningTool WorkerTool(Service, llvm::vfs::createPhysicalFileSystem());
11841181

11851182
llvm::DenseSet<ModuleID> AlreadySeenModules;
11861183
while (auto MaybeInputIndex = GetNextInputIndex()) {
@@ -1204,7 +1201,7 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
12041201
HadErrors = true;
12051202
} else if (Format == ScanningOutputFormat::IncludeTree) {
12061203
auto MaybeTree = WorkerTool.getIncludeTree(
1207-
*CAS, Input->CommandLine, CWD, LookupOutput);
1204+
CAS, Input->CommandLine, CWD, LookupOutput);
12081205
std::unique_lock<std::mutex> LockGuard(Lock);
12091206
TreeResults.emplace_back(LocalIndex, std::move(Filename),
12101207
std::move(MaybeTree));

clang/tools/driver/cc1depscan_main.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
371371
tooling::dependencies::DependencyScanningTool &Tool,
372372
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS, const char *Exec,
373373
ArrayRef<const char *> InputArgs, StringRef WorkingDirectory,
374-
SmallVectorImpl<const char *> &OutputArgs, llvm::cas::ObjectStore &DB,
374+
SmallVectorImpl<const char *> &OutputArgs, std::shared_ptr<llvm::cas::ObjectStore> DB,
375375
llvm::function_ref<const char *(const Twine &)> SaveArg);
376376

377377
#ifdef LLVM_ON_UNIX
@@ -978,13 +978,8 @@ int ScanServer::listen() {
978978

979979
// Is this safe to reuse? Or does DependendencyScanningWorkerFileSystem
980980
// make some bad assumptions about relative paths?
981-
if (!Tool) {
982-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> UnderlyingFS =
983-
llvm::vfs::createPhysicalFileSystem();
984-
UnderlyingFS = llvm::cas::createCASProvidingFileSystem(
985-
CAS, std::move(UnderlyingFS));
986-
Tool.emplace(Service, std::move(UnderlyingFS));
987-
}
981+
if (!Tool)
982+
Tool.emplace(Service, llvm::vfs::createPhysicalFileSystem());
988983

989984
std::unique_ptr<DiagnosticOptions> DiagOpts =
990985
CreateAndPopulateDiagOpts(Args);
@@ -997,7 +992,7 @@ int ScanServer::listen() {
997992
SmallVector<const char *> NewArgs;
998993
auto RootID = scanAndUpdateCC1InlineWithTool(
999994
*Tool, *DiagsConsumer, &DiagsOS, Argv0, Args, WorkingDirectory,
1000-
NewArgs, *CAS, [&](const Twine &T) { return Saver.save(T).data(); });
995+
NewArgs, CAS, [&](const Twine &T) { return Saver.save(T).data(); });
1001996
if (!RootID) {
1002997
consumeError(Comms.putScanResultFailed(toString(RootID.takeError()),
1003998
DiagsOS.str()));
@@ -1081,7 +1076,7 @@ static Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
10811076
tooling::dependencies::DependencyScanningTool &Tool,
10821077
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS, const char *Exec,
10831078
ArrayRef<const char *> InputArgs, StringRef WorkingDirectory,
1084-
SmallVectorImpl<const char *> &OutputArgs, llvm::cas::ObjectStore &DB,
1079+
SmallVectorImpl<const char *> &OutputArgs, std::shared_ptr<llvm::cas::ObjectStore> DB,
10851080
llvm::function_ref<const char *(const Twine &)> SaveArg) {
10861081
DiagnosticOptions DiagOpts;
10871082
DiagnosticsEngine Diags(new DiagnosticIDs(), DiagOpts);
@@ -1117,12 +1112,8 @@ scanAndUpdateCC1Inline(const char *Exec, ArrayRef<const char *> InputArgs,
11171112
tooling::dependencies::ScanningMode::DependencyDirectivesScan,
11181113
tooling::dependencies::ScanningOutputFormat::IncludeTree, CASOpts, DB,
11191114
Cache);
1120-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> UnderlyingFS =
1121-
llvm::vfs::createPhysicalFileSystem();
1122-
UnderlyingFS =
1123-
llvm::cas::createCASProvidingFileSystem(DB, std::move(UnderlyingFS));
11241115
tooling::dependencies::DependencyScanningTool Tool(Service,
1125-
std::move(UnderlyingFS));
1116+
llvm::vfs::createPhysicalFileSystem());
11261117

11271118
std::unique_ptr<DiagnosticOptions> DiagOpts =
11281119
CreateAndPopulateDiagOpts(InputArgs);
@@ -1131,7 +1122,7 @@ scanAndUpdateCC1Inline(const char *Exec, ArrayRef<const char *> InputArgs,
11311122

11321123
auto E = scanAndUpdateCC1InlineWithTool(
11331124
Tool, *DiagsConsumer, /*VerboseOS*/ nullptr, Exec, InputArgs,
1134-
WorkingDirectory, OutputArgs, *DB, SaveArg)
1125+
WorkingDirectory, OutputArgs, DB, SaveArg)
11351126
.moveInto(RootID);
11361127
if (E) {
11371128
Diag.Report(diag::err_cas_depscan_failed) << std::move(E);

0 commit comments

Comments
 (0)