Skip to content

Commit 3c62cab

Browse files
jyknightqiongsiwu
authored andcommitted
NFC: Clean up of IntrusiveRefCntPtr construction from raw pointers. (llvm#151782)
This commit handles the following types: - clang::ExternalASTSource - clang::TargetInfo - clang::ASTContext - clang::SourceManager - clang::FileManager Part of cleanup llvm#151026 (cherry picked from commit 4205da0) Conflicts: clang/lib/Frontend/ASTUnit.cpp clang/lib/Frontend/ChainedIncludesSource.cpp clang/lib/Frontend/CompilerInstance.cpp
1 parent f9d542f commit 3c62cab

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

+270
-240
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
@@ -1385,6 +1385,12 @@ class ASTContext : public RefCountedBase<ASTContext> {
13851385
return ExternalSource.get();
13861386
}
13871387

1388+
/// Retrieve a pointer to the external AST source associated
1389+
/// with this AST context, if any. Returns as an IntrusiveRefCntPtr.
1390+
IntrusiveRefCntPtr<ExternalASTSource> getExternalSourcePtr() const {
1391+
return ExternalSource;
1392+
}
1393+
13881394
/// Attach an AST mutation listener to the AST context.
13891395
///
13901396
/// 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
@@ -449,15 +449,21 @@ class ASTUnit {
449449

450450
const SourceManager &getSourceManager() const { return *SourceMgr; }
451451
SourceManager &getSourceManager() { return *SourceMgr; }
452+
llvm::IntrusiveRefCntPtr<SourceManager> getSourceManagerPtr() {
453+
return SourceMgr;
454+
}
452455

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

457460
const ASTContext &getASTContext() const { return *Ctx; }
458461
ASTContext &getASTContext() { return *Ctx; }
462+
llvm::IntrusiveRefCntPtr<ASTContext> getASTContextPtr() { return Ctx; }
459463

460-
void setASTContext(ASTContext *ctx) { Ctx = ctx; }
464+
void setASTContext(llvm::IntrusiveRefCntPtr<ASTContext> ctx) {
465+
Ctx = std::move(ctx);
466+
}
461467
void setPreprocessor(std::shared_ptr<Preprocessor> pp);
462468

463469
/// Enable source-range based diagnostic messages.
@@ -493,6 +499,7 @@ class ASTUnit {
493499

494500
const FileManager &getFileManager() const { return *FileMgr; }
495501
FileManager &getFileManager() { return *FileMgr; }
502+
IntrusiveRefCntPtr<FileManager> getFileManagerPtr() { return FileMgr; }
496503

497504
const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
498505

@@ -801,8 +808,8 @@ class ASTUnit {
801808
std::shared_ptr<CompilerInvocation> CI,
802809
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
803810
std::shared_ptr<DiagnosticOptions> DiagOpts,
804-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
805-
bool OnlyLocalDecls = false,
811+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
812+
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls = false,
806813
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
807814
unsigned PrecompilePreambleAfterNParses = 0,
808815
TranslationUnitKind TUKind = TU_Complete,
@@ -920,8 +927,9 @@ class ASTUnit {
920927
CodeCompleteConsumer &Consumer,
921928
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
922929
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag,
923-
LangOptions &LangOpts, SourceManager &SourceMgr,
924-
FileManager &FileMgr,
930+
LangOptions &LangOpts,
931+
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
932+
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
925933
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
926934
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
927935
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
@@ -478,7 +478,7 @@ class CompilerInstance : public ModuleLoader {
478478
}
479479

480480
/// Replace the current file manager and virtual file system.
481-
void setFileManager(FileManager *Value);
481+
void setFileManager(IntrusiveRefCntPtr<FileManager> Value);
482482

483483
/// Set the output manager.
484484
void setOutputBackend(IntrusiveRefCntPtr<llvm::vfs::OutputBackend> NewOutputs);
@@ -518,7 +518,7 @@ class CompilerInstance : public ModuleLoader {
518518
}
519519

520520
/// setSourceManager - Replace the current source manager.
521-
void setSourceManager(SourceManager *Value);
521+
void setSourceManager(llvm::IntrusiveRefCntPtr<SourceManager> Value);
522522

523523
/// @}
524524
/// @name Preprocessor
@@ -563,7 +563,7 @@ class CompilerInstance : public ModuleLoader {
563563
}
564564

565565
/// setASTContext - Replace the current AST context.
566-
void setASTContext(ASTContext *Value);
566+
void setASTContext(llvm::IntrusiveRefCntPtr<ASTContext> Value);
567567

568568
/// Replace the current Sema; the compiler instance takes ownership
569569
/// of S.

clang/include/clang/Frontend/Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void AttachHeaderIncludeGen(Preprocessor &PP,
198198
/// memory, mainly for testing.
199199
IntrusiveRefCntPtr<ExternalSemaSource>
200200
createChainedIncludesSource(CompilerInstance &CI,
201-
IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
201+
IntrusiveRefCntPtr<ASTReader> &OutReader);
202202

203203
/// Optional inputs to createInvocation.
204204
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
@@ -1136,7 +1136,7 @@ class Sema final : public SemaBase {
11361136
///
11371137
///\param[in] E - A non-null external sema source.
11381138
///
1139-
void addExternalSource(ExternalSemaSource *E);
1139+
void addExternalSource(IntrusiveRefCntPtr<ExternalSemaSource> E);
11401140

11411141
/// Print out statistics about the semantic analysis.
11421142
void PrintStats() const;

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,10 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
822822
AST->CaptureDiagnostics = CaptureDiagnostics;
823823
AST->DiagOpts = DiagOpts;
824824
AST->Diagnostics = Diags;
825-
AST->FileMgr = new FileManager(FileSystemOpts, VFS);
825+
AST->FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOpts, VFS);
826826
AST->UserFilesAreVolatile = UserFilesAreVolatile;
827-
AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
828-
AST->getFileManager(),
829-
UserFilesAreVolatile);
827+
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
828+
AST->getDiagnostics(), AST->getFileManager(), UserFilesAreVolatile);
830829
AST->ModCache = createCrossProcessModuleCache();
831830
AST->HSOpts = std::make_unique<HeaderSearchOptions>(HSOpts);
832831
AST->HSOpts->ModuleFormat = std::string(PCHContainerRdr.getFormats().front());
@@ -849,17 +848,19 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
849848
Preprocessor &PP = *AST->PP;
850849

851850
if (ToLoad >= LoadASTOnly)
852-
AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
853-
PP.getIdentifierTable(), PP.getSelectorTable(),
854-
PP.getBuiltinInfo(),
855-
AST->getTranslationUnitKind());
851+
AST->Ctx = llvm::makeIntrusiveRefCnt<ASTContext>(
852+
*AST->LangOpts, AST->getSourceManager(), PP.getIdentifierTable(),
853+
PP.getSelectorTable(), PP.getBuiltinInfo(),
854+
AST->getTranslationUnitKind());
856855

857856
DisableValidationForModuleKind disableValid =
858857
DisableValidationForModuleKind::None;
859858
if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
860859
disableValid = DisableValidationForModuleKind::All;
861-
AST->Reader = new ASTReader(
862-
PP, *AST->ModCache, AST->Ctx.get(), PCHContainerRdr, {}, /*isysroot=*/"",
860+
AST->Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
861+
PP, *AST->ModCache, AST->Ctx.get(), PCHContainerRdr,
862+
ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
863+
/*isysroot=*/"",
863864
/*DisableValidationKind=*/disableValid, AllowASTWithCompilerErrors);
864865

865866
unsigned Counter = 0;
@@ -1180,9 +1181,11 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
11801181
// changed above in AddImplicitPreamble. If VFS is nullptr, rely on
11811182
// createFileManager to create one.
11821183
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1183-
Clang->setFileManager(&*FileMgr);
1184-
else
1185-
FileMgr = Clang->createFileManager(std::move(VFS));
1184+
Clang->setFileManager(FileMgr);
1185+
else {
1186+
Clang->createFileManager(std::move(VFS));
1187+
FileMgr = Clang->getFileManagerPtr();
1188+
}
11861189

11871190
// Recover resources if we crash before exiting this method.
11881191
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
@@ -1215,15 +1218,15 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
12151218

12161219
ResetForParse();
12171220

1218-
SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1219-
UserFilesAreVolatile);
1221+
SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
1222+
getDiagnostics(), *FileMgr, +UserFilesAreVolatile);
12201223
if (!OverrideMainBuffer) {
12211224
checkAndRemoveNonDriverDiags(StoredDiagnostics);
12221225
TopLevelDeclsInPreamble.clear();
12231226
}
12241227

12251228
// Create the source manager.
1226-
Clang->setSourceManager(&getSourceManager());
1229+
Clang->setSourceManager(getSourceManagerPtr());
12271230

12281231
// If the main file has been overridden due to the use of a preamble,
12291232
// make that override happen and introduce the preamble.
@@ -1488,13 +1491,13 @@ void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
14881491
TheSema = CI.takeSema();
14891492
Consumer = CI.takeASTConsumer();
14901493
if (CI.hasASTContext())
1491-
Ctx = &CI.getASTContext();
1494+
Ctx = CI.getASTContextPtr();
14921495
if (CI.hasPreprocessor())
14931496
PP = CI.getPreprocessorPtr();
14941497
CI.setSourceManager(nullptr);
14951498
CI.setFileManager(nullptr);
14961499
if (CI.hasTarget())
1497-
Target = &CI.getTarget();
1500+
Target = CI.getTargetPtr();
14981501
Reader = CI.getASTReader();
14991502
HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
15001503
if (Invocation != CI.getInvocationPtr()) {
@@ -1544,10 +1547,11 @@ ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
15441547
AST->Diagnostics = Diags;
15451548
AST->FileSystemOpts = CI->getFileSystemOpts();
15461549
AST->Invocation = std::move(CI);
1547-
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1550+
AST->FileMgr =
1551+
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
15481552
AST->UserFilesAreVolatile = UserFilesAreVolatile;
1549-
AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1550-
UserFilesAreVolatile);
1553+
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
1554+
AST->getDiagnostics(), *AST->FileMgr, UserFilesAreVolatile);
15511555
AST->ModCache = createCrossProcessModuleCache();
15521556

15531557
return AST;
@@ -1635,10 +1639,10 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
16351639
AST->Reader = nullptr;
16361640

16371641
// Create a file manager object to provide access to and cache the filesystem.
1638-
Clang->setFileManager(&AST->getFileManager());
1642+
Clang->setFileManager(AST->getFileManagerPtr());
16391643

16401644
// Create the source manager.
1641-
Clang->setSourceManager(&AST->getSourceManager());
1645+
Clang->setSourceManager(AST->getSourceManagerPtr());
16421646

16431647
FrontendAction *Act = Action;
16441648

@@ -1732,8 +1736,9 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
17321736
std::shared_ptr<CompilerInvocation> CI,
17331737
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
17341738
std::shared_ptr<DiagnosticOptions> DiagOpts,
1735-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1736-
bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1739+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1740+
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls,
1741+
CaptureDiagsKind CaptureDiagnostics,
17371742
unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
17381743
bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
17391744
bool UserFilesAreVolatile) {
@@ -1840,7 +1845,8 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine(
18401845
AST->Diagnostics = Diags;
18411846
AST->FileSystemOpts = CI->getFileSystemOpts();
18421847
VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1843-
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1848+
AST->FileMgr =
1849+
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
18441850
AST->StorePreamblesInMemory = StorePreamblesInMemory;
18451851
AST->PreambleStoragePath = PreambleStoragePath;
18461852
AST->ModCache = createCrossProcessModuleCache();
@@ -2201,7 +2207,8 @@ void ASTUnit::CodeComplete(
22012207
CodeCompleteConsumer &Consumer,
22022208
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
22032209
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag, LangOptions &LangOpts,
2204-
SourceManager &SourceMgr, FileManager &FileMgr,
2210+
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
2211+
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
22052212
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
22062213
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
22072214
std::unique_ptr<SyntaxOnlyAction> Act) {
@@ -2256,7 +2263,7 @@ void ASTUnit::CodeComplete(
22562263
Clang->getDiagnostics(),
22572264
&StoredDiagnostics, nullptr);
22582265
ProcessWarningOptions(*Diag, Inv.getDiagnosticOpts(),
2259-
FileMgr.getVirtualFileSystem());
2266+
FileMgr->getVirtualFileSystem());
22602267

22612268
// Create the target instance.
22622269
if (!Clang->createTarget()) {
@@ -2273,8 +2280,8 @@ void ASTUnit::CodeComplete(
22732280
"IR inputs not support here!");
22742281

22752282
// Use the source and file managers that we were given.
2276-
Clang->setFileManager(&FileMgr);
2277-
Clang->setSourceManager(&SourceMgr);
2283+
Clang->setFileManager(FileMgr);
2284+
Clang->setSourceManager(SourceMgr);
22782285

22792286
// Remap files.
22802287
PreprocessorOpts.clearRemappedFiles();
@@ -2292,7 +2299,7 @@ void ASTUnit::CodeComplete(
22922299

22932300
auto getUniqueID =
22942301
[&FileMgr](StringRef Filename) -> std::optional<llvm::sys::fs::UniqueID> {
2295-
if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
2302+
if (auto Status = FileMgr->getVirtualFileSystem().status(Filename))
22962303
return Status->getUniqueID();
22972304
return std::nullopt;
22982305
};
@@ -2313,7 +2320,8 @@ void ASTUnit::CodeComplete(
23132320
std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
23142321
if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
23152322
OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2316-
PCHContainerOps, Inv, &FileMgr.getVirtualFileSystem(), false, Line - 1);
2323+
PCHContainerOps, Inv, FileMgr->getVirtualFileSystemPtr(), false,
2324+
Line - 1);
23172325
}
23182326

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

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

0 commit comments

Comments
 (0)