Skip to content

Commit 58a90cf

Browse files
committed
NFC: Clean up IntrusiveRefCntPtr construction from raw pointers for clang::FileManager
1 parent 0a55599 commit 58a90cf

File tree

19 files changed

+79
-64
lines changed

19 files changed

+79
-64
lines changed

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ class ASTUnit {
501501

502502
const FileManager &getFileManager() const { return *FileMgr; }
503503
FileManager &getFileManager() { return *FileMgr; }
504+
IntrusiveRefCntPtr<FileManager> getFileManagerPtr() { return FileMgr; }
504505

505506
const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
506507

@@ -809,8 +810,8 @@ class ASTUnit {
809810
std::shared_ptr<CompilerInvocation> CI,
810811
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
811812
std::shared_ptr<DiagnosticOptions> DiagOpts,
812-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
813-
bool OnlyLocalDecls = false,
813+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
814+
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls = false,
814815
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
815816
unsigned PrecompilePreambleAfterNParses = 0,
816817
TranslationUnitKind TUKind = TU_Complete,
@@ -930,7 +931,7 @@ class ASTUnit {
930931
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag,
931932
LangOptions &LangOpts,
932933
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
933-
FileManager &FileMgr,
934+
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
934935
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
935936
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
936937
std::unique_ptr<SyntaxOnlyAction> Act);

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class CompilerInstance : public ModuleLoader {
446446
}
447447

448448
/// Replace the current file manager and virtual file system.
449-
void setFileManager(FileManager *Value);
449+
void setFileManager(IntrusiveRefCntPtr<FileManager> Value);
450450

451451
/// @}
452452
/// @name Source Manager

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
831831
AST->CaptureDiagnostics = CaptureDiagnostics;
832832
AST->DiagOpts = DiagOpts;
833833
AST->Diagnostics = Diags;
834-
AST->FileMgr = new FileManager(FileSystemOpts, VFS);
834+
AST->FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOpts, VFS);
835835
AST->UserFilesAreVolatile = UserFilesAreVolatile;
836836
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
837837
AST->getDiagnostics(), AST->getFileManager(), UserFilesAreVolatile);
@@ -1190,9 +1190,11 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
11901190
// changed above in AddImplicitPreamble. If VFS is nullptr, rely on
11911191
// createFileManager to create one.
11921192
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1193-
Clang->setFileManager(&*FileMgr);
1194-
else
1195-
FileMgr = Clang->createFileManager(std::move(VFS));
1193+
Clang->setFileManager(FileMgr);
1194+
else {
1195+
Clang->createFileManager(std::move(VFS));
1196+
FileMgr = Clang->getFileManagerPtr();
1197+
}
11961198

11971199
// Recover resources if we crash before exiting this method.
11981200
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
@@ -1554,7 +1556,8 @@ ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
15541556
AST->Diagnostics = Diags;
15551557
AST->FileSystemOpts = CI->getFileSystemOpts();
15561558
AST->Invocation = std::move(CI);
1557-
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1559+
AST->FileMgr =
1560+
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
15581561
AST->UserFilesAreVolatile = UserFilesAreVolatile;
15591562
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
15601563
AST->getDiagnostics(), *AST->FileMgr, UserFilesAreVolatile);
@@ -1645,7 +1648,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
16451648
AST->Reader = nullptr;
16461649

16471650
// Create a file manager object to provide access to and cache the filesystem.
1648-
Clang->setFileManager(&AST->getFileManager());
1651+
Clang->setFileManager(AST->getFileManagerPtr());
16491652

16501653
// Create the source manager.
16511654
Clang->setSourceManager(AST->getSourceManagerPtr());
@@ -1742,8 +1745,9 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
17421745
std::shared_ptr<CompilerInvocation> CI,
17431746
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
17441747
std::shared_ptr<DiagnosticOptions> DiagOpts,
1745-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1746-
bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1748+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1749+
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls,
1750+
CaptureDiagsKind CaptureDiagnostics,
17471751
unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
17481752
bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
17491753
bool UserFilesAreVolatile) {
@@ -1848,7 +1852,8 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine(
18481852
AST->FileSystemOpts = CI->getFileSystemOpts();
18491853
AST->CodeGenOpts = std::make_unique<CodeGenOptions>(CI->getCodeGenOpts());
18501854
VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1851-
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1855+
AST->FileMgr =
1856+
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
18521857
AST->StorePreamblesInMemory = StorePreamblesInMemory;
18531858
AST->PreambleStoragePath = PreambleStoragePath;
18541859
AST->ModCache = createCrossProcessModuleCache();
@@ -2209,7 +2214,8 @@ void ASTUnit::CodeComplete(
22092214
CodeCompleteConsumer &Consumer,
22102215
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
22112216
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag, LangOptions &LangOpts,
2212-
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr, FileManager &FileMgr,
2217+
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
2218+
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
22132219
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
22142220
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
22152221
std::unique_ptr<SyntaxOnlyAction> Act) {
@@ -2264,7 +2270,7 @@ void ASTUnit::CodeComplete(
22642270
Clang->getDiagnostics(),
22652271
&StoredDiagnostics, nullptr);
22662272
ProcessWarningOptions(*Diag, Inv.getDiagnosticOpts(),
2267-
FileMgr.getVirtualFileSystem());
2273+
FileMgr->getVirtualFileSystem());
22682274

22692275
// Create the target instance.
22702276
if (!Clang->createTarget()) {
@@ -2281,7 +2287,7 @@ void ASTUnit::CodeComplete(
22812287
"IR inputs not support here!");
22822288

22832289
// Use the source and file managers that we were given.
2284-
Clang->setFileManager(&FileMgr);
2290+
Clang->setFileManager(FileMgr);
22852291
Clang->setSourceManager(SourceMgr);
22862292

22872293
// Remap files.
@@ -2300,7 +2306,7 @@ void ASTUnit::CodeComplete(
23002306

23012307
auto getUniqueID =
23022308
[&FileMgr](StringRef Filename) -> std::optional<llvm::sys::fs::UniqueID> {
2303-
if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
2309+
if (auto Status = FileMgr->getVirtualFileSystem().status(Filename))
23042310
return Status->getUniqueID();
23052311
return std::nullopt;
23062312
};
@@ -2321,7 +2327,7 @@ void ASTUnit::CodeComplete(
23212327
std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
23222328
if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
23232329
OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2324-
PCHContainerOps, Inv, FileMgr.getVirtualFileSystemPtr(), false,
2330+
PCHContainerOps, Inv, FileMgr->getVirtualFileSystemPtr(), false,
23252331
Line - 1);
23262332
}
23272333

@@ -2332,7 +2338,7 @@ void ASTUnit::CodeComplete(
23322338
"No preamble was built, but OverrideMainBuffer is not null");
23332339

23342340
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
2335-
FileMgr.getVirtualFileSystemPtr();
2341+
FileMgr->getVirtualFileSystemPtr();
23362342
Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
23372343
OverrideMainBuffer.get());
23382344
// FIXME: there is no way to update VFS if it was changed by

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ CompilerInstance::getVirtualFileSystemPtr() const {
166166
return getFileManager().getVirtualFileSystemPtr();
167167
}
168168

169-
void CompilerInstance::setFileManager(FileManager *Value) {
170-
FileMgr = Value;
169+
void CompilerInstance::setFileManager(
170+
llvm::IntrusiveRefCntPtr<FileManager> Value) {
171+
FileMgr = std::move(Value);
171172
}
172173

173174
void CompilerInstance::setSourceManager(
@@ -389,7 +390,8 @@ FileManager *CompilerInstance::createFileManager(
389390
if (getFrontendOpts().ShowStats)
390391
VFS =
391392
llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
392-
FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS));
393+
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(),
394+
std::move(VFS));
393395
return FileMgr.get();
394396
}
395397

@@ -1224,7 +1226,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
12241226
if (ThreadSafeConfig) {
12251227
Instance.createFileManager(ThreadSafeConfig->getVFS());
12261228
} else if (FrontendOpts.ModulesShareFileManager) {
1227-
Instance.setFileManager(&getFileManager());
1229+
Instance.setFileManager(getFileManagerPtr());
12281230
} else {
12291231
Instance.createFileManager(getVirtualFileSystemPtr());
12301232
}

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
845845

846846
// Set the shared objects, these are reset when we finish processing the
847847
// file, otherwise the CompilerInstance will happily destroy them.
848-
CI.setFileManager(&AST->getFileManager());
848+
CI.setFileManager(AST->getFileManagerPtr());
849849
CI.createSourceManager(CI.getFileManager());
850850
CI.getSourceManager().initializeForReplay(AST->getSourceManager());
851851

@@ -912,7 +912,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
912912

913913
// Set the shared objects, these are reset when we finish processing the
914914
// file, otherwise the CompilerInstance will happily destroy them.
915-
CI.setFileManager(&AST->getFileManager());
915+
CI.setFileManager(AST->getFileManagerPtr());
916916
CI.setSourceManager(AST->getSourceManagerPtr());
917917
CI.setPreprocessor(AST->getPreprocessorPtr());
918918
Preprocessor &PP = CI.getPreprocessor();

clang/lib/Frontend/PrecompiledPreamble.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
483483
VFS);
484484

485485
// Create a file manager object to provide access to and cache the filesystem.
486-
Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
486+
Clang->setFileManager(
487+
llvm::makeIntrusiveRefCnt<FileManager>(Clang->getFileSystemOpts(), VFS));
487488

488489
// Create the source manager.
489490
Clang->setSourceManager(llvm::makeIntrusiveRefCnt<SourceManager>(

clang/lib/Interpreter/CodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ void ReplCodeCompleter::codeComplete(CompilerInstance *InterpCI,
380380
false, consumer,
381381
std::make_shared<clang::PCHContainerOperations>(), diag,
382382
InterpCI->getLangOpts(), AU->getSourceManagerPtr(),
383-
AU->getFileManager(), sd, tb, std::move(Act));
383+
AU->getFileManagerPtr(), sd, tb, std::move(Act));
384384
}
385385

386386
} // namespace clang

clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void ModelInjector::onBodySynthesis(const NamedDecl *D) {
8484

8585
// The instance wants to take ownership, however DisableFree frontend option
8686
// is set to true to avoid double free issues
87-
Instance.setFileManager(&CI.getFileManager());
87+
Instance.setFileManager(CI.getFileManagerPtr());
8888
Instance.setSourceManager(SM);
8989
Instance.setPreprocessor(CI.getPreprocessorPtr());
9090
Instance.setASTContext(CI.getASTContextPtr());

clang/lib/Tooling/Tooling.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ bool runToolOnCodeWithArgs(
212212
SmallString<16> FileNameStorage;
213213
StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
214214

215-
llvm::IntrusiveRefCntPtr<FileManager> Files(
216-
new FileManager(FileSystemOptions(), VFS));
215+
llvm::IntrusiveRefCntPtr<FileManager> Files =
216+
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions(), VFS);
217217
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
218218
ToolInvocation Invocation(
219219
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
@@ -479,7 +479,8 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations,
479479
InMemoryFileSystem(
480480
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>()),
481481
Files(Files ? Files
482-
: new FileManager(FileSystemOptions(), OverlayFileSystem)) {
482+
: llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions(),
483+
OverlayFileSystem)) {
483484
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
484485
appendArgumentsAdjuster(getClangStripOutputAdjuster());
485486
appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
@@ -701,8 +702,9 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
701702
auto InMemoryFileSystem =
702703
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
703704
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
704-
llvm::IntrusiveRefCntPtr<FileManager> Files(
705-
new FileManager(FileSystemOptions(), OverlayFileSystem));
705+
llvm::IntrusiveRefCntPtr<FileManager> Files =
706+
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions(),
707+
OverlayFileSystem);
706708

707709
ToolInvocation Invocation(
708710
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName),

clang/tools/clang-installapi/ClangInstallAPI.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ static bool run(ArrayRef<const char *> Args, const char *ProgName) {
8989
auto InMemoryFileSystem =
9090
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
9191
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
92-
IntrusiveRefCntPtr<clang::FileManager> FM(
93-
new FileManager(clang::FileSystemOptions(), OverlayFileSystem));
92+
IntrusiveRefCntPtr<clang::FileManager> FM =
93+
llvm::makeIntrusiveRefCnt<FileManager>(clang::FileSystemOptions(),
94+
OverlayFileSystem);
9495

9596
// Capture all options and diagnose any errors.
9697
Options Opts(*Diag, FM.get(), Args, ProgName);
@@ -113,7 +114,7 @@ static bool run(ArrayRef<const char *> Args, const char *ProgName) {
113114

114115
// Set up compilation.
115116
std::unique_ptr<CompilerInstance> CI(new CompilerInstance());
116-
CI->setFileManager(FM.get());
117+
CI->setFileManager(FM);
117118
CI->createDiagnostics(FM->getVirtualFileSystem());
118119
if (!CI->hasDiagnostics())
119120
return EXIT_FAILURE;

0 commit comments

Comments
 (0)