Skip to content

Commit 4205da0

Browse files
authored
NFC: Clean up of IntrusiveRefCntPtr construction from raw pointers. (#151782)
This commit handles the following types: - clang::ExternalASTSource - clang::TargetInfo - clang::ASTContext - clang::SourceManager - clang::FileManager Part of cleanup #151026
1 parent 8934a6e commit 4205da0

Some content is hidden

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

41 files changed

+271
-243
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Action : public clang::ASTFrontendAction {
5353

5454
Compiler->createSema(getTranslationUnitKind(), CompletionConsumer);
5555
SemaSource->setCompilerInstance(Compiler);
56-
Compiler->getSema().addExternalSource(SemaSource.get());
56+
Compiler->getSema().addExternalSource(SemaSource);
5757

5858
clang::ParseAST(Compiler->getSema(), Compiler->getFrontendOpts().ShowStats,
5959
Compiler->getFrontendOpts().SkipFunctionBodies);

clang/include/clang/AST/ASTContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,12 @@ class ASTContext : public RefCountedBase<ASTContext> {
13351335
return ExternalSource.get();
13361336
}
13371337

1338+
/// Retrieve a pointer to the external AST source associated
1339+
/// with this AST context, if any. Returns as an IntrusiveRefCntPtr.
1340+
IntrusiveRefCntPtr<ExternalASTSource> getExternalSourcePtr() const {
1341+
return ExternalSource;
1342+
}
1343+
13381344
/// Attach an AST mutation listener to the AST context.
13391345
///
13401346
/// The AST mutation listener provides the ability to track modifications to

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,21 @@ class ASTUnit {
451451

452452
const SourceManager &getSourceManager() const { return *SourceMgr; }
453453
SourceManager &getSourceManager() { return *SourceMgr; }
454+
llvm::IntrusiveRefCntPtr<SourceManager> getSourceManagerPtr() {
455+
return SourceMgr;
456+
}
454457

455458
const Preprocessor &getPreprocessor() const { return *PP; }
456459
Preprocessor &getPreprocessor() { return *PP; }
457460
std::shared_ptr<Preprocessor> getPreprocessorPtr() const { return PP; }
458461

459462
const ASTContext &getASTContext() const { return *Ctx; }
460463
ASTContext &getASTContext() { return *Ctx; }
464+
llvm::IntrusiveRefCntPtr<ASTContext> getASTContextPtr() { return Ctx; }
461465

462-
void setASTContext(ASTContext *ctx) { Ctx = ctx; }
466+
void setASTContext(llvm::IntrusiveRefCntPtr<ASTContext> ctx) {
467+
Ctx = std::move(ctx);
468+
}
463469
void setPreprocessor(std::shared_ptr<Preprocessor> pp);
464470

465471
/// Enable source-range based diagnostic messages.
@@ -495,6 +501,7 @@ class ASTUnit {
495501

496502
const FileManager &getFileManager() const { return *FileMgr; }
497503
FileManager &getFileManager() { return *FileMgr; }
504+
IntrusiveRefCntPtr<FileManager> getFileManagerPtr() { return FileMgr; }
498505

499506
const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
500507

@@ -803,8 +810,8 @@ class ASTUnit {
803810
std::shared_ptr<CompilerInvocation> CI,
804811
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
805812
std::shared_ptr<DiagnosticOptions> DiagOpts,
806-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
807-
bool OnlyLocalDecls = false,
813+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
814+
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls = false,
808815
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
809816
unsigned PrecompilePreambleAfterNParses = 0,
810817
TranslationUnitKind TUKind = TU_Complete,
@@ -922,8 +929,9 @@ class ASTUnit {
922929
CodeCompleteConsumer &Consumer,
923930
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
924931
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag,
925-
LangOptions &LangOpts, SourceManager &SourceMgr,
926-
FileManager &FileMgr,
932+
LangOptions &LangOpts,
933+
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
934+
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
927935
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
928936
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
929937
std::unique_ptr<SyntaxOnlyAction> Act);

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 3 additions & 3 deletions
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
@@ -471,7 +471,7 @@ class CompilerInstance : public ModuleLoader {
471471
}
472472

473473
/// setSourceManager - Replace the current source manager.
474-
void setSourceManager(SourceManager *Value);
474+
void setSourceManager(llvm::IntrusiveRefCntPtr<SourceManager> Value);
475475

476476
/// @}
477477
/// @name Preprocessor
@@ -516,7 +516,7 @@ class CompilerInstance : public ModuleLoader {
516516
}
517517

518518
/// setASTContext - Replace the current AST context.
519-
void setASTContext(ASTContext *Value);
519+
void setASTContext(llvm::IntrusiveRefCntPtr<ASTContext> Value);
520520

521521
/// Replace the current Sema; the compiler instance takes ownership
522522
/// of S.

clang/include/clang/Frontend/Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void AttachHeaderIncludeGen(Preprocessor &PP,
189189
/// memory, mainly for testing.
190190
IntrusiveRefCntPtr<ExternalSemaSource>
191191
createChainedIncludesSource(CompilerInstance &CI,
192-
IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
192+
IntrusiveRefCntPtr<ASTReader> &OutReader);
193193

194194
/// Optional inputs to createInvocation.
195195
struct CreateInvocationOptions {

clang/include/clang/Sema/MultiplexExternalSemaSource.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
4040
static char ID;
4141

4242
private:
43-
SmallVector<ExternalSemaSource *, 2> Sources;
43+
SmallVector<llvm::IntrusiveRefCntPtr<ExternalSemaSource>, 2> Sources;
4444

4545
public:
4646
/// Constructs a new multiplexing external sema source and appends the
@@ -49,15 +49,14 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
4949
///\param[in] S1 - A non-null (old) ExternalSemaSource.
5050
///\param[in] S2 - A non-null (new) ExternalSemaSource.
5151
///
52-
MultiplexExternalSemaSource(ExternalSemaSource *S1, ExternalSemaSource *S2);
53-
54-
~MultiplexExternalSemaSource() override;
52+
MultiplexExternalSemaSource(llvm::IntrusiveRefCntPtr<ExternalSemaSource> S1,
53+
llvm::IntrusiveRefCntPtr<ExternalSemaSource> S2);
5554

5655
/// Appends new source to the source list.
5756
///
5857
///\param[in] Source - An ExternalSemaSource.
5958
///
60-
void AddSource(ExternalSemaSource *Source);
59+
void AddSource(llvm::IntrusiveRefCntPtr<ExternalSemaSource> Source);
6160

6261
//===--------------------------------------------------------------------===//
6362
// ExternalASTSource.

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ class Sema final : public SemaBase {
927927
///
928928
///\param[in] E - A non-null external sema source.
929929
///
930-
void addExternalSource(ExternalSemaSource *E);
930+
void addExternalSource(IntrusiveRefCntPtr<ExternalSemaSource> E);
931931

932932
/// Print out statistics about the semantic analysis.
933933
void PrintStats() const;

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,10 @@ 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;
836-
AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
837-
AST->getFileManager(),
838-
UserFilesAreVolatile);
836+
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
837+
AST->getDiagnostics(), AST->getFileManager(), UserFilesAreVolatile);
839838
AST->ModCache = createCrossProcessModuleCache();
840839
AST->HSOpts = std::make_unique<HeaderSearchOptions>(HSOpts);
841840
AST->HSOpts->ModuleFormat = std::string(PCHContainerRdr.getFormats().front());
@@ -858,20 +857,20 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
858857
Preprocessor &PP = *AST->PP;
859858

860859
if (ToLoad >= LoadASTOnly)
861-
AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
862-
PP.getIdentifierTable(), PP.getSelectorTable(),
863-
PP.getBuiltinInfo(),
864-
AST->getTranslationUnitKind());
860+
AST->Ctx = llvm::makeIntrusiveRefCnt<ASTContext>(
861+
*AST->LangOpts, AST->getSourceManager(), PP.getIdentifierTable(),
862+
PP.getSelectorTable(), PP.getBuiltinInfo(),
863+
AST->getTranslationUnitKind());
865864

866865
DisableValidationForModuleKind disableValid =
867866
DisableValidationForModuleKind::None;
868867
if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
869868
disableValid = DisableValidationForModuleKind::All;
870-
AST->Reader = new ASTReader(PP, *AST->ModCache, AST->Ctx.get(),
871-
PCHContainerRdr, *AST->CodeGenOpts, {},
872-
/*isysroot=*/"",
873-
/*DisableValidationKind=*/disableValid,
874-
AllowASTWithCompilerErrors);
869+
AST->Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
870+
PP, *AST->ModCache, AST->Ctx.get(), PCHContainerRdr, *AST->CodeGenOpts,
871+
ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
872+
/*isysroot=*/"",
873+
/*DisableValidationKind=*/disableValid, AllowASTWithCompilerErrors);
875874

876875
unsigned Counter = 0;
877876
AST->Reader->setListener(std::make_unique<ASTInfoCollector>(
@@ -1191,9 +1190,11 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
11911190
// changed above in AddImplicitPreamble. If VFS is nullptr, rely on
11921191
// createFileManager to create one.
11931192
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1194-
Clang->setFileManager(&*FileMgr);
1195-
else
1196-
FileMgr = Clang->createFileManager(std::move(VFS));
1193+
Clang->setFileManager(FileMgr);
1194+
else {
1195+
Clang->createFileManager(std::move(VFS));
1196+
FileMgr = Clang->getFileManagerPtr();
1197+
}
11971198

11981199
// Recover resources if we crash before exiting this method.
11991200
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
@@ -1226,15 +1227,15 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
12261227

12271228
ResetForParse();
12281229

1229-
SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1230-
UserFilesAreVolatile);
1230+
SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
1231+
getDiagnostics(), *FileMgr, +UserFilesAreVolatile);
12311232
if (!OverrideMainBuffer) {
12321233
checkAndRemoveNonDriverDiags(StoredDiagnostics);
12331234
TopLevelDeclsInPreamble.clear();
12341235
}
12351236

12361237
// Create the source manager.
1237-
Clang->setSourceManager(&getSourceManager());
1238+
Clang->setSourceManager(getSourceManagerPtr());
12381239

12391240
// If the main file has been overridden due to the use of a preamble,
12401241
// make that override happen and introduce the preamble.
@@ -1499,13 +1500,13 @@ void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
14991500
TheSema = CI.takeSema();
15001501
Consumer = CI.takeASTConsumer();
15011502
if (CI.hasASTContext())
1502-
Ctx = &CI.getASTContext();
1503+
Ctx = CI.getASTContextPtr();
15031504
if (CI.hasPreprocessor())
15041505
PP = CI.getPreprocessorPtr();
15051506
CI.setSourceManager(nullptr);
15061507
CI.setFileManager(nullptr);
15071508
if (CI.hasTarget())
1508-
Target = &CI.getTarget();
1509+
Target = CI.getTargetPtr();
15091510
Reader = CI.getASTReader();
15101511
HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
15111512
if (Invocation != CI.getInvocationPtr()) {
@@ -1555,10 +1556,11 @@ ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
15551556
AST->Diagnostics = Diags;
15561557
AST->FileSystemOpts = CI->getFileSystemOpts();
15571558
AST->Invocation = std::move(CI);
1558-
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1559+
AST->FileMgr =
1560+
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
15591561
AST->UserFilesAreVolatile = UserFilesAreVolatile;
1560-
AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1561-
UserFilesAreVolatile);
1562+
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
1563+
AST->getDiagnostics(), *AST->FileMgr, UserFilesAreVolatile);
15621564
AST->ModCache = createCrossProcessModuleCache();
15631565

15641566
return AST;
@@ -1646,10 +1648,10 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
16461648
AST->Reader = nullptr;
16471649

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

16511653
// Create the source manager.
1652-
Clang->setSourceManager(&AST->getSourceManager());
1654+
Clang->setSourceManager(AST->getSourceManagerPtr());
16531655

16541656
FrontendAction *Act = Action;
16551657

@@ -1743,8 +1745,9 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
17431745
std::shared_ptr<CompilerInvocation> CI,
17441746
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
17451747
std::shared_ptr<DiagnosticOptions> DiagOpts,
1746-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1747-
bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1748+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1749+
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls,
1750+
CaptureDiagsKind CaptureDiagnostics,
17481751
unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
17491752
bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
17501753
bool UserFilesAreVolatile) {
@@ -1849,7 +1852,8 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine(
18491852
AST->FileSystemOpts = CI->getFileSystemOpts();
18501853
AST->CodeGenOpts = std::make_unique<CodeGenOptions>(CI->getCodeGenOpts());
18511854
VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1852-
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1855+
AST->FileMgr =
1856+
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
18531857
AST->StorePreamblesInMemory = StorePreamblesInMemory;
18541858
AST->PreambleStoragePath = PreambleStoragePath;
18551859
AST->ModCache = createCrossProcessModuleCache();
@@ -2210,7 +2214,8 @@ void ASTUnit::CodeComplete(
22102214
CodeCompleteConsumer &Consumer,
22112215
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
22122216
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag, LangOptions &LangOpts,
2213-
SourceManager &SourceMgr, FileManager &FileMgr,
2217+
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
2218+
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
22142219
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
22152220
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
22162221
std::unique_ptr<SyntaxOnlyAction> Act) {
@@ -2265,7 +2270,7 @@ void ASTUnit::CodeComplete(
22652270
Clang->getDiagnostics(),
22662271
&StoredDiagnostics, nullptr);
22672272
ProcessWarningOptions(*Diag, Inv.getDiagnosticOpts(),
2268-
FileMgr.getVirtualFileSystem());
2273+
FileMgr->getVirtualFileSystem());
22692274

22702275
// Create the target instance.
22712276
if (!Clang->createTarget()) {
@@ -2282,8 +2287,8 @@ void ASTUnit::CodeComplete(
22822287
"IR inputs not support here!");
22832288

22842289
// Use the source and file managers that we were given.
2285-
Clang->setFileManager(&FileMgr);
2286-
Clang->setSourceManager(&SourceMgr);
2290+
Clang->setFileManager(FileMgr);
2291+
Clang->setSourceManager(SourceMgr);
22872292

22882293
// Remap files.
22892294
PreprocessorOpts.clearRemappedFiles();
@@ -2301,7 +2306,7 @@ void ASTUnit::CodeComplete(
23012306

23022307
auto getUniqueID =
23032308
[&FileMgr](StringRef Filename) -> std::optional<llvm::sys::fs::UniqueID> {
2304-
if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
2309+
if (auto Status = FileMgr->getVirtualFileSystem().status(Filename))
23052310
return Status->getUniqueID();
23062311
return std::nullopt;
23072312
};
@@ -2322,7 +2327,7 @@ void ASTUnit::CodeComplete(
23222327
std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
23232328
if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
23242329
OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2325-
PCHContainerOps, Inv, FileMgr.getVirtualFileSystemPtr(), false,
2330+
PCHContainerOps, Inv, FileMgr->getVirtualFileSystemPtr(), false,
23262331
Line - 1);
23272332
}
23282333

@@ -2333,7 +2338,7 @@ void ASTUnit::CodeComplete(
23332338
"No preamble was built, but OverrideMainBuffer is not null");
23342339

23352340
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
2336-
FileMgr.getVirtualFileSystemPtr();
2341+
FileMgr->getVirtualFileSystemPtr();
23372342
Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
23382343
OverrideMainBuffer.get());
23392344
// FIXME: there is no way to update VFS if it was changed by

0 commit comments

Comments
 (0)