Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Action : public clang::ASTFrontendAction {

Compiler->createSema(getTranslationUnitKind(), CompletionConsumer);
SemaSource->setCompilerInstance(Compiler);
Compiler->getSema().addExternalSource(SemaSource.get());
Compiler->getSema().addExternalSource(SemaSource);

clang::ParseAST(Compiler->getSema(), Compiler->getFrontendOpts().ShowStats,
Compiler->getFrontendOpts().SkipFunctionBodies);
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,12 @@ class ASTContext : public RefCountedBase<ASTContext> {
return ExternalSource.get();
}

/// Retrieve a pointer to the external AST source associated
/// with this AST context, if any. Returns as an IntrusiveRefCntPtr.
IntrusiveRefCntPtr<ExternalASTSource> getExternalSourcePtr() const {
return ExternalSource;
}

/// Attach an AST mutation listener to the AST context.
///
/// The AST mutation listener provides the ability to track modifications to
Expand Down
18 changes: 13 additions & 5 deletions clang/include/clang/Frontend/ASTUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,21 @@ class ASTUnit {

const SourceManager &getSourceManager() const { return *SourceMgr; }
SourceManager &getSourceManager() { return *SourceMgr; }
llvm::IntrusiveRefCntPtr<SourceManager> getSourceManagerPtr() {
return SourceMgr;
}

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

const ASTContext &getASTContext() const { return *Ctx; }
ASTContext &getASTContext() { return *Ctx; }
llvm::IntrusiveRefCntPtr<ASTContext> getASTContextPtr() { return Ctx; }

void setASTContext(ASTContext *ctx) { Ctx = ctx; }
void setASTContext(llvm::IntrusiveRefCntPtr<ASTContext> ctx) {
Ctx = std::move(ctx);
}
void setPreprocessor(std::shared_ptr<Preprocessor> pp);

/// Enable source-range based diagnostic messages.
Expand Down Expand Up @@ -495,6 +501,7 @@ class ASTUnit {

const FileManager &getFileManager() const { return *FileMgr; }
FileManager &getFileManager() { return *FileMgr; }
IntrusiveRefCntPtr<FileManager> getFileManagerPtr() { return FileMgr; }

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

Expand Down Expand Up @@ -803,8 +810,8 @@ class ASTUnit {
std::shared_ptr<CompilerInvocation> CI,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
std::shared_ptr<DiagnosticOptions> DiagOpts,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
bool OnlyLocalDecls = false,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls = false,
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
unsigned PrecompilePreambleAfterNParses = 0,
TranslationUnitKind TUKind = TU_Complete,
Expand Down Expand Up @@ -922,8 +929,9 @@ class ASTUnit {
CodeCompleteConsumer &Consumer,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag,
LangOptions &LangOpts, SourceManager &SourceMgr,
FileManager &FileMgr,
LangOptions &LangOpts,
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
std::unique_ptr<SyntaxOnlyAction> Act);
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class CompilerInstance : public ModuleLoader {
}

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

/// @}
/// @name Source Manager
Expand All @@ -471,7 +471,7 @@ class CompilerInstance : public ModuleLoader {
}

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

/// @}
/// @name Preprocessor
Expand Down Expand Up @@ -516,7 +516,7 @@ class CompilerInstance : public ModuleLoader {
}

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

/// Replace the current Sema; the compiler instance takes ownership
/// of S.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Frontend/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void AttachHeaderIncludeGen(Preprocessor &PP,
/// memory, mainly for testing.
IntrusiveRefCntPtr<ExternalSemaSource>
createChainedIncludesSource(CompilerInstance &CI,
IntrusiveRefCntPtr<ExternalSemaSource> &Reader);
IntrusiveRefCntPtr<ASTReader> &OutReader);

/// Optional inputs to createInvocation.
struct CreateInvocationOptions {
Expand Down
9 changes: 4 additions & 5 deletions clang/include/clang/Sema/MultiplexExternalSemaSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
static char ID;

private:
SmallVector<ExternalSemaSource *, 2> Sources;
SmallVector<llvm::IntrusiveRefCntPtr<ExternalSemaSource>, 2> Sources;

public:
/// Constructs a new multiplexing external sema source and appends the
Expand All @@ -49,15 +49,14 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
///\param[in] S1 - A non-null (old) ExternalSemaSource.
///\param[in] S2 - A non-null (new) ExternalSemaSource.
///
MultiplexExternalSemaSource(ExternalSemaSource *S1, ExternalSemaSource *S2);

~MultiplexExternalSemaSource() override;
MultiplexExternalSemaSource(llvm::IntrusiveRefCntPtr<ExternalSemaSource> S1,
llvm::IntrusiveRefCntPtr<ExternalSemaSource> S2);

/// Appends new source to the source list.
///
///\param[in] Source - An ExternalSemaSource.
///
void AddSource(ExternalSemaSource *Source);
void AddSource(llvm::IntrusiveRefCntPtr<ExternalSemaSource> Source);

//===--------------------------------------------------------------------===//
// ExternalASTSource.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ class Sema final : public SemaBase {
///
///\param[in] E - A non-null external sema source.
///
void addExternalSource(ExternalSemaSource *E);
void addExternalSource(IntrusiveRefCntPtr<ExternalSemaSource> E);

/// Print out statistics about the semantic analysis.
void PrintStats() const;
Expand Down
77 changes: 41 additions & 36 deletions clang/lib/Frontend/ASTUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,11 +831,10 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
AST->CaptureDiagnostics = CaptureDiagnostics;
AST->DiagOpts = DiagOpts;
AST->Diagnostics = Diags;
AST->FileMgr = new FileManager(FileSystemOpts, VFS);
AST->FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOpts, VFS);
AST->UserFilesAreVolatile = UserFilesAreVolatile;
AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
AST->getFileManager(),
UserFilesAreVolatile);
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
AST->getDiagnostics(), AST->getFileManager(), UserFilesAreVolatile);
AST->ModCache = createCrossProcessModuleCache();
AST->HSOpts = std::make_unique<HeaderSearchOptions>(HSOpts);
AST->HSOpts->ModuleFormat = std::string(PCHContainerRdr.getFormats().front());
Expand All @@ -858,20 +857,20 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
Preprocessor &PP = *AST->PP;

if (ToLoad >= LoadASTOnly)
AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
PP.getIdentifierTable(), PP.getSelectorTable(),
PP.getBuiltinInfo(),
AST->getTranslationUnitKind());
AST->Ctx = llvm::makeIntrusiveRefCnt<ASTContext>(
*AST->LangOpts, AST->getSourceManager(), PP.getIdentifierTable(),
PP.getSelectorTable(), PP.getBuiltinInfo(),
AST->getTranslationUnitKind());

DisableValidationForModuleKind disableValid =
DisableValidationForModuleKind::None;
if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
disableValid = DisableValidationForModuleKind::All;
AST->Reader = new ASTReader(PP, *AST->ModCache, AST->Ctx.get(),
PCHContainerRdr, *AST->CodeGenOpts, {},
/*isysroot=*/"",
/*DisableValidationKind=*/disableValid,
AllowASTWithCompilerErrors);
AST->Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
PP, *AST->ModCache, AST->Ctx.get(), PCHContainerRdr, *AST->CodeGenOpts,
ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
/*isysroot=*/"",
/*DisableValidationKind=*/disableValid, AllowASTWithCompilerErrors);

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

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

ResetForParse();

SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
UserFilesAreVolatile);
SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
getDiagnostics(), *FileMgr, +UserFilesAreVolatile);
if (!OverrideMainBuffer) {
checkAndRemoveNonDriverDiags(StoredDiagnostics);
TopLevelDeclsInPreamble.clear();
}

// Create the source manager.
Clang->setSourceManager(&getSourceManager());
Clang->setSourceManager(getSourceManagerPtr());

// If the main file has been overridden due to the use of a preamble,
// make that override happen and introduce the preamble.
Expand Down Expand Up @@ -1499,13 +1500,13 @@ void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
TheSema = CI.takeSema();
Consumer = CI.takeASTConsumer();
if (CI.hasASTContext())
Ctx = &CI.getASTContext();
Ctx = CI.getASTContextPtr();
if (CI.hasPreprocessor())
PP = CI.getPreprocessorPtr();
CI.setSourceManager(nullptr);
CI.setFileManager(nullptr);
if (CI.hasTarget())
Target = &CI.getTarget();
Target = CI.getTargetPtr();
Reader = CI.getASTReader();
HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
if (Invocation != CI.getInvocationPtr()) {
Expand Down Expand Up @@ -1555,10 +1556,11 @@ ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
AST->Diagnostics = Diags;
AST->FileSystemOpts = CI->getFileSystemOpts();
AST->Invocation = std::move(CI);
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
AST->FileMgr =
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
AST->UserFilesAreVolatile = UserFilesAreVolatile;
AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
UserFilesAreVolatile);
AST->SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(
AST->getDiagnostics(), *AST->FileMgr, UserFilesAreVolatile);
AST->ModCache = createCrossProcessModuleCache();

return AST;
Expand Down Expand Up @@ -1646,10 +1648,10 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
AST->Reader = nullptr;

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

// Create the source manager.
Clang->setSourceManager(&AST->getSourceManager());
Clang->setSourceManager(AST->getSourceManagerPtr());

FrontendAction *Act = Action;

Expand Down Expand Up @@ -1743,8 +1745,9 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
std::shared_ptr<CompilerInvocation> CI,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
std::shared_ptr<DiagnosticOptions> DiagOpts,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
IntrusiveRefCntPtr<FileManager> FileMgr, bool OnlyLocalDecls,
CaptureDiagsKind CaptureDiagnostics,
unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
bool UserFilesAreVolatile) {
Expand Down Expand Up @@ -1849,7 +1852,8 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine(
AST->FileSystemOpts = CI->getFileSystemOpts();
AST->CodeGenOpts = std::make_unique<CodeGenOptions>(CI->getCodeGenOpts());
VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
AST->FileMgr =
llvm::makeIntrusiveRefCnt<FileManager>(AST->FileSystemOpts, VFS);
AST->StorePreamblesInMemory = StorePreamblesInMemory;
AST->PreambleStoragePath = PreambleStoragePath;
AST->ModCache = createCrossProcessModuleCache();
Expand Down Expand Up @@ -2210,7 +2214,8 @@ void ASTUnit::CodeComplete(
CodeCompleteConsumer &Consumer,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diag, LangOptions &LangOpts,
SourceManager &SourceMgr, FileManager &FileMgr,
llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr,
llvm::IntrusiveRefCntPtr<FileManager> FileMgr,
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
std::unique_ptr<SyntaxOnlyAction> Act) {
Expand Down Expand Up @@ -2265,7 +2270,7 @@ void ASTUnit::CodeComplete(
Clang->getDiagnostics(),
&StoredDiagnostics, nullptr);
ProcessWarningOptions(*Diag, Inv.getDiagnosticOpts(),
FileMgr.getVirtualFileSystem());
FileMgr->getVirtualFileSystem());

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

// Use the source and file managers that we were given.
Clang->setFileManager(&FileMgr);
Clang->setSourceManager(&SourceMgr);
Clang->setFileManager(FileMgr);
Clang->setSourceManager(SourceMgr);

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

auto getUniqueID =
[&FileMgr](StringRef Filename) -> std::optional<llvm::sys::fs::UniqueID> {
if (auto Status = FileMgr.getVirtualFileSystem().status(Filename))
if (auto Status = FileMgr->getVirtualFileSystem().status(Filename))
return Status->getUniqueID();
return std::nullopt;
};
Expand All @@ -2322,7 +2327,7 @@ void ASTUnit::CodeComplete(
std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
PCHContainerOps, Inv, FileMgr.getVirtualFileSystemPtr(), false,
PCHContainerOps, Inv, FileMgr->getVirtualFileSystemPtr(), false,
Line - 1);
}

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

IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
FileMgr.getVirtualFileSystemPtr();
FileMgr->getVirtualFileSystemPtr();
Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
OverrideMainBuffer.get());
// FIXME: there is no way to update VFS if it was changed by
Expand Down
Loading
Loading