From c45b5c87351f6fd018c60eb4839db3a12f289179 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sat, 16 Jul 2016 00:35:14 +0000 Subject: [PATCH] Reimplement ExternalSemaSource delegation in terms of MultiplexExternalSemaSource to remove one of the places that needs updating every time the ExternalSemaSource interface changes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@275653 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/ChainedIncludesSource.cpp | 156 ++++++------------------- 1 file changed, 36 insertions(+), 120 deletions(-) diff --git a/lib/Frontend/ChainedIncludesSource.cpp b/lib/Frontend/ChainedIncludesSource.cpp index 51771bfb54..ec3207ab8f 100644 --- a/lib/Frontend/ChainedIncludesSource.cpp +++ b/lib/Frontend/ChainedIncludesSource.cpp @@ -18,6 +18,7 @@ #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Lex/Preprocessor.h" #include "clang/Parse/ParseAST.h" +#include "clang/Sema/MultiplexExternalSemaSource.h" #include "clang/Serialization/ASTReader.h" #include "clang/Serialization/ASTWriter.h" #include "llvm/Support/MemoryBuffer.h" @@ -25,51 +26,48 @@ using namespace clang; namespace { -class ChainedIncludesSource : public ExternalSemaSource { +class ChainedIncludesSourceImpl : public ExternalSemaSource { public: - ~ChainedIncludesSource() override; - - ExternalSemaSource &getFinalReader() const { return *FinalReader; } - - std::vector CIs; - IntrusiveRefCntPtr FinalReader; + ChainedIncludesSourceImpl(std::vector> CIs) + : CIs(std::move(CIs)) {} protected: //===----------------------------------------------------------------------===// // ExternalASTSource interface. //===----------------------------------------------------------------------===// - Decl *GetExternalDecl(uint32_t ID) override; - Selector GetExternalSelector(uint32_t ID) override; - uint32_t GetNumExternalSelectors() override; - Stmt *GetExternalDeclStmt(uint64_t Offset) override; - CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override; - CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override; - bool FindExternalVisibleDeclsByName(const DeclContext *DC, - DeclarationName Name) override; - void - FindExternalLexicalDecls(const DeclContext *DC, - llvm::function_ref IsKindWeWant, - SmallVectorImpl &Result) override; - void CompleteType(TagDecl *Tag) override; - void CompleteType(ObjCInterfaceDecl *Class) override; - void StartedDeserializing() override; - void FinishedDeserializing() override; - void StartTranslationUnit(ASTConsumer *Consumer) override; - void PrintStats() override; - /// Return the amount of memory used by memory buffers, breaking down /// by heap-backed versus mmap'ed memory. - void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override; + void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override { + for (unsigned i = 0, e = CIs.size(); i != e; ++i) { + if (const ExternalASTSource *eSrc = + CIs[i]->getASTContext().getExternalSource()) { + eSrc->getMemoryBufferSizes(sizes); + } + } + } - //===----------------------------------------------------------------------===// - // ExternalSemaSource interface. - //===----------------------------------------------------------------------===// +private: + std::vector> CIs; +}; + +/// Members of ChainedIncludesSource, factored out so we can initialize +/// them before we initialize the ExternalSemaSource base class. +struct ChainedIncludesSourceMembers { + ChainedIncludesSourceImpl Impl; + IntrusiveRefCntPtr FinalReader; +}; - void InitializeSema(Sema &S) override; - void ForgetSema() override; - void ReadMethodPool(Selector Sel) override; - bool LookupUnqualified(LookupResult &R, Scope *S) override; +/// Use MultiplexExternalSemaSource to dispatch all ExternalSemaSource +/// calls to the final reader. +class ChainedIncludesSource + : private ChainedIncludesSourceMembers, + public MultiplexExternalSemaSource { +public: + ChainedIncludesSource(std::vector> CIs, + IntrusiveRefCntPtr FinalReader) + : ChainedIncludesSourceMembers{{std::move(CIs)}, std::move(FinalReader)}, + MultiplexExternalSemaSource(Impl, *this->FinalReader) {} }; } @@ -107,18 +105,13 @@ createASTReader(CompilerInstance &CI, StringRef pchFile, return nullptr; } -ChainedIncludesSource::~ChainedIncludesSource() { - for (unsigned i = 0, e = CIs.size(); i != e; ++i) - delete CIs[i]; -} - IntrusiveRefCntPtr clang::createChainedIncludesSource( CompilerInstance &CI, IntrusiveRefCntPtr &Reader) { std::vector &includes = CI.getPreprocessorOpts().ChainedIncludes; assert(!includes.empty() && "No '-chain-include' in options!"); - IntrusiveRefCntPtr source(new ChainedIncludesSource()); + std::vector> CIs; InputKind IK = CI.getFrontendOpts().Inputs[0].getKind(); SmallVector, 4> SerialBufs; @@ -206,7 +199,7 @@ IntrusiveRefCntPtr clang::createChainedIncludesSource( SerialBufs.push_back(llvm::MemoryBuffer::getMemBufferCopy( StringRef(serialAST.data(), serialAST.size()))); serialAST.clear(); - source->CIs.push_back(Clang.release()); + CIs.push_back(std::move(Clang)); } assert(!SerialBufs.empty()); @@ -216,83 +209,6 @@ IntrusiveRefCntPtr clang::createChainedIncludesSource( if (!Reader) return nullptr; - source->FinalReader = Reader; - return source; -} - -//===----------------------------------------------------------------------===// -// ExternalASTSource interface. -//===----------------------------------------------------------------------===// - -Decl *ChainedIncludesSource::GetExternalDecl(uint32_t ID) { - return getFinalReader().GetExternalDecl(ID); -} -Selector ChainedIncludesSource::GetExternalSelector(uint32_t ID) { - return getFinalReader().GetExternalSelector(ID); -} -uint32_t ChainedIncludesSource::GetNumExternalSelectors() { - return getFinalReader().GetNumExternalSelectors(); + return IntrusiveRefCntPtr( + new ChainedIncludesSource(std::move(CIs), Reader)); } -Stmt *ChainedIncludesSource::GetExternalDeclStmt(uint64_t Offset) { - return getFinalReader().GetExternalDeclStmt(Offset); -} -CXXBaseSpecifier * -ChainedIncludesSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) { - return getFinalReader().GetExternalCXXBaseSpecifiers(Offset); -} -CXXCtorInitializer ** -ChainedIncludesSource::GetExternalCXXCtorInitializers(uint64_t Offset) { - return getFinalReader().GetExternalCXXCtorInitializers(Offset); -} -bool -ChainedIncludesSource::FindExternalVisibleDeclsByName(const DeclContext *DC, - DeclarationName Name) { - return getFinalReader().FindExternalVisibleDeclsByName(DC, Name); -} -void ChainedIncludesSource::FindExternalLexicalDecls( - const DeclContext *DC, llvm::function_ref IsKindWeWant, - SmallVectorImpl &Result) { - return getFinalReader().FindExternalLexicalDecls(DC, IsKindWeWant, Result); -} -void ChainedIncludesSource::CompleteType(TagDecl *Tag) { - return getFinalReader().CompleteType(Tag); -} -void ChainedIncludesSource::CompleteType(ObjCInterfaceDecl *Class) { - return getFinalReader().CompleteType(Class); -} -void ChainedIncludesSource::StartedDeserializing() { - return getFinalReader().StartedDeserializing(); -} -void ChainedIncludesSource::FinishedDeserializing() { - return getFinalReader().FinishedDeserializing(); -} -void ChainedIncludesSource::StartTranslationUnit(ASTConsumer *Consumer) { - return getFinalReader().StartTranslationUnit(Consumer); -} -void ChainedIncludesSource::PrintStats() { - return getFinalReader().PrintStats(); -} -void ChainedIncludesSource::getMemoryBufferSizes(MemoryBufferSizes &sizes)const{ - for (unsigned i = 0, e = CIs.size(); i != e; ++i) { - if (const ExternalASTSource *eSrc = - CIs[i]->getASTContext().getExternalSource()) { - eSrc->getMemoryBufferSizes(sizes); - } - } - - getFinalReader().getMemoryBufferSizes(sizes); -} - -void ChainedIncludesSource::InitializeSema(Sema &S) { - return getFinalReader().InitializeSema(S); -} -void ChainedIncludesSource::ForgetSema() { - return getFinalReader().ForgetSema(); -} -void ChainedIncludesSource::ReadMethodPool(Selector Sel) { - getFinalReader().ReadMethodPool(Sel); -} -bool ChainedIncludesSource::LookupUnqualified(LookupResult &R, Scope *S) { - return getFinalReader().LookupUnqualified(R, S); -} - -- 2.40.0