Skip to content

Commit b00c5ca

Browse files
committed
[clang][deps] Split lookupModuleOutput out of DependencyConsumer NFC
The idea is to split the callbacks that are used to consume dependency information (DependencyConsumer) from callbacks that modify the scan behaviour itself in any way (DependencyActionController). Currently this is just lookupModuleOutput, but we have additional callbacks related to CAS support that we intend to upstream in the future. Differential Revision: https://reviews.llvm.org/D144058 (cherry-picked from commit 296ba5b)
1 parent 84ec9db commit b00c5ca

File tree

6 files changed

+319
-223
lines changed

6 files changed

+319
-223
lines changed

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

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,8 @@ class DependencyScanningTool {
186186

187187
class FullDependencyConsumer : public DependencyConsumer {
188188
public:
189-
FullDependencyConsumer(const llvm::StringSet<> &AlreadySeen,
190-
LookupModuleOutputCallback LookupModuleOutput,
191-
CachingOnDiskFileSystemPtr CacheFS = nullptr,
192-
DepscanPrefixMapping PrefixMapping = {})
193-
: CacheFS(std::move(CacheFS)), PrefixMapping(std::move(PrefixMapping)),
194-
AlreadySeen(AlreadySeen), LookupModuleOutput(LookupModuleOutput) {}
195-
196-
llvm::Error initialize(CompilerInstance &ScanInstance,
197-
CompilerInvocation &NewInvocation) override;
198-
llvm::Error finalize(CompilerInstance &ScanInstance,
199-
CompilerInvocation &NewInvocation) override;
200-
llvm::Error
201-
initializeModuleBuild(CompilerInstance &ModuleScanInstance) override;
202-
llvm::Error
203-
finalizeModuleBuild(CompilerInstance &ModuleScanInstance) override;
204-
llvm::Error finalizeModuleInvocation(CompilerInvocation &CI,
205-
const ModuleDeps &MD) override;
189+
FullDependencyConsumer(const llvm::StringSet<> &AlreadySeen)
190+
: AlreadySeen(AlreadySeen) {}
206191

207192
void handleBuildCommand(Command Cmd) override {
208193
Commands.push_back(std::move(Cmd));
@@ -226,17 +211,8 @@ class FullDependencyConsumer : public DependencyConsumer {
226211
ContextHash = std::move(Hash);
227212
}
228213

229-
void handleCASFileSystemRootID(cas::CASID ID) override {
230-
CASFileSystemRootID = ID;
231-
}
232-
233-
Optional<cas::CASID> getCASFileSystemRootID() const {
234-
return CASFileSystemRootID;
235-
}
236-
237-
std::string lookupModuleOutput(const ModuleID &ID,
238-
ModuleOutputKind Kind) override {
239-
return LookupModuleOutput(ID, Kind);
214+
void handleCASFileSystemRootID(std::string ID) override {
215+
CASFileSystemRootID = std::move(ID);
240216
}
241217

242218
TranslationUnitDeps takeTranslationUnitDeps();
@@ -249,13 +225,33 @@ class FullDependencyConsumer : public DependencyConsumer {
249225
ClangModuleDeps;
250226
std::vector<Command> Commands;
251227
std::string ContextHash;
252-
CachingOnDiskFileSystemPtr CacheFS;
253-
DepscanPrefixMapping PrefixMapping;
254-
std::optional<llvm::TreePathPrefixMapper> Mapper;
255-
CASOptions CASOpts;
256-
Optional<cas::CASID> CASFileSystemRootID;
228+
Optional<std::string> CASFileSystemRootID;
257229
std::vector<std::string> OutputPaths;
258230
const llvm::StringSet<> &AlreadySeen;
231+
};
232+
233+
/// A simple dependency action controller that uses a callback. If no callback
234+
/// is provided, it is assumed that looking up module outputs is unreachable.
235+
class CallbackActionController : public DependencyActionController {
236+
public:
237+
virtual ~CallbackActionController();
238+
239+
CallbackActionController(LookupModuleOutputCallback LMO)
240+
: LookupModuleOutput(std::move(LMO)) {
241+
if (!LookupModuleOutput) {
242+
LookupModuleOutput = [](const ModuleID &,
243+
ModuleOutputKind) -> std::string {
244+
llvm::report_fatal_error("unexpected call to lookupModuleOutput");
245+
};
246+
}
247+
}
248+
249+
std::string lookupModuleOutput(const ModuleID &ID,
250+
ModuleOutputKind Kind) override {
251+
return LookupModuleOutput(ID, Kind);
252+
}
253+
254+
private:
259255
LookupModuleOutputCallback LookupModuleOutput;
260256
};
261257

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ class DependencyConsumer {
8484
virtual void handleContextHash(std::string Hash) = 0;
8585

8686
virtual void handleCASFileSystemRootID(cas::CASID ID) = 0;
87+
};
88+
89+
/// Dependency scanner callbacks that are used during scanning to influence the
90+
/// behaviour of the scan - for example, to customize the scanned invocations.
91+
class DependencyActionController {
92+
public:
93+
virtual ~DependencyActionController();
8794

8895
virtual std::string lookupModuleOutput(const ModuleID &ID,
8996
ModuleOutputKind Kind) = 0;
@@ -152,16 +159,15 @@ class DependencyScanningWorker {
152159
bool computeDependencies(StringRef WorkingDirectory,
153160
const std::vector<std::string> &CommandLine,
154161
DependencyConsumer &DepConsumer,
162+
DependencyActionController &Controller,
155163
DiagnosticConsumer &DiagConsumer,
156164
llvm::Optional<StringRef> ModuleName = None);
157165
/// \returns A \c StringError with the diagnostic output if clang errors
158166
/// occurred, success otherwise.
159-
llvm::Error computeDependencies(StringRef WorkingDirectory,
160-
const std::vector<std::string> &CommandLine,
161-
DependencyConsumer &Consumer,
162-
llvm::Optional<StringRef> ModuleName = None);
163-
164-
ScanningOutputFormat getFormat() const { return Format; }
167+
llvm::Error computeDependencies(
168+
StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
169+
DependencyConsumer &Consumer, DependencyActionController &Controller,
170+
llvm::Optional<StringRef> ModuleName = None);
165171

166172
/// Scan from a compiler invocation.
167173
/// If \p DiagGenerationAsCompilation is true it will generate error
@@ -176,7 +182,6 @@ class DependencyScanningWorker {
176182

177183
ScanningOutputFormat getScanningFormat() const { return Format; }
178184

179-
llvm::vfs::FileSystem &getRealFS() { return *BaseFS; }
180185
CachingOnDiskFileSystemPtr getCASFS() { return CacheFS; }
181186
bool useCAS() const { return UseCAS; }
182187
const CASOptions &getCASOpts() const { return CASOpts; }
@@ -201,16 +206,15 @@ class DependencyScanningWorker {
201206
ScanningOutputFormat Format;
202207
/// Whether to optimize the modules' command-line arguments.
203208
bool OptimizeArgs;
209+
/// Whether to set up command-lines to load PCM files eagerly.
210+
bool EagerLoadModules;
204211

205212
/// The caching file system.
206213
CachingOnDiskFileSystemPtr CacheFS;
207214
/// The CAS Dependency Filesytem. This is not set at the sametime as DepFS;
208215
llvm::IntrusiveRefCntPtr<DependencyScanningCASFilesystem> DepCASFS;
209216
CASOptions CASOpts;
210217
bool UseCAS;
211-
212-
/// Whether to set up command-lines to load PCM files eagerly.
213-
bool EagerLoadModules;
214218
};
215219

216220
} // end namespace dependencies

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace clang {
2828
namespace tooling {
2929
namespace dependencies {
3030

31+
class DependencyActionController;
3132
class DependencyConsumer;
3233

3334
/// Modular dependency that has already been built prior to the dependency scan.
@@ -192,6 +193,7 @@ class ModuleDepCollector final : public DependencyCollector {
192193
public:
193194
ModuleDepCollector(std::unique_ptr<DependencyOutputOptions> Opts,
194195
CompilerInstance &ScanInstance, DependencyConsumer &C,
196+
DependencyActionController &Controller,
195197
CompilerInvocation OriginalCI, bool OptimizeArgs,
196198
bool EagerLoadModules);
197199

@@ -209,6 +211,8 @@ class ModuleDepCollector final : public DependencyCollector {
209211
CompilerInstance &ScanInstance;
210212
/// The consumer of collected dependency information.
211213
DependencyConsumer &Consumer;
214+
/// Callbacks for computing dependency information.
215+
DependencyActionController &Controller;
212216
/// Path to the main source file.
213217
std::string MainFile;
214218
/// Hash identifying the compilation conditions of the current TU.

0 commit comments

Comments
 (0)