diff --git a/lld/COFF/PDB.cpp b/lld/COFF/PDB.cpp --- a/lld/COFF/PDB.cpp +++ b/lld/COFF/PDB.cpp @@ -812,7 +812,7 @@ if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) { SmallVector Refs; auto Content = RecordData.drop_front(sizeof(RecordPrefix)); - CVSymbol Sym(Kind, RecordData); + CVSymbol Sym(RecordData); discoverTypeIndicesInSymbol(Sym, Refs); assert(Refs.size() == 1); assert(Refs.front().Count == 1); @@ -1012,7 +1012,7 @@ MutableArrayRef RecordBytes; if (NeedsRealignment) { RecordBytes = copyAndAlignSymbol(Sym, AlignedSymbolMem); - Sym = CVSymbol(Sym.kind(), RecordBytes); + Sym = CVSymbol(RecordBytes); } else { // Otherwise, we can actually mutate the symbol directly, since we // copied it to apply relocations. @@ -1036,7 +1036,7 @@ // An object file may have S_xxx_ID symbols, but these get converted to // "real" symbols in a PDB. translateIdSymbols(RecordBytes, getIDTable()); - Sym = CVSymbol(symbolKind(RecordBytes), RecordBytes); + Sym = CVSymbol(RecordBytes); // If this record refers to an offset in the object file's string table, // add that item to the global PDB string table and re-write the index. diff --git a/llvm/include/llvm/DebugInfo/CodeView/CVRecord.h b/llvm/include/llvm/DebugInfo/CodeView/CVRecord.h --- a/llvm/include/llvm/DebugInfo/CodeView/CVRecord.h +++ b/llvm/include/llvm/DebugInfo/CodeView/CVRecord.h @@ -26,15 +26,23 @@ template class CVRecord { public: - CVRecord() : Type(static_cast(0)) {} + CVRecord() = default; - CVRecord(Kind K, ArrayRef Data) : Type(K), RecordData(Data) {} + CVRecord(ArrayRef Data) : RecordData(Data) {} - bool valid() const { return Type != static_cast(0); } + bool valid() const { return kind() != static_cast(0); } uint32_t length() const { return RecordData.size(); } - Kind kind() const { return Type; } + + Kind kind() const { + if (RecordData.size() < sizeof(RecordPrefix)) + return Kind(0); + return static_cast(static_cast( + reinterpret_cast(RecordData.data())->RecordKind)); + } + ArrayRef data() const { return RecordData; } + StringRef str_data() const { return StringRef(reinterpret_cast(RecordData.data()), RecordData.size()); @@ -44,7 +52,6 @@ return RecordData.drop_front(sizeof(RecordPrefix)); } - Kind Type; ArrayRef RecordData; }; @@ -71,8 +78,7 @@ ArrayRef Data = StreamBuffer.take_front(RealLen); StreamBuffer = StreamBuffer.drop_front(RealLen); - Record R(static_cast((uint16_t)Prefix->RecordKind), - Data); + Record R(Data); if (auto EC = F(R)) return EC; } @@ -91,13 +97,12 @@ return std::move(EC); if (Prefix->RecordLen < 2) return make_error(cv_error_code::corrupt_record); - Kind K = static_cast(uint16_t(Prefix->RecordKind)); Reader.setOffset(Offset); ArrayRef RawData; if (auto EC = Reader.readBytes(RawData, Prefix->RecordLen + sizeof(uint16_t))) return std::move(EC); - return codeview::CVRecord(K, RawData); + return codeview::CVRecord(RawData); } } // end namespace codeview diff --git a/llvm/include/llvm/DebugInfo/CodeView/SymbolSerializer.h b/llvm/include/llvm/DebugInfo/CodeView/SymbolSerializer.h --- a/llvm/include/llvm/DebugInfo/CodeView/SymbolSerializer.h +++ b/llvm/include/llvm/DebugInfo/CodeView/SymbolSerializer.h @@ -51,8 +51,11 @@ template static CVSymbol writeOneSymbol(SymType &Sym, BumpPtrAllocator &Storage, CodeViewContainer Container) { - CVSymbol Result; - Result.Type = static_cast(Sym.Kind); + uint8_t PreBytes[sizeof(RecordPrefix)]; + RecordPrefix *Prefix = reinterpret_cast(&PreBytes[0]); + Prefix->RecordLen = 0; + Prefix->RecordKind = uint16_t(Sym.Kind); + CVSymbol Result(PreBytes); SymbolSerializer Serializer(Storage, Container); consumeError(Serializer.visitSymbolBegin(Result)); consumeError(Serializer.visitKnownRecord(Result, Sym)); diff --git a/llvm/include/llvm/DebugInfo/CodeView/TypeDeserializer.h b/llvm/include/llvm/DebugInfo/CodeView/TypeDeserializer.h --- a/llvm/include/llvm/DebugInfo/CodeView/TypeDeserializer.h +++ b/llvm/include/llvm/DebugInfo/CodeView/TypeDeserializer.h @@ -109,16 +109,24 @@ uint32_t StartOffset; }; + CVType makeFakeFieldList(RecordPrefix &FakeFieldList) { + FakeFieldList.RecordLen = 2; + FakeFieldList.RecordKind = uint16_t(TypeLeafKind::LF_FIELDLIST); + return CVType( + ArrayRef(reinterpret_cast(&FakeFieldList), + sizeof(FakeFieldList))); + } + public: explicit FieldListDeserializer(BinaryStreamReader &Reader) : Mapping(Reader) { - CVType FieldList; - FieldList.Type = TypeLeafKind::LF_FIELDLIST; + RecordPrefix Pre; + CVType FieldList = makeFakeFieldList(Pre); consumeError(Mapping.Mapping.visitTypeBegin(FieldList)); } ~FieldListDeserializer() override { - CVType FieldList; - FieldList.Type = TypeLeafKind::LF_FIELDLIST; + RecordPrefix Pre; + CVType FieldList = makeFakeFieldList(Pre); consumeError(Mapping.Mapping.visitTypeEnd(FieldList)); } diff --git a/llvm/lib/DebugInfo/CodeView/AppendingTypeTableBuilder.cpp b/llvm/lib/DebugInfo/CodeView/AppendingTypeTableBuilder.cpp --- a/llvm/lib/DebugInfo/CodeView/AppendingTypeTableBuilder.cpp +++ b/llvm/lib/DebugInfo/CodeView/AppendingTypeTableBuilder.cpp @@ -49,13 +49,8 @@ return Prev; } -CVType AppendingTypeTableBuilder::getType(TypeIndex Index) { - CVType Type; - Type.RecordData = SeenRecords[Index.toArrayIndex()]; - const RecordPrefix *P = - reinterpret_cast(Type.RecordData.data()); - Type.Type = static_cast(uint16_t(P->RecordKind)); - return Type; +CVType AppendingTypeTableBuilder::getType(TypeIndex Index){ + return CVType(SeenRecords[Index.toArrayIndex()]); } StringRef AppendingTypeTableBuilder::getTypeName(TypeIndex Index) { diff --git a/llvm/lib/DebugInfo/CodeView/CVSymbolVisitor.cpp b/llvm/lib/DebugInfo/CodeView/CVSymbolVisitor.cpp --- a/llvm/lib/DebugInfo/CodeView/CVSymbolVisitor.cpp +++ b/llvm/lib/DebugInfo/CodeView/CVSymbolVisitor.cpp @@ -20,7 +20,7 @@ template static Error visitKnownRecord(CVSymbol &Record, SymbolVisitorCallbacks &Callbacks) { - SymbolRecordKind RK = static_cast(Record.Type); + SymbolRecordKind RK = static_cast(Record.kind()); T KnownRecord(RK); if (auto EC = Callbacks.visitKnownRecord(Record, KnownRecord)) return EC; @@ -29,7 +29,7 @@ static Error finishVisitation(CVSymbol &Record, SymbolVisitorCallbacks &Callbacks) { - switch (Record.Type) { + switch (Record.kind()) { default: if (auto EC = Callbacks.visitUnknownSymbol(Record)) return EC; diff --git a/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp b/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp --- a/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp +++ b/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp @@ -22,7 +22,7 @@ template static Error visitKnownRecord(CVType &Record, TypeVisitorCallbacks &Callbacks) { - TypeRecordKind RK = static_cast(Record.Type); + TypeRecordKind RK = static_cast(Record.kind()); T KnownRecord(RK); if (auto EC = Callbacks.visitKnownRecord(Record, KnownRecord)) return EC; @@ -96,7 +96,7 @@ : Callbacks(Callbacks) {} Error CVTypeVisitor::finishVisitation(CVType &Record) { - switch (Record.Type) { + switch (Record.kind()) { default: if (auto EC = Callbacks.visitUnknownType(Record)) return EC; diff --git a/llvm/lib/DebugInfo/CodeView/ContinuationRecordBuilder.cpp b/llvm/lib/DebugInfo/CodeView/ContinuationRecordBuilder.cpp --- a/llvm/lib/DebugInfo/CodeView/ContinuationRecordBuilder.cpp +++ b/llvm/lib/DebugInfo/CodeView/ContinuationRecordBuilder.cpp @@ -66,15 +66,15 @@ InjectedSegmentBytes = ArrayRef(FLIB, FLIB + sizeof(SegmentInjection)); - CVType Type; - Type.Type = getTypeLeafKind(RecordKind); + // Seed the first trecord with an appropriate record prefix. + uint8_t PreBytes[sizeof(RecordPrefix)]; + RecordPrefix *Prefix = reinterpret_cast(&PreBytes[0]); + Prefix->RecordLen = 0; + Prefix->RecordKind = getTypeLeafKind(RecordKind); + CVType Type(PreBytes); cantFail(Mapping.visitTypeBegin(Type)); - // Seed the first trecord with an appropriate record prefix. - RecordPrefix Prefix; - Prefix.RecordLen = 0; - Prefix.RecordKind = Type.Type; - cantFail(SegmentWriter.writeObject(Prefix)); + cantFail(SegmentWriter.writeObject(PreBytes)); } template @@ -156,14 +156,12 @@ MutableArrayRef Data = Buffer.data(); Data = Data.slice(OffBegin, OffEnd - OffBegin); - CVType Type; - Type.Type = getTypeLeafKind(*Kind); - Type.RecordData = Data; + CVType Type(Data); // Write the length to the RecordPrefix, making sure it does not include // sizeof(RecordPrefix.Length) RecordPrefix *Prefix = reinterpret_cast(Data.data()); - assert(Prefix->RecordKind == Type.Type); + assert(Prefix->RecordKind == Type.kind()); Prefix->RecordLen = Data.size() - sizeof(RecordPrefix::RecordLen); if (RefersTo.hasValue()) { @@ -179,8 +177,11 @@ } std::vector ContinuationRecordBuilder::end(TypeIndex Index) { - CVType Type; - Type.Type = getTypeLeafKind(*Kind); + uint8_t PreBytes[sizeof(RecordPrefix)]; + RecordPrefix *Prefix = reinterpret_cast(&PreBytes[0]); + Prefix->RecordLen = 0; + Prefix->RecordKind = getTypeLeafKind(*Kind); + CVType Type(PreBytes); cantFail(Mapping.visitTypeEnd(Type)); // We're now done, and we have a series of segments each beginning at an diff --git a/llvm/lib/DebugInfo/CodeView/GlobalTypeTableBuilder.cpp b/llvm/lib/DebugInfo/CodeView/GlobalTypeTableBuilder.cpp --- a/llvm/lib/DebugInfo/CodeView/GlobalTypeTableBuilder.cpp +++ b/llvm/lib/DebugInfo/CodeView/GlobalTypeTableBuilder.cpp @@ -52,14 +52,7 @@ } CVType GlobalTypeTableBuilder::getType(TypeIndex Index) { - CVType Type; - Type.RecordData = SeenRecords[Index.toArrayIndex()]; - if (!Type.RecordData.empty()) { - assert(Type.RecordData.size() >= sizeof(RecordPrefix)); - const RecordPrefix *P = - reinterpret_cast(Type.RecordData.data()); - Type.Type = static_cast(uint16_t(P->RecordKind)); - } + CVType Type(SeenRecords[Index.toArrayIndex()]); return Type; } diff --git a/llvm/lib/DebugInfo/CodeView/MergingTypeTableBuilder.cpp b/llvm/lib/DebugInfo/CodeView/MergingTypeTableBuilder.cpp --- a/llvm/lib/DebugInfo/CodeView/MergingTypeTableBuilder.cpp +++ b/llvm/lib/DebugInfo/CodeView/MergingTypeTableBuilder.cpp @@ -52,11 +52,7 @@ } CVType MergingTypeTableBuilder::getType(TypeIndex Index) { - CVType Type; - Type.RecordData = SeenRecords[Index.toArrayIndex()]; - const RecordPrefix *P = - reinterpret_cast(Type.RecordData.data()); - Type.Type = static_cast(uint16_t(P->RecordKind)); + CVType Type(SeenRecords[Index.toArrayIndex()]); return Type; } diff --git a/llvm/lib/DebugInfo/CodeView/SimpleTypeSerializer.cpp b/llvm/lib/DebugInfo/CodeView/SimpleTypeSerializer.cpp --- a/llvm/lib/DebugInfo/CodeView/SimpleTypeSerializer.cpp +++ b/llvm/lib/DebugInfo/CodeView/SimpleTypeSerializer.cpp @@ -32,11 +32,9 @@ BinaryStreamWriter Writer(ScratchBuffer, support::little); TypeRecordMapping Mapping(Writer); - CVType CVT; - CVT.Type = static_cast(Record.getKind()); - - writeRecordPrefix(Writer, CVT.Type); + writeRecordPrefix(Writer, static_cast(Record.getKind())); + CVType CVT(ArrayRef(ScratchBuffer.data(), sizeof(RecordPrefix))); cantFail(Mapping.visitTypeBegin(CVT)); cantFail(Mapping.visitKnownRecord(CVT, Record)); cantFail(Mapping.visitTypeEnd(CVT)); diff --git a/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp b/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp --- a/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp +++ b/llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp @@ -101,10 +101,10 @@ } Error CVSymbolDumperImpl::visitSymbolBegin(CVSymbol &CVR) { - W.startLine() << getSymbolKindName(CVR.Type); + W.startLine() << getSymbolKindName(CVR.kind()); W.getOStream() << " {\n"; W.indent(); - W.printEnum("Kind", unsigned(CVR.Type), getSymbolTypeNames()); + W.printEnum("Kind", unsigned(CVR.kind()), getSymbolTypeNames()); return Error::success(); } diff --git a/llvm/lib/DebugInfo/CodeView/TypeDumpVisitor.cpp b/llvm/lib/DebugInfo/CodeView/TypeDumpVisitor.cpp --- a/llvm/lib/DebugInfo/CodeView/TypeDumpVisitor.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeDumpVisitor.cpp @@ -171,11 +171,11 @@ } Error TypeDumpVisitor::visitTypeBegin(CVType &Record, TypeIndex Index) { - W->startLine() << getLeafTypeName(Record.Type); + W->startLine() << getLeafTypeName(Record.kind()); W->getOStream() << " (" << HexNumber(Index.getIndex()) << ")"; W->getOStream() << " {\n"; W->indent(); - W->printEnum("TypeLeafKind", unsigned(Record.Type), + W->printEnum("TypeLeafKind", unsigned(Record.kind()), makeArrayRef(LeafTypeNames)); return Error::success(); } diff --git a/llvm/lib/DebugInfo/CodeView/TypeRecordMapping.cpp b/llvm/lib/DebugInfo/CodeView/TypeRecordMapping.cpp --- a/llvm/lib/DebugInfo/CodeView/TypeRecordMapping.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeRecordMapping.cpp @@ -88,11 +88,11 @@ // split with continuation records. All other record types cannot be // longer than the maximum record length. Optional MaxLen; - if (CVR.Type != TypeLeafKind::LF_FIELDLIST && - CVR.Type != TypeLeafKind::LF_METHODLIST) + if (CVR.kind() != TypeLeafKind::LF_FIELDLIST && + CVR.kind() != TypeLeafKind::LF_METHODLIST) MaxLen = MaxRecordLength - sizeof(RecordPrefix); error(IO.beginRecord(MaxLen)); - TypeKind = CVR.Type; + TypeKind = CVR.kind(); return Error::success(); } @@ -211,9 +211,9 @@ } Error TypeRecordMapping::visitKnownRecord(CVType &CVR, ClassRecord &Record) { - assert((CVR.Type == TypeLeafKind::LF_STRUCTURE) || - (CVR.Type == TypeLeafKind::LF_CLASS) || - (CVR.Type == TypeLeafKind::LF_INTERFACE)); + assert((CVR.kind() == TypeLeafKind::LF_STRUCTURE) || + (CVR.kind() == TypeLeafKind::LF_CLASS) || + (CVR.kind() == TypeLeafKind::LF_INTERFACE)); error(IO.mapInteger(Record.MemberCount)); error(IO.mapEnum(Record.Options)); diff --git a/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp b/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp --- a/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp @@ -36,11 +36,7 @@ CVType TypeTableCollection::getType(TypeIndex Index) { assert(Index.toArrayIndex() < Records.size()); - ArrayRef Bytes = Records[Index.toArrayIndex()]; - const RecordPrefix *Prefix = - reinterpret_cast(Bytes.data()); - TypeLeafKind Kind = static_cast(uint16_t(Prefix->RecordKind)); - return CVType(Kind, Bytes); + return CVType(Records[Index.toArrayIndex()]); } StringRef TypeTableCollection::getTypeName(TypeIndex Index) { diff --git a/llvm/lib/DebugInfo/PDB/Native/GSIStreamBuilder.cpp b/llvm/lib/DebugInfo/PDB/Native/GSIStreamBuilder.cpp --- a/llvm/lib/DebugInfo/PDB/Native/GSIStreamBuilder.cpp +++ b/llvm/lib/DebugInfo/PDB/Native/GSIStreamBuilder.cpp @@ -36,8 +36,8 @@ return Empty; } static inline CVSymbol getTombstoneKey() { - static CVSymbol Tombstone(static_cast(-1), - ArrayRef()); + static CVSymbol Tombstone( + ArrayRef(DenseMapInfo::getTombstoneKey(), 1)); return Tombstone; } static unsigned getHashValue(const CVSymbol &Val) { diff --git a/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp b/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp --- a/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp +++ b/llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp @@ -248,7 +248,7 @@ uint8_t *Buffer = Allocator.Allocate(TotalLen); ::memcpy(Buffer, &Prefix, sizeof(RecordPrefix)); ::memcpy(Buffer + sizeof(RecordPrefix), Data.data(), Data.size()); - return CVSymbol(Kind, ArrayRef(Buffer, TotalLen)); + return CVSymbol(ArrayRef(Buffer, TotalLen)); } Error fromCodeViewSymbol(CVSymbol CVS) override { diff --git a/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp b/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp --- a/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp +++ b/llvm/lib/ObjectYAML/CodeViewYAMLTypes.cpp @@ -98,7 +98,7 @@ CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const override { TS.writeLeafType(Record); - return CVType(Kind, TS.records().back()); + return CVType(TS.records().back()); } mutable T Record; @@ -496,7 +496,7 @@ Member.Member->writeTo(CRB); } TS.insertRecord(CRB); - return CVType(Kind, TS.records().back()); + return CVType(TS.records().back()); } void MappingTraits::mapping(IO &io, OneMethodRecord &Record) { diff --git a/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp b/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp --- a/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp +++ b/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp @@ -331,7 +331,7 @@ // append to the existing line. P.formatLine("{0} | {1} [size = {2}]", fmt_align(Offset, AlignStyle::Right, 6), - formatSymbolKind(Record.Type), Record.length()); + formatSymbolKind(Record.kind()), Record.length()); P.Indent(); return Error::success(); } diff --git a/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp b/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp --- a/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp +++ b/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp @@ -224,7 +224,7 @@ // append to the existing line. P.formatLine("{0} | {1} [size = {2}", fmt_align(Index, AlignStyle::Right, Width), - formatTypeLeafKind(Record.Type), Record.length()); + formatTypeLeafKind(Record.kind()), Record.length()); if (Hashes) { std::string H; if (Index.toArrayIndex() >= HashValues.size()) { diff --git a/llvm/unittests/DebugInfo/CodeView/RandomAccessVisitorTest.cpp b/llvm/unittests/DebugInfo/CodeView/RandomAccessVisitorTest.cpp --- a/llvm/unittests/DebugInfo/CodeView/RandomAccessVisitorTest.cpp +++ b/llvm/unittests/DebugInfo/CodeView/RandomAccessVisitorTest.cpp @@ -42,8 +42,6 @@ } inline bool operator==(const CVType &R1, const CVType &R2) { - if (R1.Type != R2.Type) - return false; if (R1.RecordData != R2.RecordData) return false; return true; @@ -107,7 +105,7 @@ GlobalState->Records.push_back(AR); GlobalState->Indices.push_back(Builder.writeLeafType(AR)); - CVType Type(TypeLeafKind::LF_ARRAY, Builder.records().back()); + CVType Type(Builder.records().back()); GlobalState->TypeVector.push_back(Type); GlobalState->AllOffsets.push_back( @@ -370,10 +368,8 @@ // set up a type stream that refers to the above two serialized records. std::vector TypeArray; - TypeArray.push_back( - CVType(static_cast(Class.Kind), Builder.records()[0])); - TypeArray.push_back( - CVType(static_cast(Modifier.Kind), Builder.records()[1])); + TypeArray.push_back(CVType(Builder.records()[0])); + TypeArray.push_back(CVType(Builder.records()[1])); BinaryItemStream ItemStream(llvm::support::little); ItemStream.setItems(TypeArray); VarStreamArray TypeStream(ItemStream);