Skip to content

Commit 8ef15fc

Browse files
[clang][DependencyScanning] Add API to register callback for cmd transform
Add a callback to allow client to transform build command of the module through CompilerInvocation interface. This allows API user to modified the build command to its need with very little cost.
1 parent 9ae4b59 commit 8ef15fc

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "llvm/CAS/ActionCache.h"
1818

1919
namespace clang {
20+
class CowCompilerInvocation;
21+
2022
namespace tooling {
2123
namespace dependencies {
2224

@@ -98,13 +100,16 @@ enum class ScanningOptimizations {
98100
/// is used by the individual dependency scanning workers.
99101
class DependencyScanningService {
100102
public:
103+
using InvocationCallbackTy = std::function<void(CowCompilerInvocation &)>;
104+
101105
DependencyScanningService(
102106
ScanningMode Mode, ScanningOutputFormat Format, CASOptions CASOpts,
103107
std::shared_ptr<llvm::cas::ObjectStore> CAS,
104108
std::shared_ptr<llvm::cas::ActionCache> Cache,
105109
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> SharedFS,
106110
ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
107-
bool EagerLoadModules = false, bool TraceVFS = false);
111+
bool EagerLoadModules = false, bool TraceVFS = false,
112+
InvocationCallbackTy TransForm = nullptr);
108113

109114
ScanningMode getMode() const { return Mode; }
110115

@@ -133,6 +138,10 @@ class DependencyScanningService {
133138

134139
ModuleCacheMutexes &getModuleCacheMutexes() { return ModCacheMutexes; }
135140

141+
InvocationCallbackTy getCompilerInvocationTransformation() const {
142+
return Transform;
143+
}
144+
136145
private:
137146
const ScanningMode Mode;
138147
const ScanningOutputFormat Format;
@@ -152,6 +161,8 @@ class DependencyScanningService {
152161
std::optional<DependencyScanningFilesystemSharedCache> SharedCache;
153162
/// The global module cache mutexes.
154163
ModuleCacheMutexes ModCacheMutexes;
164+
/// Callback to transform compiler invocation.
165+
const InvocationCallbackTy Transform;
155166
};
156167

157168
} // end namespace dependencies

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,13 @@ class ModuleDepCollectorPP final : public PPCallbacks {
266266
/// \c ModuleDepCollectorPP to the preprocessor.
267267
class ModuleDepCollector final : public DependencyCollector {
268268
public:
269-
ModuleDepCollector(DependencyScanningService &Service,
270-
std::unique_ptr<DependencyOutputOptions> Opts,
271-
CompilerInstance &ScanInstance, DependencyConsumer &C,
272-
DependencyActionController &Controller,
273-
CompilerInvocation OriginalCI,
274-
PrebuiltModuleVFSMapT PrebuiltModuleVFSMap);
269+
ModuleDepCollector(
270+
DependencyScanningService &Service,
271+
std::unique_ptr<DependencyOutputOptions> Opts,
272+
CompilerInstance &ScanInstance, DependencyConsumer &C,
273+
DependencyActionController &Controller, CompilerInvocation OriginalCI,
274+
PrebuiltModuleVFSMapT PrebuiltModuleVFSMap,
275+
std::function<void(CowCompilerInvocation &)> TransformInvocation);
275276

276277
void attachToPreprocessor(Preprocessor &PP) override;
277278
void attachToASTReader(ASTReader &R) override;
@@ -320,6 +321,8 @@ class ModuleDepCollector final : public DependencyCollector {
320321
std::optional<P1689ModuleInfo> ProvidedStdCXXModule;
321322
std::vector<P1689ModuleInfo> RequiredStdCXXModules;
322323

324+
std::function<void(CowCompilerInvocation &)> TransformInvocation;
325+
323326
/// Checks whether the module is known as being prebuilt.
324327
bool isPrebuiltModule(const Module *M);
325328

clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ DependencyScanningService::DependencyScanningService(
2121
std::shared_ptr<llvm::cas::ObjectStore> CAS,
2222
std::shared_ptr<llvm::cas::ActionCache> Cache,
2323
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> SharedFS,
24-
ScanningOptimizations OptimizeArgs, bool EagerLoadModules, bool TraceVFS)
25-
: Mode(Mode), Format(Format), CASOpts(std::move(CASOpts)), CAS(std::move(CAS)), Cache(std::move(Cache)),
26-
OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules), TraceVFS(TraceVFS),
27-
SharedFS(std::move(SharedFS)) {
24+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules, bool TraceVFS,
25+
InvocationCallbackTy Transform)
26+
: Mode(Mode), Format(Format), CASOpts(std::move(CASOpts)),
27+
CAS(std::move(CAS)), Cache(std::move(Cache)), OptimizeArgs(OptimizeArgs),
28+
EagerLoadModules(EagerLoadModules), TraceVFS(TraceVFS),
29+
SharedFS(std::move(SharedFS)), Transform(Transform) {
2830
if (!this->SharedFS)
2931
SharedCache.emplace();
3032

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,11 @@ class DependencyScanningAction : public tooling::ToolAction {
404404
bool DiagGenerationAsCompilation, const CASOptions &CASOpts,
405405
std::optional<StringRef> ModuleName = std::nullopt,
406406
raw_ostream *VerboseOS = nullptr)
407-
: Service(Service), WorkingDirectory(WorkingDirectory), Consumer(Consumer),
408-
Controller(Controller), DepFS(std::move(DepFS)),
407+
: Service(Service), WorkingDirectory(WorkingDirectory),
408+
Consumer(Consumer), Controller(Controller), DepFS(std::move(DepFS)),
409409
DepCASFS(std::move(DepCASFS)), CacheFS(std::move(CacheFS)),
410-
DisableFree(DisableFree),
411-
CASOpts(CASOpts), EmitDependencyFile(EmitDependencyFile),
410+
DisableFree(DisableFree), CASOpts(CASOpts),
411+
EmitDependencyFile(EmitDependencyFile),
412412
DiagGenerationAsCompilation(DiagGenerationAsCompilation),
413413
ModuleName(ModuleName), VerboseOS(VerboseOS) {}
414414

@@ -569,7 +569,8 @@ class DependencyScanningAction : public tooling::ToolAction {
569569

570570
MDC = std::make_shared<ModuleDepCollector>(
571571
Service, std::move(Opts), ScanInstance, Consumer, Controller,
572-
OriginalInvocation, std::move(PrebuiltModuleVFSMap));
572+
OriginalInvocation, std::move(PrebuiltModuleVFSMap),
573+
Service.getCompilerInvocationTransformation());
573574
ScanInstance.addDependencyCollector(MDC);
574575
ScanInstance.setGenModuleActionWrapper(
575576
[&Controller = Controller](const FrontendOptions &Opts,

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,12 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
956956
// Finish the compiler invocation. Requires dependencies and the context hash.
957957
MDC.addOutputPaths(CI, MD);
958958

959+
if (MDC.TransformInvocation)
960+
MDC.TransformInvocation(CI);
961+
959962
#ifndef NDEBUG
960963
// Verify the key has not changed with final arguments.
961-
if (MD.ModuleCacheKey) {
964+
if (MD.ModuleCacheKey && !MDC.TransformInvocation) {
962965
auto &CAS = MDC.ScanInstance.getOrCreateObjectStore();
963966
auto Key = createCompileJobCacheKey(CAS, Diags, CI);
964967
assert(Key);
@@ -1078,13 +1081,15 @@ ModuleDepCollector::ModuleDepCollector(
10781081
std::unique_ptr<DependencyOutputOptions> Opts,
10791082
CompilerInstance &ScanInstance, DependencyConsumer &C,
10801083
DependencyActionController &Controller, CompilerInvocation OriginalCI,
1081-
PrebuiltModuleVFSMapT PrebuiltModuleVFSMap)
1084+
PrebuiltModuleVFSMapT PrebuiltModuleVFSMap,
1085+
std::function<void(CowCompilerInvocation &)> TransformInvocation)
10821086
: Service(Service), ScanInstance(ScanInstance), Consumer(C),
10831087
Controller(Controller),
10841088
PrebuiltModuleVFSMap(std::move(PrebuiltModuleVFSMap)),
10851089
Opts(std::move(Opts)),
10861090
CommonInvocation(
1087-
makeCommonInvocationForModuleBuild(std::move(OriginalCI))) {}
1091+
makeCommonInvocationForModuleBuild(std::move(OriginalCI))),
1092+
TransformInvocation(TransformInvocation) {}
10881093

10891094
void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
10901095
PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(*this));

0 commit comments

Comments
 (0)