Skip to content

Commit 98baff9

Browse files
jansvoboda11qiongsiwu
authored andcommitted
[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. (cherry picked from commit b86ddae) Conflicts: clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp
1 parent 6398a13 commit 98baff9

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
@@ -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
@@ -128,7 +128,7 @@ clang::createChainedIncludesSource(CompilerInstance &CI,
128128
Clang->setTarget(TargetInfo::CreateTargetInfo(
129129
Clang->getDiagnostics(), Clang->getInvocation().getTargetOpts()));
130130
Clang->createFileManager();
131-
Clang->createSourceManager(Clang->getFileManager());
131+
Clang->createSourceManager();
132132
Clang->createPreprocessor(TU_Prefix);
133133
Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
134134
&Clang->getPreprocessor());

clang/lib/Frontend/CompilerInstance.cpp

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

401401
// File Manager
402402

403-
FileManager *CompilerInstance::createFileManager() {
403+
void CompilerInstance::createFileManager() {
404404
assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
405405
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
406-
return FileMgr.get();
407406
}
408407

409408
// Source Manager
410409

411-
void CompilerInstance::createSourceManager(FileManager &FileMgr) {
412-
SourceMgr =
413-
llvm::makeIntrusiveRefCnt<SourceManager>(getDiagnostics(), FileMgr);
410+
void CompilerInstance::createSourceManager() {
411+
assert(Diagnostics && "DiagnosticsEngine needed for creating SourceManager");
412+
assert(FileMgr && "FileManager needed for creating SourceManager");
413+
SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(getDiagnostics(),
414+
getFileManager());
414415
}
415416

416417
// Initialize the remapping of files to alternative contents, e.g.,
@@ -1421,7 +1422,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
14211422
if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
14221423
Instance.getDiagnostics().setSuppressSystemWarnings(false);
14231424

1424-
Instance.createSourceManager(Instance.getFileManager());
1425+
Instance.createSourceManager();
14251426
SourceManager &SourceMgr = Instance.getSourceManager();
14261427

14271428
if (ThreadSafeConfig) {

clang/lib/Frontend/FrontendAction.cpp

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

924924
// Preload all the module files loaded transitively by the AST unit. Also
@@ -1010,13 +1010,10 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
10101010
// Set up the file system, file and source managers, if needed.
10111011
if (!CI.hasVirtualFileSystem())
10121012
CI.createVirtualFileSystem();
1013-
if (!CI.hasFileManager()) {
1014-
if (!CI.createFileManager()) {
1015-
return false;
1016-
}
1017-
}
1013+
if (!CI.hasFileManager())
1014+
CI.createFileManager();
10181015
if (!CI.hasSourceManager()) {
1019-
CI.createSourceManager(CI.getFileManager());
1016+
CI.createSourceManager();
10201017
if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
10211018
static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
10221019
->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
@@ -560,29 +560,30 @@ bool DependencyScanningAction::runInvocation(
560560
any(Service.getOptimizeArgs() & ScanningOptimizations::VFS);
561561

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

565565
// Use the dependency scanning optimized file system if requested to do so.
566566
if (DepFS) {
567567
DepFS->resetBypassedPathPrefix();
568568
if (!ScanInstance.getHeaderSearchOpts().ModuleCachePath.empty()) {
569569
SmallString<256> ModulesCachePath;
570570
normalizeModuleCachePath(
571-
*FileMgr, ScanInstance.getHeaderSearchOpts().ModuleCachePath,
572-
ModulesCachePath);
571+
ScanInstance.getFileManager(),
572+
ScanInstance.getHeaderSearchOpts().ModuleCachePath, ModulesCachePath);
573573
DepFS->setBypassedPathPrefix(ModulesCachePath);
574574
}
575575

576576
ScanInstance.setDependencyDirectivesGetter(
577-
std::make_unique<ScanningDependencyDirectivesGetter>(*FileMgr));
577+
std::make_unique<ScanningDependencyDirectivesGetter>(
578+
ScanInstance.getFileManager()));
578579
}
579580

580581
// CAS Implementation.
581582
if (DepCASFS)
582583
ScanInstance.setDependencyDirectivesGetter(
583584
std::make_unique<CASDependencyDirectivesGetter>(DepCASFS.get()));
584585

585-
ScanInstance.createSourceManager(*FileMgr);
586+
ScanInstance.createSourceManager();
586587

587588
// Create a collection of stable directories derived from the ScanInstance
588589
// 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)