Skip to content

Commit fe94d6c

Browse files
committed
[clang][deps][cas] Perform prefix mapping inside FullDependencyConsumer
This makes FullDependencyConsumer behave more like the include-tree consumer, which does prefix mapping internally rather than via callback. This is in preparation for expanding the availability of prefix mapping to all users of the FullDependenciesConsumer rather than only in ScanAndUpdateArgs. Note: there is some duplication of prefix mappers in scanAndUpdateArgs because it is prefix-mapping a different CompilerInvocation than the one that is inside the consumer. That duplication is the same for the existing include-tree code path. (cherry picked from commit aea1b44)
1 parent 811b6fa commit fe94d6c

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
1313
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
1414
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
15+
#include "clang/Tooling/DependencyScanning/ScanAndUpdateArgs.h"
1516
#include "clang/Tooling/JSONCompilationDatabase.h"
1617
#include "llvm/ADT/MapVector.h"
17-
#include "llvm/ADT/StringSet.h"
1818
#include "llvm/ADT/StringMap.h"
19+
#include "llvm/ADT/StringSet.h"
1920
#include "llvm/CAS/CASID.h"
21+
#include "llvm/Support/PrefixMapper.h"
22+
#include <optional>
2023
#include <string>
2124
#include <vector>
2225

@@ -33,8 +36,6 @@ class IncludeTreeRoot;
3336
namespace tooling {
3437
namespace dependencies {
3538

36-
struct DepscanPrefixMapping;
37-
3839
/// A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
3940
using LookupModuleOutputCallback =
4041
llvm::function_ref<std::string(const ModuleID &, ModuleOutputKind)>;
@@ -81,8 +82,6 @@ struct FullDependenciesResult {
8182
std::vector<ModuleDeps> DiscoveredModules;
8283
};
8384

84-
using RemapPathCallback = llvm::cas::CachingOnDiskFileSystem::RemapPathCallback;
85-
8685
/// The high-level implementation of the dependency discovery tool that runs on
8786
/// an individual worker thread.
8887
class DependencyScanningTool {
@@ -115,7 +114,8 @@ class DependencyScanningTool {
115114
getDependencyTreeFromCompilerInvocation(
116115
std::shared_ptr<CompilerInvocation> Invocation, StringRef CWD,
117116
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
118-
bool DiagGenerationAsCompilation, RemapPathCallback RemapPath = nullptr);
117+
bool DiagGenerationAsCompilation,
118+
DepscanPrefixMapping PrefixMapping = {});
119119

120120
Expected<cas::IncludeTreeRoot>
121121
getIncludeTree(cas::ObjectStore &DB,
@@ -186,8 +186,8 @@ class FullDependencyConsumer : public DependencyConsumer {
186186
LookupModuleOutputCallback LookupModuleOutput,
187187
bool EagerLoadModules,
188188
CachingOnDiskFileSystemPtr CacheFS = nullptr,
189-
RemapPathCallback RemapPath = nullptr)
190-
: CacheFS(std::move(CacheFS)), RemapPath(RemapPath),
189+
DepscanPrefixMapping PrefixMapping = {})
190+
: CacheFS(std::move(CacheFS)), PrefixMapping(std::move(PrefixMapping)),
191191
AlreadySeen(AlreadySeen), LookupModuleOutput(LookupModuleOutput),
192192
EagerLoadModules(EagerLoadModules) {}
193193

@@ -250,7 +250,8 @@ class FullDependencyConsumer : public DependencyConsumer {
250250
std::vector<Command> Commands;
251251
std::string ContextHash;
252252
CachingOnDiskFileSystemPtr CacheFS;
253-
RemapPathCallback RemapPath;
253+
DepscanPrefixMapping PrefixMapping;
254+
std::optional<llvm::TreePathPrefixMapper> Mapper;
254255
CASOptions CASOpts;
255256
Optional<cas::CASID> CASFileSystemRootID;
256257
std::vector<std::string> OutputPaths;

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ class GetDependencyTree : public FullDependencyConsumer {
143143
}
144144

145145
GetDependencyTree(llvm::cas::CachingOnDiskFileSystem &FS,
146-
RemapPathCallback RemapPath)
146+
DepscanPrefixMapping PrefixMapping)
147147
: FullDependencyConsumer(AlreadySeen, nullptr, /*EagerModules=*/false,
148-
&FS, RemapPath),
148+
&FS, std::move(PrefixMapping)),
149149
FS(FS) {}
150150

151151
private:
@@ -157,7 +157,7 @@ class GetDependencyTree : public FullDependencyConsumer {
157157
llvm::Expected<llvm::cas::ObjectProxy>
158158
DependencyScanningTool::getDependencyTree(
159159
const std::vector<std::string> &CommandLine, StringRef CWD) {
160-
GetDependencyTree Consumer(*Worker.getCASFS(), /*RemapPath=*/nullptr);
160+
GetDependencyTree Consumer(*Worker.getCASFS(), /*PrefixMapping=*/{});
161161
auto Result = Worker.computeDependencies(CWD, CommandLine, Consumer);
162162
if (Result)
163163
return std::move(Result);
@@ -168,8 +168,8 @@ llvm::Expected<llvm::cas::ObjectProxy>
168168
DependencyScanningTool::getDependencyTreeFromCompilerInvocation(
169169
std::shared_ptr<CompilerInvocation> Invocation, StringRef CWD,
170170
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
171-
bool DiagGenerationAsCompilation, RemapPathCallback RemapPath) {
172-
GetDependencyTree Consumer(*Worker.getCASFS(), RemapPath);
171+
bool DiagGenerationAsCompilation, DepscanPrefixMapping PrefixMapping) {
172+
GetDependencyTree Consumer(*Worker.getCASFS(), std::move(PrefixMapping));
173173
Worker.computeDependenciesFromCompilerInvocation(
174174
std::move(Invocation), CWD, Consumer, DiagsConsumer, VerboseOS,
175175
DiagGenerationAsCompilation);
@@ -588,6 +588,11 @@ DependencyScanningTool::getFullDependenciesLegacyDriverCommand(
588588
Error FullDependencyConsumer::initialize(CompilerInstance &ScanInstance,
589589
CompilerInvocation &NewInvocation) {
590590
if (CacheFS) {
591+
// Setup prefix mapping.
592+
Mapper.emplace(CacheFS);
593+
if (Error E = PrefixMapping.configurePrefixMapper(NewInvocation, *Mapper))
594+
return E;
595+
591596
CacheFS->trackNewAccesses();
592597
if (auto CWD =
593598
ScanInstance.getVirtualFileSystem().getCurrentWorkingDirectory())
@@ -617,8 +622,14 @@ Error FullDependencyConsumer::finalize(CompilerInstance &ScanInstance,
617622
(void)CacheFS->status(NewInvocation.getCodeGenOpts().SampleProfileFile);
618623
(void)CacheFS->status(NewInvocation.getCodeGenOpts().ProfileRemappingFile);
619624

620-
if (auto E = CacheFS->createTreeFromNewAccesses(RemapPath).moveInto(
621-
CASFileSystemRootID))
625+
auto E = CacheFS
626+
->createTreeFromNewAccesses(
627+
[&](const llvm::vfs::CachedDirectoryEntry &Entry,
628+
SmallVectorImpl<char> &Storage) {
629+
return Mapper->mapDirEntry(Entry, Storage);
630+
})
631+
.moveInto(CASFileSystemRootID);
632+
if (E)
622633
return E;
623634

624635
configureInvocationForCaching(NewInvocation, CASOpts,
@@ -654,7 +665,14 @@ Error FullDependencyConsumer::finalizeModuleBuild(
654665
CompilerInstance &ModuleScanInstance) {
655666
if (CacheFS) {
656667
std::optional<cas::CASID> RootID;
657-
if (auto E = CacheFS->createTreeFromNewAccesses(RemapPath).moveInto(RootID))
668+
auto E = CacheFS
669+
->createTreeFromNewAccesses(
670+
[&](const llvm::vfs::CachedDirectoryEntry &Entry,
671+
SmallVectorImpl<char> &Storage) {
672+
return Mapper->mapDirEntry(Entry, Storage);
673+
})
674+
.moveInto(RootID);
675+
if (E)
658676
return E;
659677

660678
Module *M = ModuleScanInstance.getPreprocessor().getCurrentModule();

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,11 @@ Expected<llvm::cas::CASID> clang::scanAndUpdateCC1InlineWithTool(
257257
.moveInto(Root))
258258
return std::move(E);
259259
} else {
260-
if (Error E =
261-
Tool.getDependencyTreeFromCompilerInvocation(
262-
std::move(ScanInvocation), WorkingDirectory, DiagsConsumer,
263-
VerboseOS,
264-
/*DiagGenerationAsCompilation*/ true,
265-
[&](const llvm::vfs::CachedDirectoryEntry &Entry,
266-
SmallVectorImpl<char> &Storage) {
267-
return static_cast<llvm::TreePathPrefixMapper &>(Mapper)
268-
.mapDirEntry(Entry, Storage);
269-
})
270-
.moveInto(Root))
260+
if (Error E = Tool.getDependencyTreeFromCompilerInvocation(
261+
std::move(ScanInvocation), WorkingDirectory,
262+
DiagsConsumer, VerboseOS,
263+
/*DiagGenerationAsCompilation*/ true, PrefixMapping)
264+
.moveInto(Root))
271265
return std::move(E);
272266
}
273267

0 commit comments

Comments
 (0)