Skip to content

Commit 217a752

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 217a752

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

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

+12-1
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/lib/Tooling/DependencyScanning/DependencyScanningService.cpp

+6-4
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/ModuleDepCollector.cpp

+4-1
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 (auto Transform = MDC.Service.getCompilerInvocationTransformation())
960+
Transform(CI);
961+
959962
#ifndef NDEBUG
960963
// Verify the key has not changed with final arguments.
961-
if (MD.ModuleCacheKey) {
964+
if (MD.ModuleCacheKey && !MDC.Service.getCompilerInvocationTransformation()) {
962965
auto &CAS = MDC.ScanInstance.getOrCreateObjectStore();
963966
auto Key = createCompileJobCacheKey(CAS, Diags, CI);
964967
assert(Key);

0 commit comments

Comments
 (0)