Skip to content

Commit 9ddbb47

Browse files
authored
NFC: Clean up construction of IntrusiveRefCntPtr from raw pointers for llvm::vfs::FileSystem. (#151407)
This switches to `makeIntrusiveRefCnt<FileSystem>` where creating a new object, and to passing/returning by `IntrusiveRefCntPtr<FileSystem>` instead of `FileSystem*` or `FileSystem&`, when dealing with existing objects. Part of cleanup #151026.
1 parent 920d5bb commit 9ddbb47

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+273
-287
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ class CompilerInstance : public ModuleLoader {
420420
/// @{
421421

422422
llvm::vfs::FileSystem &getVirtualFileSystem() const;
423+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
424+
getVirtualFileSystemPtr() const;
423425

424426
/// @}
425427
/// @name File Manager

clang/lib/Basic/SourceManager.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,18 +2366,16 @@ size_t SourceManager::getDataStructureSizes() const {
23662366

23672367
SourceManagerForFile::SourceManagerForFile(StringRef FileName,
23682368
StringRef Content) {
2369-
// This is referenced by `FileMgr` and will be released by `FileMgr` when it
2370-
// is deleted.
2371-
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
2372-
new llvm::vfs::InMemoryFileSystem);
2369+
auto InMemoryFileSystem =
2370+
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
23732371
InMemoryFileSystem->addFile(
23742372
FileName, 0,
23752373
llvm::MemoryBuffer::getMemBuffer(Content, FileName,
23762374
/*RequiresNullTerminator=*/false));
23772375
// This is passed to `SM` as reference, so the pointer has to be referenced
23782376
// in `Environment` so that `FileMgr` can out-live this function scope.
2379-
FileMgr =
2380-
std::make_unique<FileManager>(FileSystemOptions(), InMemoryFileSystem);
2377+
FileMgr = std::make_unique<FileManager>(FileSystemOptions(),
2378+
std::move(InMemoryFileSystem));
23812379
DiagOpts = std::make_unique<DiagnosticOptions>();
23822380
// This is passed to `SM` as reference, so the pointer has to be referenced
23832381
// by `Environment` due to the same reason above.

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
978978
CI.getPreprocessor());
979979

980980
std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
981-
CI, BA, &CI.getVirtualFileSystem(), *VMContext, std::move(LinkModules),
981+
CI, BA, CI.getVirtualFileSystemPtr(), *VMContext, std::move(LinkModules),
982982
InFile, std::move(OS), CoverageInfo));
983983
BEConsumer = Result.get();
984984

@@ -1156,7 +1156,7 @@ void CodeGenAction::ExecuteAction() {
11561156

11571157
// Set clang diagnostic handler. To do this we need to create a fake
11581158
// BackendConsumer.
1159-
BackendConsumer Result(CI, BA, &CI.getVirtualFileSystem(), *VMContext,
1159+
BackendConsumer Result(CI, BA, CI.getVirtualFileSystemPtr(), *VMContext,
11601160
std::move(LinkModules), "", nullptr, nullptr,
11611161
TheModule.get());
11621162

clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class PCHContainerGenerator : public ASTConsumer {
146146
: CI(CI), Diags(CI.getDiagnostics()), MainFileName(MainFileName),
147147
OutputFileName(OutputFileName), Ctx(nullptr),
148148
MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
149-
FS(&CI.getVirtualFileSystem()),
149+
FS(CI.getVirtualFileSystemPtr()),
150150
HeaderSearchOpts(CI.getHeaderSearchOpts()),
151151
PreprocessorOpts(CI.getPreprocessorOpts()),
152152
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
17731773

17741774
if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
17751775
PrecompilePreambleAfterNParses,
1776-
&AST->FileMgr->getVirtualFileSystem()))
1776+
AST->FileMgr->getVirtualFileSystemPtr()))
17771777
return nullptr;
17781778
return AST;
17791779
}
@@ -1895,7 +1895,7 @@ bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
18951895

18961896
if (!VFS) {
18971897
assert(FileMgr && "FileMgr is null on Reparse call");
1898-
VFS = &FileMgr->getVirtualFileSystem();
1898+
VFS = FileMgr->getVirtualFileSystemPtr();
18991899
}
19001900

19011901
clearFileLevelDecls();
@@ -2321,7 +2321,8 @@ void ASTUnit::CodeComplete(
23212321
std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
23222322
if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
23232323
OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2324-
PCHContainerOps, Inv, &FileMgr.getVirtualFileSystem(), false, Line - 1);
2324+
PCHContainerOps, Inv, FileMgr.getVirtualFileSystemPtr(), false,
2325+
Line - 1);
23252326
}
23262327

23272328
// If the main file has been overridden due to the use of a preamble,
@@ -2331,7 +2332,7 @@ void ASTUnit::CodeComplete(
23312332
"No preamble was built, but OverrideMainBuffer is not null");
23322333

23332334
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
2334-
&FileMgr.getVirtualFileSystem();
2335+
FileMgr.getVirtualFileSystemPtr();
23352336
Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
23362337
OverrideMainBuffer.get());
23372338
// FIXME: there is no way to update VFS if it was changed by

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const {
160160
return getFileManager().getVirtualFileSystem();
161161
}
162162

163+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
164+
CompilerInstance::getVirtualFileSystemPtr() const {
165+
return getFileManager().getVirtualFileSystemPtr();
166+
}
167+
163168
void CompilerInstance::setFileManager(FileManager *Value) {
164169
FileMgr = Value;
165170
}
@@ -375,7 +380,7 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
375380
FileManager *CompilerInstance::createFileManager(
376381
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
377382
if (!VFS)
378-
VFS = FileMgr ? &FileMgr->getVirtualFileSystem()
383+
VFS = FileMgr ? FileMgr->getVirtualFileSystemPtr()
379384
: createVFSFromCompilerInvocation(getInvocation(),
380385
getDiagnostics());
381386
assert(VFS && "FileManager has no VFS?");
@@ -1218,7 +1223,7 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
12181223
} else if (FrontendOpts.ModulesShareFileManager) {
12191224
Instance.setFileManager(&getFileManager());
12201225
} else {
1221-
Instance.createFileManager(&getVirtualFileSystem());
1226+
Instance.createFileManager(getVirtualFileSystemPtr());
12221227
}
12231228

12241229
if (ThreadSafeConfig) {

clang/lib/Frontend/PrecompiledPreamble.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ createVFSOverlayForPreamblePCH(StringRef PCHFilename,
5757
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
5858
// We want only the PCH file from the real filesystem to be available,
5959
// so we create an in-memory VFS with just that and overlay it on top.
60-
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> PCHFS(
61-
new llvm::vfs::InMemoryFileSystem());
60+
auto PCHFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
6261
PCHFS->addFile(PCHFilename, 0, std::move(PCHBuffer));
63-
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> Overlay(
64-
new llvm::vfs::OverlayFileSystem(VFS));
62+
auto Overlay = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(VFS);
6563
Overlay->pushOverlay(PCHFS);
6664
return Overlay;
6765
}

clang/lib/Tooling/Core/Replacement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,8 @@ llvm::Expected<std::string> applyAllReplacements(StringRef Code,
581581
if (Replaces.empty())
582582
return Code.str();
583583

584-
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
585-
new llvm::vfs::InMemoryFileSystem);
584+
auto InMemoryFileSystem =
585+
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
586586
FileManager Files(FileSystemOptions(), InMemoryFileSystem);
587587
DiagnosticOptions DiagOpts;
588588
DiagnosticsEngine Diagnostics(

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ DependencyScanningWorker::DependencyScanningWorker(
605605

606606
switch (Service.getMode()) {
607607
case ScanningMode::DependencyDirectivesScan:
608-
DepFS =
609-
new DependencyScanningWorkerFilesystem(Service.getSharedCache(), FS);
608+
DepFS = llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
609+
Service.getSharedCache(), FS);
610610
BaseFS = DepFS;
611611
break;
612612
case ScanningMode::CanonicalPreprocessing:

clang/lib/Tooling/Tooling.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,11 @@ bool runToolOnCodeWithArgs(
227227
const Twine &ToolName,
228228
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
229229
const FileContentMappings &VirtualMappedFiles) {
230-
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
231-
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
232-
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
233-
new llvm::vfs::InMemoryFileSystem);
230+
auto OverlayFileSystem =
231+
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
232+
llvm::vfs::getRealFileSystem());
233+
auto InMemoryFileSystem =
234+
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
234235
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
235236

236237
SmallString<1024> CodeStorage;
@@ -403,7 +404,7 @@ bool ToolInvocation::run() {
403404
}
404405

405406
const std::unique_ptr<driver::Driver> Driver(
406-
newDriver(&*Diagnostics, BinaryName, &Files->getVirtualFileSystem()));
407+
newDriver(&*Diagnostics, BinaryName, Files->getVirtualFileSystemPtr()));
407408
// The "input file not found" diagnostics from the driver are useful.
408409
// The driver is only aware of the VFS working directory, but some clients
409410
// change this at the FileManager level instead.
@@ -473,8 +474,10 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations,
473474
IntrusiveRefCntPtr<FileManager> Files)
474475
: Compilations(Compilations), SourcePaths(SourcePaths),
475476
PCHContainerOps(std::move(PCHContainerOps)),
476-
OverlayFileSystem(new llvm::vfs::OverlayFileSystem(std::move(BaseFS))),
477-
InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
477+
OverlayFileSystem(llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
478+
std::move(BaseFS))),
479+
InMemoryFileSystem(
480+
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>()),
478481
Files(Files ? Files
479482
: new FileManager(FileSystemOptions(), OverlayFileSystem)) {
480483
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
@@ -692,10 +695,11 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
692695
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
693696
std::vector<std::unique_ptr<ASTUnit>> ASTs;
694697
ASTBuilderAction Action(ASTs);
695-
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
696-
new llvm::vfs::OverlayFileSystem(std::move(BaseFS)));
697-
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
698-
new llvm::vfs::InMemoryFileSystem);
698+
auto OverlayFileSystem =
699+
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
700+
std::move(BaseFS));
701+
auto InMemoryFileSystem =
702+
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
699703
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
700704
llvm::IntrusiveRefCntPtr<FileManager> Files(
701705
new FileManager(FileSystemOptions(), OverlayFileSystem));

0 commit comments

Comments
 (0)