Skip to content

Commit 93cddf0

Browse files
committed
[clang] NFCI: Clean up CompilerInstance::create{File,Source}Manager() (llvm#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 b76ea70 commit 93cddf0

File tree

15 files changed

+34
-35
lines changed

15 files changed

+34
-35
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
@@ -628,11 +628,12 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
628628
Clang->createVirtualFileSystem(VFS);
629629
Clang->createDiagnostics();
630630

631-
auto *FM = Clang->createFileManager();
631+
Clang->createFileManager();
632+
FileManager &FM = Clang->getFileManager();
632633
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
633634
EXPECT_THAT(
634-
PI.getExporters(llvm::cantFail(FM->getFileRef("foo.h")), *FM),
635-
testing::ElementsAre(llvm::cantFail(FM->getFileRef("exporter.h"))));
635+
PI.getExporters(llvm::cantFail(FM.getFileRef("foo.h")), FM),
636+
testing::ElementsAre(llvm::cantFail(FM.getFileRef("exporter.h"))));
636637
}
637638

638639
TEST_F(PragmaIncludeTest, OutlivesFMAndSM) {

clang/include/clang/Frontend/CompilerInstance.h

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

757757
/// Create the file manager and replace any existing one with it.
758-
///
759-
/// \return The new file manager on success, or null on failure.
760-
FileManager *createFileManager();
758+
void createFileManager();
761759

762760
/// Create the source manager and replace any existing one with it.
763-
void createSourceManager(FileManager &FileMgr);
761+
void createSourceManager();
764762

765763
/// Create the preprocessor, using the invocation, file, and source managers,
766764
/// 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 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,18 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
398398

399399
// File Manager
400400

401-
FileManager *CompilerInstance::createFileManager() {
401+
void CompilerInstance::createFileManager() {
402402
assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
403403
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
404-
return FileMgr.get();
405404
}
406405

407406
// Source Manager
408407

409-
void CompilerInstance::createSourceManager(FileManager &FileMgr) {
410-
SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
408+
void CompilerInstance::createSourceManager() {
409+
assert(Diagnostics && "DiagnosticsEngine needed for creating SourceManager");
410+
assert(FileMgr && "FileManager needed for creating SourceManager");
411+
SourceMgr = new SourceManager(getDiagnostics(),
412+
getFileManager());
411413
}
412414

413415
// Initialize the remapping of files to alternative contents, e.g.,
@@ -1418,7 +1420,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
14181420
if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
14191421
Instance.getDiagnostics().setSuppressSystemWarnings(false);
14201422

1421-
Instance.createSourceManager(Instance.getFileManager());
1423+
Instance.createSourceManager();
14221424
SourceManager &SourceMgr = Instance.getSourceManager();
14231425

14241426
if (ThreadSafeConfig) {

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
919919
// file, otherwise the CompilerInstance will happily destroy them.
920920
CI.setVirtualFileSystem(AST->getFileManager().getVirtualFileSystemPtr());
921921
CI.setFileManager(&AST->getFileManager());
922-
CI.createSourceManager(CI.getFileManager());
922+
CI.createSourceManager();
923923
CI.getSourceManager().initializeForReplay(AST->getSourceManager());
924924

925925
// Preload all the module files loaded transitively by the AST unit. Also
@@ -1011,13 +1011,10 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
10111011
// Set up the file system, file and source managers, if needed.
10121012
if (!CI.hasVirtualFileSystem())
10131013
CI.createVirtualFileSystem();
1014-
if (!CI.hasFileManager()) {
1015-
if (!CI.createFileManager()) {
1016-
return false;
1017-
}
1018-
}
1014+
if (!CI.hasFileManager())
1015+
CI.createFileManager();
10191016
if (!CI.hasSourceManager()) {
1020-
CI.createSourceManager(CI.getFileManager());
1017+
CI.createSourceManager();
10211018
if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
10221019
static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
10231020
->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/DependencyScanningWorker.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,29 +598,31 @@ class DependencyScanningAction {
598598
any(Service.getOptimizeArgs() & ScanningOptimizations::VFS);
599599

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

603603
// Use the dependency scanning optimized file system if requested to do so.
604604
if (DepFS) {
605605
DepFS->resetBypassedPathPrefix();
606606
if (!ScanInstance.getHeaderSearchOpts().ModuleCachePath.empty()) {
607607
SmallString<256> ModulesCachePath;
608608
normalizeModuleCachePath(
609-
*FileMgr, ScanInstance.getHeaderSearchOpts().ModuleCachePath,
609+
ScanInstance.getFileManager(),
610+
ScanInstance.getHeaderSearchOpts().ModuleCachePath,
610611
ModulesCachePath);
611612
DepFS->setBypassedPathPrefix(ModulesCachePath);
612613
}
613614

614615
ScanInstance.setDependencyDirectivesGetter(
615-
std::make_unique<ScanningDependencyDirectivesGetter>(*FileMgr));
616+
std::make_unique<ScanningDependencyDirectivesGetter>(
617+
ScanInstance.getFileManager()));
616618
}
617619

618620
// CAS Implementation.
619621
if (DepCASFS)
620622
ScanInstance.setDependencyDirectivesGetter(
621623
std::make_unique<CASDependencyDirectivesGetter>(DepCASFS.get()));
622624

623-
ScanInstance.createSourceManager(*FileMgr);
625+
ScanInstance.createSourceManager();
624626

625627
// Create a collection of stable directories derived from the ScanInstance
626628
// 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
@@ -457,7 +457,7 @@ bool FrontendActionFactory::runInvocation(
457457
if (!Compiler.hasDiagnostics())
458458
return false;
459459

460-
Compiler.createSourceManager(*Files);
460+
Compiler.createSourceManager();
461461

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

0 commit comments

Comments
 (0)