Skip to content

Commit b86ddae

Browse files
authored
[clang] NFCI: Clean up CompilerInstance::create{File,Source}Manager() (#160748)
The `CompilerInstance::createSourceManager()` function currently accepts the `FileManager` to be used. However, all clients call `CompilerInstance::createFileManager()` prior to creating the `SourceManager`, and it never makes sense to use a `FileManager` in the `SourceManager` that's different from the rest of the compiler. Passing the `FileManager` explicitly is redundant, error-prone, and deviates from the style of other `CompilerInstance` initialization APIs. This PR therefore removes the `FileManager` parameter from `createSourceManager()` and also stops returning the `FileManager` pointer from `createFileManager()`, since that was its primary use. Now, `createSourceManager()` internally calls `getFileManager()` instead.
1 parent 99d8590 commit b86ddae

File tree

15 files changed

+34
-37
lines changed

15 files changed

+34
-37
lines changed

clang-tools-extra/clang-include-fixer/IncludeFixer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ bool IncludeFixerActionFactory::runInvocation(
9696
// diagnostics here.
9797
Compiler.createDiagnostics(new clang::IgnoringDiagConsumer,
9898
/*ShouldOwnClient=*/true);
99-
Compiler.createSourceManager(*Files);
99+
Compiler.createSourceManager();
100100

101101
// We abort on fatal errors so don't let a large number of errors become
102102
// fatal. A missing #include can cause thousands of errors.

clang-tools-extra/include-cleaner/unittests/RecordTest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,12 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
649649
Clang->createVirtualFileSystem(VFS);
650650
Clang->createDiagnostics();
651651

652-
auto *FM = Clang->createFileManager();
652+
Clang->createFileManager();
653+
FileManager &FM = Clang->getFileManager();
653654
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
654655
EXPECT_THAT(
655-
PI.getExporters(llvm::cantFail(FM->getFileRef("foo.h")), *FM),
656-
testing::ElementsAre(llvm::cantFail(FM->getFileRef("exporter.h"))));
656+
PI.getExporters(llvm::cantFail(FM.getFileRef("foo.h")), FM),
657+
testing::ElementsAre(llvm::cantFail(FM.getFileRef("exporter.h"))));
657658
}
658659

659660
TEST_F(PragmaIncludeTest, OutlivesFMAndSM) {

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,10 @@ class CompilerInstance : public ModuleLoader {
712712
const CodeGenOptions *CodeGenOpts = nullptr);
713713

714714
/// Create the file manager and replace any existing one with it.
715-
///
716-
/// \return The new file manager on success, or null on failure.
717-
FileManager *createFileManager();
715+
void createFileManager();
718716

719717
/// Create the source manager and replace any existing one with it.
720-
void createSourceManager(FileManager &FileMgr);
718+
void createSourceManager();
721719

722720
/// Create the preprocessor, using the invocation, file, and source managers,
723721
/// and replace any existing one with it.

clang/lib/ExtractAPI/ExtractAPIConsumer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ bool ExtractAPIAction::PrepareToExecuteAction(CompilerInstance &CI) {
444444
return true;
445445

446446
if (!CI.hasFileManager())
447-
if (!CI.createFileManager())
448-
return false;
447+
CI.createFileManager();
449448

450449
auto Kind = Inputs[0].getKind();
451450

clang/lib/Frontend/ChainedIncludesSource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ clang::createChainedIncludesSource(CompilerInstance &CI,
129129
Clang->setTarget(TargetInfo::CreateTargetInfo(
130130
Clang->getDiagnostics(), Clang->getInvocation().getTargetOpts()));
131131
Clang->createFileManager();
132-
Clang->createSourceManager(Clang->getFileManager());
132+
Clang->createSourceManager();
133133
Clang->createPreprocessor(TU_Prefix);
134134
Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
135135
&Clang->getPreprocessor());

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,18 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
382382

383383
// File Manager
384384

385-
FileManager *CompilerInstance::createFileManager() {
385+
void CompilerInstance::createFileManager() {
386386
assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
387387
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
388-
return FileMgr.get();
389388
}
390389

391390
// Source Manager
392391

393-
void CompilerInstance::createSourceManager(FileManager &FileMgr) {
394-
SourceMgr =
395-
llvm::makeIntrusiveRefCnt<SourceManager>(getDiagnostics(), FileMgr);
392+
void CompilerInstance::createSourceManager() {
393+
assert(Diagnostics && "DiagnosticsEngine needed for creating SourceManager");
394+
assert(FileMgr && "FileManager needed for creating SourceManager");
395+
SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(getDiagnostics(),
396+
getFileManager());
396397
}
397398

398399
// Initialize the remapping of files to alternative contents, e.g.,
@@ -1186,7 +1187,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
11861187
if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
11871188
Instance.getDiagnostics().setSuppressSystemWarnings(false);
11881189

1189-
Instance.createSourceManager(Instance.getFileManager());
1190+
Instance.createSourceManager();
11901191
SourceManager &SourceMgr = Instance.getSourceManager();
11911192

11921193
if (ThreadSafeConfig) {

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
879879
// file, otherwise the CompilerInstance will happily destroy them.
880880
CI.setVirtualFileSystem(AST->getFileManager().getVirtualFileSystemPtr());
881881
CI.setFileManager(AST->getFileManagerPtr());
882-
CI.createSourceManager(CI.getFileManager());
882+
CI.createSourceManager();
883883
CI.getSourceManager().initializeForReplay(AST->getSourceManager());
884884

885885
// Preload all the module files loaded transitively by the AST unit. Also
@@ -971,13 +971,10 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
971971
// Set up the file system, file and source managers, if needed.
972972
if (!CI.hasVirtualFileSystem())
973973
CI.createVirtualFileSystem();
974-
if (!CI.hasFileManager()) {
975-
if (!CI.createFileManager()) {
976-
return false;
977-
}
978-
}
974+
if (!CI.hasFileManager())
975+
CI.createFileManager();
979976
if (!CI.hasSourceManager()) {
980-
CI.createSourceManager(CI.getFileManager());
977+
CI.createSourceManager();
981978
if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
982979
static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
983980
->setSarifWriter(

clang/lib/Testing/TestAST.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void createMissingComponents(CompilerInstance &Clang) {
6161
if (!Clang.hasFileManager())
6262
Clang.createFileManager();
6363
if (!Clang.hasSourceManager())
64-
Clang.createSourceManager(Clang.getFileManager());
64+
Clang.createSourceManager();
6565
if (!Clang.hasTarget())
6666
Clang.createTarget();
6767
if (!Clang.hasPreprocessor())

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,24 +415,25 @@ bool DependencyScanningAction::runInvocation(
415415
any(Service.getOptimizeArgs() & ScanningOptimizations::VFS);
416416

417417
// Create a new FileManager to match the invocation's FileSystemOptions.
418-
auto *FileMgr = ScanInstance.createFileManager();
418+
ScanInstance.createFileManager();
419419

420420
// Use the dependency scanning optimized file system if requested to do so.
421421
if (DepFS) {
422422
DepFS->resetBypassedPathPrefix();
423423
if (!ScanInstance.getHeaderSearchOpts().ModuleCachePath.empty()) {
424424
SmallString<256> ModulesCachePath;
425425
normalizeModuleCachePath(
426-
*FileMgr, ScanInstance.getHeaderSearchOpts().ModuleCachePath,
427-
ModulesCachePath);
426+
ScanInstance.getFileManager(),
427+
ScanInstance.getHeaderSearchOpts().ModuleCachePath, ModulesCachePath);
428428
DepFS->setBypassedPathPrefix(ModulesCachePath);
429429
}
430430

431431
ScanInstance.setDependencyDirectivesGetter(
432-
std::make_unique<ScanningDependencyDirectivesGetter>(*FileMgr));
432+
std::make_unique<ScanningDependencyDirectivesGetter>(
433+
ScanInstance.getFileManager()));
433434
}
434435

435-
ScanInstance.createSourceManager(*FileMgr);
436+
ScanInstance.createSourceManager();
436437

437438
// Create a collection of stable directories derived from the ScanInstance
438439
// for determining whether module dependencies would fully resolve from

clang/lib/Tooling/Tooling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ bool FrontendActionFactory::runInvocation(
458458
if (!Compiler.hasDiagnostics())
459459
return false;
460460

461-
Compiler.createSourceManager(*Files);
461+
Compiler.createSourceManager();
462462

463463
const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
464464

0 commit comments

Comments
 (0)