From 4b1f4908606163256443880136bad63ae9eff0f9 Mon Sep 17 00:00:00 2001 From: Sebastian Redl Date: Tue, 27 Jul 2010 18:24:41 +0000 Subject: [PATCH] Update the list of lexical decls in the TU for chained PCHs. This makes -ast-print show the decls from the dependent PCH. llvm-svn: 109524 --- clang/include/clang/Frontend/PCHBitCodes.h | 8 ++++++-- clang/lib/Frontend/PCHReader.cpp | 10 ++++++++++ clang/lib/Frontend/PCHReaderDecl.cpp | 6 +++++- clang/lib/Frontend/PCHWriter.cpp | 16 ++++++++++++---- clang/test/PCH/chain-function.c | 4 ++++ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/Frontend/PCHBitCodes.h b/clang/include/clang/Frontend/PCHBitCodes.h index 76c4a7cb2ac9..774a96becb98 100644 --- a/clang/include/clang/Frontend/PCHBitCodes.h +++ b/clang/include/clang/Frontend/PCHBitCodes.h @@ -237,9 +237,13 @@ namespace clang { /// \brief Record code for the chained PCH metadata, including the /// PCH version and the name of the PCH this is chained to. CHAINED_METADATA = 26, - + /// \brief Record code for referenced selector pool. - REFERENCED_SELECTOR_POOL = 27 + REFERENCED_SELECTOR_POOL = 27, + + /// \brief Record code for an update to the TU's lexically contained + /// declarations. + TU_UPDATE_LEXICAL = 28 }; /// \brief Record types used within a source manager block. diff --git a/clang/lib/Frontend/PCHReader.cpp b/clang/lib/Frontend/PCHReader.cpp index 37e11249f88e..a0e3148bc73e 100644 --- a/clang/lib/Frontend/PCHReader.cpp +++ b/clang/lib/Frontend/PCHReader.cpp @@ -1539,6 +1539,16 @@ PCHReader::ReadPCHBlock(PerFileData &F) { F.LocalNumDecls = Record[0]; break; + case pch::TU_UPDATE_LEXICAL: { + DeclContextInfo Info = { + /* No visible information */ 0, 0, + reinterpret_cast(BlobStart), + BlobLen / sizeof(pch::DeclID) + }; + DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info); + break; + } + case pch::LANGUAGE_OPTIONS: if (ParseLanguageOptions(Record) && !DisableValidation) return IgnorePCH; diff --git a/clang/lib/Frontend/PCHReaderDecl.cpp b/clang/lib/Frontend/PCHReaderDecl.cpp index 6f6ed84df775..fe2947d64f9b 100644 --- a/clang/lib/Frontend/PCHReaderDecl.cpp +++ b/clang/lib/Frontend/PCHReaderDecl.cpp @@ -1520,7 +1520,11 @@ Decl *PCHReader::ReadDeclRecord(unsigned Index) { DeclContextInfo Info; if (ReadDeclContextStorage(DeclsCursor, Offsets, Info)) return 0; - DeclContextOffsets[DC].push_back(Info); + DeclContextInfos &Infos = DeclContextOffsets[DC]; + // Reading the TU will happen after reading its update blocks, so we need + // to make sure we insert in front. For all other contexts, the vector + // is empty here anyway, so there's no loss in efficiency. + Infos.insert(Infos.begin(), Info); } } assert(Idx == Record.size()); diff --git a/clang/lib/Frontend/PCHWriter.cpp b/clang/lib/Frontend/PCHWriter.cpp index 0ade10f7f86b..96c64169cf3f 100644 --- a/clang/lib/Frontend/PCHWriter.cpp +++ b/clang/lib/Frontend/PCHWriter.cpp @@ -2354,15 +2354,23 @@ void PCHWriter::WritePCHChain(Sema &SemaRef, MemorizeStatCalls *StatCalls, // The TU was loaded before we managed to register ourselves as a listener. // Thus we need to add it manually. DeclIDs[TU] = 1; - Record.clear(); + llvm::SmallVector NewGlobalDecls; for (DeclContext::decl_iterator I = TU->noload_decls_begin(), E = TU->noload_decls_end(); I != E; ++I) { - if ((*I)->getPCHLevel() == 0) { - AddDeclRef(*I, Record); - } + if ((*I)->getPCHLevel() == 0) + NewGlobalDecls.push_back(GetDeclRef(*I)); } // We also need to write a lexical updates block for the TU. + llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); + Abv->Add(llvm::BitCodeAbbrevOp(pch::TU_UPDATE_LEXICAL)); + Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); + unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); + Record.clear(); + Record.push_back(pch::TU_UPDATE_LEXICAL); + Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, + reinterpret_cast(NewGlobalDecls.data()), + NewGlobalDecls.size() * sizeof(pch::DeclID)); Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3); WriteDeclsBlockAbbrevs(); diff --git a/clang/test/PCH/chain-function.c b/clang/test/PCH/chain-function.c index 29bfdd4c3eac..0d766fbfc385 100644 --- a/clang/test/PCH/chain-function.c +++ b/clang/test/PCH/chain-function.c @@ -1,6 +1,10 @@ // RUN: %clang_cc1 -emit-pch -o %t1 %S/Inputs/chain-function1.h // RUN: %clang_cc1 -emit-pch -o %t2 %S/Inputs/chain-function2.h -include-pch %t1 -chained-pch // RUN: %clang_cc1 -fsyntax-only -verify -include-pch %t2 %s +// RUN: %clang_cc1 -ast-print -include-pch %t2 %s | FileCheck %s + +// CHECK: void f(); +// CHECK: void g(); void h() { f();