Skip to content

Commit bc6487f

Browse files
committed
[clang][deps] NFC: Rename scanning CompilerInstance
The dependency scanner works with multiple instances of `Compiler{Instance,Invocation}`. From names of the variables/members, their purpose is not obvious. This patch gives a distinct name to the `CompilerInstance` that's used to run the implicit build during dependency scan. Depends on D111724. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D111725
1 parent 3ba28c0 commit bc6487f

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class ModuleDepCollectorPP final : public PPCallbacks {
196196
class ModuleDepCollector final : public DependencyCollector {
197197
public:
198198
ModuleDepCollector(std::unique_ptr<DependencyOutputOptions> Opts,
199-
CompilerInstance &I, DependencyConsumer &C,
199+
CompilerInstance &ScanInstance, DependencyConsumer &C,
200200
CompilerInvocation &&OriginalCI, bool OptimizeArgs);
201201

202202
void attachToPreprocessor(Preprocessor &PP) override;
@@ -205,8 +205,8 @@ class ModuleDepCollector final : public DependencyCollector {
205205
private:
206206
friend ModuleDepCollectorPP;
207207

208-
/// The compiler instance for the current translation unit.
209-
CompilerInstance &Instance;
208+
/// The compiler instance for scanning the current translation unit.
209+
CompilerInstance &ScanInstance;
210210
/// The consumer of collected dependency information.
211211
DependencyConsumer &Consumer;
212212
/// Path to the main source file.

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -165,34 +165,34 @@ class DependencyScanningAction : public tooling::ToolAction {
165165
CompilerInvocation OriginalInvocation(*Invocation);
166166

167167
// Create a compiler instance to handle the actual work.
168-
CompilerInstance Compiler(std::move(PCHContainerOps));
169-
Compiler.setInvocation(std::move(Invocation));
168+
CompilerInstance ScanInstance(std::move(PCHContainerOps));
169+
ScanInstance.setInvocation(std::move(Invocation));
170170

171171
// Create the compiler's actual diagnostics engine.
172-
sanitizeDiagOpts(Compiler.getDiagnosticOpts());
173-
Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
174-
if (!Compiler.hasDiagnostics())
172+
sanitizeDiagOpts(ScanInstance.getDiagnosticOpts());
173+
ScanInstance.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
174+
if (!ScanInstance.hasDiagnostics())
175175
return false;
176176

177-
Compiler.getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath = true;
177+
ScanInstance.getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath =
178+
true;
178179

179180
FileMgr->getFileSystemOpts().WorkingDir = std::string(WorkingDirectory);
180-
Compiler.setFileManager(FileMgr);
181-
Compiler.createSourceManager(*FileMgr);
181+
ScanInstance.setFileManager(FileMgr);
182+
ScanInstance.createSourceManager(*FileMgr);
182183

183184
llvm::StringSet<> PrebuiltModulesInputFiles;
184185
// Store the list of prebuilt module files into header search options. This
185186
// will prevent the implicit build to create duplicate modules and will
186187
// force reuse of the existing prebuilt module files instead.
187-
if (!Compiler.getPreprocessorOpts().ImplicitPCHInclude.empty())
188+
if (!ScanInstance.getPreprocessorOpts().ImplicitPCHInclude.empty())
188189
visitPrebuiltModule(
189-
Compiler.getPreprocessorOpts().ImplicitPCHInclude, Compiler,
190-
Compiler.getHeaderSearchOpts().PrebuiltModuleFiles,
190+
ScanInstance.getPreprocessorOpts().ImplicitPCHInclude, ScanInstance,
191+
ScanInstance.getHeaderSearchOpts().PrebuiltModuleFiles,
191192
PrebuiltModulesInputFiles, /*VisitInputFiles=*/DepFS != nullptr);
192193

193194
// Use the dependency scanning optimized file system if requested to do so.
194195
if (DepFS) {
195-
const CompilerInvocation &CI = Compiler.getInvocation();
196196
DepFS->clearIgnoredFiles();
197197
// Ignore any files that contributed to prebuilt modules. The implicit
198198
// build validates the modules by comparing the reported sizes of their
@@ -203,20 +203,20 @@ class DependencyScanningAction : public tooling::ToolAction {
203203
// Add any filenames that were explicity passed in the build settings and
204204
// that might be opened, as we want to ensure we don't run source
205205
// minimization on them.
206-
for (const auto &Entry : CI.getHeaderSearchOpts().UserEntries)
207-
DepFS->ignoreFile(Entry.Path);
208-
for (const auto &Entry : CI.getHeaderSearchOpts().VFSOverlayFiles)
209-
DepFS->ignoreFile(Entry);
206+
for (const auto &E : ScanInstance.getHeaderSearchOpts().UserEntries)
207+
DepFS->ignoreFile(E.Path);
208+
for (const auto &F : ScanInstance.getHeaderSearchOpts().VFSOverlayFiles)
209+
DepFS->ignoreFile(F);
210210

211211
// Support for virtual file system overlays on top of the caching
212212
// filesystem.
213213
FileMgr->setVirtualFileSystem(createVFSFromCompilerInvocation(
214-
CI, Compiler.getDiagnostics(), DepFS));
214+
ScanInstance.getInvocation(), ScanInstance.getDiagnostics(), DepFS));
215215

216216
// Pass the skip mappings which should speed up excluded conditional block
217217
// skipping in the preprocessor.
218218
if (PPSkipMappings)
219-
Compiler.getPreprocessorOpts()
219+
ScanInstance.getPreprocessorOpts()
220220
.ExcludedConditionalDirectiveSkipMappings = PPSkipMappings;
221221
}
222222

@@ -228,24 +228,25 @@ class DependencyScanningAction : public tooling::ToolAction {
228228
// which ensures that the compiler won't create new dependency collectors,
229229
// and thus won't write out the extra '.d' files to disk.
230230
auto Opts = std::make_unique<DependencyOutputOptions>();
231-
std::swap(*Opts, Compiler.getInvocation().getDependencyOutputOpts());
231+
std::swap(*Opts, ScanInstance.getInvocation().getDependencyOutputOpts());
232232
// We need at least one -MT equivalent for the generator of make dependency
233233
// files to work.
234234
if (Opts->Targets.empty())
235-
Opts->Targets = {deduceDepTarget(Compiler.getFrontendOpts().OutputFile,
236-
Compiler.getFrontendOpts().Inputs)};
235+
Opts->Targets = {
236+
deduceDepTarget(ScanInstance.getFrontendOpts().OutputFile,
237+
ScanInstance.getFrontendOpts().Inputs)};
237238
Opts->IncludeSystemHeaders = true;
238239

239240
switch (Format) {
240241
case ScanningOutputFormat::Make:
241-
Compiler.addDependencyCollector(
242+
ScanInstance.addDependencyCollector(
242243
std::make_shared<DependencyConsumerForwarder>(std::move(Opts),
243244
Consumer));
244245
break;
245246
case ScanningOutputFormat::Full:
246-
Compiler.addDependencyCollector(std::make_shared<ModuleDepCollector>(
247-
std::move(Opts), Compiler, Consumer, std::move(OriginalInvocation),
248-
OptimizeArgs));
247+
ScanInstance.addDependencyCollector(std::make_shared<ModuleDepCollector>(
248+
std::move(Opts), ScanInstance, Consumer,
249+
std::move(OriginalInvocation), OptimizeArgs));
249250
break;
250251
}
251252

@@ -254,7 +255,7 @@ class DependencyScanningAction : public tooling::ToolAction {
254255
//
255256
// TODO: Implement diagnostic bucketing to reduce the impact of strict
256257
// context hashing.
257-
Compiler.getHeaderSearchOpts().ModulesStrictContextHash = true;
258+
ScanInstance.getHeaderSearchOpts().ModulesStrictContextHash = true;
258259

259260
std::unique_ptr<FrontendAction> Action;
260261

@@ -263,7 +264,7 @@ class DependencyScanningAction : public tooling::ToolAction {
263264
else
264265
Action = std::make_unique<ReadPCHAndPreprocessAction>();
265266

266-
const bool Result = Compiler.ExecuteAction(*Action);
267+
const bool Result = ScanInstance.ExecuteAction(*Action);
267268
if (!DepFS)
268269
FileMgr->clearStatCache();
269270
return Result;

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ void ModuleDepCollectorPP::FileChanged(SourceLocation Loc,
151151
FileID PrevFID) {
152152
if (Reason != PPCallbacks::EnterFile)
153153
return;
154-
154+
155155
// This has to be delayed as the context hash can change at the start of
156156
// `CompilerInstance::ExecuteAction`.
157157
if (MDC.ContextHash.empty()) {
158-
MDC.ContextHash = MDC.Instance.getInvocation().getModuleHash(
159-
MDC.Instance.getDiagnostics());
158+
MDC.ContextHash = MDC.ScanInstance.getInvocation().getModuleHash(
159+
MDC.ScanInstance.getDiagnostics());
160160
MDC.Consumer.handleContextHash(MDC.ContextHash);
161161
}
162162

163-
SourceManager &SM = MDC.Instance.getSourceManager();
163+
SourceManager &SM = MDC.ScanInstance.getSourceManager();
164164

165165
// Dependency generation really does want to go all the way to the
166166
// file entry for a source location to find out what is depended on.
@@ -203,13 +203,14 @@ void ModuleDepCollectorPP::handleImport(const Module *Imported) {
203203
}
204204

205205
void ModuleDepCollectorPP::EndOfMainFile() {
206-
FileID MainFileID = MDC.Instance.getSourceManager().getMainFileID();
207-
MDC.MainFile = std::string(
208-
MDC.Instance.getSourceManager().getFileEntryForID(MainFileID)->getName());
206+
FileID MainFileID = MDC.ScanInstance.getSourceManager().getMainFileID();
207+
MDC.MainFile = std::string(MDC.ScanInstance.getSourceManager()
208+
.getFileEntryForID(MainFileID)
209+
->getName());
209210

210-
if (!MDC.Instance.getPreprocessorOpts().ImplicitPCHInclude.empty())
211+
if (!MDC.ScanInstance.getPreprocessorOpts().ImplicitPCHInclude.empty())
211212
MDC.FileDeps.push_back(
212-
MDC.Instance.getPreprocessorOpts().ImplicitPCHInclude);
213+
MDC.ScanInstance.getPreprocessorOpts().ImplicitPCHInclude);
213214

214215
for (const Module *M : DirectModularDeps) {
215216
// A top-level module might not be actually imported as a module when
@@ -248,15 +249,16 @@ ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
248249
MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
249250
MD.IsSystem = M->IsSystem;
250251

251-
const FileEntry *ModuleMap = MDC.Instance.getPreprocessor()
252+
const FileEntry *ModuleMap = MDC.ScanInstance.getPreprocessor()
252253
.getHeaderSearchInfo()
253254
.getModuleMap()
254255
.getModuleMapFileForUniquing(M);
255256
MD.ClangModuleMapFile = std::string(ModuleMap ? ModuleMap->getName() : "");
256257

257258
serialization::ModuleFile *MF =
258-
MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
259-
MDC.Instance.getASTReader()->visitInputFiles(
259+
MDC.ScanInstance.getASTReader()->getModuleManager().lookup(
260+
M->getASTFile());
261+
MDC.ScanInstance.getASTReader()->visitInputFiles(
260262
*MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
261263
// __inferred_module.map is the result of the way in which an implicit
262264
// module build handles inferred modules. It adds an overlay VFS with
@@ -280,7 +282,7 @@ ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
280282
MD, [&](CompilerInvocation &CI) {
281283
if (MDC.OptimizeArgs)
282284
optimizeHeaderSearchOpts(CI.getHeaderSearchOpts(),
283-
*MDC.Instance.getASTReader(), *MF);
285+
*MDC.ScanInstance.getASTReader(), *MF);
284286
});
285287
MD.ID.ContextHash = MD.Invocation.getModuleHash(Instance.getDiagnostics());
286288

@@ -332,9 +334,10 @@ void ModuleDepCollectorPP::addModuleDep(
332334
}
333335

334336
ModuleDepCollector::ModuleDepCollector(
335-
std::unique_ptr<DependencyOutputOptions> Opts, CompilerInstance &I,
336-
DependencyConsumer &C, CompilerInvocation &&OriginalCI, bool OptimizeArgs)
337-
: Instance(I), Consumer(C), Opts(std::move(Opts)),
337+
std::unique_ptr<DependencyOutputOptions> Opts,
338+
CompilerInstance &ScanInstance, DependencyConsumer &C,
339+
CompilerInvocation &&OriginalCI, bool OptimizeArgs)
340+
: ScanInstance(ScanInstance), Consumer(C), Opts(std::move(Opts)),
338341
OriginalInvocation(std::move(OriginalCI)), OptimizeArgs(OptimizeArgs) {}
339342

340343
void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
@@ -346,7 +349,7 @@ void ModuleDepCollector::attachToASTReader(ASTReader &R) {}
346349
bool ModuleDepCollector::isPrebuiltModule(const Module *M) {
347350
std::string Name(M->getTopLevelModuleName());
348351
const auto &PrebuiltModuleFiles =
349-
Instance.getHeaderSearchOpts().PrebuiltModuleFiles;
352+
ScanInstance.getHeaderSearchOpts().PrebuiltModuleFiles;
350353
auto PrebuiltModuleFileIt = PrebuiltModuleFiles.find(Name);
351354
if (PrebuiltModuleFileIt == PrebuiltModuleFiles.end())
352355
return false;

0 commit comments

Comments
 (0)