Index: include/llvm/DebugInfo/CodeView/CVTypeVisitor.h =================================================================== --- include/llvm/DebugInfo/CodeView/CVTypeVisitor.h +++ include/llvm/DebugInfo/CodeView/CVTypeVisitor.h @@ -11,152 +11,31 @@ #define LLVM_DEBUGINFO_CODEVIEW_CVTYPEVISITOR_H #include "llvm/DebugInfo/CodeView/CVRecord.h" -#include "llvm/DebugInfo/CodeView/CodeView.h" -#include "llvm/DebugInfo/CodeView/TypeIndex.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h" -#include "llvm/Support/ErrorOr.h" +#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" +#include "llvm/Support/Error.h" namespace llvm { namespace codeview { -template class CVTypeVisitor { public: - CVTypeVisitor() {} + explicit CVTypeVisitor(TypeVisitorCallbacks &Callbacks); - bool hadError() const { return HadError; } - - template - bool consumeObject(ArrayRef &Data, const T *&Res) { - if (Data.size() < sizeof(*Res)) { - HadError = true; - return false; - } - Res = reinterpret_cast(Data.data()); - Data = Data.drop_front(sizeof(*Res)); - return true; - } - - /// Actions to take on known types. By default, they do nothing. Visit methods - /// for member records take the FieldData by non-const reference and are - /// expected to consume the trailing bytes used by the field. - /// FIXME: Make the visitor interpret the trailing bytes so that clients don't - /// need to. -#define TYPE_RECORD(EnumName, EnumVal, Name) \ - void visit##Name(TypeLeafKind LeafType, Name##Record &Record) {} -#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) -#define MEMBER_RECORD(EnumName, EnumVal, Name) \ - void visit##Name(TypeLeafKind LeafType, Name##Record &Record) {} -#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) -#include "TypeRecords.def" - - void visitTypeRecord(const CVRecord &Record) { - ArrayRef LeafData = Record.Data; - ArrayRef RecordData = LeafData; - auto *DerivedThis = static_cast(this); - DerivedThis->visitTypeBegin(Record.Type, RecordData); - switch (Record.Type) { - default: - DerivedThis->visitUnknownType(Record.Type, RecordData); - break; - case LF_FIELDLIST: - DerivedThis->visitFieldList(Record.Type, LeafData); - break; -#define TYPE_RECORD(EnumName, EnumVal, Name) \ - case EnumName: { \ - TypeRecordKind RK = static_cast(EnumName); \ - auto Result = Name##Record::deserialize(RK, LeafData); \ - if (Result.getError()) \ - return parseError(); \ - DerivedThis->visit##Name(Record.Type, *Result); \ - break; \ - } -#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \ - TYPE_RECORD(EnumVal, EnumVal, AliasName) -#define MEMBER_RECORD(EnumName, EnumVal, Name) -#include "TypeRecords.def" - } - DerivedThis->visitTypeEnd(Record.Type, RecordData); - } + Error visitTypeRecord(const CVRecord &Record); /// Visits the type records in Data. Sets the error flag on parse failures. - void visitTypeStream(const CVTypeArray &Types) { - for (const auto &I : Types) { - visitTypeRecord(I); - if (hadError()) - break; - } - } - - /// Action to take on unknown types. By default, they are ignored. - void visitUnknownType(TypeLeafKind Leaf, ArrayRef RecordData) {} + Error visitTypeStream(const CVTypeArray &Types); - /// Paired begin/end actions for all types. Receives all record data, - /// including the fixed-length record prefix. - void visitTypeBegin(TypeLeafKind Leaf, ArrayRef RecordData) {} - void visitTypeEnd(TypeLeafKind Leaf, ArrayRef RecordData) {} - - ArrayRef skipPadding(ArrayRef Data) { - if (Data.empty()) - return Data; - uint8_t Leaf = Data.front(); - if (Leaf < LF_PAD0) - return Data; - // Leaf is greater than 0xf0. We should advance by the number of bytes in - // the low 4 bits. - unsigned BytesToAdvance = Leaf & 0x0F; - if (Data.size() < BytesToAdvance) { - parseError(); - return None; - } - return Data.drop_front(BytesToAdvance); - } + Error skipPadding(ArrayRef &Data); /// Visits individual member records of a field list record. Member records do /// not describe their own length, and need special handling. - void visitFieldList(TypeLeafKind Leaf, ArrayRef FieldData) { - auto *DerivedThis = static_cast(this); - while (!FieldData.empty()) { - const ulittle16_t *LeafPtr; - if (!CVTypeVisitor::consumeObject(FieldData, LeafPtr)) - return; - TypeLeafKind Leaf = TypeLeafKind(unsigned(*LeafPtr)); - switch (Leaf) { - default: - // Field list records do not describe their own length, so we cannot - // continue parsing past an unknown member type. - DerivedThis->visitUnknownMember(Leaf); - return parseError(); -#define MEMBER_RECORD(EnumName, EnumVal, Name) \ - case EnumName: { \ - TypeRecordKind RK = static_cast(EnumName); \ - auto Result = Name##Record::deserialize(RK, FieldData); \ - if (Result.getError()) \ - return parseError(); \ - DerivedThis->visit##Name(Leaf, *Result); \ - break; \ - } -#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \ - MEMBER_RECORD(EnumVal, EnumVal, AliasName) -#include "TypeRecords.def" - } - FieldData = skipPadding(FieldData); - if (hadError()) - break; - } - } - - /// Action to take on unknown members. By default, they are ignored. Member - /// record parsing cannot recover from an unknown member record, so this - /// method is only called at most once per field list record. - void visitUnknownMember(TypeLeafKind Leaf) {} - - /// Helper for returning from a void function when the stream is corrupted. - void parseError() { HadError = true; } + Error visitFieldList(TypeLeafKind Leaf, ArrayRef FieldData); private: - /// Whether a type stream parsing error was encountered. - bool HadError = false; + /// The interface to the class that gets notified of each visitation. + TypeVisitorCallbacks &Callbacks; }; } // end namespace codeview Index: include/llvm/DebugInfo/CodeView/TypeDumper.h =================================================================== --- include/llvm/DebugInfo/CodeView/TypeDumper.h +++ include/llvm/DebugInfo/CodeView/TypeDumper.h @@ -14,6 +14,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/DebugInfo/CodeView/TypeIndex.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h" +#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" namespace llvm { class ScopedPrinter; @@ -21,7 +22,7 @@ namespace codeview { /// Dumper for CodeView type streams found in COFF object files and PDB files. -class CVTypeDumper { +class CVTypeDumper : public TypeVisitorCallbacks { public: CVTypeDumper(ScopedPrinter *W, bool PrintRecordBytes) : W(W), PrintRecordBytes(PrintRecordBytes) {} @@ -33,17 +34,17 @@ /// and true otherwise. This should be called in order, since the dumper /// maintains state about previous records which are necessary for cross /// type references. - bool dump(const CVRecord &Record); + Error dump(const CVRecord &Record); /// Dumps the type records in Types. Returns false if there was a type stream /// parse error, and true otherwise. - bool dump(const CVTypeArray &Types); + Error dump(const CVTypeArray &Types); /// Dumps the type records in Data. Returns false if there was a type stream /// parse error, and true otherwise. Use this method instead of the /// CVTypeArray overload when type records are laid out contiguously in /// memory. - bool dump(ArrayRef Data); + Error dump(ArrayRef Data); /// Gets the type index for the next type record. unsigned getNextTypeIndex() const { @@ -61,11 +62,37 @@ void setPrinter(ScopedPrinter *P); ScopedPrinter *getPrinter() { return W; } + /// Action to take on unknown types. By default, they are ignored. + Error visitUnknownType(TypeLeafKind Leaf, + ArrayRef RecordData) override; + Error visitUnknownMember(TypeLeafKind Leaf) override; + + /// Paired begin/end actions for all types. Receives all record data, + /// including the fixed-length record prefix. + Error visitTypeBegin(TypeLeafKind Leaf, + ArrayRef RecordData) override; + Error visitTypeEnd(TypeLeafKind Leaf, ArrayRef RecordData) override; + +#define TYPE_RECORD(EnumName, EnumVal, Name) \ + Error visit##Name(TypeLeafKind LeafType, Name##Record &Record) override; +#define MEMBER_RECORD(EnumName, EnumVal, Name) \ + TYPE_RECORD(EnumName, EnumVal, Name) +#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) +#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) +#include "TypeRecords.def" + private: + void printMemberAttributes(MemberAttributes Attrs); + void printMemberAttributes(MemberAccess Access, MethodKind Kind, + MethodOptions Options); + ScopedPrinter *W; bool PrintRecordBytes = false; + /// Name of the current type. Only valid before visitTypeEnd. + StringRef Name; + /// All user defined type records in .debug$T live in here. Type indices /// greater than 0x1000 are user defined. Subtract 0x1000 from the index to /// index into this vector. Index: include/llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h =================================================================== --- /dev/null +++ include/llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h @@ -0,0 +1,57 @@ +//===- TypeVisitorCallbacks.h -----------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_DEBUGINFO_CODEVIEW_TYPEVISITORCALLBACKS_H +#define LLVM_DEBUGINFO_CODEVIEW_TYPEVISITORCALLBACKS_H + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/DebugInfo/CodeView/CodeView.h" +#include "llvm/Support/Error.h" + +namespace llvm { +namespace codeview { +class TypeVisitorCallbacks { + friend class CVTypeVisitor; + +public: + virtual ~TypeVisitorCallbacks() {} + + /// Action to take on unknown types. By default, they are ignored. + virtual Error visitUnknownType(TypeLeafKind Leaf, + ArrayRef RecordData) { + return Error::success(); + } + virtual Error visitUnknownMember(TypeLeafKind Leaf) { + return Error::success(); + } + + /// Paired begin/end actions for all types. Receives all record data, + /// including the fixed-length record prefix. + virtual Error visitTypeBegin(TypeLeafKind Leaf, + ArrayRef RecordData) { + return Error::success(); + } + virtual Error visitTypeEnd(TypeLeafKind Leaf, ArrayRef RecordData) { + return Error::success(); + } + +#define TYPE_RECORD(EnumName, EnumVal, Name) \ + virtual Error visit##Name(TypeLeafKind LeafType, Name##Record &Record) { \ + return Error::success(); \ + } +#define MEMBER_RECORD(EnumName, EnumVal, Name) \ + TYPE_RECORD(EnumName, EnumVal, Name) +#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) +#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) +#include "TypeRecords.def" +}; +} +} + +#endif \ No newline at end of file Index: lib/DebugInfo/CodeView/CMakeLists.txt =================================================================== --- lib/DebugInfo/CodeView/CMakeLists.txt +++ lib/DebugInfo/CodeView/CMakeLists.txt @@ -1,6 +1,7 @@ add_llvm_library(LLVMDebugInfoCodeView ByteStream.cpp CodeViewError.cpp + CVTypeVisitor.cpp EnumTables.cpp FieldListRecordBuilder.cpp Line.cpp Index: lib/DebugInfo/CodeView/CVTypeVisitor.cpp =================================================================== --- /dev/null +++ lib/DebugInfo/CodeView/CVTypeVisitor.cpp @@ -0,0 +1,120 @@ +//===- CVTypeVisitor.cpp ----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" + +using namespace llvm; +using namespace llvm::codeview; + +template +static Error takeObject(ArrayRef &Data, const T *&Res) { + if (Data.size() < sizeof(*Res)) + return llvm::make_error(cv_error_code::insufficient_buffer); + Res = reinterpret_cast(Data.data()); + Data = Data.drop_front(sizeof(*Res)); + return Error::success(); +} + +CVTypeVisitor::CVTypeVisitor(TypeVisitorCallbacks &Callbacks) + : Callbacks(Callbacks) {} + +Error CVTypeVisitor::visitTypeRecord(const CVRecord &Record) { + ArrayRef LeafData = Record.Data; + ArrayRef RecordData = LeafData; + if (auto EC = Callbacks.visitTypeBegin(Record.Type, RecordData)) + return EC; + switch (Record.Type) { + default: + if (auto EC = Callbacks.visitUnknownType(Record.Type, RecordData)) + return EC; + break; + case LF_FIELDLIST: + if (auto EC = visitFieldList(Record.Type, LeafData)) + return EC; + break; +#define TYPE_RECORD(EnumName, EnumVal, Name) \ + case EnumName: { \ + TypeRecordKind RK = static_cast(EnumName); \ + auto Result = Name##Record::deserialize(RK, LeafData); \ + if (Result.getError()) \ + return llvm::make_error(cv_error_code::corrupt_record); \ + if (auto EC = Callbacks.visit##Name(Record.Type, *Result)) \ + return EC; \ + break; \ + } +#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \ + TYPE_RECORD(EnumVal, EnumVal, AliasName) +#define MEMBER_RECORD(EnumName, EnumVal, Name) +#include "llvm/DebugInfo/CodeView/TypeRecords.def" + } + if (auto EC = Callbacks.visitTypeEnd(Record.Type, RecordData)) + return EC; + return Error::success(); +} + +/// Visits the type records in Data. Sets the error flag on parse failures. +Error CVTypeVisitor::visitTypeStream(const CVTypeArray &Types) { + for (const auto &I : Types) { + if (auto EC = visitTypeRecord(I)) + return EC; + } + return Error::success(); +} + +Error CVTypeVisitor::skipPadding(ArrayRef &Data) { + if (Data.empty()) + return Error::success(); + uint8_t Leaf = Data.front(); + if (Leaf < LF_PAD0) + return Error::success(); + // Leaf is greater than 0xf0. We should advance by the number of bytes in + // the low 4 bits. + unsigned BytesToAdvance = Leaf & 0x0F; + if (Data.size() < BytesToAdvance) { + return llvm::make_error(cv_error_code::corrupt_record, + "Invalid padding bytes!"); + } + Data = Data.drop_front(BytesToAdvance); + return Error::success(); +} + +/// Visits individual member records of a field list record. Member records do +/// not describe their own length, and need special handling. +Error CVTypeVisitor::visitFieldList(TypeLeafKind Leaf, + ArrayRef FieldData) { + while (!FieldData.empty()) { + const ulittle16_t *LeafPtr; + if (auto EC = takeObject(FieldData, LeafPtr)) + return EC; + TypeLeafKind Leaf = TypeLeafKind(unsigned(*LeafPtr)); + switch (Leaf) { + default: + // Field list records do not describe their own length, so we cannot + // continue parsing past an unknown member type. + if (auto EC = Callbacks.visitUnknownMember(Leaf)) + return llvm::make_error(cv_error_code::corrupt_record); +#define MEMBER_RECORD(EnumName, EnumVal, Name) \ + case EnumName: { \ + TypeRecordKind RK = static_cast(EnumName); \ + auto Result = Name##Record::deserialize(RK, FieldData); \ + if (Result.getError()) \ + return llvm::make_error(cv_error_code::corrupt_record); \ + if (auto EC = Callbacks.visit##Name(Leaf, *Result)) \ + return EC; \ + break; \ + } +#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \ + MEMBER_RECORD(EnumVal, EnumVal, AliasName) +#include "llvm/DebugInfo/CodeView/TypeRecords.def" + } + if (auto EC = skipPadding(FieldData)) + return EC; + } + return Error::success(); +} Index: lib/DebugInfo/CodeView/TypeDumper.cpp =================================================================== --- lib/DebugInfo/CodeView/TypeDumper.cpp +++ lib/DebugInfo/CodeView/TypeDumper.cpp @@ -189,55 +189,6 @@ #undef ENUM_ENTRY - -namespace { - -/// Use this private dumper implementation to keep implementation details about -/// the visitor out of TypeDumper.h. -class CVTypeDumperImpl : public CVTypeVisitor { -public: - CVTypeDumperImpl(CVTypeDumper &CVTD, ScopedPrinter &W, bool PrintRecordBytes) - : CVTD(CVTD), W(W), PrintRecordBytes(PrintRecordBytes) {} - - /// CVTypeVisitor overrides. -#define TYPE_RECORD(EnumName, EnumVal, Name) \ - void visit##Name(TypeLeafKind LeafType, Name##Record &Record); -#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) -#define MEMBER_RECORD(EnumName, EnumVal, Name) \ - void visit##Name(TypeLeafKind LeafType, Name##Record &Record); -#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) -#include "llvm/DebugInfo/CodeView/TypeRecords.def" - - void visitUnknownMember(TypeLeafKind Leaf); - void visitUnknownType(TypeLeafKind Leaf, ArrayRef LeafData); - - void visitTypeBegin(TypeLeafKind Leaf, ArrayRef LeafData); - void visitTypeEnd(TypeLeafKind Leaf, ArrayRef LeafData); - - void printMemberAttributes(MemberAttributes Attrs); - void printMemberAttributes(MemberAccess Access, MethodKind Kind, - MethodOptions Options); - -private: - /// Forwards to the dumper, which holds the persistent state from visitation. - StringRef getTypeName(TypeIndex TI) { - return CVTD.getTypeName(TI); - } - - void printTypeIndex(StringRef FieldName, TypeIndex TI) { - CVTD.printTypeIndex(FieldName, TI); - } - - CVTypeDumper &CVTD; - ScopedPrinter &W; - bool PrintRecordBytes = false; - - /// Name of the current type. Only valid before visitTypeEnd. - StringRef Name; -}; - -} // end anonymous namespace - static StringRef getLeafTypeName(TypeLeafKind LT) { switch (LT) { #define TYPE_RECORD(ename, value, name) \ @@ -250,43 +201,45 @@ return "UnknownLeaf"; } -void CVTypeDumperImpl::visitTypeBegin(TypeLeafKind Leaf, - ArrayRef LeafData) { +Error CVTypeDumper::visitTypeBegin(TypeLeafKind Leaf, + ArrayRef LeafData) { // Reset Name to the empty string. If the visitor sets it, we know it. Name = ""; - W.startLine() << getLeafTypeName(Leaf) << " (" - << HexNumber(CVTD.getNextTypeIndex()) << ") {\n"; - W.indent(); - W.printEnum("TypeLeafKind", unsigned(Leaf), makeArrayRef(LeafTypeNames)); + W->startLine() << getLeafTypeName(Leaf) << " (" + << HexNumber(getNextTypeIndex()) << ") {\n"; + W->indent(); + W->printEnum("TypeLeafKind", unsigned(Leaf), makeArrayRef(LeafTypeNames)); + return Error::success(); } -void CVTypeDumperImpl::visitTypeEnd(TypeLeafKind Leaf, - ArrayRef LeafData) { +Error CVTypeDumper::visitTypeEnd(TypeLeafKind Leaf, + ArrayRef LeafData) { // Always record some name for every type, even if Name is empty. CVUDTNames // is indexed by type index, and must have one entry for every type. - CVTD.recordType(Name); + recordType(Name); if (PrintRecordBytes) - W.printBinaryBlock("LeafData", getBytesAsCharacters(LeafData)); + W->printBinaryBlock("LeafData", getBytesAsCharacters(LeafData)); - W.unindent(); - W.startLine() << "}\n"; + W->unindent(); + W->startLine() << "}\n"; + return Error::success(); } -void CVTypeDumperImpl::visitStringId(TypeLeafKind Leaf, - StringIdRecord &String) { +Error CVTypeDumper::visitStringId(TypeLeafKind Leaf, StringIdRecord &String) { printTypeIndex("Id", String.getId()); - W.printString("StringData", String.getString()); + W->printString("StringData", String.getString()); // Put this in CVUDTNames so it gets printed with LF_UDT_SRC_LINE. Name = String.getString(); + return Error::success(); } -void CVTypeDumperImpl::visitArgList(TypeLeafKind Leaf, ArgListRecord &Args) { +Error CVTypeDumper::visitArgList(TypeLeafKind Leaf, ArgListRecord &Args) { auto Indices = Args.getIndices(); uint32_t Size = Indices.size(); - W.printNumber("NumArgs", Size); - ListScope Arguments(W, "Arguments"); + W->printNumber("NumArgs", Size); + ListScope Arguments(*W, "Arguments"); SmallString<256> TypeName("("); for (uint32_t I = 0; I < Size; ++I) { printTypeIndex("ArgType", Indices[I]); @@ -296,79 +249,85 @@ TypeName.append(", "); } TypeName.push_back(')'); - Name = CVTD.saveName(TypeName); + Name = saveName(TypeName); + return Error::success(); } -void CVTypeDumperImpl::visitClass(TypeLeafKind Leaf, ClassRecord &Class) { +Error CVTypeDumper::visitClass(TypeLeafKind Leaf, ClassRecord &Class) { uint16_t Props = static_cast(Class.getOptions()); - W.printNumber("MemberCount", Class.getMemberCount()); - W.printFlags("Properties", Props, makeArrayRef(ClassOptionNames)); + W->printNumber("MemberCount", Class.getMemberCount()); + W->printFlags("Properties", Props, makeArrayRef(ClassOptionNames)); printTypeIndex("FieldList", Class.getFieldList()); printTypeIndex("DerivedFrom", Class.getDerivationList()); printTypeIndex("VShape", Class.getVTableShape()); - W.printNumber("SizeOf", Class.getSize()); - W.printString("Name", Class.getName()); + W->printNumber("SizeOf", Class.getSize()); + W->printString("Name", Class.getName()); if (Props & uint16_t(ClassOptions::HasUniqueName)) - W.printString("LinkageName", Class.getUniqueName()); + W->printString("LinkageName", Class.getUniqueName()); Name = Class.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitUnion(TypeLeafKind Leaf, UnionRecord &Union) { +Error CVTypeDumper::visitUnion(TypeLeafKind Leaf, UnionRecord &Union) { uint16_t Props = static_cast(Union.getOptions()); - W.printNumber("MemberCount", Union.getMemberCount()); - W.printFlags("Properties", Props, makeArrayRef(ClassOptionNames)); + W->printNumber("MemberCount", Union.getMemberCount()); + W->printFlags("Properties", Props, makeArrayRef(ClassOptionNames)); printTypeIndex("FieldList", Union.getFieldList()); - W.printNumber("SizeOf", Union.getSize()); - W.printString("Name", Union.getName()); + W->printNumber("SizeOf", Union.getSize()); + W->printString("Name", Union.getName()); if (Props & uint16_t(ClassOptions::HasUniqueName)) - W.printString("LinkageName", Union.getUniqueName()); + W->printString("LinkageName", Union.getUniqueName()); Name = Union.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitEnum(TypeLeafKind Leaf, EnumRecord &Enum) { - W.printNumber("NumEnumerators", Enum.getMemberCount()); - W.printFlags("Properties", uint16_t(Enum.getOptions()), - makeArrayRef(ClassOptionNames)); +Error CVTypeDumper::visitEnum(TypeLeafKind Leaf, EnumRecord &Enum) { + W->printNumber("NumEnumerators", Enum.getMemberCount()); + W->printFlags("Properties", uint16_t(Enum.getOptions()), + makeArrayRef(ClassOptionNames)); printTypeIndex("UnderlyingType", Enum.getUnderlyingType()); printTypeIndex("FieldListType", Enum.getFieldList()); - W.printString("Name", Enum.getName()); + W->printString("Name", Enum.getName()); Name = Enum.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitArray(TypeLeafKind Leaf, ArrayRecord &AT) { +Error CVTypeDumper::visitArray(TypeLeafKind Leaf, ArrayRecord &AT) { printTypeIndex("ElementType", AT.getElementType()); printTypeIndex("IndexType", AT.getIndexType()); - W.printNumber("SizeOf", AT.getSize()); - W.printString("Name", AT.getName()); + W->printNumber("SizeOf", AT.getSize()); + W->printString("Name", AT.getName()); Name = AT.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitVFTable(TypeLeafKind Leaf, VFTableRecord &VFT) { +Error CVTypeDumper::visitVFTable(TypeLeafKind Leaf, VFTableRecord &VFT) { printTypeIndex("CompleteClass", VFT.getCompleteClass()); printTypeIndex("OverriddenVFTable", VFT.getOverriddenVTable()); - W.printHex("VFPtrOffset", VFT.getVFPtrOffset()); - W.printString("VFTableName", VFT.getName()); + W->printHex("VFPtrOffset", VFT.getVFPtrOffset()); + W->printString("VFTableName", VFT.getName()); for (auto N : VFT.getMethodNames()) - W.printString("MethodName", N); + W->printString("MethodName", N); Name = VFT.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitMemberFuncId(TypeLeafKind Leaf, - MemberFuncIdRecord &Id) { +Error CVTypeDumper::visitMemberFuncId(TypeLeafKind Leaf, + MemberFuncIdRecord &Id) { printTypeIndex("ClassType", Id.getClassType()); printTypeIndex("FunctionType", Id.getFunctionType()); - W.printString("Name", Id.getName()); + W->printString("Name", Id.getName()); Name = Id.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitProcedure(TypeLeafKind Leaf, - ProcedureRecord &Proc) { +Error CVTypeDumper::visitProcedure(TypeLeafKind Leaf, ProcedureRecord &Proc) { printTypeIndex("ReturnType", Proc.getReturnType()); - W.printEnum("CallingConvention", uint8_t(Proc.getCallConv()), - makeArrayRef(CallingConventions)); - W.printFlags("FunctionOptions", uint8_t(Proc.getOptions()), - makeArrayRef(FunctionOptionEnum)); - W.printNumber("NumParameters", Proc.getParameterCount()); + W->printEnum("CallingConvention", uint8_t(Proc.getCallConv()), + makeArrayRef(CallingConventions)); + W->printFlags("FunctionOptions", uint8_t(Proc.getOptions()), + makeArrayRef(FunctionOptionEnum)); + W->printNumber("NumParameters", Proc.getParameterCount()); printTypeIndex("ArgListType", Proc.getArgumentList()); StringRef ReturnTypeName = getTypeName(Proc.getReturnType()); @@ -376,21 +335,22 @@ SmallString<256> TypeName(ReturnTypeName); TypeName.push_back(' '); TypeName.append(ArgListTypeName); - Name = CVTD.saveName(TypeName); + Name = saveName(TypeName); + return Error::success(); } -void CVTypeDumperImpl::visitMemberFunction(TypeLeafKind Leaf, - MemberFunctionRecord &MF) { +Error CVTypeDumper::visitMemberFunction(TypeLeafKind Leaf, + MemberFunctionRecord &MF) { printTypeIndex("ReturnType", MF.getReturnType()); printTypeIndex("ClassType", MF.getClassType()); printTypeIndex("ThisType", MF.getThisType()); - W.printEnum("CallingConvention", uint8_t(MF.getCallConv()), - makeArrayRef(CallingConventions)); - W.printFlags("FunctionOptions", uint8_t(MF.getOptions()), - makeArrayRef(FunctionOptionEnum)); - W.printNumber("NumParameters", MF.getParameterCount()); + W->printEnum("CallingConvention", uint8_t(MF.getCallConv()), + makeArrayRef(CallingConventions)); + W->printFlags("FunctionOptions", uint8_t(MF.getOptions()), + makeArrayRef(FunctionOptionEnum)); + W->printNumber("NumParameters", MF.getParameterCount()); printTypeIndex("ArgListType", MF.getArgumentList()); - W.printNumber("ThisAdjustment", MF.getThisPointerAdjustment()); + W->printNumber("ThisAdjustment", MF.getThisPointerAdjustment()); StringRef ReturnTypeName = getTypeName(MF.getReturnType()); StringRef ClassTypeName = getTypeName(MF.getClassType()); @@ -400,53 +360,56 @@ TypeName.append(ClassTypeName); TypeName.append("::"); TypeName.append(ArgListTypeName); - Name = CVTD.saveName(TypeName); + Name = saveName(TypeName); + return Error::success(); } -void CVTypeDumperImpl::visitMethodOverloadList( +Error CVTypeDumper::visitMethodOverloadList( TypeLeafKind Leaf, MethodOverloadListRecord &MethodList) { for (auto &M : MethodList.getMethods()) { - ListScope S(W, "Method"); + ListScope S(*W, "Method"); printMemberAttributes(M.getAccess(), M.getKind(), M.getOptions()); printTypeIndex("Type", M.getType()); if (M.isIntroducingVirtual()) - W.printHex("VFTableOffset", M.getVFTableOffset()); + W->printHex("VFTableOffset", M.getVFTableOffset()); } + return Error::success(); } -void CVTypeDumperImpl::visitFuncId(TypeLeafKind Leaf, FuncIdRecord &Func) { +Error CVTypeDumper::visitFuncId(TypeLeafKind Leaf, FuncIdRecord &Func) { printTypeIndex("ParentScope", Func.getParentScope()); printTypeIndex("FunctionType", Func.getFunctionType()); - W.printString("Name", Func.getName()); + W->printString("Name", Func.getName()); Name = Func.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitTypeServer2(TypeLeafKind Leaf, - TypeServer2Record &TS) { - W.printBinary("Signature", TS.getGuid()); - W.printNumber("Age", TS.getAge()); - W.printString("Name", TS.getName()); +Error CVTypeDumper::visitTypeServer2(TypeLeafKind Leaf, TypeServer2Record &TS) { + W->printBinary("Signature", TS.getGuid()); + W->printNumber("Age", TS.getAge()); + W->printString("Name", TS.getName()); Name = TS.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitPointer(TypeLeafKind Leaf, PointerRecord &Ptr) { +Error CVTypeDumper::visitPointer(TypeLeafKind Leaf, PointerRecord &Ptr) { printTypeIndex("PointeeType", Ptr.getReferentType()); - W.printHex("PointerAttributes", uint32_t(Ptr.getOptions())); - W.printEnum("PtrType", unsigned(Ptr.getPointerKind()), - makeArrayRef(PtrKindNames)); - W.printEnum("PtrMode", unsigned(Ptr.getMode()), makeArrayRef(PtrModeNames)); + W->printHex("PointerAttributes", uint32_t(Ptr.getOptions())); + W->printEnum("PtrType", unsigned(Ptr.getPointerKind()), + makeArrayRef(PtrKindNames)); + W->printEnum("PtrMode", unsigned(Ptr.getMode()), makeArrayRef(PtrModeNames)); - W.printNumber("IsFlat", Ptr.isFlat()); - W.printNumber("IsConst", Ptr.isConst()); - W.printNumber("IsVolatile", Ptr.isVolatile()); - W.printNumber("IsUnaligned", Ptr.isUnaligned()); + W->printNumber("IsFlat", Ptr.isFlat()); + W->printNumber("IsConst", Ptr.isConst()); + W->printNumber("IsVolatile", Ptr.isVolatile()); + W->printNumber("IsUnaligned", Ptr.isUnaligned()); if (Ptr.isPointerToMember()) { const MemberPointerInfo &MI = Ptr.getMemberInfo(); printTypeIndex("ClassType", MI.getContainingType()); - W.printEnum("Representation", uint16_t(MI.getRepresentation()), - makeArrayRef(PtrMemberRepNames)); + W->printEnum("Representation", uint16_t(MI.getRepresentation()), + makeArrayRef(PtrMemberRepNames)); StringRef PointeeName = getTypeName(Ptr.getReferentType()); StringRef ClassName = getTypeName(MI.getContainingType()); @@ -454,7 +417,7 @@ TypeName.push_back(' '); TypeName.append(ClassName); TypeName.append("::*"); - Name = CVTD.saveName(TypeName); + Name = saveName(TypeName); } else { SmallString<256> TypeName; if (Ptr.isConst()) @@ -474,14 +437,15 @@ TypeName.append("*"); if (!TypeName.empty()) - Name = CVTD.saveName(TypeName); + Name = saveName(TypeName); } + return Error::success(); } -void CVTypeDumperImpl::visitModifier(TypeLeafKind Leaf, ModifierRecord &Mod) { +Error CVTypeDumper::visitModifier(TypeLeafKind Leaf, ModifierRecord &Mod) { uint16_t Mods = static_cast(Mod.getModifiers()); printTypeIndex("ModifiedType", Mod.getModifiedType()); - W.printFlags("Modifiers", Mods, makeArrayRef(TypeModifierNames)); + W->printFlags("Modifiers", Mods, makeArrayRef(TypeModifierNames)); StringRef ModifiedName = getTypeName(Mod.getModifiedType()); SmallString<256> TypeName; @@ -492,152 +456,162 @@ if (Mods & uint16_t(ModifierOptions::Unaligned)) TypeName.append("__unaligned "); TypeName.append(ModifiedName); - Name = CVTD.saveName(TypeName); + Name = saveName(TypeName); + return Error::success(); } -void CVTypeDumperImpl::visitBitField(TypeLeafKind Leaf, - BitFieldRecord &BitField) { +Error CVTypeDumper::visitBitField(TypeLeafKind Leaf, BitFieldRecord &BitField) { printTypeIndex("Type", BitField.getType()); - W.printNumber("BitSize", BitField.getBitSize()); - W.printNumber("BitOffset", BitField.getBitOffset()); + W->printNumber("BitSize", BitField.getBitSize()); + W->printNumber("BitOffset", BitField.getBitOffset()); + return Error::success(); } -void CVTypeDumperImpl::visitVFTableShape(TypeLeafKind Leaf, - VFTableShapeRecord &Shape) { - W.printNumber("VFEntryCount", Shape.getEntryCount()); +Error CVTypeDumper::visitVFTableShape(TypeLeafKind Leaf, + VFTableShapeRecord &Shape) { + W->printNumber("VFEntryCount", Shape.getEntryCount()); + return Error::success(); } -void CVTypeDumperImpl::visitUdtSourceLine(TypeLeafKind Leaf, - UdtSourceLineRecord &Line) { +Error CVTypeDumper::visitUdtSourceLine(TypeLeafKind Leaf, + UdtSourceLineRecord &Line) { printTypeIndex("UDT", Line.getUDT()); printTypeIndex("SourceFile", Line.getSourceFile()); - W.printNumber("LineNumber", Line.getLineNumber()); + W->printNumber("LineNumber", Line.getLineNumber()); + return Error::success(); } -void CVTypeDumperImpl::visitBuildInfo(TypeLeafKind Leaf, - BuildInfoRecord &Args) { - W.printNumber("NumArgs", static_cast(Args.getArgs().size())); +Error CVTypeDumper::visitBuildInfo(TypeLeafKind Leaf, BuildInfoRecord &Args) { + W->printNumber("NumArgs", static_cast(Args.getArgs().size())); - ListScope Arguments(W, "Arguments"); + ListScope Arguments(*W, "Arguments"); for (auto Arg : Args.getArgs()) { printTypeIndex("ArgType", Arg); } + return Error::success(); } -void CVTypeDumperImpl::printMemberAttributes(MemberAttributes Attrs) { +void CVTypeDumper::printMemberAttributes(MemberAttributes Attrs) { return printMemberAttributes(Attrs.getAccess(), Attrs.getMethodKind(), Attrs.getFlags()); } -void CVTypeDumperImpl::printMemberAttributes(MemberAccess Access, - MethodKind Kind, - MethodOptions Options) { - W.printEnum("AccessSpecifier", uint8_t(Access), - makeArrayRef(MemberAccessNames)); +void CVTypeDumper::printMemberAttributes(MemberAccess Access, MethodKind Kind, + MethodOptions Options) { + W->printEnum("AccessSpecifier", uint8_t(Access), + makeArrayRef(MemberAccessNames)); // Data members will be vanilla. Don't try to print a method kind for them. if (Kind != MethodKind::Vanilla) - W.printEnum("MethodKind", unsigned(Kind), makeArrayRef(MemberKindNames)); + W->printEnum("MethodKind", unsigned(Kind), makeArrayRef(MemberKindNames)); if (Options != MethodOptions::None) { - W.printFlags("MethodOptions", unsigned(Options), - makeArrayRef(MethodOptionNames)); + W->printFlags("MethodOptions", unsigned(Options), + makeArrayRef(MethodOptionNames)); } } -void CVTypeDumperImpl::visitUnknownMember(TypeLeafKind Leaf) { - W.printHex("UnknownMember", unsigned(Leaf)); +Error CVTypeDumper::visitUnknownMember(TypeLeafKind Leaf) { + W->printHex("UnknownMember", unsigned(Leaf)); + return Error::success(); } -void CVTypeDumperImpl::visitUnknownType(TypeLeafKind Leaf, - ArrayRef RecordData) { - DictScope S(W, "UnknownType"); - W.printEnum("Kind", uint16_t(Leaf), makeArrayRef(LeafTypeNames)); - W.printNumber("Length", uint32_t(RecordData.size())); +Error CVTypeDumper::visitUnknownType(TypeLeafKind Leaf, + ArrayRef RecordData) { + DictScope S(*W, "UnknownType"); + W->printEnum("Kind", uint16_t(Leaf), makeArrayRef(LeafTypeNames)); + W->printNumber("Length", uint32_t(RecordData.size())); + return Error::success(); } -void CVTypeDumperImpl::visitNestedType(TypeLeafKind Leaf, - NestedTypeRecord &Nested) { - DictScope S(W, "NestedType"); +Error CVTypeDumper::visitNestedType(TypeLeafKind Leaf, + NestedTypeRecord &Nested) { + DictScope S(*W, "NestedType"); printTypeIndex("Type", Nested.getNestedType()); - W.printString("Name", Nested.getName()); + W->printString("Name", Nested.getName()); Name = Nested.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitOneMethod(TypeLeafKind Leaf, - OneMethodRecord &Method) { - DictScope S(W, "OneMethod"); +Error CVTypeDumper::visitOneMethod(TypeLeafKind Leaf, OneMethodRecord &Method) { + DictScope S(*W, "OneMethod"); MethodKind K = Method.getKind(); printMemberAttributes(Method.getAccess(), K, Method.getOptions()); printTypeIndex("Type", Method.getType()); // If virtual, then read the vftable offset. if (Method.isIntroducingVirtual()) - W.printHex("VFTableOffset", Method.getVFTableOffset()); - W.printString("Name", Method.getName()); + W->printHex("VFTableOffset", Method.getVFTableOffset()); + W->printString("Name", Method.getName()); Name = Method.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitOverloadedMethod(TypeLeafKind Leaf, - OverloadedMethodRecord &Method) { - DictScope S(W, "OverloadedMethod"); - W.printHex("MethodCount", Method.getNumOverloads()); +Error CVTypeDumper::visitOverloadedMethod(TypeLeafKind Leaf, + OverloadedMethodRecord &Method) { + DictScope S(*W, "OverloadedMethod"); + W->printHex("MethodCount", Method.getNumOverloads()); printTypeIndex("MethodListIndex", Method.getMethodList()); - W.printString("Name", Method.getName()); + W->printString("Name", Method.getName()); Name = Method.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitDataMember(TypeLeafKind Leaf, - DataMemberRecord &Field) { - DictScope S(W, "DataMember"); +Error CVTypeDumper::visitDataMember(TypeLeafKind Leaf, + DataMemberRecord &Field) { + DictScope S(*W, "DataMember"); printMemberAttributes(Field.getAccess(), MethodKind::Vanilla, MethodOptions::None); printTypeIndex("Type", Field.getType()); - W.printHex("FieldOffset", Field.getFieldOffset()); - W.printString("Name", Field.getName()); + W->printHex("FieldOffset", Field.getFieldOffset()); + W->printString("Name", Field.getName()); Name = Field.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitStaticDataMember(TypeLeafKind Leaf, - StaticDataMemberRecord &Field) { - DictScope S(W, "StaticDataMember"); +Error CVTypeDumper::visitStaticDataMember(TypeLeafKind Leaf, + StaticDataMemberRecord &Field) { + DictScope S(*W, "StaticDataMember"); printMemberAttributes(Field.getAccess(), MethodKind::Vanilla, MethodOptions::None); printTypeIndex("Type", Field.getType()); - W.printString("Name", Field.getName()); + W->printString("Name", Field.getName()); Name = Field.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitVFPtr(TypeLeafKind Leaf, VFPtrRecord &VFTable) { - DictScope S(W, "VFPtr"); +Error CVTypeDumper::visitVFPtr(TypeLeafKind Leaf, VFPtrRecord &VFTable) { + DictScope S(*W, "VFPtr"); printTypeIndex("Type", VFTable.getType()); + return Error::success(); } -void CVTypeDumperImpl::visitEnumerator(TypeLeafKind Leaf, - EnumeratorRecord &Enum) { - DictScope S(W, "Enumerator"); +Error CVTypeDumper::visitEnumerator(TypeLeafKind Leaf, EnumeratorRecord &Enum) { + DictScope S(*W, "Enumerator"); printMemberAttributes(Enum.getAccess(), MethodKind::Vanilla, MethodOptions::None); - W.printNumber("EnumValue", Enum.getValue()); - W.printString("Name", Enum.getName()); + W->printNumber("EnumValue", Enum.getValue()); + W->printString("Name", Enum.getName()); Name = Enum.getName(); + return Error::success(); } -void CVTypeDumperImpl::visitBaseClass(TypeLeafKind Leaf, - BaseClassRecord &Base) { - DictScope S(W, "BaseClass"); +Error CVTypeDumper::visitBaseClass(TypeLeafKind Leaf, BaseClassRecord &Base) { + DictScope S(*W, "BaseClass"); printMemberAttributes(Base.getAccess(), MethodKind::Vanilla, MethodOptions::None); printTypeIndex("BaseType", Base.getBaseType()); - W.printHex("BaseOffset", Base.getBaseOffset()); + W->printHex("BaseOffset", Base.getBaseOffset()); + return Error::success(); } -void CVTypeDumperImpl::visitVirtualBaseClass(TypeLeafKind Leaf, - VirtualBaseClassRecord &Base) { - DictScope S(W, "VirtualBaseClass"); +Error CVTypeDumper::visitVirtualBaseClass(TypeLeafKind Leaf, + VirtualBaseClassRecord &Base) { + DictScope S(*W, "VirtualBaseClass"); printMemberAttributes(Base.getAccess(), MethodKind::Vanilla, MethodOptions::None); printTypeIndex("BaseType", Base.getBaseType()); printTypeIndex("VBPtrType", Base.getVBPtrType()); - W.printHex("VBPtrOffset", Base.getVBPtrOffset()); - W.printHex("VBTableIndex", Base.getVTableIndex()); + W->printHex("VBPtrOffset", Base.getVBPtrOffset()); + W->printHex("VBTableIndex", Base.getVTableIndex()); + return Error::success(); } StringRef CVTypeDumper::getTypeName(TypeIndex TI) { @@ -677,28 +651,29 @@ W->printHex(FieldName, TI.getIndex()); } -bool CVTypeDumper::dump(const CVRecord &Record) { +Error CVTypeDumper::dump(const CVRecord &Record) { assert(W && "printer should not be null"); - CVTypeDumperImpl Dumper(*this, *W, PrintRecordBytes); - Dumper.visitTypeRecord(Record); - return !Dumper.hadError(); + CVTypeVisitor Visitor(*this); + + if (auto EC = Visitor.visitTypeRecord(Record)) + return EC; + return Error::success(); } -bool CVTypeDumper::dump(const CVTypeArray &Types) { +Error CVTypeDumper::dump(const CVTypeArray &Types) { assert(W && "printer should not be null"); - CVTypeDumperImpl Dumper(*this, *W, PrintRecordBytes); - Dumper.visitTypeStream(Types); - return !Dumper.hadError(); + CVTypeVisitor Visitor(*this); + if (auto EC = Visitor.visitTypeStream(Types)) + return EC; + return Error::success(); } -bool CVTypeDumper::dump(ArrayRef Data) { +Error CVTypeDumper::dump(ArrayRef Data) { ByteStream<> Stream(Data); CVTypeArray Types; StreamReader Reader(Stream); - if (auto EC = Reader.readArray(Types, Reader.getLength())) { - consumeError(std::move(EC)); - return false; - } + if (auto EC = Reader.readArray(Types, Reader.getLength())) + return EC; return dump(Types); } Index: lib/DebugInfo/CodeView/TypeStreamMerger.cpp =================================================================== --- lib/DebugInfo/CodeView/TypeStreamMerger.cpp +++ lib/DebugInfo/CodeView/TypeStreamMerger.cpp @@ -15,6 +15,8 @@ #include "llvm/DebugInfo/CodeView/StreamRef.h" #include "llvm/DebugInfo/CodeView/TypeIndex.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h" +#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ScopedPrinter.h" using namespace llvm; @@ -49,32 +51,32 @@ /// - If the type record already exists in the destination stream, discard it /// and update the type index map to forward the source type index to the /// existing destination type index. -class TypeStreamMerger : public CVTypeVisitor { +class TypeStreamMerger : public TypeVisitorCallbacks { public: TypeStreamMerger(TypeTableBuilder &DestStream) : DestStream(DestStream) { assert(!hadError()); } - /// CVTypeVisitor overrides. +/// TypeVisitorCallbacks overrides. #define TYPE_RECORD(EnumName, EnumVal, Name) \ - void visit##Name(TypeLeafKind LeafType, Name##Record &Record); -#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) + Error visit##Name(TypeLeafKind LeafType, Name##Record &Record) override; #define MEMBER_RECORD(EnumName, EnumVal, Name) \ - void visit##Name(TypeLeafKind LeafType, Name##Record &Record); + TYPE_RECORD(EnumName, EnumVal, Name) +#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) #include "llvm/DebugInfo/CodeView/TypeRecords.def" - void visitUnknownType(TypeLeafKind Leaf, ArrayRef RecordData); + Error visitUnknownType(TypeLeafKind Leaf, + ArrayRef RecordData) override; - void visitTypeBegin(TypeLeafKind Leaf, ArrayRef RecordData); - void visitTypeEnd(TypeLeafKind Leaf, ArrayRef RecordData); - - void visitFieldList(TypeLeafKind Leaf, ArrayRef FieldData); + Error visitTypeBegin(TypeLeafKind Leaf, + ArrayRef RecordData) override; + Error visitTypeEnd(TypeLeafKind Leaf, ArrayRef RecordData) override; bool mergeStream(const CVTypeArray &Types); private: - bool hadError() { return FoundBadTypeIndex || CVTypeVisitor::hadError(); } + bool hadError() { return FoundBadTypeIndex; } bool FoundBadTypeIndex = false; @@ -91,50 +93,56 @@ } // end anonymous namespace -void TypeStreamMerger::visitTypeBegin(TypeLeafKind Leaf, - ArrayRef RecordData) { +Error TypeStreamMerger::visitTypeBegin(TypeLeafKind Leaf, + ArrayRef RecordData) { BeginIndexMapSize = IndexMap.size(); + return Error::success(); } -void TypeStreamMerger::visitTypeEnd(TypeLeafKind Leaf, - ArrayRef RecordData) { - assert(IndexMap.size() == BeginIndexMapSize + 1); -} +Error TypeStreamMerger::visitTypeEnd(TypeLeafKind Leaf, + ArrayRef RecordData) { + if (Leaf == TypeLeafKind::LF_FIELDLIST) { + IndexMap.push_back(DestStream.writeFieldList(FieldBuilder)); + FieldBuilder.reset(); + } -void TypeStreamMerger::visitFieldList(TypeLeafKind Leaf, - ArrayRef FieldData) { - CVTypeVisitor::visitFieldList(Leaf, FieldData); - IndexMap.push_back(DestStream.writeFieldList(FieldBuilder)); - FieldBuilder.reset(); + assert(IndexMap.size() == BeginIndexMapSize + 1); + return Error::success(); } #define TYPE_RECORD(EnumName, EnumVal, Name) \ - void TypeStreamMerger::visit##Name(TypeLeafKind LeafType, \ - Name##Record &Record) { \ + Error TypeStreamMerger::visit##Name(TypeLeafKind LeafType, \ + Name##Record &Record) { \ FoundBadTypeIndex |= !Record.remapTypeIndices(IndexMap); \ IndexMap.push_back(DestStream.write##Name(Record)); \ + return Error::success(); \ } #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) #define MEMBER_RECORD(EnumName, EnumVal, Name) \ - void TypeStreamMerger::visit##Name(TypeLeafKind LeafType, \ - Name##Record &Record) { \ + Error TypeStreamMerger::visit##Name(TypeLeafKind LeafType, \ + Name##Record &Record) { \ FoundBadTypeIndex |= !Record.remapTypeIndices(IndexMap); \ FieldBuilder.write##Name(Record); \ + return Error::success(); \ } #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) #include "llvm/DebugInfo/CodeView/TypeRecords.def" -void TypeStreamMerger::visitUnknownType(TypeLeafKind Leaf, - ArrayRef RecordData) { +Error TypeStreamMerger::visitUnknownType(TypeLeafKind Leaf, + ArrayRef RecordData) { // We failed to translate a type. Translate this index as "not translated". IndexMap.push_back( TypeIndex(SimpleTypeKind::NotTranslated, SimpleTypeMode::Direct)); - parseError(); + return llvm::make_error(cv_error_code::corrupt_record); } bool TypeStreamMerger::mergeStream(const CVTypeArray &Types) { assert(IndexMap.empty()); - visitTypeStream(Types); + CVTypeVisitor Visitor(*this); + if (auto EC = Visitor.visitTypeStream(Types)) { + consumeError(std::move(EC)); + return false; + } IndexMap.clear(); return !hadError(); } Index: tools/llvm-pdbdump/LLVMOutputStyle.cpp =================================================================== --- tools/llvm-pdbdump/LLVMOutputStyle.cpp +++ tools/llvm-pdbdump/LLVMOutputStyle.cpp @@ -328,8 +328,10 @@ for (auto &Type : Tpi->types(&HadError)) { DictScope DD(P, ""); - if (DumpRecords) - TD.dump(Type); + if (DumpRecords) { + if (auto EC = TD.dump(Type)) + return EC; + } if (DumpRecordBytes) P.printBinaryBlock("Bytes", Type.Data); @@ -347,8 +349,10 @@ TD.setPrinter(nullptr); bool HadError = false; - for (auto &Type : Tpi->types(&HadError)) - TD.dump(Type); + for (auto &Type : Tpi->types(&HadError)) { + if (auto EC = TD.dump(Type)) + return EC; + } TD.setPrinter(OldP); dumpTpiHash(P, *Tpi);