From: Argyrios Kyrtzidis Date: Thu, 27 Oct 2011 18:47:35 +0000 (+0000) Subject: [PCH] Pull the location out of the serialized declarations and put it in the array X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9d31fa75bc05fe4cb903a7701550f22cfb73ea8b;p=clang [PCH] Pull the location out of the serialized declarations and put it in the array of decl bit offsets. This allows us to easily get at the location of a decl without deserializing it. It increases size of Cocoa PCH by only 0.2%. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@143123 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index 485b8fe3bb..9301a833ba 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -156,6 +156,22 @@ namespace clang { BitOffset(BitOffset) { } }; + /// \brief Source range/offset of a preprocessed entity. + struct DeclOffset { + /// \brief Raw source location. + unsigned Loc; + /// \brief Offset in the AST file. + uint32_t BitOffset; + + DeclOffset() : Loc(0), BitOffset(0) { } + DeclOffset(SourceLocation Loc, uint32_t BitOffset) + : Loc(Loc.getRawEncoding()), + BitOffset(BitOffset) { } + void setLocation(SourceLocation L) { + Loc = L.getRawEncoding(); + } + }; + /// \brief The number of predefined preprocessed entity IDs. const unsigned int NUM_PREDEF_PP_ENTITY_IDS = 1; diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h index 6470fd634e..a463a280d3 100644 --- a/include/clang/Serialization/ASTReader.h +++ b/include/clang/Serialization/ASTReader.h @@ -679,7 +679,8 @@ private: RecordLocation TypeCursorForIndex(unsigned Index); void LoadedDecl(unsigned Index, Decl *D); Decl *ReadDeclRecord(serialization::DeclID ID); - RecordLocation DeclCursorForID(serialization::DeclID ID); + RecordLocation DeclCursorForID(serialization::DeclID ID, + unsigned &RawLocation); void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D); void loadObjCChainedCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D); diff --git a/include/clang/Serialization/ASTWriter.h b/include/clang/Serialization/ASTWriter.h index 44d8825829..184897fba9 100644 --- a/include/clang/Serialization/ASTWriter.h +++ b/include/clang/Serialization/ASTWriter.h @@ -142,7 +142,7 @@ private: /// \brief Offset of each declaration in the bitstream, indexed by /// the declaration's ID. - std::vector DeclOffsets; + std::vector DeclOffsets; /// \brief The first ID number we can use for our own types. serialization::TypeID FirstTypeID; diff --git a/include/clang/Serialization/Module.h b/include/clang/Serialization/Module.h index 42b5a58e08..5c0cc9d296 100644 --- a/include/clang/Serialization/Module.h +++ b/include/clang/Serialization/Module.h @@ -241,7 +241,7 @@ public: /// \brief Offset of each declaration within the bitstream, indexed /// by the declaration ID (-1). - const uint32_t *DeclOffsets; + const DeclOffset *DeclOffsets; /// \brief Base declaration ID for declarations local to this module. serialization::DeclID BaseDeclID; diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 5898ff0961..4678930702 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -1756,7 +1756,7 @@ ASTReader::ReadASTBlock(Module &F) { Error("duplicate DECL_OFFSET record in AST file"); return Failure; } - F.DeclOffsets = (const uint32_t *)BlobStart; + F.DeclOffsets = (const DeclOffset *)BlobStart; F.LocalNumDecls = Record[0]; unsigned LocalBaseDeclID = Record[1]; F.BaseDeclID = getTotalNumDecls(); diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index f64aa20a22..7b37bbfa1b 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -35,6 +35,7 @@ namespace clang { Module &F; llvm::BitstreamCursor &Cursor; const DeclID ThisDeclID; + const unsigned RawLocation; typedef ASTReader::RecordData RecordData; const RecordData &Record; unsigned &Idx; @@ -94,9 +95,11 @@ namespace clang { public: ASTDeclReader(ASTReader &Reader, Module &F, llvm::BitstreamCursor &Cursor, DeclID thisDeclID, + unsigned RawLocation, const RecordData &Record, unsigned &Idx) : Reader(Reader), F(F), Cursor(Cursor), ThisDeclID(thisDeclID), - Record(Record), Idx(Idx), TypeIDForTypeDecl(0) { } + RawLocation(RawLocation), Record(Record), Idx(Idx), + TypeIDForTypeDecl(0) { } static void attachPreviousDecl(Decl *D, Decl *previous); @@ -236,7 +239,7 @@ void ASTDeclReader::VisitDecl(Decl *D) { D->setDeclContext(ReadDeclAs(Record, Idx)); D->setLexicalDeclContext(ReadDeclAs(Record, Idx)); } - D->setLocation(ReadSourceLocation(Record, Idx)); + D->setLocation(Reader.ReadSourceLocation(F, RawLocation)); D->setInvalidDecl(Record[Idx++]); if (Record[Idx++]) { // hasAttrs AttrVec Attrs; @@ -1443,7 +1446,7 @@ static bool isConsumerInterestedIn(Decl *D) { /// \brief Get the correct cursor and offset for loading a declaration. ASTReader::RecordLocation -ASTReader::DeclCursorForID(DeclID ID) { +ASTReader::DeclCursorForID(DeclID ID, unsigned &RawLocation) { // See if there's an override. DeclReplacementMap::iterator It = ReplacedDecls.find(ID); if (It != ReplacedDecls.end()) @@ -1452,8 +1455,10 @@ ASTReader::DeclCursorForID(DeclID ID) { GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID); assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); Module *M = I->second; - return RecordLocation(M, - M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]); + const DeclOffset & + DOffs = M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]; + RawLocation = DOffs.Loc; + return RecordLocation(M, DOffs.BitOffset); } ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) { @@ -1490,7 +1495,8 @@ void ASTReader::loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID) { /// \brief Read the declaration at the given offset from the AST file. Decl *ASTReader::ReadDeclRecord(DeclID ID) { unsigned Index = ID - NUM_PREDEF_DECL_IDS; - RecordLocation Loc = DeclCursorForID(ID); + unsigned RawLocation = 0; + RecordLocation Loc = DeclCursorForID(ID, RawLocation); llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; // Keep track of where we are in the stream, then jump back there // after reading this declaration. @@ -1505,7 +1511,7 @@ Decl *ASTReader::ReadDeclRecord(DeclID ID) { RecordData Record; unsigned Code = DeclsCursor.ReadCode(); unsigned Idx = 0; - ASTDeclReader Reader(*this, *Loc.F, DeclsCursor, ID, Record, Idx); + ASTDeclReader Reader(*this, *Loc.F, DeclsCursor, ID, RawLocation, Record,Idx); Decl *D = 0; switch ((DeclCode)DeclsCursor.ReadRecord(Code, Record)) { @@ -1799,7 +1805,7 @@ void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) { assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!"); unsigned Idx = 0; - ASTDeclReader Reader(*this, *F, Cursor, ID, Record, Idx); + ASTDeclReader Reader(*this, *F, Cursor, ID, 0, Record, Idx); Reader.UpdateDecl(D, *F, Record); } } diff --git a/lib/Serialization/ASTWriterDecl.cpp b/lib/Serialization/ASTWriterDecl.cpp index f613867926..13cdd0ef2b 100644 --- a/lib/Serialization/ASTWriterDecl.cpp +++ b/lib/Serialization/ASTWriterDecl.cpp @@ -146,7 +146,6 @@ void ASTDeclWriter::Visit(Decl *D) { void ASTDeclWriter::VisitDecl(Decl *D) { Writer.AddDeclRef(cast_or_null(D->getDeclContext()), Record); Writer.AddDeclRef(cast_or_null(D->getLexicalDeclContext()), Record); - Writer.AddSourceLocation(D->getLocation(), Record); Record.push_back(D->isInvalidDecl()); Record.push_back(D->hasAttrs()); if (D->hasAttrs()) @@ -1274,7 +1273,6 @@ void ASTWriter::WriteDeclsBlockAbbrevs() { // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs Abv->Add(BitCodeAbbrevOp(0)); // isImplicit @@ -1305,7 +1303,6 @@ void ASTWriter::WriteDeclsBlockAbbrevs() { // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs Abv->Add(BitCodeAbbrevOp(0)); // isImplicit @@ -1341,7 +1338,6 @@ void ASTWriter::WriteDeclsBlockAbbrevs() { // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs Abv->Add(BitCodeAbbrevOp(0)); // isImplicit @@ -1387,7 +1383,6 @@ void ASTWriter::WriteDeclsBlockAbbrevs() { // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs Abv->Add(BitCodeAbbrevOp(0)); // isImplicit @@ -1427,7 +1422,6 @@ void ASTWriter::WriteDeclsBlockAbbrevs() { // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs Abv->Add(BitCodeAbbrevOp(0)); // isImplicit @@ -1474,7 +1468,6 @@ void ASTWriter::WriteDeclsBlockAbbrevs() { // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs Abv->Add(BitCodeAbbrevOp(0)); // isImplicit @@ -1501,7 +1494,6 @@ void ASTWriter::WriteDeclsBlockAbbrevs() { // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?) Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs Abv->Add(BitCodeAbbrevOp(0)); // isImplicit @@ -1660,10 +1652,12 @@ void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) { // Record the offset for this declaration if (DeclOffsets.size() == Index) - DeclOffsets.push_back(Stream.GetCurrentBitNo()); + DeclOffsets.push_back(DeclOffset(D->getLocation(), + Stream.GetCurrentBitNo())); else if (DeclOffsets.size() < Index) { DeclOffsets.resize(Index+1); - DeclOffsets[Index] = Stream.GetCurrentBitNo(); + DeclOffsets[Index].setLocation(D->getLocation()); + DeclOffsets[Index].BitOffset = Stream.GetCurrentBitNo(); } }