diff --git a/clang/include/clang/Frontend/PCHReader.h b/clang/include/clang/Frontend/PCHReader.h index d5157bc3ce4e..a32be0dd7b49 100644 --- a/clang/include/clang/Frontend/PCHReader.h +++ b/clang/include/clang/Frontend/PCHReader.h @@ -39,6 +39,7 @@ namespace llvm { namespace clang { class AddrLabelExpr; +class ASTConsumer; class ASTContext; class Attr; class Decl; @@ -77,6 +78,9 @@ private: /// \brief The AST context into which we'll read the PCH file. ASTContext &Context; + /// \brief The AST consumer. + ASTConsumer *Consumer; + /// \brief The bitstream reader from which we'll read the PCH file. llvm::BitstreamReader Stream; @@ -198,7 +202,7 @@ public: typedef llvm::SmallVector RecordData; explicit PCHReader(Preprocessor &PP, ASTContext &Context) - : SemaObj(0), PP(PP), Context(Context), + : SemaObj(0), PP(PP), Context(Context), Consumer(0), IdentifierTableData(0), NumStatementsRead(0) { } ~PCHReader() {} diff --git a/clang/lib/Frontend/PCHReader.cpp b/clang/lib/Frontend/PCHReader.cpp index 976b98dd11cf..c6c34e9749fa 100644 --- a/clang/lib/Frontend/PCHReader.cpp +++ b/clang/lib/Frontend/PCHReader.cpp @@ -377,9 +377,7 @@ void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) { std::pair PCHDeclReader::VisitDeclContext(DeclContext *DC) { uint64_t LexicalOffset = Record[Idx++]; - uint64_t VisibleOffset = 0; - if (DC->getPrimaryContext() == DC) - VisibleOffset = Record[Idx++]; + uint64_t VisibleOffset = Record[Idx++]; return std::make_pair(LexicalOffset, VisibleOffset); } @@ -2317,6 +2315,23 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { } assert(Idx == Record.size()); + if (Consumer) { + // If we have deserialized a declaration that has a definition the + // AST consumer might need to know about, notify the consumer + // about that definition now. + if (VarDecl *Var = dyn_cast(D)) { + if (Var->isFileVarDecl() && Var->getInit()) { + DeclGroupRef DG(Var); + Consumer->HandleTopLevelDecl(DG); + } + } else if (FunctionDecl *Func = dyn_cast(D)) { + if (Func->isThisDeclarationADefinition()) { + DeclGroupRef DG(Func); + Consumer->HandleTopLevelDecl(DG); + } + } + } + return D; } @@ -2458,6 +2473,8 @@ bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, } void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { + this->Consumer = Consumer; + if (!Consumer) return; diff --git a/clang/lib/Frontend/PCHWriter.cpp b/clang/lib/Frontend/PCHWriter.cpp index 15ee2369dd3b..b081a2813f9b 100644 --- a/clang/lib/Frontend/PCHWriter.cpp +++ b/clang/lib/Frontend/PCHWriter.cpp @@ -574,8 +574,7 @@ void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, uint64_t VisibleOffset) { Record.push_back(LexicalOffset); - if (DC->getPrimaryContext() == DC) - Record.push_back(VisibleOffset); + Record.push_back(VisibleOffset); } //===----------------------------------------------------------------------===// @@ -1728,20 +1727,6 @@ void PCHWriter::WriteDeclsBlock(ASTContext &Context) { // in the PCH file later. if (isa(D)) ExternalDefinitions.push_back(ID); - else if (VarDecl *Var = dyn_cast(D)) { - if (// Non-static file-scope variables with initializers or that - // are tentative definitions. - (Var->isFileVarDecl() && - (Var->getInit() || Var->getStorageClass() == VarDecl::None)) || - // Out-of-line definitions of static data members (C++). - (Var->getDeclContext()->isRecord() && - !Var->getLexicalDeclContext()->isRecord() && - Var->getStorageClass() == VarDecl::Static)) - ExternalDefinitions.push_back(ID); - } else if (FunctionDecl *Func = dyn_cast(D)) { - if (Func->isThisDeclarationADefinition()) - ExternalDefinitions.push_back(ID); - } } // Exit the declarations block