From: Sebastian Redl Date: Thu, 22 Jul 2010 22:43:28 +0000 (+0000) Subject: Thread bitstream cursors all the way through the AST reading stuff. This way, reading... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=577d4796d358c0e72ebaa023113505226ab51b4f;p=clang Thread bitstream cursors all the way through the AST reading stuff. This way, reading a trivial 2-element chained file actually works. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@109191 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/PCHReader.h b/include/clang/Frontend/PCHReader.h index 34712369e7..8e2e3cafa6 100644 --- a/include/clang/Frontend/PCHReader.h +++ b/include/clang/Frontend/PCHReader.h @@ -670,15 +670,17 @@ public: /// given TemplateArgument kind. TemplateArgumentLocInfo GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, + llvm::BitstreamCursor &DeclsCursor, const RecordData &Record, unsigned &Idx); /// \brief Reads a TemplateArgumentLoc. - TemplateArgumentLoc ReadTemplateArgumentLoc(const RecordData &Record, - unsigned &Idx); + TemplateArgumentLoc + ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor, + const RecordData &Record, unsigned &Idx); /// \brief Reads a declarator info from the given record. - TypeSourceInfo *GetTypeSourceInfo(const RecordData &Record, - unsigned &Idx); + TypeSourceInfo *GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor, + const RecordData &Record, unsigned &Idx); /// \brief Resolve and return the translation unit declaration. TranslationUnitDecl *GetTranslationUnitDecl(); @@ -807,7 +809,8 @@ public: TemplateName ReadTemplateName(const RecordData &Record, unsigned &Idx); /// \brief Read a template argument. - TemplateArgument ReadTemplateArgument(const RecordData &Record,unsigned &Idx); + TemplateArgument ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor, + const RecordData &Record,unsigned &Idx); /// \brief Read a template parameter list. TemplateParameterList *ReadTemplateParameterList(const RecordData &Record, @@ -816,6 +819,7 @@ public: /// \brief Read a template argument array. void ReadTemplateArgumentList(llvm::SmallVector &TemplArgs, + llvm::BitstreamCursor &DeclsCursor, const RecordData &Record, unsigned &Idx); /// \brief Read a UnresolvedSet structure. @@ -848,13 +852,13 @@ public: CXXTemporary *ReadCXXTemporary(const RecordData &Record, unsigned &Idx); /// \brief Reads attributes from the current stream position. - Attr *ReadAttributes(); + Attr *ReadAttributes(llvm::BitstreamCursor &DeclsCursor); /// \brief Reads a statement. - Stmt *ReadStmt(); + Stmt *ReadStmt(llvm::BitstreamCursor &Cursor); /// \brief Reads an expression. - Expr *ReadExpr(); + Expr *ReadExpr(llvm::BitstreamCursor &Cursor); /// \brief Reads a sub-statement operand during statement reading. Stmt *ReadSubStmt() { diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp index cc44c30072..1ccd851ae3 100644 --- a/lib/Frontend/PCHReader.cpp +++ b/lib/Frontend/PCHReader.cpp @@ -2296,7 +2296,7 @@ QualType PCHReader::ReadTypeRecord(unsigned Index) { unsigned IndexTypeQuals = Record[2]; SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]); SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]); - return Context->getVariableArrayType(ElementType, ReadExpr(), + return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor), ASM, IndexTypeQuals, SourceRange(LBLoc, RBLoc)); } @@ -2376,7 +2376,7 @@ QualType PCHReader::ReadTypeRecord(unsigned Index) { } case pch::TYPE_TYPEOF_EXPR: - return Context->getTypeOfExprType(ReadExpr()); + return Context->getTypeOfExprType(ReadExpr(DeclsCursor)); case pch::TYPE_TYPEOF: { if (Record.size() != 1) { @@ -2388,7 +2388,7 @@ QualType PCHReader::ReadTypeRecord(unsigned Index) { } case pch::TYPE_DECLTYPE: - return Context->getDecltypeType(ReadExpr()); + return Context->getDecltypeType(ReadExpr(DeclsCursor)); case pch::TYPE_RECORD: { if (Record.size() != 2) { @@ -2487,7 +2487,7 @@ QualType PCHReader::ReadTypeRecord(unsigned Index) { llvm::SmallVector Args; Args.reserve(NumArgs); while (NumArgs--) - Args.push_back(ReadTemplateArgument(Record, Idx)); + Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name, Args.size(), Args.data()); } @@ -2502,7 +2502,7 @@ QualType PCHReader::ReadTypeRecord(unsigned Index) { unsigned IndexTypeQuals = Record[Idx++]; // DependentSizedArrayType - Expr *NumElts = ReadExpr(); + Expr *NumElts = ReadExpr(DeclsCursor); SourceRange Brackets = ReadSourceRange(Record, Idx); return Context->getDependentSizedArrayType(ElementType, NumElts, ASM, @@ -2514,7 +2514,7 @@ QualType PCHReader::ReadTypeRecord(unsigned Index) { bool IsDependent = Record[Idx++]; TemplateName Name = ReadTemplateName(Record, Idx); llvm::SmallVector Args; - ReadTemplateArgumentList(Args, Record, Idx); + ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx); QualType Canon = GetType(Record[Idx++]); QualType T; if (Canon.isNull()) @@ -2535,13 +2535,14 @@ namespace { class TypeLocReader : public TypeLocVisitor { PCHReader &Reader; + llvm::BitstreamCursor &DeclsCursor; const PCHReader::RecordData &Record; unsigned &Idx; public: - TypeLocReader(PCHReader &Reader, const PCHReader::RecordData &Record, - unsigned &Idx) - : Reader(Reader), Record(Record), Idx(Idx) { } + TypeLocReader(PCHReader &Reader, llvm::BitstreamCursor &Cursor, + const PCHReader::RecordData &Record, unsigned &Idx) + : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { } // We want compile-time assurance that we've enumerated all of // these, so unfortunately we have to declare them first, then @@ -2591,7 +2592,7 @@ void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); if (Record[Idx++]) - TL.setSizeExpr(Reader.ReadExpr()); + TL.setSizeExpr(Reader.ReadExpr(DeclsCursor)); else TL.setSizeExpr(0); } @@ -2646,7 +2647,7 @@ void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(Record, Idx)); + TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); } void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); @@ -2672,7 +2673,7 @@ void TypeLocReader::VisitTemplateSpecializationTypeLoc( for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) TL.setArgLocInfo(i, Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(), - Record, Idx)); + DeclsCursor, Record, Idx)); } void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); @@ -2696,7 +2697,7 @@ void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) TL.setArgLocInfo(I, Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(), - Record, Idx)); + DeclsCursor, Record, Idx)); } void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); @@ -2712,14 +2713,15 @@ void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); } -TypeSourceInfo *PCHReader::GetTypeSourceInfo(const RecordData &Record, +TypeSourceInfo *PCHReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor, + const RecordData &Record, unsigned &Idx) { QualType InfoTy = GetType(Record[Idx++]); if (InfoTy.isNull()) return 0; TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy); - TypeLocReader TLR(*this, Record, Idx); + TypeLocReader TLR(*this, DeclsCursor, Record, Idx); for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) TLR.Visit(TL); return TInfo; @@ -2787,13 +2789,14 @@ QualType PCHReader::GetType(pch::TypeID ID) { TemplateArgumentLocInfo PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, + llvm::BitstreamCursor &DeclsCursor, const RecordData &Record, unsigned &Index) { switch (Kind) { case TemplateArgument::Expression: - return ReadExpr(); + return ReadExpr(DeclsCursor); case TemplateArgument::Type: - return GetTypeSourceInfo(Record, Index); + return GetTypeSourceInfo(DeclsCursor, Record, Index); case TemplateArgument::Template: { SourceRange QualifierRange = ReadSourceRange(Record, Index); SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index); @@ -2810,14 +2813,16 @@ PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, } TemplateArgumentLoc -PCHReader::ReadTemplateArgumentLoc(const RecordData &Record, unsigned &Index) { - TemplateArgument Arg = ReadTemplateArgument(Record, Index); +PCHReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor, + const RecordData &Record, unsigned &Index) { + TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index); if (Arg.getKind() == TemplateArgument::Expression) { if (Record[Index++]) // bool InfoHasSameExpr. return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); } return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(), + DeclsCursor, Record, Index)); } @@ -3370,7 +3375,8 @@ PCHReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) { } TemplateArgument -PCHReader::ReadTemplateArgument(const RecordData &Record, unsigned &Idx) { +PCHReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor, + const RecordData &Record, unsigned &Idx) { switch ((TemplateArgument::ArgKind)Record[Idx++]) { case TemplateArgument::Null: return TemplateArgument(); @@ -3386,13 +3392,13 @@ PCHReader::ReadTemplateArgument(const RecordData &Record, unsigned &Idx) { case TemplateArgument::Template: return TemplateArgument(ReadTemplateName(Record, Idx)); case TemplateArgument::Expression: - return TemplateArgument(ReadExpr()); + return TemplateArgument(ReadExpr(DeclsCursor)); case TemplateArgument::Pack: { unsigned NumArgs = Record[Idx++]; llvm::SmallVector Args; Args.reserve(NumArgs); while (NumArgs--) - Args.push_back(ReadTemplateArgument(Record, Idx)); + Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); TemplateArgument TemplArg; TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true); return TemplArg; @@ -3424,11 +3430,12 @@ PCHReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) { void PCHReader:: ReadTemplateArgumentList(llvm::SmallVector &TemplArgs, + llvm::BitstreamCursor &DeclsCursor, const RecordData &Record, unsigned &Idx) { unsigned NumTemplateArgs = Record[Idx++]; TemplArgs.reserve(NumTemplateArgs); while (NumTemplateArgs--) - TemplArgs.push_back(ReadTemplateArgument(Record, Idx)); + TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); } /// \brief Read a UnresolvedSet structure. diff --git a/lib/Frontend/PCHReaderDecl.cpp b/lib/Frontend/PCHReaderDecl.cpp index 058b19dab3..046dce6f4d 100644 --- a/lib/Frontend/PCHReaderDecl.cpp +++ b/lib/Frontend/PCHReaderDecl.cpp @@ -30,14 +30,18 @@ using namespace clang; namespace clang { class PCHDeclReader : public DeclVisitor { PCHReader &Reader; + llvm::BitstreamCursor &Cursor; const PCHReader::RecordData &Record; unsigned &Idx; pch::TypeID TypeIDForTypeDecl; + uint64_t GetCurrentCursorOffset(); + public: - PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record, - unsigned &Idx) - : Reader(Reader), Record(Record), Idx(Idx), TypeIDForTypeDecl(0) { } + PCHDeclReader(PCHReader &Reader, llvm::BitstreamCursor &Cursor, + const PCHReader::RecordData &Record, unsigned &Idx) + : Reader(Reader), Cursor(Cursor), Record(Record), Idx(Idx), + TypeIDForTypeDecl(0) { } void Visit(Decl *D); @@ -108,6 +112,19 @@ namespace clang { }; } +uint64_t PCHDeclReader::GetCurrentCursorOffset() { + uint64_t Off = 0; + for (unsigned I = 0, N = Reader.Chain.size(); I != N; ++I) { + PCHReader::PerFileData &F = *Reader.Chain[N - I - 1]; + if (&Cursor == &F.DeclsCursor) { + Off += F.DeclsCursor.GetCurrentBitNo(); + break; + } + Off += F.SizeInBits; + } + return Off; +} + void PCHDeclReader::Visit(Decl *D) { DeclVisitor::Visit(D); @@ -117,7 +134,7 @@ void PCHDeclReader::Visit(Decl *D) { } else if (FunctionDecl *FD = dyn_cast(D)) { // FunctionDecl's body was written last after all other Stmts/Exprs. if (Record[Idx++]) - FD->setLazyBody(Reader.Chain[0]->DeclsCursor.GetCurrentBitNo()); + FD->setLazyBody(GetCurrentCursorOffset()); } } @@ -128,7 +145,7 @@ void PCHDeclReader::VisitDecl(Decl *D) { D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); D->setInvalidDecl(Record[Idx++]); if (Record[Idx++]) - D->initAttrs(Reader.ReadAttributes()); + D->initAttrs(Reader.ReadAttributes(Cursor)); D->setImplicit(Record[Idx++]); D->setUsed(Record[Idx++]); D->setAccess((AccessSpecifier)Record[Idx++]); @@ -154,7 +171,7 @@ void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) { void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) { VisitTypeDecl(TD); - TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); + TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx)); } void PCHDeclReader::VisitTagDecl(TagDecl *TD) { @@ -197,13 +214,13 @@ void PCHDeclReader::VisitValueDecl(ValueDecl *VD) { void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) { VisitValueDecl(ECD); if (Record[Idx++]) - ECD->setInitExpr(Reader.ReadExpr()); + ECD->setInitExpr(Reader.ReadExpr(Cursor)); ECD->setInitVal(Reader.ReadAPSInt(Record, Idx)); } void PCHDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) { VisitValueDecl(DD); - TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Record, Idx); + TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx); if (TInfo) DD->setTypeSourceInfo(TInfo); // FIXME: read optional qualifier and its range. @@ -237,7 +254,7 @@ void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { // Template arguments. llvm::SmallVector TemplArgs; - Reader.ReadTemplateArgumentList(TemplArgs, Record, Idx); + Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx); // Template args as written. llvm::SmallVector TemplArgLocs; @@ -246,7 +263,8 @@ void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { unsigned NumTemplateArgLocs = Record[Idx++]; TemplArgLocs.reserve(NumTemplateArgLocs); for (unsigned i=0; i != NumTemplateArgLocs; ++i) - TemplArgLocs.push_back(Reader.ReadTemplateArgumentLoc(Record, Idx)); + TemplArgLocs.push_back( + Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx)); LAngleLoc = Reader.ReadSourceLocation(Record, Idx); RAngleLoc = Reader.ReadSourceLocation(Record, Idx); @@ -273,7 +291,7 @@ void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { TemplateArgumentListInfo TemplArgs; unsigned NumArgs = Record[Idx++]; while (NumArgs--) - TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx)); + TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(Cursor,Record, Idx)); TemplArgs.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx)); TemplArgs.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx)); @@ -317,7 +335,7 @@ void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) { if (Record[Idx++]) { // In practice, this won't be executed (since method definitions // don't occur in header files). - MD->setBody(Reader.ReadStmt()); + MD->setBody(Reader.ReadStmt(Cursor)); MD->setSelfDecl(cast(Reader.GetDecl(Record[Idx++]))); MD->setCmdDecl(cast(Reader.GetDecl(Record[Idx++]))); } @@ -329,7 +347,7 @@ void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) { MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]); MD->setNumSelectorArgs(unsigned(Record[Idx++])); MD->setResultType(Reader.GetType(Record[Idx++])); - MD->setResultTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); + MD->setResultTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx)); MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); unsigned NumParams = Record[Idx++]; llvm::SmallVector Params; @@ -462,7 +480,7 @@ void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) { void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { VisitNamedDecl(D); D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - D->setType(Reader.GetTypeSourceInfo(Record, Idx)); + D->setType(Reader.GetTypeSourceInfo(Cursor, Record, Idx)); // FIXME: stable encoding D->setPropertyAttributes( (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]); @@ -514,7 +532,7 @@ void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) { VisitDeclaratorDecl(FD); FD->setMutable(Record[Idx++]); if (Record[Idx++]) - FD->setBitWidth(Reader.ReadExpr()); + FD->setBitWidth(Reader.ReadExpr(Cursor)); if (!FD->getDeclName()) { FieldDecl *Tmpl = cast_or_null(Reader.GetDecl(Record[Idx++])); if (Tmpl) @@ -534,7 +552,7 @@ void PCHDeclReader::VisitVarDecl(VarDecl *VD) { VD->setPreviousDeclaration( cast_or_null(Reader.GetDecl(Record[Idx++]))); if (Record[Idx++]) - VD->setInit(Reader.ReadExpr()); + VD->setInit(Reader.ReadExpr(Cursor)); if (Record[Idx++]) { // HasMemberSpecializationInfo. VarDecl *Tmpl = cast(Reader.GetDecl(Record[Idx++])); @@ -553,18 +571,18 @@ void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) { PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]); PD->setHasInheritedDefaultArg(Record[Idx++]); if (Record[Idx++]) // hasUninstantiatedDefaultArg. - PD->setUninstantiatedDefaultArg(Reader.ReadExpr()); + PD->setUninstantiatedDefaultArg(Reader.ReadExpr(Cursor)); } void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) { VisitDecl(AD); - AD->setAsmString(cast(Reader.ReadExpr())); + AD->setAsmString(cast(Reader.ReadExpr(Cursor))); } void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) { VisitDecl(BD); - BD->setBody(cast_or_null(Reader.ReadStmt())); - BD->setSignatureAsWritten(Reader.GetTypeSourceInfo(Record, Idx)); + BD->setBody(cast_or_null(Reader.ReadStmt(Cursor))); + BD->setSignatureAsWritten(Reader.GetTypeSourceInfo(Cursor, Record, Idx)); unsigned NumParams = Record[Idx++]; llvm::SmallVector Params; Params.reserve(NumParams); @@ -782,13 +800,13 @@ void PCHDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) { bool IsBaseInitializer = Record[Idx++]; if (IsBaseInitializer) { - BaseClassInfo = Reader.GetTypeSourceInfo(Record, Idx); + BaseClassInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx); IsBaseVirtual = Record[Idx++]; } else { Member = cast(Reader.GetDecl(Record[Idx++])); } SourceLocation MemberLoc = Reader.ReadSourceLocation(Record, Idx); - Expr *Init = Reader.ReadExpr(); + Expr *Init = Reader.ReadExpr(Cursor); FieldDecl *AnonUnionMember = cast_or_null(Reader.GetDecl(Record[Idx++])); SourceLocation LParenLoc = Reader.ReadSourceLocation(Record, Idx); @@ -846,7 +864,7 @@ void PCHDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) { void PCHDeclReader::VisitFriendDecl(FriendDecl *D) { VisitDecl(D); if (Record[Idx++]) - D->Friend = Reader.GetTypeSourceInfo(Record, Idx); + D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx); else D->Friend = cast(Reader.GetDecl(Record[Idx++])); D->NextFriend = cast_or_null(Reader.GetDecl(Record[Idx++])); @@ -863,7 +881,7 @@ void PCHDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) { if (Record[Idx++]) // HasFriendDecl D->Friend = cast(Reader.GetDecl(Record[Idx++])); else - D->Friend = Reader.GetTypeSourceInfo(Record, Idx); + D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx); D->FriendLoc = Reader.ReadSourceLocation(Record, Idx); } @@ -917,21 +935,21 @@ void PCHDeclReader::VisitClassTemplateSpecializationDecl( D->setInstantiationOf(CTD); } else { llvm::SmallVector TemplArgs; - Reader.ReadTemplateArgumentList(TemplArgs, Record, Idx); + Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx); D->setInstantiationOf(cast(InstD), TemplArgs.data(), TemplArgs.size()); } } // Explicit info. - if (TypeSourceInfo *TyInfo = Reader.GetTypeSourceInfo(Record, Idx)) { + if (TypeSourceInfo *TyInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx)) { D->setTypeAsWritten(TyInfo); D->setExternLoc(Reader.ReadSourceLocation(Record, Idx)); D->setTemplateKeywordLoc(Reader.ReadSourceLocation(Record, Idx)); } llvm::SmallVector TemplArgs; - Reader.ReadTemplateArgumentList(TemplArgs, Record, Idx); + Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx); D->initTemplateArgs(TemplArgs.data(), TemplArgs.size()); SourceLocation POI = Reader.ReadSourceLocation(Record, Idx); if (POI.isValid()) @@ -959,7 +977,7 @@ void PCHDeclReader::VisitClassTemplatePartialSpecializationDecl( TemplateArgumentListInfo ArgInfos; unsigned NumArgs = Record[Idx++]; while (NumArgs--) - ArgInfos.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx)); + ArgInfos.addArgument(Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx)); D->initTemplateArgsAsWritten(ArgInfos); D->setSequenceNumber(Record[Idx++]); @@ -1007,7 +1025,7 @@ void PCHDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { D->setParameterPack(Record[Idx++]); bool Inherited = Record[Idx++]; - TypeSourceInfo *DefArg = Reader.GetTypeSourceInfo(Record, Idx); + TypeSourceInfo *DefArg = Reader.GetTypeSourceInfo(Cursor, Record, Idx); D->setDefaultArgument(DefArg, Inherited); } @@ -1018,7 +1036,7 @@ void PCHDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { D->setPosition(Record[Idx++]); // Rest of NonTypeTemplateParmDecl. if (Record[Idx++]) { - Expr *DefArg = Reader.ReadExpr(); + Expr *DefArg = Reader.ReadExpr(Cursor); bool Inherited = Record[Idx++]; D->setDefaultArgument(DefArg, Inherited); } @@ -1030,15 +1048,15 @@ void PCHDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { D->setDepth(Record[Idx++]); D->setPosition(Record[Idx++]); // Rest of TemplateTemplateParmDecl. - TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(Record, Idx); + TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx); bool IsInherited = Record[Idx++]; D->setDefaultArgument(Arg, IsInherited); } void PCHDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) { VisitDecl(D); - D->AssertExpr = Reader.ReadExpr(); - D->Message = cast(Reader.ReadExpr()); + D->AssertExpr = Reader.ReadExpr(Cursor); + D->Message = cast(Reader.ReadExpr(Cursor)); } std::pair @@ -1053,8 +1071,7 @@ PCHDeclReader::VisitDeclContext(DeclContext *DC) { //===----------------------------------------------------------------------===// /// \brief Reads attributes from the current stream position. -Attr *PCHReader::ReadAttributes() { - llvm::BitstreamCursor &DeclsCursor = Chain[0]->DeclsCursor; +Attr *PCHReader::ReadAttributes(llvm::BitstreamCursor &DeclsCursor) { unsigned Code = DeclsCursor.ReadCode(); assert(Code == llvm::bitc::UNABBREV_RECORD && "Expected unabbreviated record"); (void)Code; @@ -1297,7 +1314,7 @@ Decl *PCHReader::ReadDeclRecord(unsigned Index) { RecordData Record; unsigned Code = DeclsCursor.ReadCode(); unsigned Idx = 0; - PCHDeclReader Reader(*this, Record, Idx); + PCHDeclReader Reader(*this, DeclsCursor, Record, Idx); Decl *D = 0; switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) { diff --git a/lib/Frontend/PCHReaderStmt.cpp b/lib/Frontend/PCHReaderStmt.cpp index 65502b8e26..160e45a2bb 100644 --- a/lib/Frontend/PCHReaderStmt.cpp +++ b/lib/Frontend/PCHReaderStmt.cpp @@ -21,13 +21,14 @@ namespace clang { class PCHStmtReader : public StmtVisitor { PCHReader &Reader; + llvm::BitstreamCursor &DeclsCursor; const PCHReader::RecordData &Record; unsigned &Idx; public: - PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record, - unsigned &Idx) - : Reader(Reader), Record(Record), Idx(Idx) { } + PCHStmtReader(PCHReader &Reader, llvm::BitstreamCursor &Cursor, + const PCHReader::RecordData &Record, unsigned &Idx) + : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { } /// \brief The number of record fields required for the Stmt class /// itself. @@ -164,7 +165,8 @@ ReadExplicitTemplateArgumentList(ExplicitTemplateArgumentList &ArgList, ArgInfo.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx)); ArgInfo.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx)); for (unsigned i = 0; i != NumTemplateArgs; ++i) - ArgInfo.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx)); + ArgInfo.addArgument( + Reader.ReadTemplateArgumentLoc(DeclsCursor, Record, Idx)); ArgList.initializeFrom(ArgInfo); } @@ -479,7 +481,7 @@ void PCHStmtReader::VisitOffsetOfExpr(OffsetOfExpr *E) { ++Idx; E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); + E->setTypeSourceInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) { Node::Kind Kind = static_cast(Record[Idx++]); SourceLocation Start = SourceLocation::getFromRawEncoding(Record[Idx++]); @@ -518,7 +520,7 @@ void PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { E->setArgument(Reader.ReadSubExpr()); ++Idx; } else { - E->setArgument(Reader.GetTypeSourceInfo(Record, Idx)); + E->setArgument(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); } E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); @@ -597,7 +599,7 @@ void PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) { void PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) { VisitCastExpr(E); - E->setTypeInfoAsWritten(Reader.GetTypeSourceInfo(Record, Idx)); + E->setTypeInfoAsWritten(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); } void PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) { @@ -609,7 +611,7 @@ void PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) { void PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { VisitExpr(E); E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); + E->setTypeSourceInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); E->setInitializer(Reader.ReadSubExpr()); E->setFileScope(Record[Idx++]); } @@ -783,7 +785,7 @@ void PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) { void PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { VisitExpr(E); - E->setEncodedTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); + E->setEncodedTypeSourceInfo(Reader.GetTypeSourceInfo(DeclsCursor,Record,Idx)); E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); } @@ -844,7 +846,7 @@ void PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) { break; case ObjCMessageExpr::Class: - E->setClassReceiver(Reader.GetTypeSourceInfo(Record, Idx)); + E->setClassReceiver(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); break; case ObjCMessageExpr::SuperClass: @@ -1013,7 +1015,8 @@ void PCHStmtReader::VisitCXXTypeidExpr(CXXTypeidExpr *E) { VisitExpr(E); E->setSourceRange(Reader.ReadSourceRange(Record, Idx)); if (E->isTypeOperand()) { // typeid(int) - E->setTypeOperandSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); + E->setTypeOperandSourceInfo( + Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); return; } @@ -1107,7 +1110,7 @@ void PCHStmtReader::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { E->setOperatorLoc(Reader.ReadSourceLocation(Record, Idx)); E->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx)); E->setQualifierRange(Reader.ReadSourceRange(Record, Idx)); - E->setScopeTypeInfo(Reader.GetTypeSourceInfo(Record, Idx)); + E->setScopeTypeInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); E->setColonColonLoc(Reader.ReadSourceLocation(Record, Idx)); E->setTildeLoc(Reader.ReadSourceLocation(Record, Idx)); @@ -1115,7 +1118,7 @@ void PCHStmtReader::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { if (II) E->setDestroyedType(II, Reader.ReadSourceLocation(Record, Idx)); else - E->setDestroyedType(Reader.GetTypeSourceInfo(Record, Idx)); + E->setDestroyedType(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); } void PCHStmtReader::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { @@ -1232,12 +1235,11 @@ void PCHStmtReader::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { E->QueriedType = Reader.GetType(Record[Idx++]); } -Stmt *PCHReader::ReadStmt() { +Stmt *PCHReader::ReadStmt(llvm::BitstreamCursor &Cursor) { switch (ReadingKind) { case Read_Decl: case Read_Type: - // Read a statement from the current DeclCursor. - return ReadStmtFromStream(Chain[0]->DeclsCursor); + return ReadStmtFromStream(Cursor); case Read_Stmt: return ReadSubStmt(); } @@ -1246,8 +1248,8 @@ Stmt *PCHReader::ReadStmt() { return 0; } -Expr *PCHReader::ReadExpr() { - return cast_or_null(ReadStmt()); +Expr *PCHReader::ReadExpr(llvm::BitstreamCursor &Cursor) { + return cast_or_null(ReadStmt(Cursor)); } Expr *PCHReader::ReadSubExpr() { @@ -1271,7 +1273,7 @@ Stmt *PCHReader::ReadStmtFromStream(llvm::BitstreamCursor &Cursor) { RecordData Record; unsigned Idx; - PCHStmtReader Reader(*this, Record, Idx); + PCHStmtReader Reader(*this, Cursor, Record, Idx); Stmt::EmptyShell Empty; while (true) { @@ -1460,7 +1462,7 @@ Stmt *PCHReader::ReadStmtFromStream(llvm::BitstreamCursor &Cursor) { ArgInfo.setLAngleLoc(ReadSourceLocation(Record, Idx)); ArgInfo.setRAngleLoc(ReadSourceLocation(Record, Idx)); for (unsigned i = 0; i != NumTemplateArgs; ++i) - ArgInfo.addArgument(ReadTemplateArgumentLoc(Record, Idx)); + ArgInfo.addArgument(ReadTemplateArgumentLoc(Cursor, Record, Idx)); } NamedDecl *FoundD = cast_or_null(GetDecl(Record[Idx++])); diff --git a/test/PCH/Inputs/chain-trivial1.h b/test/PCH/Inputs/chain-trivial1.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/PCH/Inputs/chain-trivial2.h b/test/PCH/Inputs/chain-trivial2.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/PCH/chain-trivial.c b/test/PCH/chain-trivial.c new file mode 100644 index 0000000000..c78b0e44ef --- /dev/null +++ b/test/PCH/chain-trivial.c @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-pch -o %t1 %S/Inputs/chain-trivial1.h +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-pch -o %t2 -include-pch %t1 -chained-pch %S/Inputs/chain-trivial2.h +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ast-print -include-pch %t2 %s | FileCheck %s +// CHECK: struct __va_list_tag {