diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -32,6 +32,7 @@ #include "clang/Serialization/ModuleFile.h" #include "clang/Serialization/ModuleFileExtension.h" #include "clang/Serialization/ModuleManager.h" +#include "clang/Serialization/SourceLocationEncoding.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" @@ -2155,16 +2156,18 @@ /// Read a source location from raw form and return it in its /// originating module file's source location space. - SourceLocation - ReadUntranslatedSourceLocation(SourceLocation::UIntTy Raw) const { - return SourceLocation::getFromRawEncoding((Raw >> 1) | - (Raw << (8 * sizeof(Raw) - 1))); + SourceLocation ReadUntranslatedSourceLocation( + uint64_t Raw, serialization::SourceLocationSeq *Seq = nullptr) const { + return Seq ? Seq->decode(Raw) + : SourceLocation::getFromRawEncoding( + serialization::SourceLocations::decodeRaw(Raw)); } /// Read a source location from raw form. - SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, - SourceLocation::UIntTy Raw) const { - SourceLocation Loc = ReadUntranslatedSourceLocation(Raw); + SourceLocation + ReadSourceLocation(ModuleFile &ModuleFile, uint64_t Raw, + serialization::SourceLocationSeq *Seq = nullptr) const { + SourceLocation Loc = ReadUntranslatedSourceLocation(Raw, Seq); return TranslateSourceLocation(ModuleFile, Loc); } @@ -2183,15 +2186,21 @@ } /// Read a source location. - SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, - const RecordDataImpl &Record, - unsigned &Idx) { - return ReadSourceLocation(ModuleFile, Record[Idx++]); + SourceLocation + ReadSourceLocation(ModuleFile &ModuleFile, const RecordDataImpl &Record, + unsigned &Idx, + serialization::SourceLocationSeq *Seq = nullptr) { + return ReadSourceLocation(ModuleFile, Record[Idx++], Seq); } /// Read a source range. - SourceRange ReadSourceRange(ModuleFile &F, - const RecordData &Record, unsigned &Idx); + SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record, + unsigned &Idx, + serialization::SourceLocationSeq *Seq = nullptr) { + SourceLocation beg = ReadSourceLocation(F, Record, Idx, Seq); + SourceLocation end = ReadSourceLocation(F, Record, Idx, Seq); + return SourceRange(beg, end); + } // Read a string static std::string ReadString(const RecordData &Record, unsigned &Idx); diff --git a/clang/include/clang/Serialization/ASTRecordReader.h b/clang/include/clang/Serialization/ASTRecordReader.h --- a/clang/include/clang/Serialization/ASTRecordReader.h +++ b/clang/include/clang/Serialization/ASTRecordReader.h @@ -148,7 +148,8 @@ /// Reads a TemplateArgumentLocInfo appropriate for the /// given TemplateArgument kind, advancing Idx. TemplateArgumentLocInfo - readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind); + readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, + serialization::SourceLocationSeq *Seq = nullptr); /// Reads a TemplateArgumentLoc, advancing Idx. TemplateArgumentLoc readTemplateArgumentLoc(); @@ -157,10 +158,10 @@ readASTTemplateArgumentListInfo(); /// Reads a declarator info from the given record, advancing Idx. - TypeSourceInfo *readTypeSourceInfo(); + TypeSourceInfo *readTypeSourceInfo(serialization::SourceLocationSeq *Seq = nullptr); /// Reads the location information for a type. - void readTypeLoc(TypeLoc TL); + void readTypeLoc(TypeLoc TL, serialization::SourceLocationSeq *Seq = nullptr); /// Map a local type ID within a given AST file to a global type ID. @@ -217,15 +218,19 @@ /// Read a declaration name, advancing Idx. // DeclarationName readDeclarationName(); (inherited) - DeclarationNameLoc readDeclarationNameLoc(DeclarationName Name); + DeclarationNameLoc + readDeclarationNameLoc(DeclarationName Name, + serialization::SourceLocationSeq *Seq = nullptr); DeclarationNameInfo readDeclarationNameInfo(); - void readQualifierInfo(QualifierInfo &Info); + void readQualifierInfo(QualifierInfo &Info, + serialization::SourceLocationSeq *Seq = nullptr); /// Return a nested name specifier, advancing Idx. // NestedNameSpecifier *readNestedNameSpecifier(); (inherited) - NestedNameSpecifierLoc readNestedNameSpecifierLoc(); + NestedNameSpecifierLoc + readNestedNameSpecifierLoc(serialization::SourceLocationSeq *Seq = nullptr); /// Read a template name, advancing Idx. // TemplateName readTemplateName(); (inherited) @@ -242,7 +247,8 @@ } /// Read a template parameter list, advancing Idx. - TemplateParameterList *readTemplateParameterList(); + TemplateParameterList * + readTemplateParameterList(serialization::SourceLocationSeq *Seq = nullptr); /// Read a template argument array, advancing Idx. void readTemplateArgumentList(SmallVectorImpl &TemplArgs, @@ -271,13 +277,14 @@ void readOMPChildren(OMPChildren *Data); /// Read a source location, advancing Idx. - SourceLocation readSourceLocation() { - return Reader->ReadSourceLocation(*F, Record, Idx); + SourceLocation + readSourceLocation(serialization::SourceLocationSeq *Seq = nullptr) { + return Reader->ReadSourceLocation(*F, Record, Idx, Seq); } /// Read a source range, advancing Idx. - SourceRange readSourceRange() { - return Reader->ReadSourceRange(*F, Record, Idx); + SourceRange readSourceRange(serialization::SourceLocationSeq *Seq = nullptr) { + return Reader->ReadSourceRange(*F, Record, Idx, Seq); } /// Read an arbitrary constant value, advancing Idx. diff --git a/clang/include/clang/Serialization/ASTRecordWriter.h b/clang/include/clang/Serialization/ASTRecordWriter.h --- a/clang/include/clang/Serialization/ASTRecordWriter.h +++ b/clang/include/clang/Serialization/ASTRecordWriter.h @@ -131,16 +131,18 @@ void AddFunctionDefinition(const FunctionDecl *FD); /// Emit a source location. - void AddSourceLocation(SourceLocation Loc) { - return Writer->AddSourceLocation(Loc, *Record); + void AddSourceLocation(SourceLocation Loc, + serialization::SourceLocationSeq *Seq = nullptr) { + return Writer->AddSourceLocation(Loc, *Record, Seq); } void writeSourceLocation(SourceLocation Loc) { AddSourceLocation(Loc); } /// Emit a source range. - void AddSourceRange(SourceRange Range) { - return Writer->AddSourceRange(Range, *Record); + void AddSourceRange(SourceRange Range, + serialization::SourceLocationSeq *Seq = nullptr) { + return Writer->AddSourceRange(Range, *Record, Seq); } void writeBool(bool Value) { @@ -203,14 +205,17 @@ } /// Emits a reference to a declarator info. - void AddTypeSourceInfo(TypeSourceInfo *TInfo); + void AddTypeSourceInfo(TypeSourceInfo *TInfo, + serialization::SourceLocationSeq *Seq = nullptr); /// Emits source location information for a type. Does not emit the type. - void AddTypeLoc(TypeLoc TL); + void AddTypeLoc(TypeLoc TL, serialization::SourceLocationSeq *Seq = nullptr); /// Emits a template argument location info. - void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, - const TemplateArgumentLocInfo &Arg); + void + AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, + const TemplateArgumentLocInfo &Arg, + serialization::SourceLocationSeq *Seq = nullptr); /// Emits a template argument location. void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg); @@ -233,10 +238,12 @@ } void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, - DeclarationName Name); + DeclarationName Name, + serialization::SourceLocationSeq *Seq = nullptr); void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo); - void AddQualifierInfo(const QualifierInfo &Info); + void AddQualifierInfo(const QualifierInfo &Info, + serialization::SourceLocationSeq *Seq = nullptr); /// Emit a nested name specifier. void AddNestedNameSpecifier(NestedNameSpecifier *NNS) { @@ -244,7 +251,9 @@ } /// Emit a nested name specifier with source-location information. - void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); + void + AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, + serialization::SourceLocationSeq *Seq = nullptr); /// Emit a template name. void AddTemplateName(TemplateName Name) { @@ -257,7 +266,9 @@ } /// Emit a template parameter list. - void AddTemplateParameterList(const TemplateParameterList *TemplateParams); + void + AddTemplateParameterList(const TemplateParameterList *TemplateParams, + serialization::SourceLocationSeq *Seq = nullptr); /// Emit a template argument list. void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs); diff --git a/clang/include/clang/Serialization/ASTWriter.h b/clang/include/clang/Serialization/ASTWriter.h --- a/clang/include/clang/Serialization/ASTWriter.h +++ b/clang/include/clang/Serialization/ASTWriter.h @@ -25,6 +25,7 @@ #include "clang/Serialization/ASTBitCodes.h" #include "clang/Serialization/ASTDeserializationListener.h" #include "clang/Serialization/PCHContainerOperations.h" +#include "clang/Serialization/SourceLocationEncoding.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" @@ -581,10 +582,20 @@ RecordDataImpl &Record); /// Emit a source location. - void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record); + void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record, + serialization::SourceLocationSeq *Seq = nullptr) { + Record.push_back( + Seq ? Seq->encode(Loc) + : serialization::SourceLocations::encodeRaw(Loc.getRawEncoding())); + } /// Emit a source range. - void AddSourceRange(SourceRange Range, RecordDataImpl &Record); + void AddSourceRange(SourceRange Range, RecordDataImpl &Record, + serialization::SourceLocationSeq *Seq = nullptr) { + // FIXME: add fallback sequence? + AddSourceLocation(Range.getBegin(), Record, Seq); + AddSourceLocation(Range.getEnd(), Record, Seq); + } /// Emit a reference to an identifier. void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record); diff --git a/clang/include/clang/Serialization/SourceLocationEncoding.h b/clang/include/clang/Serialization/SourceLocationEncoding.h new file mode 100644 --- /dev/null +++ b/clang/include/clang/Serialization/SourceLocationEncoding.h @@ -0,0 +1,102 @@ +//===--- SourceLocationEncoding.h - Small serialized locations --*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +#include "clang/Basic/SourceLocation.h" + +#ifndef LLVM_CLANG_SERIALIZATION_SOURCELOCATIONENCODING_H +#define LLVM_CLANG_SERIALIZATION_SOURCELOCATIONENCODING_H + +namespace clang { +namespace serialization { + +/// Serialized encoding of SourceLocations without context. +/// Optimized to have small unsigned values (=> small after VBR encoding). +/// +// Macro locations have the top bit set, we rotate by one so it is the low bit. +struct SourceLocations { + using UIntTy = SourceLocation::UIntTy; + + static UIntTy encodeRaw(UIntTy Raw) { + return (Raw << 1) | (Raw >> (CHAR_BIT * sizeof(Raw) - 1)); + } + static UIntTy decodeRaw(UIntTy Raw) { + return (Raw >> 1) | (Raw << (CHAR_BIT * sizeof(Raw) - 1)); + } +}; + +/// Serialized encoding of a fixed-length sequence of SourceLocations. +/// +/// Optimized to have small values in common cases: locations are null, or +/// locations within the sequence are similar. +/// +/// The sequence has a "state": initially zero, later the raw value of the last +/// non-null location encoded. +/// +/// Each subsequent location can be encoded: +/// - directly: as SourceLocations::encodeRaw(value) (if state is zero) +/// - absolutely: as SourceLocations::encodeRaw(value) << 1 (low bit 0) +/// - relatively: as zigzag(value - state)<<1 | 1 (low bit 1) +class SourceLocationSeq { + using UIntTy = SourceLocation::UIntTy; + UIntTy State = 0; + using EncodedTy = uint64_t; + static_assert(sizeof(EncodedTy) > sizeof(UIntTy), "Need one extra bit!"); + constexpr static unsigned LocBits = sizeof(UIntTy) * CHAR_BIT; + constexpr static unsigned MaxDeltaEncodingBits = 18; // 3xVBR6 + +public: + EncodedTy encodeRaw(UIntTy Raw) { + if (Raw == 0) + return 0; + UIntTy State = this->State; + this->State = Raw; + + if (State == 0) + return SourceLocations::encodeRaw(Raw); // direct encoding + + UIntTy Delta = Raw - State; // two's complement! + UIntTy Sign = (Delta & (1 << (LocBits - 1))) ? UIntTy(-1) : UIntTy(0); + UIntTy ZigZag = Sign ^ (Delta << 1); + if (ZigZag < (1 << (MaxDeltaEncodingBits - 1))) + return (ZigZag << 1) | 1; // relative + return EncodedTy(SourceLocations::encodeRaw(Raw)) << 1; // absolute + } + UIntTy decodeRaw(EncodedTy Encoded) { + if (State == 0) + return State = SourceLocations::decodeRaw(Encoded); // direct encoding + if (Encoded == 0) + return 0; + if ((Encoded & 1) == 0) + return State = SourceLocations::decodeRaw(Encoded >> 1); // absolute + UIntTy ZigZag = Encoded >> 1; + UIntTy Delta = (ZigZag >> 1) ^ -(ZigZag & 1); // two's complement! + return State = State + Delta; // relative + } + + SourceLocation decode(EncodedTy Encoded) { + return SourceLocation::getFromRawEncoding(decodeRaw(Encoded)); + } + EncodedTy encode(SourceLocation Loc) { + return encodeRaw(Loc.getRawEncoding()); + } +}; + +class SourceLocationSeqRoot { + SourceLocationSeq *Seq; + SourceLocationSeq Local; + +public: + SourceLocationSeqRoot(SourceLocationSeq *Parent = nullptr) + : Seq(Parent ? Parent : &Local) {} + + operator SourceLocationSeq *() const { return Seq; } +}; + +} // namespace serialization +} // namespace clang +#endif diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -121,6 +121,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/SaveAndRestore.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/Timer.h" #include "llvm/Support/VersionTuple.h" #include "llvm/Support/raw_ostream.h" @@ -1589,14 +1590,13 @@ } case SM_SLOC_EXPANSION_ENTRY: { - SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); - SourceMgr.createExpansionLoc(SpellingLoc, - ReadSourceLocation(*F, Record[2]), - ReadSourceLocation(*F, Record[3]), - Record[5], - Record[4], - ID, - BaseOffset + Record[0]); + SourceLocationSeqRoot Seq; + SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1], Seq); + SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2], Seq); + SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3], Seq); + SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd, + Record[5], Record[4], ID, + BaseOffset + Record[0]); break; } } @@ -1746,9 +1746,10 @@ return Macro; unsigned NextIndex = 1; // Skip identifier ID. - SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); + SourceLocationSeqRoot Seq; + SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex, Seq); MacroInfo *MI = PP.AllocateMacroInfo(Loc); - MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); + MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex, Seq)); MI->setIsUsed(Record[NextIndex++]); MI->setUsedForHeaderGuard(Record[NextIndex++]); MacroTokens = MI->allocateTokens(Record[NextIndex++], @@ -6463,17 +6464,32 @@ class TypeLocReader : public TypeLocVisitor { ASTRecordReader &Reader; + SourceLocationSeq *Seq; - SourceLocation readSourceLocation() { - return Reader.readSourceLocation(); + SourceLocation + readSourceLocation() { + //auto Raw = Reader.peekInt(); + auto Loc = Reader.readSourceLocation(Seq); + //llvm::errs() << "read: " << llvm::to_hexString(Raw) << " => " << llvm::to_hexString(Loc.getRawEncoding()) << "\n"; + return Loc; } - TypeSourceInfo *GetTypeSourceInfo() { - return Reader.readTypeSourceInfo(); + SourceRange + readSourceRange() { + SourceLocation Begin = readSourceLocation(); + SourceLocation End = readSourceLocation(); + return SourceRange(Begin, End); } + TypeSourceInfo *GetTypeSourceInfo() { return Reader.readTypeSourceInfo(Seq); } + NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { - return Reader.readNestedNameSpecifierLoc(); + return Reader.readNestedNameSpecifierLoc(Seq); + } + + TemplateArgumentLocInfo + readTemplateArgumentLocInfo(TemplateArgument::ArgKind K) { + return Reader.readTemplateArgumentLocInfo(K, Seq); } Attr *ReadAttr() { @@ -6481,7 +6497,11 @@ } public: - TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} + TypeLocReader(ASTRecordReader &Reader, SourceLocationSeq *Seq) + : Reader(Reader), Seq(Seq) { + //llvm::errs() << "locreader\n"; + } + //~TypeLocReader() { llvm::errs() << "~locreader\n"; } // We want compile-time assurance that we've enumerated all of // these, so unfortunately we have to declare them first, then @@ -6578,7 +6598,7 @@ DependentAddressSpaceTypeLoc TL) { TL.setAttrNameLoc(readSourceLocation()); - TL.setAttrOperandParensRange(Reader.readSourceRange()); + TL.setAttrOperandParensRange(readSourceRange()); TL.setAttrExprOperand(Reader.readExpr()); } @@ -6602,7 +6622,7 @@ void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { TL.setAttrNameLoc(readSourceLocation()); - TL.setAttrOperandParensRange(Reader.readSourceRange()); + TL.setAttrOperandParensRange(readSourceRange()); TL.setAttrRowOperand(Reader.readExpr()); TL.setAttrColumnOperand(Reader.readExpr()); } @@ -6610,7 +6630,7 @@ void TypeLocReader::VisitDependentSizedMatrixTypeLoc( DependentSizedMatrixTypeLoc TL) { TL.setAttrNameLoc(readSourceLocation()); - TL.setAttrOperandParensRange(Reader.readSourceRange()); + TL.setAttrOperandParensRange(readSourceRange()); TL.setAttrRowOperand(Reader.readExpr()); TL.setAttrColumnOperand(Reader.readExpr()); } @@ -6619,7 +6639,7 @@ TL.setLocalRangeBegin(readSourceLocation()); TL.setLParenLoc(readSourceLocation()); TL.setRParenLoc(readSourceLocation()); - TL.setExceptionSpecRange(Reader.readSourceRange()); + TL.setExceptionSpecRange(readSourceRange()); TL.setLocalRangeEnd(readSourceLocation()); for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { TL.setParam(i, Reader.readDeclAs()); @@ -6681,8 +6701,8 @@ TL.setLAngleLoc(readSourceLocation()); TL.setRAngleLoc(readSourceLocation()); for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) - TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( - TL.getTypePtr()->getArg(i).getKind())); + TL.setArgLocInfo( + i, readTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind())); } if (Reader.readBool()) TL.setRParenLoc(readSourceLocation()); @@ -6731,9 +6751,7 @@ TL.setRAngleLoc(readSourceLocation()); for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) TL.setArgLocInfo( - i, - Reader.readTemplateArgumentLocInfo( - TL.getTypePtr()->getArg(i).getKind())); + i, readTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind())); } void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { @@ -6766,9 +6784,7 @@ TL.setRAngleLoc(readSourceLocation()); for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) TL.setArgLocInfo( - I, - Reader.readTemplateArgumentLocInfo( - TL.getTypePtr()->getArg(I).getKind())); + I, readTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind())); } void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { @@ -6822,20 +6838,25 @@ TL.setNameLoc(readSourceLocation()); } - -void ASTRecordReader::readTypeLoc(TypeLoc TL) { - TypeLocReader TLR(*this); - for (; !TL.isNull(); TL = TL.getNextTypeLoc()) +void ASTRecordReader::readTypeLoc(TypeLoc TL, + serialization::SourceLocationSeq *OuterSeq) { + SourceLocationSeqRoot Seq(OuterSeq); + //llvm::errs() << "reading type loc nested=" << bool(Seq)<< "\n"; + TypeLocReader TLR(*this, Seq); + for (; !TL.isNull(); TL = TL.getNextTypeLoc()) { TLR.Visit(TL); + //if (!TL.isNull()) + // llvm::errs() << "read " << TL.getType()->getTypeClassName() << "\n"; + } } -TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { +TypeSourceInfo *ASTRecordReader::readTypeSourceInfo(SourceLocationSeq *Seq) { QualType InfoTy = readType(); if (InfoTy.isNull()) return nullptr; TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); - readTypeLoc(TInfo->getTypeLoc()); + readTypeLoc(TInfo->getTypeLoc(), Seq); return TInfo; } @@ -7143,23 +7164,24 @@ } TemplateArgumentLocInfo -ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { +ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, + SourceLocationSeq *Seq) { switch (Kind) { case TemplateArgument::Expression: return readExpr(); case TemplateArgument::Type: - return readTypeSourceInfo(); + return readTypeSourceInfo(Seq); case TemplateArgument::Template: { NestedNameSpecifierLoc QualifierLoc = - readNestedNameSpecifierLoc(); - SourceLocation TemplateNameLoc = readSourceLocation(); + readNestedNameSpecifierLoc(Seq); + SourceLocation TemplateNameLoc = readSourceLocation(Seq); return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, TemplateNameLoc, SourceLocation()); } case TemplateArgument::TemplateExpansion: { - NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); - SourceLocation TemplateNameLoc = readSourceLocation(); - SourceLocation EllipsisLoc = readSourceLocation(); + NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(Seq); + SourceLocation TemplateNameLoc = readSourceLocation(Seq); + SourceLocation EllipsisLoc = readSourceLocation(Seq); return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, TemplateNameLoc, EllipsisLoc); } @@ -8764,19 +8786,20 @@ } DeclarationNameLoc -ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { +ASTRecordReader::readDeclarationNameLoc(DeclarationName Name, + SourceLocationSeq *Seq) { switch (Name.getNameKind()) { case DeclarationName::CXXConstructorName: case DeclarationName::CXXDestructorName: case DeclarationName::CXXConversionFunctionName: - return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); + return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo(Seq)); case DeclarationName::CXXOperatorName: - return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); + return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange(Seq)); case DeclarationName::CXXLiteralOperatorName: return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( - readSourceLocation()); + readSourceLocation(Seq)); case DeclarationName::Identifier: case DeclarationName::ObjCZeroArgSelector: @@ -8797,23 +8820,24 @@ return NameInfo; } -void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { - Info.QualifierLoc = readNestedNameSpecifierLoc(); +void ASTRecordReader::readQualifierInfo(QualifierInfo &Info, + SourceLocationSeq *Seq) { + Info.QualifierLoc = readNestedNameSpecifierLoc(Seq); unsigned NumTPLists = readInt(); Info.NumTemplParamLists = NumTPLists; if (NumTPLists) { Info.TemplParamLists = new (getContext()) TemplateParameterList *[NumTPLists]; for (unsigned i = 0; i != NumTPLists; ++i) - Info.TemplParamLists[i] = readTemplateParameterList(); + Info.TemplParamLists[i] = readTemplateParameterList(Seq); } } TemplateParameterList * -ASTRecordReader::readTemplateParameterList() { - SourceLocation TemplateLoc = readSourceLocation(); - SourceLocation LAngleLoc = readSourceLocation(); - SourceLocation RAngleLoc = readSourceLocation(); +ASTRecordReader::readTemplateParameterList(SourceLocationSeq *Seq) { + SourceLocation TemplateLoc = readSourceLocation(Seq); + SourceLocation LAngleLoc = readSourceLocation(Seq); + SourceLocation RAngleLoc = readSourceLocation(Seq); unsigned NumParams = readInt(); SmallVector Params; @@ -8930,7 +8954,8 @@ } NestedNameSpecifierLoc -ASTRecordReader::readNestedNameSpecifierLoc() { +ASTRecordReader::readNestedNameSpecifierLoc(SourceLocationSeq *OuterSeq) { + SourceLocationSeqRoot Seq(OuterSeq); ASTContext &Context = getContext(); unsigned N = readInt(); NestedNameSpecifierLocBuilder Builder; @@ -8939,21 +8964,21 @@ switch (Kind) { case NestedNameSpecifier::Identifier: { IdentifierInfo *II = readIdentifier(); - SourceRange Range = readSourceRange(); + SourceRange Range = readSourceRange(Seq); Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); break; } case NestedNameSpecifier::Namespace: { NamespaceDecl *NS = readDeclAs(); - SourceRange Range = readSourceRange(); + SourceRange Range = readSourceRange(Seq); Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); break; } case NestedNameSpecifier::NamespaceAlias: { NamespaceAliasDecl *Alias = readDeclAs(); - SourceRange Range = readSourceRange(); + SourceRange Range = readSourceRange(Seq); Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); break; } @@ -8961,10 +8986,10 @@ case NestedNameSpecifier::TypeSpec: case NestedNameSpecifier::TypeSpecWithTemplate: { bool Template = readBool(); - TypeSourceInfo *T = readTypeSourceInfo(); + TypeSourceInfo *T = readTypeSourceInfo(Seq); if (!T) return NestedNameSpecifierLoc(); - SourceLocation ColonColonLoc = readSourceLocation(); + SourceLocation ColonColonLoc = readSourceLocation(Seq); // FIXME: 'template' keyword location not saved anywhere, so we fake it. Builder.Extend(Context, @@ -8974,14 +8999,14 @@ } case NestedNameSpecifier::Global: { - SourceLocation ColonColonLoc = readSourceLocation(); + SourceLocation ColonColonLoc = readSourceLocation(Seq); Builder.MakeGlobal(Context, ColonColonLoc); break; } case NestedNameSpecifier::Super: { CXXRecordDecl *RD = readDeclAs(); - SourceRange Range = readSourceRange(); + SourceRange Range = readSourceRange(Seq); Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); break; } @@ -8991,14 +9016,6 @@ return Builder.getWithLocInContext(Context); } -SourceRange -ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, - unsigned &Idx) { - SourceLocation beg = ReadSourceLocation(F, Record, Idx); - SourceLocation end = ReadSourceLocation(F, Record, Idx); - return SourceRange(beg, end); -} - /// Read a floating-point value llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { return llvm::APFloat(Sem, readAPInt()); diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -112,16 +112,16 @@ return Local ? Record.getGlobalBitOffset(Local) : 0; } - SourceLocation readSourceLocation() { - return Record.readSourceLocation(); + SourceLocation readSourceLocation(SourceLocationSeq *Seq = nullptr) { + return Record.readSourceLocation(Seq); } - SourceRange readSourceRange() { - return Record.readSourceRange(); + SourceRange readSourceRange(SourceLocationSeq *Seq = nullptr) { + return Record.readSourceRange(Seq); } - TypeSourceInfo *readTypeSourceInfo() { - return Record.readTypeSourceInfo(); + TypeSourceInfo *readTypeSourceInfo(SourceLocationSeq *Seq = nullptr) { + return Record.readTypeSourceInfo(Seq); } serialization::DeclID readDeclID() { @@ -361,7 +361,8 @@ void VisitValueDecl(ValueDecl *VD); void VisitEnumConstantDecl(EnumConstantDecl *ECD); void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); - void VisitDeclaratorDecl(DeclaratorDecl *DD); + void VisitDeclaratorDecl(DeclaratorDecl *DD, + SourceLocationSeq *Seq = nullptr); void VisitFunctionDecl(FunctionDecl *FD); void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD); void VisitCXXMethodDecl(CXXMethodDecl *D); @@ -857,12 +858,13 @@ mergeMergeable(ECD); } -void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) { +void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD, + SourceLocationSeq *Seq) { VisitValueDecl(DD); - DD->setInnerLocStart(readSourceLocation()); + DD->setInnerLocStart(readSourceLocation(Seq)); if (Record.readInt()) { // hasExtInfo auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo(); - Record.readQualifierInfo(*Info); + Record.readQualifierInfo(*Info, Seq); Info->TrailingRequiresClause = Record.readExpr(); DD->DeclInfo = Info; } @@ -873,8 +875,9 @@ } void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) { + SourceLocationSeqRoot Seq; RedeclarableResult Redecl = VisitRedeclarable(FD); - VisitDeclaratorDecl(FD); + VisitDeclaratorDecl(FD, Seq); // Attach a type to this function. Use the real type if possible, but fall // back to the type as written if it involves a deduced return type. @@ -890,7 +893,7 @@ } DeferredTypeID = 0; - FD->DNLoc = Record.readDeclarationNameLoc(FD->getDeclName()); + FD->DNLoc = Record.readDeclarationNameLoc(FD->getDeclName(), Seq); FD->IdentifierNamespace = Record.readInt(); // FunctionDecl's body is handled last at ASTDeclReader::Visit, @@ -919,7 +922,7 @@ FD->setLateTemplateParsed(Record.readInt()); FD->setCachedLinkage(static_cast(Record.readInt())); - FD->EndRangeLoc = readSourceLocation(); + FD->EndRangeLoc = readSourceLocation(Seq); FD->ODRHash = Record.readInt(); FD->setHasODRHash(true); @@ -948,7 +951,7 @@ case FunctionDecl::TK_MemberSpecialization: { auto *InstFD = readDeclAs(); auto TSK = (TemplateSpecializationKind)Record.readInt(); - SourceLocation POI = readSourceLocation(); + SourceLocation POI = readSourceLocation(Seq); FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK); FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI); mergeRedeclarable(FD, Redecl); @@ -970,13 +973,13 @@ unsigned NumTemplateArgLocs = Record.readInt(); TemplArgLocs.reserve(NumTemplateArgLocs); for (unsigned i = 0; i != NumTemplateArgLocs; ++i) - TemplArgLocs.push_back(Record.readTemplateArgumentLoc()); + TemplArgLocs.push_back(Record.readTemplateArgumentLoc()); // FIXME: seq - LAngleLoc = readSourceLocation(); - RAngleLoc = readSourceLocation(); + LAngleLoc = readSourceLocation(Seq); + RAngleLoc = readSourceLocation(Seq); } - SourceLocation POI = readSourceLocation(); + SourceLocation POI = readSourceLocation(Seq); ASTContext &C = Reader.getContext(); TemplateArgumentList *TemplArgList @@ -989,7 +992,7 @@ if (Record.readInt()) { auto *FD = readDeclAs(); auto TSK = (TemplateSpecializationKind)Record.readInt(); - SourceLocation POI = readSourceLocation(); + SourceLocation POI = readSourceLocation(Seq); MSInfo = new (C) MemberSpecializationInfo(FD, TSK); MSInfo->setPointOfInstantiation(POI); @@ -1038,9 +1041,9 @@ TemplateArgumentListInfo TemplArgs; unsigned NumArgs = Record.readInt(); while (NumArgs--) - TemplArgs.addArgument(Record.readTemplateArgumentLoc()); - TemplArgs.setLAngleLoc(readSourceLocation()); - TemplArgs.setRAngleLoc(readSourceLocation()); + TemplArgs.addArgument(Record.readTemplateArgumentLoc()); // FIXME: seq + TemplArgs.setLAngleLoc(readSourceLocation(Seq)); + TemplArgs.setRAngleLoc(readSourceLocation(Seq)); FD->setDependentTemplateSpecialization(Reader.getContext(), TemplDecls, TemplArgs); diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -72,8 +72,8 @@ ASTRecordReader &Record; llvm::BitstreamCursor &DeclsCursor; - SourceLocation readSourceLocation() { - return Record.readSourceLocation(); + SourceLocation readSourceLocation(SourceLocationSeq *Seq = nullptr) { + return Record.readSourceLocation(Seq); } SourceRange readSourceRange() { @@ -1110,24 +1110,26 @@ } void ASTStmtReader::VisitConditionalOperator(ConditionalOperator *E) { + serialization::SourceLocationSeq Seq; VisitExpr(E); E->SubExprs[ConditionalOperator::COND] = Record.readSubExpr(); E->SubExprs[ConditionalOperator::LHS] = Record.readSubExpr(); E->SubExprs[ConditionalOperator::RHS] = Record.readSubExpr(); - E->QuestionLoc = readSourceLocation(); - E->ColonLoc = readSourceLocation(); + E->QuestionLoc = readSourceLocation(&Seq); + E->ColonLoc = readSourceLocation(&Seq); } void ASTStmtReader::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { + serialization::SourceLocationSeq Seq; VisitExpr(E); E->OpaqueValue = cast(Record.readSubExpr()); E->SubExprs[BinaryConditionalOperator::COMMON] = Record.readSubExpr(); E->SubExprs[BinaryConditionalOperator::COND] = Record.readSubExpr(); E->SubExprs[BinaryConditionalOperator::LHS] = Record.readSubExpr(); E->SubExprs[BinaryConditionalOperator::RHS] = Record.readSubExpr(); - E->QuestionLoc = readSourceLocation(); - E->ColonLoc = readSourceLocation(); + E->QuestionLoc = readSourceLocation(&Seq); + E->ColonLoc = readSourceLocation(&Seq); } void ASTStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) { @@ -1141,9 +1143,10 @@ } void ASTStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) { + serialization::SourceLocationSeq Seq; VisitExplicitCastExpr(E); - E->setLParenLoc(readSourceLocation()); - E->setRParenLoc(readSourceLocation()); + E->setLParenLoc(readSourceLocation(&Seq)); + E->setRParenLoc(readSourceLocation(&Seq)); } void ASTStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -100,6 +100,7 @@ #include "llvm/Support/OnDiskHashTable.h" #include "llvm/Support/Path.h" #include "llvm/Support/SHA1.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/VersionTuple.h" #include "llvm/Support/raw_ostream.h" #include @@ -246,9 +247,36 @@ class TypeLocWriter : public TypeLocVisitor { ASTRecordWriter &Record; + SourceLocationSeq *Seq; + + void AddSourceLocation(SourceLocation Loc) { + //llvm::errs() << "wrote: " << llvm::to_hexString(Loc.getRawEncoding()); + Record.AddSourceLocation(Loc, Seq); + //llvm::errs() << " => " << llvm::to_hexString(Record.getRecordData().back()) << "\n"; + } + void AddSourceRange(SourceRange Range) { + AddSourceLocation(Range.getBegin()); + AddSourceLocation(Range.getEnd()); + } + void AddTypeSourceInfo(TypeSourceInfo *TInfo) { + Record.AddTypeSourceInfo(TInfo, Seq); + } + void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { + Record.AddNestedNameSpecifierLoc(NNS, Seq); + } + void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, + const TemplateArgumentLocInfo &Arg) { + Record.AddTemplateArgumentLocInfo(Kind, Arg, Seq); + } public: - TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {} + TypeLocWriter(ASTRecordWriter &Record, SourceLocationSeq *Seq) + : Record(Record), Seq(Seq) { + //llvm::errs() << "locwriter\n"; + } + ~TypeLocWriter() { + //llvm::errs() << "~locwriter\n"; + } #define ABSTRACT_TYPELOC(CLASS, PARENT) #define TYPELOC(CLASS, PARENT) \ @@ -266,7 +294,7 @@ } void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { - Record.AddSourceLocation(TL.getBuiltinLoc()); + AddSourceLocation(TL.getBuiltinLoc()); if (TL.needsExtraLocalData()) { Record.push_back(TL.getWrittenTypeSpec()); Record.push_back(static_cast(TL.getWrittenSignSpec())); @@ -276,11 +304,11 @@ } void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { - Record.AddSourceLocation(TL.getStarLoc()); + AddSourceLocation(TL.getStarLoc()); } void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) { @@ -292,25 +320,25 @@ } void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { - Record.AddSourceLocation(TL.getCaretLoc()); + AddSourceLocation(TL.getCaretLoc()); } void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { - Record.AddSourceLocation(TL.getAmpLoc()); + AddSourceLocation(TL.getAmpLoc()); } void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { - Record.AddSourceLocation(TL.getAmpAmpLoc()); + AddSourceLocation(TL.getAmpAmpLoc()); } void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { - Record.AddSourceLocation(TL.getStarLoc()); - Record.AddTypeSourceInfo(TL.getClassTInfo()); + AddSourceLocation(TL.getStarLoc()); + AddTypeSourceInfo(TL.getClassTInfo()); } void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { - Record.AddSourceLocation(TL.getLBracketLoc()); - Record.AddSourceLocation(TL.getRBracketLoc()); + AddSourceLocation(TL.getLBracketLoc()); + AddSourceLocation(TL.getRBracketLoc()); Record.push_back(TL.getSizeExpr() ? 1 : 0); if (TL.getSizeExpr()) Record.AddStmt(TL.getSizeExpr()); @@ -335,56 +363,56 @@ void TypeLocWriter::VisitDependentAddressSpaceTypeLoc( DependentAddressSpaceTypeLoc TL) { - Record.AddSourceLocation(TL.getAttrNameLoc()); + AddSourceLocation(TL.getAttrNameLoc()); SourceRange range = TL.getAttrOperandParensRange(); - Record.AddSourceLocation(range.getBegin()); - Record.AddSourceLocation(range.getEnd()); + AddSourceLocation(range.getBegin()); + AddSourceLocation(range.getEnd()); Record.AddStmt(TL.getAttrExprOperand()); } void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( DependentSizedExtVectorTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitDependentVectorTypeLoc( DependentVectorTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { - Record.AddSourceLocation(TL.getAttrNameLoc()); + AddSourceLocation(TL.getAttrNameLoc()); SourceRange range = TL.getAttrOperandParensRange(); - Record.AddSourceLocation(range.getBegin()); - Record.AddSourceLocation(range.getEnd()); + AddSourceLocation(range.getBegin()); + AddSourceLocation(range.getEnd()); Record.AddStmt(TL.getAttrRowOperand()); Record.AddStmt(TL.getAttrColumnOperand()); } void TypeLocWriter::VisitDependentSizedMatrixTypeLoc( DependentSizedMatrixTypeLoc TL) { - Record.AddSourceLocation(TL.getAttrNameLoc()); + AddSourceLocation(TL.getAttrNameLoc()); SourceRange range = TL.getAttrOperandParensRange(); - Record.AddSourceLocation(range.getBegin()); - Record.AddSourceLocation(range.getEnd()); + AddSourceLocation(range.getBegin()); + AddSourceLocation(range.getEnd()); Record.AddStmt(TL.getAttrRowOperand()); Record.AddStmt(TL.getAttrColumnOperand()); } void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { - Record.AddSourceLocation(TL.getLocalRangeBegin()); - Record.AddSourceLocation(TL.getLParenLoc()); - Record.AddSourceLocation(TL.getRParenLoc()); - Record.AddSourceRange(TL.getExceptionSpecRange()); - Record.AddSourceLocation(TL.getLocalRangeEnd()); + AddSourceLocation(TL.getLocalRangeBegin()); + AddSourceLocation(TL.getLParenLoc()); + AddSourceLocation(TL.getRParenLoc()); + AddSourceRange(TL.getExceptionSpecRange()); + AddSourceLocation(TL.getLocalRangeEnd()); for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) Record.AddDeclRef(TL.getParam(i)); } @@ -398,81 +426,81 @@ } void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { if (TL.getNumProtocols()) { - Record.AddSourceLocation(TL.getProtocolLAngleLoc()); - Record.AddSourceLocation(TL.getProtocolRAngleLoc()); + AddSourceLocation(TL.getProtocolLAngleLoc()); + AddSourceLocation(TL.getProtocolRAngleLoc()); } for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) - Record.AddSourceLocation(TL.getProtocolLoc(i)); + AddSourceLocation(TL.getProtocolLoc(i)); } void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { - Record.AddSourceLocation(TL.getTypeofLoc()); - Record.AddSourceLocation(TL.getLParenLoc()); - Record.AddSourceLocation(TL.getRParenLoc()); + AddSourceLocation(TL.getTypeofLoc()); + AddSourceLocation(TL.getLParenLoc()); + AddSourceLocation(TL.getRParenLoc()); } void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { - Record.AddSourceLocation(TL.getTypeofLoc()); - Record.AddSourceLocation(TL.getLParenLoc()); - Record.AddSourceLocation(TL.getRParenLoc()); - Record.AddTypeSourceInfo(TL.getUnderlyingTInfo()); + AddSourceLocation(TL.getTypeofLoc()); + AddSourceLocation(TL.getLParenLoc()); + AddSourceLocation(TL.getRParenLoc()); + AddTypeSourceInfo(TL.getUnderlyingTInfo()); } void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { - Record.AddSourceLocation(TL.getDecltypeLoc()); - Record.AddSourceLocation(TL.getRParenLoc()); + AddSourceLocation(TL.getDecltypeLoc()); + AddSourceLocation(TL.getRParenLoc()); } void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { - Record.AddSourceLocation(TL.getKWLoc()); - Record.AddSourceLocation(TL.getLParenLoc()); - Record.AddSourceLocation(TL.getRParenLoc()); - Record.AddTypeSourceInfo(TL.getUnderlyingTInfo()); + AddSourceLocation(TL.getKWLoc()); + AddSourceLocation(TL.getLParenLoc()); + AddSourceLocation(TL.getRParenLoc()); + AddTypeSourceInfo(TL.getUnderlyingTInfo()); } void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); Record.push_back(TL.isConstrained()); if (TL.isConstrained()) { - Record.AddNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc()); - Record.AddSourceLocation(TL.getTemplateKWLoc()); - Record.AddSourceLocation(TL.getConceptNameLoc()); + AddNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc()); + AddSourceLocation(TL.getTemplateKWLoc()); + AddSourceLocation(TL.getConceptNameLoc()); Record.AddDeclRef(TL.getFoundDecl()); - Record.AddSourceLocation(TL.getLAngleLoc()); - Record.AddSourceLocation(TL.getRAngleLoc()); + AddSourceLocation(TL.getLAngleLoc()); + AddSourceLocation(TL.getRAngleLoc()); for (unsigned I = 0; I < TL.getNumArgs(); ++I) - Record.AddTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(), + AddTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(), TL.getArgLocInfo(I)); } Record.push_back(TL.isDecltypeAuto()); if (TL.isDecltypeAuto()) - Record.AddSourceLocation(TL.getRParenLoc()); + AddSourceLocation(TL.getRParenLoc()); } void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc( DeducedTemplateSpecializationTypeLoc TL) { - Record.AddSourceLocation(TL.getTemplateNameLoc()); + AddSourceLocation(TL.getTemplateNameLoc()); } void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) { @@ -484,107 +512,107 @@ } void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( SubstTemplateTypeParmTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc( SubstTemplateTypeParmPackTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitTemplateSpecializationTypeLoc( TemplateSpecializationTypeLoc TL) { - Record.AddSourceLocation(TL.getTemplateKeywordLoc()); - Record.AddSourceLocation(TL.getTemplateNameLoc()); - Record.AddSourceLocation(TL.getLAngleLoc()); - Record.AddSourceLocation(TL.getRAngleLoc()); + AddSourceLocation(TL.getTemplateKeywordLoc()); + AddSourceLocation(TL.getTemplateNameLoc()); + AddSourceLocation(TL.getLAngleLoc()); + AddSourceLocation(TL.getRAngleLoc()); for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) - Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), + AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), TL.getArgLoc(i).getLocInfo()); } void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { - Record.AddSourceLocation(TL.getLParenLoc()); - Record.AddSourceLocation(TL.getRParenLoc()); + AddSourceLocation(TL.getLParenLoc()); + AddSourceLocation(TL.getRParenLoc()); } void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { - Record.AddSourceLocation(TL.getExpansionLoc()); + AddSourceLocation(TL.getExpansionLoc()); } void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { - Record.AddSourceLocation(TL.getElaboratedKeywordLoc()); - Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc()); + AddSourceLocation(TL.getElaboratedKeywordLoc()); + AddNestedNameSpecifierLoc(TL.getQualifierLoc()); } void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { - Record.AddSourceLocation(TL.getElaboratedKeywordLoc()); - Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc()); - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getElaboratedKeywordLoc()); + AddNestedNameSpecifierLoc(TL.getQualifierLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( DependentTemplateSpecializationTypeLoc TL) { - Record.AddSourceLocation(TL.getElaboratedKeywordLoc()); - Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc()); - Record.AddSourceLocation(TL.getTemplateKeywordLoc()); - Record.AddSourceLocation(TL.getTemplateNameLoc()); - Record.AddSourceLocation(TL.getLAngleLoc()); - Record.AddSourceLocation(TL.getRAngleLoc()); + AddSourceLocation(TL.getElaboratedKeywordLoc()); + AddNestedNameSpecifierLoc(TL.getQualifierLoc()); + AddSourceLocation(TL.getTemplateKeywordLoc()); + AddSourceLocation(TL.getTemplateNameLoc()); + AddSourceLocation(TL.getLAngleLoc()); + AddSourceLocation(TL.getRAngleLoc()); for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) - Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), - TL.getArgLoc(I).getLocInfo()); + AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), + TL.getArgLoc(I).getLocInfo()); } void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { - Record.AddSourceLocation(TL.getEllipsisLoc()); + AddSourceLocation(TL.getEllipsisLoc()); } void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { Record.push_back(TL.hasBaseTypeAsWritten()); - Record.AddSourceLocation(TL.getTypeArgsLAngleLoc()); - Record.AddSourceLocation(TL.getTypeArgsRAngleLoc()); + AddSourceLocation(TL.getTypeArgsLAngleLoc()); + AddSourceLocation(TL.getTypeArgsRAngleLoc()); for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) - Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i)); - Record.AddSourceLocation(TL.getProtocolLAngleLoc()); - Record.AddSourceLocation(TL.getProtocolRAngleLoc()); + AddTypeSourceInfo(TL.getTypeArgTInfo(i)); + AddSourceLocation(TL.getProtocolLAngleLoc()); + AddSourceLocation(TL.getProtocolRAngleLoc()); for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) - Record.AddSourceLocation(TL.getProtocolLoc(i)); + AddSourceLocation(TL.getProtocolLoc(i)); } void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { - Record.AddSourceLocation(TL.getStarLoc()); + AddSourceLocation(TL.getStarLoc()); } void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) { - Record.AddSourceLocation(TL.getKWLoc()); - Record.AddSourceLocation(TL.getLParenLoc()); - Record.AddSourceLocation(TL.getRParenLoc()); + AddSourceLocation(TL.getKWLoc()); + AddSourceLocation(TL.getLParenLoc()); + AddSourceLocation(TL.getRParenLoc()); } void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) { - Record.AddSourceLocation(TL.getKWLoc()); + AddSourceLocation(TL.getKWLoc()); } void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void TypeLocWriter::VisitDependentBitIntTypeLoc( clang::DependentBitIntTypeLoc TL) { - Record.AddSourceLocation(TL.getNameLoc()); + AddSourceLocation(TL.getNameLoc()); } void ASTWriter::WriteTypeAbbrevs() { @@ -2122,12 +2150,13 @@ } else { // The source location entry is a macro expansion. const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion(); - AddSourceLocation(Expansion.getSpellingLoc(), Record); - AddSourceLocation(Expansion.getExpansionLocStart(), Record); + SourceLocationSeqRoot Seq; + AddSourceLocation(Expansion.getSpellingLoc(), Record, Seq); + AddSourceLocation(Expansion.getExpansionLocStart(), Record, Seq); AddSourceLocation(Expansion.isMacroArgExpansion() ? SourceLocation() : Expansion.getExpansionLocEnd(), - Record); + Record, Seq); Record.push_back(Expansion.isExpansionTokenRange()); // Compute the token length for this macro expansion. @@ -2442,8 +2471,9 @@ MacroOffsets[Index] = Offset; AddIdentifierRef(Name, Record); - AddSourceLocation(MI->getDefinitionLoc(), Record); - AddSourceLocation(MI->getDefinitionEndLoc(), Record); + SourceLocationSeqRoot Seq; + AddSourceLocation(MI->getDefinitionLoc(), Record, Seq); + AddSourceLocation(MI->getDefinitionEndLoc(), Record, Seq); Record.push_back(MI->isUsed()); Record.push_back(MI->isUsedForHeaderGuard()); Record.push_back(MI->getNumTokens()); @@ -5203,16 +5233,6 @@ Record.push_back(Raw); } -void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { - SourceLocation::UIntTy Raw = Loc.getRawEncoding(); - Record.push_back((Raw << 1) | (Raw >> (8 * sizeof(Raw) - 1))); -} - -void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { - AddSourceLocation(Range.getBegin(), Record); - AddSourceLocation(Range.getEnd(), Record); -} - void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) { AddAPInt(Value.bitcastToAPInt()); } @@ -5287,22 +5307,23 @@ } void ASTRecordWriter::AddTemplateArgumentLocInfo( - TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) { + TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg, + SourceLocationSeq *Seq) { switch (Kind) { case TemplateArgument::Expression: AddStmt(Arg.getAsExpr()); break; case TemplateArgument::Type: - AddTypeSourceInfo(Arg.getAsTypeSourceInfo()); + AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Seq); break; case TemplateArgument::Template: - AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc()); - AddSourceLocation(Arg.getTemplateNameLoc()); + AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Seq); + AddSourceLocation(Arg.getTemplateNameLoc(), Seq); break; case TemplateArgument::TemplateExpansion: - AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc()); - AddSourceLocation(Arg.getTemplateNameLoc()); - AddSourceLocation(Arg.getTemplateEllipsisLoc()); + AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Seq); + AddSourceLocation(Arg.getTemplateNameLoc(), Seq); + AddSourceLocation(Arg.getTemplateEllipsisLoc(), Seq); break; case TemplateArgument::Null: case TemplateArgument::Integral: @@ -5327,18 +5348,25 @@ AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo()); } -void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) { +void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, + serialization::SourceLocationSeq *Seq) { if (!TInfo) { AddTypeRef(QualType()); return; } AddTypeRef(TInfo->getType()); - AddTypeLoc(TInfo->getTypeLoc()); + AddTypeLoc(TInfo->getTypeLoc(), Seq); } -void ASTRecordWriter::AddTypeLoc(TypeLoc TL) { - TypeLocWriter TLW(*this); +void ASTRecordWriter::AddTypeLoc(TypeLoc TL, + serialization::SourceLocationSeq *OuterSeq) { + SourceLocationSeqRoot Seq(OuterSeq); + // + //llvm::errs() << "writing type loc nested=" << bool(Seq)<< "\n"; + // if (!TL.isNull()) + // llvm::errs() << "writing " << TL.getType()->getTypeClassName() << "\n"; + TypeLocWriter TLW(*this, Seq); for (; !TL.isNull(); TL = TL.getNextTypeLoc()) TLW.Visit(TL); } @@ -5488,20 +5516,21 @@ } void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, - DeclarationName Name) { + DeclarationName Name, + SourceLocationSeq *Seq) { switch (Name.getNameKind()) { case DeclarationName::CXXConstructorName: case DeclarationName::CXXDestructorName: case DeclarationName::CXXConversionFunctionName: - AddTypeSourceInfo(DNLoc.getNamedTypeInfo()); + AddTypeSourceInfo(DNLoc.getNamedTypeInfo(), Seq); break; case DeclarationName::CXXOperatorName: - AddSourceRange(DNLoc.getCXXOperatorNameRange()); + AddSourceRange(DNLoc.getCXXOperatorNameRange(), Seq); break; case DeclarationName::CXXLiteralOperatorName: - AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc()); + AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc(), Seq); break; case DeclarationName::Identifier: @@ -5521,14 +5550,17 @@ AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName()); } -void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) { - AddNestedNameSpecifierLoc(Info.QualifierLoc); +void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info, + SourceLocationSeq *Seq) { + AddNestedNameSpecifierLoc(Info.QualifierLoc, Seq); Record->push_back(Info.NumTemplParamLists); for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i) - AddTemplateParameterList(Info.TemplParamLists[i]); + AddTemplateParameterList(Info.TemplParamLists[i], Seq); } -void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { +void ASTRecordWriter::AddNestedNameSpecifierLoc( + NestedNameSpecifierLoc NNS, serialization::SourceLocationSeq *OuterSeq) { + SourceLocationSeqRoot Seq(OuterSeq); // Nested name specifiers usually aren't too long. I think that 8 would // typically accommodate the vast majority. SmallVector NestedNames; @@ -5549,45 +5581,45 @@ switch (Kind) { case NestedNameSpecifier::Identifier: AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier()); - AddSourceRange(NNS.getLocalSourceRange()); + AddSourceRange(NNS.getLocalSourceRange(), Seq); break; case NestedNameSpecifier::Namespace: AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace()); - AddSourceRange(NNS.getLocalSourceRange()); + AddSourceRange(NNS.getLocalSourceRange(), Seq); break; case NestedNameSpecifier::NamespaceAlias: AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias()); - AddSourceRange(NNS.getLocalSourceRange()); + AddSourceRange(NNS.getLocalSourceRange(), Seq); break; case NestedNameSpecifier::TypeSpec: case NestedNameSpecifier::TypeSpecWithTemplate: Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); AddTypeRef(NNS.getTypeLoc().getType()); - AddTypeLoc(NNS.getTypeLoc()); - AddSourceLocation(NNS.getLocalSourceRange().getEnd()); + AddTypeLoc(NNS.getTypeLoc(), Seq); + AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Seq); break; case NestedNameSpecifier::Global: - AddSourceLocation(NNS.getLocalSourceRange().getEnd()); + AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Seq); break; case NestedNameSpecifier::Super: AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl()); - AddSourceRange(NNS.getLocalSourceRange()); + AddSourceRange(NNS.getLocalSourceRange(), Seq); break; } } } void ASTRecordWriter::AddTemplateParameterList( - const TemplateParameterList *TemplateParams) { + const TemplateParameterList *TemplateParams, SourceLocationSeq *Seq) { assert(TemplateParams && "No TemplateParams!"); - AddSourceLocation(TemplateParams->getTemplateLoc()); - AddSourceLocation(TemplateParams->getLAngleLoc()); - AddSourceLocation(TemplateParams->getRAngleLoc()); + AddSourceLocation(TemplateParams->getTemplateLoc(), Seq); + AddSourceLocation(TemplateParams->getLAngleLoc(), Seq); + AddSourceLocation(TemplateParams->getRAngleLoc(), Seq); Record->push_back(TemplateParams->size()); for (const auto &P : *TemplateParams) diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -86,7 +86,8 @@ void VisitValueDecl(ValueDecl *D); void VisitEnumConstantDecl(EnumConstantDecl *D); void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); - void VisitDeclaratorDecl(DeclaratorDecl *D); + void VisitDeclaratorDecl(DeclaratorDecl *D, + serialization::SourceLocationSeq *Seq = nullptr); void VisitFunctionDecl(FunctionDecl *D); void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D); void VisitCXXMethodDecl(CXXMethodDecl *D); @@ -524,13 +525,14 @@ Code = serialization::DECL_ENUM_CONSTANT; } -void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) { +void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D, + serialization::SourceLocationSeq *Seq) { VisitValueDecl(D); - Record.AddSourceLocation(D->getInnerLocStart()); + Record.AddSourceLocation(D->getInnerLocStart(), Seq); Record.push_back(D->hasExtInfo()); if (D->hasExtInfo()) { DeclaratorDecl::ExtInfo *Info = D->getExtInfo(); - Record.AddQualifierInfo(*Info); + Record.AddQualifierInfo(*Info, Seq); Record.AddStmt(Info->TrailingRequiresClause); } // The location information is deferred until the end of the record. @@ -539,9 +541,10 @@ } void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) { + SourceLocationSeqRoot Seq; VisitRedeclarable(D); - VisitDeclaratorDecl(D); - Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName()); + VisitDeclaratorDecl(D, Seq); + Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Seq); Record.push_back(D->getIdentifierNamespace()); // FunctionDecl's body is handled last at ASTWriterDecl::Visit, @@ -565,7 +568,7 @@ Record.push_back(D->isMultiVersion()); Record.push_back(D->isLateTemplateParsed()); Record.push_back(D->getLinkageInternal()); - Record.AddSourceLocation(D->getEndLoc()); + Record.AddSourceLocation(D->getEndLoc(), Seq); Record.push_back(D->getODRHash()); @@ -592,7 +595,7 @@ MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo(); Record.AddDeclRef(MemberInfo->getInstantiatedFrom()); Record.push_back(MemberInfo->getTemplateSpecializationKind()); - Record.AddSourceLocation(MemberInfo->getPointOfInstantiation()); + Record.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Seq); break; } case FunctionDecl::TK_FunctionTemplateSpecialization: { @@ -614,19 +617,19 @@ for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs; i!=e; ++i) Record.AddTemplateArgumentLoc( - (*FTSInfo->TemplateArgumentsAsWritten)[i]); - Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->LAngleLoc); - Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->RAngleLoc); + (*FTSInfo->TemplateArgumentsAsWritten)[i]); // FIXME: seq + Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->LAngleLoc, Seq); + Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->RAngleLoc, Seq); } - Record.AddSourceLocation(FTSInfo->getPointOfInstantiation()); + Record.AddSourceLocation(FTSInfo->getPointOfInstantiation(), Seq); if (MemberSpecializationInfo *MemberInfo = FTSInfo->getMemberSpecializationInfo()) { Record.push_back(1); Record.AddDeclRef(MemberInfo->getInstantiatedFrom()); Record.push_back(MemberInfo->getTemplateSpecializationKind()); - Record.AddSourceLocation(MemberInfo->getPointOfInstantiation()); + Record.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Seq); } else { Record.push_back(0); } @@ -650,9 +653,9 @@ // Templates args. Record.push_back(DFTSInfo->getNumTemplateArgs()); for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i) - Record.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i)); - Record.AddSourceLocation(DFTSInfo->getLAngleLoc()); - Record.AddSourceLocation(DFTSInfo->getRAngleLoc()); + Record.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i)); // FIXME: seq + Record.AddSourceLocation(DFTSInfo->getLAngleLoc(), Seq); + Record.AddSourceLocation(DFTSInfo->getRAngleLoc(), Seq); break; } } diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -989,25 +989,27 @@ } void ASTStmtWriter::VisitConditionalOperator(ConditionalOperator *E) { + serialization::SourceLocationSeq Seq; VisitExpr(E); Record.AddStmt(E->getCond()); Record.AddStmt(E->getLHS()); Record.AddStmt(E->getRHS()); - Record.AddSourceLocation(E->getQuestionLoc()); - Record.AddSourceLocation(E->getColonLoc()); + Record.AddSourceLocation(E->getQuestionLoc(), &Seq); + Record.AddSourceLocation(E->getColonLoc(), &Seq); Code = serialization::EXPR_CONDITIONAL_OPERATOR; } void ASTStmtWriter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { + serialization::SourceLocationSeq Seq; VisitExpr(E); Record.AddStmt(E->getOpaqueValue()); Record.AddStmt(E->getCommon()); Record.AddStmt(E->getCond()); Record.AddStmt(E->getTrueExpr()); Record.AddStmt(E->getFalseExpr()); - Record.AddSourceLocation(E->getQuestionLoc()); - Record.AddSourceLocation(E->getColonLoc()); + Record.AddSourceLocation(E->getQuestionLoc(), &Seq); + Record.AddSourceLocation(E->getColonLoc(), &Seq); Code = serialization::EXPR_BINARY_CONDITIONAL_OPERATOR; } @@ -1027,9 +1029,10 @@ } void ASTStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) { + serialization::SourceLocationSeq Seq; VisitExplicitCastExpr(E); - Record.AddSourceLocation(E->getLParenLoc()); - Record.AddSourceLocation(E->getRParenLoc()); + Record.AddSourceLocation(E->getLParenLoc(), &Seq); + Record.AddSourceLocation(E->getRParenLoc(), &Seq); Code = serialization::EXPR_CSTYLE_CAST; } diff --git a/clang/unittests/Serialization/CMakeLists.txt b/clang/unittests/Serialization/CMakeLists.txt --- a/clang/unittests/Serialization/CMakeLists.txt +++ b/clang/unittests/Serialization/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_unittest(SerializationTests InMemoryModuleCacheTest.cpp ModuleCacheTest.cpp + SourceLocationEncodingTest.cpp ) clang_target_link_libraries(SerializationTests diff --git a/clang/unittests/Serialization/SourceLocationEncodingTest.cpp b/clang/unittests/Serialization/SourceLocationEncodingTest.cpp new file mode 100644 --- /dev/null +++ b/clang/unittests/Serialization/SourceLocationEncodingTest.cpp @@ -0,0 +1,62 @@ +//===- unittests/Serialization/SourceLocationEncodingTests.cpp ------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang/Serialization/SourceLocationEncoding.h" + +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +namespace { + +void roundTrip(std::vector Locs, + std::vector ExpectedEncoded = {}) { + serialization::SourceLocationSeq Seq; + std::vector ActualEncoded; + for (auto L : Locs) + ActualEncoded.push_back(Seq.encodeRaw(L)); + if (!ExpectedEncoded.empty()) + ASSERT_EQ(ActualEncoded, ExpectedEncoded) + << "Encoding " << testing::PrintToString(Locs); + Seq = serialization::SourceLocationSeq(); + std::vector DecodedEncoded; + for (auto L : ActualEncoded) + DecodedEncoded.push_back(Seq.decodeRaw(L)); + ASSERT_EQ(DecodedEncoded, Locs) + << "Decoding " << testing::PrintToString(ActualEncoded); +} + +TEST(SourceLocationEncoding, RoundTrips) { + roundTrip({1, 2, 3, 3, 2, 1}, + {2, // 1 + 5, // +1 + 5, // +1 + 1, // +0 + 3, // -1 + 3} // -1 + ); + roundTrip({100, 0, 100}, + {200, // 100 + 0, // 0 + 1} // +0 + ); + + SourceLocation::UIntTy MacroBit = + 1 << (sizeof(SourceLocation::UIntTy) * CHAR_BIT - 1); + roundTrip({1, MacroBit | 5, MacroBit | 4, 3}, {2, + 22, // =5|MacroBit + 3, // -1 + 12} // =3 + ); + + unsigned Big = 1 << 20; + roundTrip({123 | MacroBit, 1, 9, Big, Big + 1, 0, MacroBit | Big, 0}); +} + +} // namespace