Skip to content

Commit c85e43b

Browse files
authored
[clang] Hide the LangOptions pointer from CompilerInvocation (#137675)
This PR makes `CompilerInvocation` the sole owner of the `LangOptions` instance.
1 parent 0f90a7b commit c85e43b

File tree

5 files changed

+19
-28
lines changed

5 files changed

+19
-28
lines changed

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class ASTUnit {
106106
};
107107

108108
private:
109-
std::shared_ptr<LangOptions> LangOpts;
109+
std::unique_ptr<LangOptions> LangOpts;
110110
IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
111111
IntrusiveRefCntPtr<FileManager> FileMgr;
112112
IntrusiveRefCntPtr<SourceManager> SourceMgr;
@@ -698,19 +698,17 @@ class ASTUnit {
698698
/// lifetime is expected to extend past that of the returned ASTUnit.
699699
///
700700
/// \returns - The initialized ASTUnit or null if the AST failed to load.
701-
static std::unique_ptr<ASTUnit>
702-
LoadFromASTFile(StringRef Filename, const PCHContainerReader &PCHContainerRdr,
703-
WhatToLoad ToLoad,
704-
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
705-
const FileSystemOptions &FileSystemOpts,
706-
const HeaderSearchOptions &HSOpts,
707-
std::shared_ptr<LangOptions> LangOpts = nullptr,
708-
bool OnlyLocalDecls = false,
709-
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
710-
bool AllowASTWithCompilerErrors = false,
711-
bool UserFilesAreVolatile = false,
712-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
713-
llvm::vfs::getRealFileSystem());
701+
static std::unique_ptr<ASTUnit> LoadFromASTFile(
702+
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
703+
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
704+
const FileSystemOptions &FileSystemOpts,
705+
const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts = nullptr,
706+
bool OnlyLocalDecls = false,
707+
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
708+
bool AllowASTWithCompilerErrors = false,
709+
bool UserFilesAreVolatile = false,
710+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
711+
llvm::vfs::getRealFileSystem());
714712

715713
private:
716714
/// Helper function for \c LoadFromCompilerInvocation() and

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,6 @@ class CompilerInstance : public ModuleLoader {
327327

328328
LangOptions &getLangOpts() { return Invocation->getLangOpts(); }
329329
const LangOptions &getLangOpts() const { return Invocation->getLangOpts(); }
330-
std::shared_ptr<LangOptions> getLangOptsPtr() const {
331-
return Invocation->getLangOptsPtr();
332-
}
333330

334331
PreprocessorOptions &getPreprocessorOpts() {
335332
return Invocation->getPreprocessorOpts();

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,6 @@ class CompilerInvocation : public CompilerInvocationBase {
265265
}
266266
/// @}
267267

268-
/// Base class internals.
269-
/// @{
270-
using CompilerInvocationBase::LangOpts;
271-
std::shared_ptr<LangOptions> getLangOptsPtr() { return LangOpts; }
272-
/// @}
273-
274268
/// Create a compiler invocation from a list of input options.
275269
/// \returns true on success.
276270
///

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
805805
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
806806
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
807807
const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts,
808-
std::shared_ptr<LangOptions> LangOpts, bool OnlyLocalDecls,
808+
const LangOptions *LangOpts, bool OnlyLocalDecls,
809809
CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
810810
bool UserFilesAreVolatile, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
811811
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
@@ -819,7 +819,8 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
819819

820820
ConfigureDiags(Diags, *AST, CaptureDiagnostics);
821821

822-
AST->LangOpts = LangOpts ? LangOpts : std::make_shared<LangOptions>();
822+
AST->LangOpts = LangOpts ? std::make_unique<LangOptions>(*LangOpts)
823+
: std::make_unique<LangOptions>();
823824
AST->OnlyLocalDecls = OnlyLocalDecls;
824825
AST->CaptureDiagnostics = CaptureDiagnostics;
825826
AST->Diagnostics = Diags;
@@ -1211,7 +1212,8 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
12111212
"IR inputs not support here!");
12121213

12131214
// Configure the various subsystems.
1214-
LangOpts = Clang->getInvocation().LangOpts;
1215+
LangOpts =
1216+
std::make_unique<LangOptions>(Clang->getInvocation().getLangOpts());
12151217
FileSystemOpts = Clang->getFileSystemOpts();
12161218

12171219
ResetForParse();
@@ -1486,7 +1488,7 @@ void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
14861488
// Steal the created target, context, and preprocessor if they have been
14871489
// created.
14881490
assert(CI.hasInvocation() && "missing invocation");
1489-
LangOpts = CI.getInvocation().LangOpts;
1491+
LangOpts = std::make_unique<LangOptions>(CI.getInvocation().getLangOpts());
14901492
TheSema = CI.takeSema();
14911493
Consumer = CI.takeASTConsumer();
14921494
if (CI.hasASTContext())

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
847847

848848
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
849849
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
850-
CI.getFileSystemOpts(), CI.getHeaderSearchOpts(), CI.getLangOptsPtr());
850+
CI.getFileSystemOpts(), CI.getHeaderSearchOpts(), &CI.getLangOpts());
851851

852852
if (!AST)
853853
return false;

0 commit comments

Comments
 (0)