From: Zachary Turner Date: Thu, 4 Aug 2016 19:39:55 +0000 (+0000) Subject: [CodeView] Use llvm::Error instead of std::error_code. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=660230eba420afe66178bd8895a64327e290a3fc;p=platform%2Fupstream%2Fllvm.git [CodeView] Use llvm::Error instead of std::error_code. This eliminates the remnants of std::error_code from the DebugInfo libraries. llvm-svn: 277758 --- diff --git a/llvm/include/llvm/DebugInfo/CodeView/CVSymbolVisitor.h b/llvm/include/llvm/DebugInfo/CodeView/CVSymbolVisitor.h index 7c88956..e417f36 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/CVSymbolVisitor.h +++ b/llvm/include/llvm/DebugInfo/CodeView/CVSymbolVisitor.h @@ -58,10 +58,12 @@ public: #define SYMBOL_RECORD(EnumName, EnumVal, Name) \ case EnumName: { \ SymbolRecordKind RK = static_cast(EnumName); \ - auto Result = Name::deserialize(RK, RecordOffset, Data); \ - if (Result.getError()) \ + auto ExpectedResult = Name::deserialize(RK, RecordOffset, Data); \ + if (!ExpectedResult) { \ + consumeError(ExpectedResult.takeError()); \ return parseError(); \ - DerivedThis->visit##Name(Record.Type, *Result); \ + } \ + DerivedThis->visit##Name(Record.Type, *ExpectedResult); \ break; \ } #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \ diff --git a/llvm/include/llvm/DebugInfo/CodeView/RecordSerialization.h b/llvm/include/llvm/DebugInfo/CodeView/RecordSerialization.h index 84179f5..606536b 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/RecordSerialization.h +++ b/llvm/include/llvm/DebugInfo/CodeView/RecordSerialization.h @@ -13,8 +13,10 @@ #include "llvm/ADT/APSInt.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/Endian.h" #include "llvm/DebugInfo/CodeView/CodeView.h" +#include "llvm/DebugInfo/CodeView/CodeViewError.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" #include #include @@ -38,49 +40,48 @@ StringRef getBytesAsCString(ArrayRef LeafData); /// there are not enough bytes remaining. Reinterprets the consumed bytes as a /// T object and points 'Res' at them. template -inline std::error_code consumeObject(U &Data, const T *&Res) { +inline Error consumeObject(U &Data, const T *&Res) { if (Data.size() < sizeof(*Res)) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Error consuming object. Not enough data for requested object size."); Res = reinterpret_cast(Data.data()); Data = Data.drop_front(sizeof(*Res)); - return std::error_code(); + return Error::success(); } -inline std::error_code consume(ArrayRef &Data) { - return std::error_code(); -} +inline Error consume(ArrayRef &Data) { return Error::success(); } /// Decodes a numeric "leaf" value. These are integer literals encountered in /// the type stream. If the value is positive and less than LF_NUMERIC (1 << /// 15), it is emitted directly in Data. Otherwise, it has a tag like LF_CHAR /// that indicates the bitwidth and sign of the numeric data. -std::error_code consume(ArrayRef &Data, APSInt &Num); -std::error_code consume(StringRef &Data, APSInt &Num); +Error consume(ArrayRef &Data, APSInt &Num); +Error consume(StringRef &Data, APSInt &Num); /// Decodes a numeric leaf value that is known to be a particular type. -std::error_code consume_numeric(ArrayRef &Data, uint64_t &Value); +Error consume_numeric(ArrayRef &Data, uint64_t &Value); /// Decodes signed and unsigned fixed-length integers. -std::error_code consume(ArrayRef &Data, uint32_t &Item); -std::error_code consume(StringRef &Data, uint32_t &Item); -std::error_code consume(ArrayRef &Data, int32_t &Item); +Error consume(ArrayRef &Data, uint32_t &Item); +Error consume(StringRef &Data, uint32_t &Item); +Error consume(ArrayRef &Data, int32_t &Item); /// Decodes a null terminated string. -std::error_code consume(ArrayRef &Data, StringRef &Item); +Error consume(ArrayRef &Data, StringRef &Item); /// Decodes an arbitrary object whose layout matches that of the underlying /// byte sequence, and returns a pointer to the object. -template -std::error_code consume(ArrayRef &Data, T *&Item) { +template Error consume(ArrayRef &Data, T *&Item) { return consumeObject(Data, Item); } template struct serialize_conditional_impl { serialize_conditional_impl(T &Item, U Func) : Item(Item), Func(Func) {} - std::error_code deserialize(ArrayRef &Data) const { + Error deserialize(ArrayRef &Data) const { if (!Func()) - return std::error_code(); + return Error::success(); return consume(Data, Item); } @@ -96,22 +97,26 @@ serialize_conditional_impl serialize_conditional(T &Item, U Func) { template struct serialize_array_impl { serialize_array_impl(ArrayRef &Item, U Func) : Item(Item), Func(Func) {} - std::error_code deserialize(ArrayRef &Data) const { + Error deserialize(ArrayRef &Data) const { uint32_t N = Func(); if (N == 0) - return std::error_code(); + return Error::success(); uint32_t Size = sizeof(T) * N; if (Size / sizeof(T) != N) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::corrupt_record, + "Array length is not a multiple of sizeof(T)"); if (Data.size() < Size) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::corrupt_record, + "Array does not contain enough data for all elements"); Item = ArrayRef(reinterpret_cast(Data.data()), N); Data = Data.drop_front(Size); - return std::error_code(); + return Error::success(); } ArrayRef &Item; @@ -121,7 +126,7 @@ template struct serialize_array_impl { template struct serialize_vector_tail_impl { serialize_vector_tail_impl(std::vector &Item) : Item(Item) {} - std::error_code deserialize(ArrayRef &Data) const { + Error deserialize(ArrayRef &Data) const { T Field; // Stop when we run out of bytes or we hit record padding bytes. while (!Data.empty() && Data.front() < LF_PAD0) { @@ -129,7 +134,7 @@ template struct serialize_vector_tail_impl { return EC; Item.push_back(Field); } - return std::error_code(); + return Error::success(); } std::vector &Item; @@ -139,9 +144,11 @@ struct serialize_null_term_string_array_impl { serialize_null_term_string_array_impl(std::vector &Item) : Item(Item) {} - std::error_code deserialize(ArrayRef &Data) const { + Error deserialize(ArrayRef &Data) const { if (Data.empty()) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Null terminated string buffer is empty!"); StringRef Field; // Stop when we run out of bytes or we hit record padding bytes. @@ -150,10 +157,12 @@ struct serialize_null_term_string_array_impl { return EC; Item.push_back(Field); if (Data.empty()) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Null terminated string buffer is empty!"); } Data = Data.drop_front(1); - return std::error_code(); + return Error::success(); } std::vector &Item; @@ -162,10 +171,10 @@ struct serialize_null_term_string_array_impl { template struct serialize_arrayref_tail_impl { serialize_arrayref_tail_impl(ArrayRef &Item) : Item(Item) {} - std::error_code deserialize(ArrayRef &Data) const { + Error deserialize(ArrayRef &Data) const { uint32_t Count = Data.size() / sizeof(T); Item = ArrayRef(reinterpret_cast(Data.begin()), Count); - return std::error_code(); + return Error::success(); } ArrayRef &Item; @@ -174,7 +183,7 @@ template struct serialize_arrayref_tail_impl { template struct serialize_numeric_impl { serialize_numeric_impl(T &Item) : Item(Item) {} - std::error_code deserialize(ArrayRef &Data) const { + Error deserialize(ArrayRef &Data) const { return consume_numeric(Data, Item); } @@ -226,44 +235,40 @@ template serialize_numeric_impl serialize_numeric(T &Item) { #define CV_NUMERIC_FIELD(I) serialize_numeric(I) template -std::error_code consume(ArrayRef &Data, - const serialize_conditional_impl &Item) { +Error consume(ArrayRef &Data, + const serialize_conditional_impl &Item) { return Item.deserialize(Data); } template -std::error_code consume(ArrayRef &Data, - const serialize_array_impl &Item) { +Error consume(ArrayRef &Data, const serialize_array_impl &Item) { return Item.deserialize(Data); } -inline std::error_code -consume(ArrayRef &Data, - const serialize_null_term_string_array_impl &Item) { +inline Error consume(ArrayRef &Data, + const serialize_null_term_string_array_impl &Item) { return Item.deserialize(Data); } template -std::error_code consume(ArrayRef &Data, - const serialize_vector_tail_impl &Item) { +Error consume(ArrayRef &Data, + const serialize_vector_tail_impl &Item) { return Item.deserialize(Data); } template -std::error_code consume(ArrayRef &Data, - const serialize_arrayref_tail_impl &Item) { +Error consume(ArrayRef &Data, + const serialize_arrayref_tail_impl &Item) { return Item.deserialize(Data); } template -std::error_code consume(ArrayRef &Data, - const serialize_numeric_impl &Item) { +Error consume(ArrayRef &Data, const serialize_numeric_impl &Item) { return Item.deserialize(Data); } template -std::error_code consume(ArrayRef &Data, T &&X, U &&Y, - Args &&... Rest) { +Error consume(ArrayRef &Data, T &&X, U &&Y, Args &&... Rest) { if (auto EC = consume(Data, X)) return EC; return consume(Data, Y, std::forward(Rest)...); @@ -271,7 +276,7 @@ std::error_code consume(ArrayRef &Data, T &&X, U &&Y, #define CV_DESERIALIZE(...) \ if (auto EC = consume(__VA_ARGS__)) \ - return EC; + return std::move(EC); } } diff --git a/llvm/include/llvm/DebugInfo/CodeView/SymbolRecord.h b/llvm/include/llvm/DebugInfo/CodeView/SymbolRecord.h index 21ad71f..dbea892 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/SymbolRecord.h +++ b/llvm/include/llvm/DebugInfo/CodeView/SymbolRecord.h @@ -61,9 +61,9 @@ public: : SymbolRecord(Kind), RecordOffset(RecordOffset), Header(*H), Name(Name) { } - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -100,9 +100,9 @@ public: : SymbolRecord(Kind), RecordOffset(RecordOffset), Header(*H), Name(Name), VariantData(VariantData) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; ArrayRef VariantData; @@ -133,9 +133,9 @@ public: TrampolineSym(SymbolRecordKind Kind, uint32_t RecordOffset, const Hdr *H) : SymbolRecord(Kind), RecordOffset(RecordOffset), Header(*H) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; CV_DESERIALIZE(Data, H); @@ -165,9 +165,9 @@ public: : SymbolRecord(Kind), RecordOffset(RecordOffset), Header(*H), Name(Name) { } - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; @@ -197,9 +197,9 @@ public: : SymbolRecord(Kind), RecordOffset(RecordOffset), Header(*H), Name(Name) { } - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; @@ -218,9 +218,9 @@ public: ScopeEndSym(SymbolRecordKind Kind, uint32_t RecordOffset) : SymbolRecord(Kind), RecordOffset(RecordOffset) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { return ScopeEndSym(Kind, RecordOffset); } uint32_t RecordOffset; @@ -237,9 +237,9 @@ public: : SymbolRecord(Kind), RecordOffset(RecordOffset), Header(*Header), Indices(Indices) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *Header; ArrayRef Indices; @@ -446,9 +446,9 @@ public: : SymbolRecord(SymbolRecordKind::InlineSiteSym), RecordOffset(RecordOffset), Header(*H), Annotations(Annotations) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; ArrayRef Annotations; CV_DESERIALIZE(Data, H, CV_ARRAY_FIELD_TAIL(Annotations)); @@ -482,9 +482,9 @@ public: : SymbolRecord(SymbolRecordKind::PublicSym32), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -510,9 +510,9 @@ public: : SymbolRecord(SymbolRecordKind::RegisterSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -539,9 +539,9 @@ public: : SymbolRecord(SymbolRecordKind::ProcRefSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -567,9 +567,9 @@ public: : SymbolRecord(SymbolRecordKind::LocalSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -609,9 +609,9 @@ public: : SymbolRecord(SymbolRecordKind::DefRangeSym), RecordOffset(RecordOffset), Header(*H), Gaps(Gaps) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; ArrayRef Gaps; CV_DESERIALIZE(Data, H, CV_ARRAY_FIELD_TAIL(Gaps)); @@ -642,9 +642,9 @@ public: : SymbolRecord(SymbolRecordKind::DefRangeSubfieldSym), RecordOffset(RecordOffset), Header(*H), Gaps(Gaps) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; ArrayRef Gaps; CV_DESERIALIZE(Data, H, CV_ARRAY_FIELD_TAIL(Gaps)); @@ -688,9 +688,9 @@ public: Header.Range.Range = Range; } - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; ArrayRef Gaps; CV_DESERIALIZE(Data, H, CV_ARRAY_FIELD_TAIL(Gaps)); @@ -733,7 +733,7 @@ public: Header.OffsetInParent = OffsetInParent; } - static ErrorOr + static Expected deserialize(SymbolRecordKind Kind, uint32_t RecordOffset, ArrayRef &Data) { const Hdr *H = nullptr; @@ -766,7 +766,7 @@ public: : SymbolRecord(SymbolRecordKind::DefRangeFramePointerRelSym), RecordOffset(RecordOffset), Header(*H), Gaps(Gaps) {} - static ErrorOr + static Expected deserialize(SymbolRecordKind Kind, uint32_t RecordOffset, ArrayRef &Data) { const Hdr *H = nullptr; @@ -815,9 +815,9 @@ public: Header.Range.Range = Range; } - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; ArrayRef Gaps; CV_DESERIALIZE(Data, H, CV_ARRAY_FIELD_TAIL(Gaps)); @@ -848,7 +848,7 @@ public: : SymbolRecord(SymbolRecordKind::DefRangeFramePointerRelFullScopeSym), RecordOffset(RecordOffset), Header(*H) {} - static ErrorOr + static Expected deserialize(SymbolRecordKind Kind, uint32_t RecordOffset, ArrayRef &Data) { const Hdr *H = nullptr; @@ -877,9 +877,9 @@ public: : SymbolRecord(SymbolRecordKind::BlockSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -910,9 +910,9 @@ public: : SymbolRecord(SymbolRecordKind::LabelSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -941,9 +941,9 @@ public: : SymbolRecord(SymbolRecordKind::ObjNameSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -969,9 +969,9 @@ public: : SymbolRecord(SymbolRecordKind::EnvBlockSym), RecordOffset(RecordOffset), Header(*H), Fields(Fields) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; std::vector Fields; CV_DESERIALIZE(Data, H, CV_STRING_ARRAY_NULL_TERM(Fields)); @@ -997,9 +997,9 @@ public: : SymbolRecord(SymbolRecordKind::ExportSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -1026,9 +1026,9 @@ public: : SymbolRecord(SymbolRecordKind::FileStaticSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -1062,9 +1062,9 @@ public: : SymbolRecord(SymbolRecordKind::Compile2Sym), RecordOffset(RecordOffset), Header(*H), Version(Version) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Version; CV_DESERIALIZE(Data, H, Version); @@ -1099,9 +1099,9 @@ public: : SymbolRecord(SymbolRecordKind::Compile3Sym), RecordOffset(RecordOffset), Header(*H), Version(Version) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Version; CV_DESERIALIZE(Data, H, Version); @@ -1131,9 +1131,9 @@ public: : SymbolRecord(SymbolRecordKind::FrameProcSym), RecordOffset(RecordOffset), Header(*H) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; CV_DESERIALIZE(Data, H); @@ -1158,9 +1158,9 @@ public: : SymbolRecord(SymbolRecordKind::CallSiteInfoSym), RecordOffset(RecordOffset), Header(*H) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; CV_DESERIALIZE(Data, H); @@ -1189,9 +1189,9 @@ public: : SymbolRecord(SymbolRecordKind::HeapAllocationSiteSym), RecordOffset(RecordOffset), Header(*H) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; CV_DESERIALIZE(Data, H); @@ -1220,9 +1220,9 @@ public: : SymbolRecord(SymbolRecordKind::FrameCookieSym), RecordOffset(RecordOffset), Header(*H) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; CV_DESERIALIZE(Data, H); @@ -1249,9 +1249,9 @@ public: : SymbolRecord(SymbolRecordKind::UDTSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -1275,9 +1275,9 @@ public: : SymbolRecord(SymbolRecordKind::BuildInfoSym), RecordOffset(RecordOffset), Header(*H) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; CV_DESERIALIZE(Data, H); @@ -1301,9 +1301,9 @@ public: : SymbolRecord(SymbolRecordKind::BPRelativeSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -1330,9 +1330,9 @@ public: : SymbolRecord(SymbolRecordKind::RegRelativeSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -1359,9 +1359,9 @@ public: : SymbolRecord(SymbolRecordKind::ConstantSym), RecordOffset(RecordOffset), Header(*H), Value(Value), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; APSInt Value; StringRef Name; @@ -1390,9 +1390,9 @@ public: : SymbolRecord(SymbolRecordKind::DataSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); @@ -1423,9 +1423,9 @@ public: : SymbolRecord(SymbolRecordKind::ThreadLocalDataSym), RecordOffset(RecordOffset), Header(*H), Name(Name) {} - static ErrorOr deserialize(SymbolRecordKind Kind, - uint32_t RecordOffset, - ArrayRef &Data) { + static Expected deserialize(SymbolRecordKind Kind, + uint32_t RecordOffset, + ArrayRef &Data) { const Hdr *H = nullptr; StringRef Name; CV_DESERIALIZE(Data, H, Name); diff --git a/llvm/include/llvm/DebugInfo/CodeView/TypeRecord.h b/llvm/include/llvm/DebugInfo/CodeView/TypeRecord.h index bbb3769..8bc61f9 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/TypeRecord.h +++ b/llvm/include/llvm/DebugInfo/CodeView/TypeRecord.h @@ -16,7 +16,7 @@ #include "llvm/DebugInfo/CodeView/CVRecord.h" #include "llvm/DebugInfo/CodeView/CodeView.h" #include "llvm/DebugInfo/CodeView/TypeIndex.h" -#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/Error.h" #include #include @@ -83,7 +83,7 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(ArrayRef &Data); + static Expected deserialize(ArrayRef &Data); TypeIndex getContainingType() const { return ContainingType; } PointerToMemberRepresentation getRepresentation() const { @@ -122,8 +122,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getModifiedType() const { return ModifiedType; } ModifierOptions getModifiers() const { return Modifiers; } @@ -152,8 +152,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); static uint32_t getLayoutSize() { return 2 + sizeof(Layout); } @@ -196,8 +196,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getReturnType() const { return ReturnType; } TypeIndex getClassType() const { return ClassType; } @@ -242,8 +242,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getClassType() const { return ClassType; } TypeIndex getFunctionType() const { return FunctionType; } StringRef getName() const { return Name; } @@ -269,8 +269,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); ArrayRef getIndices() const { return StringIndices; } @@ -313,8 +313,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getReferentType() const { return ReferentType; } PointerKind getPointerKind() const { return PtrKind; } @@ -389,8 +389,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getNestedType() const { return Type; } StringRef getName() const { return Name; } @@ -418,8 +418,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getElementType() const { return ElementType; } TypeIndex getIndexType() const { return IndexType; } @@ -486,8 +486,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); HfaKind getHfa() const { return Hfa; } WindowsRTClassKind getWinRTKind() const { return WinRTKind; } @@ -527,8 +527,8 @@ struct UnionRecord : public TagRecord { UniqueName), Hfa(Hfa), Size(Size) {} - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); HfaKind getHfa() const { return Hfa; } uint64_t getSize() const { return Size; } @@ -563,8 +563,8 @@ public: /// Rewrite member type indices with IndexMap. Returns false if a type index is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getUnderlyingType() const { return UnderlyingType; } @@ -595,8 +595,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getType() const { return Type; } uint8_t getBitOffset() const { return BitOffset; } @@ -626,8 +626,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); ArrayRef getSlots() const { if (!SlotsRef.empty()) @@ -661,8 +661,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); StringRef getGuid() const { return Guid; } @@ -692,8 +692,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getId() const { return Id; } @@ -720,8 +720,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getParentScope() const { return ParentScope; } @@ -752,8 +752,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getUDT() const { return UDT; } TypeIndex getSourceFile() const { return SourceFile; } @@ -781,8 +781,8 @@ public: bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data) { + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; CV_DESERIALIZE(Data, L); @@ -820,8 +820,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); ArrayRef getArgs() const { return ArgIndices; } @@ -853,8 +853,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getCompleteClass() const { return CompleteClass; } TypeIndex getOverriddenVTable() const { return OverriddenVFTable; } @@ -897,8 +897,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getType() const { return Type; } MethodKind getKind() const { return Kind; } @@ -939,8 +939,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected + deserialize(TypeRecordKind Kind, ArrayRef &Data); ArrayRef getMethods() const { return Methods; } @@ -969,8 +969,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); uint16_t getNumOverloads() const { return NumOverloads; } TypeIndex getMethodList() const { return MethodList; } @@ -1000,8 +1000,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); MemberAccess getAccess() const { return Access; } TypeIndex getType() const { return Type; } @@ -1033,8 +1033,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); MemberAccess getAccess() const { return Access; } TypeIndex getType() const { return Type; } @@ -1063,8 +1063,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); MemberAccess getAccess() const { return Access; } APSInt getValue() const { return Value; } @@ -1092,8 +1092,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); TypeIndex getType() const { return Type; } @@ -1116,8 +1116,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); MemberAccess getAccess() const { return Access; } TypeIndex getBaseType() const { return Type; } @@ -1147,8 +1147,8 @@ public: /// is not in the map. bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); MemberAccess getAccess() const { return Access; } TypeIndex getBaseType() const { return BaseType; } @@ -1183,8 +1183,8 @@ public: bool remapTypeIndices(ArrayRef IndexMap); - static ErrorOr deserialize(TypeRecordKind Kind, - ArrayRef &Data); + static Expected deserialize(TypeRecordKind Kind, + ArrayRef &Data); private: struct Layout { diff --git a/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp b/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp index f1f9acf..a7b7f3a 100644 --- a/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp +++ b/llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp @@ -45,10 +45,10 @@ Error CVTypeVisitor::visitTypeRecord(const CVRecord &Record) { #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(*Result)) \ + auto ExpectedRecord = Name##Record::deserialize(RK, LeafData); \ + if (!ExpectedRecord) \ + return ExpectedRecord.takeError(); \ + if (auto EC = Callbacks.visit##Name(*ExpectedRecord)) \ return EC; \ break; \ } @@ -106,10 +106,10 @@ Error CVTypeVisitor::visitFieldList(const CVRecord &Record) { #define MEMBER_RECORD(EnumName, EnumVal, Name) \ case EnumName: { \ TypeRecordKind RK = static_cast(EnumName); \ - auto Result = Name##Record::deserialize(RK, RecordData); \ - if (Result.getError()) \ - return llvm::make_error(cv_error_code::corrupt_record); \ - if (auto EC = Callbacks.visit##Name(*Result)) \ + auto ExpectedRecord = Name##Record::deserialize(RK, RecordData); \ + if (!ExpectedRecord) \ + return ExpectedRecord.takeError(); \ + if (auto EC = Callbacks.visit##Name(*ExpectedRecord)) \ return EC; \ break; \ } diff --git a/llvm/lib/DebugInfo/CodeView/RecordSerialization.cpp b/llvm/lib/DebugInfo/CodeView/RecordSerialization.cpp index ab9206a..9386d29 100644 --- a/llvm/lib/DebugInfo/CodeView/RecordSerialization.cpp +++ b/llvm/lib/DebugInfo/CodeView/RecordSerialization.cpp @@ -14,6 +14,7 @@ #include "llvm/DebugInfo/CodeView/RecordSerialization.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/APSInt.h" +#include "llvm/DebugInfo/CodeView/CodeViewError.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h" using namespace llvm; @@ -31,87 +32,104 @@ StringRef llvm::codeview::getBytesAsCString(ArrayRef LeafData) { return getBytesAsCharacters(LeafData).split('\0').first; } -std::error_code llvm::codeview::consume(ArrayRef &Data, APSInt &Num) { +Error llvm::codeview::consume(ArrayRef &Data, APSInt &Num) { // Used to avoid overload ambiguity on APInt construtor. bool FalseVal = false; if (Data.size() < 2) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an APSInt"); uint16_t Short = *reinterpret_cast(Data.data()); Data = Data.drop_front(2); if (Short < LF_NUMERIC) { Num = APSInt(APInt(/*numBits=*/16, Short, /*isSigned=*/false), /*isUnsigned=*/true); - return std::error_code(); + return Error::success(); } switch (Short) { case LF_CHAR: if (Data.size() < 1) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an LF_CHAR"); Num = APSInt(APInt(/*numBits=*/8, *reinterpret_cast(Data.data()), /*isSigned=*/true), /*isUnsigned=*/false); Data = Data.drop_front(1); - return std::error_code(); + return Error::success(); case LF_SHORT: if (Data.size() < 2) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an LF_SHORT"); Num = APSInt(APInt(/*numBits=*/16, *reinterpret_cast(Data.data()), /*isSigned=*/true), /*isUnsigned=*/false); Data = Data.drop_front(2); - return std::error_code(); + return Error::success(); case LF_USHORT: if (Data.size() < 2) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an LF_USHORT"); Num = APSInt(APInt(/*numBits=*/16, *reinterpret_cast(Data.data()), /*isSigned=*/false), /*isUnsigned=*/true); Data = Data.drop_front(2); - return std::error_code(); + return Error::success(); case LF_LONG: if (Data.size() < 4) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an LF_LONG"); Num = APSInt(APInt(/*numBits=*/32, *reinterpret_cast(Data.data()), /*isSigned=*/true), /*isUnsigned=*/false); Data = Data.drop_front(4); - return std::error_code(); + return Error::success(); case LF_ULONG: if (Data.size() < 4) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an LF_ULONG"); Num = APSInt(APInt(/*numBits=*/32, *reinterpret_cast(Data.data()), /*isSigned=*/FalseVal), /*isUnsigned=*/true); Data = Data.drop_front(4); - return std::error_code(); + return Error::success(); case LF_QUADWORD: if (Data.size() < 8) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an LF_QUADWORD"); Num = APSInt(APInt(/*numBits=*/64, *reinterpret_cast(Data.data()), /*isSigned=*/true), /*isUnsigned=*/false); Data = Data.drop_front(8); - return std::error_code(); + return Error::success(); case LF_UQUADWORD: if (Data.size() < 8) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::insufficient_buffer, + "Buffer does not contain enough data for an LF_UQUADWORD"); Num = APSInt(APInt(/*numBits=*/64, *reinterpret_cast(Data.data()), /*isSigned=*/false), /*isUnsigned=*/true); Data = Data.drop_front(8); - return std::error_code(); + return Error::success(); } - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error(cv_error_code::corrupt_record, + "Buffer contains invalid APSInt type"); } -std::error_code llvm::codeview::consume(StringRef &Data, APSInt &Num) { +Error llvm::codeview::consume(StringRef &Data, APSInt &Num) { ArrayRef Bytes(Data.bytes_begin(), Data.bytes_end()); auto EC = consume(Bytes, Num); Data = StringRef(reinterpret_cast(Bytes.data()), Bytes.size()); @@ -119,53 +137,52 @@ std::error_code llvm::codeview::consume(StringRef &Data, APSInt &Num) { } /// Decode a numeric leaf value that is known to be a uint64_t. -std::error_code llvm::codeview::consume_numeric(ArrayRef &Data, - uint64_t &Num) { +Error llvm::codeview::consume_numeric(ArrayRef &Data, uint64_t &Num) { APSInt N; if (auto EC = consume(Data, N)) return EC; if (N.isSigned() || !N.isIntN(64)) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error(cv_error_code::corrupt_record, + "Data is not a numeric value!"); Num = N.getLimitedValue(); - return std::error_code(); + return Error::success(); } -std::error_code llvm::codeview::consume(ArrayRef &Data, - uint32_t &Item) { +Error llvm::codeview::consume(ArrayRef &Data, uint32_t &Item) { const support::ulittle32_t *IntPtr; if (auto EC = consumeObject(Data, IntPtr)) return EC; Item = *IntPtr; - return std::error_code(); + return Error::success(); } -std::error_code llvm::codeview::consume(StringRef &Data, uint32_t &Item) { +Error llvm::codeview::consume(StringRef &Data, uint32_t &Item) { ArrayRef Bytes(Data.bytes_begin(), Data.bytes_end()); auto EC = consume(Bytes, Item); Data = StringRef(reinterpret_cast(Bytes.data()), Bytes.size()); return EC; } -std::error_code llvm::codeview::consume(ArrayRef &Data, - int32_t &Item) { +Error llvm::codeview::consume(ArrayRef &Data, int32_t &Item) { const support::little32_t *IntPtr; if (auto EC = consumeObject(Data, IntPtr)) return EC; Item = *IntPtr; - return std::error_code(); + return Error::success(); } -std::error_code llvm::codeview::consume(ArrayRef &Data, - StringRef &Item) { +Error llvm::codeview::consume(ArrayRef &Data, StringRef &Item) { if (Data.empty()) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error(cv_error_code::corrupt_record, + "Null terminated string buffer is empty!"); StringRef Rest; std::tie(Item, Rest) = getBytesAsCharacters(Data).split('\0'); // We expect this to be null terminated. If it was not, it is an error. if (Data.size() == Item.size()) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error(cv_error_code::corrupt_record, + "Expected null terminator!"); Data = ArrayRef(Rest.bytes_begin(), Rest.bytes_end()); - return std::error_code(); + return Error::success(); } diff --git a/llvm/lib/DebugInfo/CodeView/TypeRecord.cpp b/llvm/lib/DebugInfo/CodeView/TypeRecord.cpp index f63371e..4bbd261 100644 --- a/llvm/lib/DebugInfo/CodeView/TypeRecord.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeRecord.cpp @@ -18,11 +18,11 @@ using namespace llvm::codeview; // Type record deserialization //===----------------------------------------------------------------------===// -ErrorOr +Expected MemberPointerInfo::deserialize(ArrayRef &Data) { const Layout *L = nullptr; if (auto EC = consumeObject(Data, L)) - return EC; + return std::move(EC); TypeIndex T = L->ClassType; uint16_t R = L->Representation; @@ -31,11 +31,11 @@ MemberPointerInfo::deserialize(ArrayRef &Data) { return MemberPointerInfo(T, PMR); } -ErrorOr ModifierRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected ModifierRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; if (auto EC = consumeObject(Data, L)) - return EC; + return std::move(EC); TypeIndex M = L->ModifiedType; uint16_t O = L->Modifiers; @@ -43,16 +43,16 @@ ErrorOr ModifierRecord::deserialize(TypeRecordKind Kind, return ModifierRecord(M, MO); } -ErrorOr ProcedureRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected +ProcedureRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; if (auto EC = consumeObject(Data, L)) - return EC; + return std::move(EC); return ProcedureRecord(L->ReturnType, L->CallConv, L->Options, L->NumParameters, L->ArgListType); } -ErrorOr +Expected MemberFunctionRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; @@ -62,7 +62,7 @@ MemberFunctionRecord::deserialize(TypeRecordKind Kind, L->ArgListType, L->ThisAdjustment); } -ErrorOr +Expected MemberFuncIdRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; @@ -70,10 +70,12 @@ MemberFuncIdRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { return MemberFuncIdRecord(L->ClassType, L->FunctionType, Name); } -ErrorOr ArgListRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected ArgListRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { if (Kind != TypeRecordKind::StringList && Kind != TypeRecordKind::ArgList) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error( + cv_error_code::corrupt_record, + "ArgListRecord contains unexpected TypeRecordKind"); const Layout *L = nullptr; ArrayRef Indices; @@ -81,11 +83,11 @@ ErrorOr ArgListRecord::deserialize(TypeRecordKind Kind, return ArgListRecord(Kind, Indices); } -ErrorOr PointerRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected PointerRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; if (auto EC = consumeObject(Data, L)) - return EC; + return std::move(EC); PointerKind PtrKind = L->getPtrKind(); PointerMode Mode = L->getPtrMode(); @@ -94,16 +96,17 @@ ErrorOr PointerRecord::deserialize(TypeRecordKind Kind, uint8_t Size = L->getPtrSize(); if (L->isPointerToMember()) { - auto E = MemberPointerInfo::deserialize(Data); - if (E.getError()) - return std::make_error_code(std::errc::illegal_byte_sequence); - return PointerRecord(L->PointeeType, PtrKind, Mode, Options, Size, *E); + if (auto ExpectedMPI = MemberPointerInfo::deserialize(Data)) + return PointerRecord(L->PointeeType, PtrKind, Mode, Options, Size, + *ExpectedMPI); + else + return ExpectedMPI.takeError(); } return PointerRecord(L->PointeeType, PtrKind, Mode, Options, Size); } -ErrorOr +Expected NestedTypeRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; @@ -111,8 +114,8 @@ NestedTypeRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { return NestedTypeRecord(L->Type, Name); } -ErrorOr ArrayRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected ArrayRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; uint64_t Size; StringRef Name; @@ -120,8 +123,8 @@ ErrorOr ArrayRecord::deserialize(TypeRecordKind Kind, return ArrayRecord(L->ElementType, L->IndexType, Size, Name); } -ErrorOr ClassRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected ClassRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { uint64_t Size = 0; StringRef Name; StringRef UniqueName; @@ -142,8 +145,8 @@ ErrorOr ClassRecord::deserialize(TypeRecordKind Kind, L->DerivedFrom, L->VShape, Size, Name, UniqueName); } -ErrorOr UnionRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected UnionRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { uint64_t Size = 0; StringRef Name; StringRef UniqueName; @@ -162,8 +165,8 @@ ErrorOr UnionRecord::deserialize(TypeRecordKind Kind, UniqueName); } -ErrorOr EnumRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected EnumRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; StringRef UniqueName; @@ -176,24 +179,25 @@ ErrorOr EnumRecord::deserialize(TypeRecordKind Kind, UniqueName, L->UnderlyingType); } -ErrorOr BitFieldRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected BitFieldRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; CV_DESERIALIZE(Data, L); return BitFieldRecord(L->Type, L->BitSize, L->BitOffset); } -ErrorOr +Expected VFTableShapeRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; if (auto EC = consumeObject(Data, L)) - return EC; + return std::move(EC); std::vector Slots; uint16_t Count = L->VFEntryCount; while (Count > 0) { if (Data.empty()) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error(cv_error_code::corrupt_record, + "VTableShapeRecord contains no entries"); // Process up to 2 nibbles at a time (if there are at least 2 remaining) uint8_t Value = Data[0] & 0x0F; @@ -209,7 +213,7 @@ VFTableShapeRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { return VFTableShapeRecord(Slots); } -ErrorOr +Expected TypeServer2Record::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; @@ -217,39 +221,39 @@ TypeServer2Record::deserialize(TypeRecordKind Kind, ArrayRef &Data) { return TypeServer2Record(StringRef(L->Guid, 16), L->Age, Name); } -ErrorOr StringIdRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected StringIdRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; CV_DESERIALIZE(Data, L, Name); return StringIdRecord(L->id, Name); } -ErrorOr FuncIdRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected FuncIdRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; CV_DESERIALIZE(Data, L, Name); return FuncIdRecord(L->ParentScope, L->FunctionType, Name); } -ErrorOr +Expected UdtSourceLineRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; CV_DESERIALIZE(Data, L); return UdtSourceLineRecord(L->UDT, L->SourceFile, L->LineNumber); } -ErrorOr BuildInfoRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected +BuildInfoRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; ArrayRef Indices; CV_DESERIALIZE(Data, L, CV_ARRAY_FIELD_N(Indices, L->NumArgs)); return BuildInfoRecord(Indices); } -ErrorOr VFTableRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected VFTableRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; std::vector Names; @@ -258,8 +262,8 @@ ErrorOr VFTableRecord::deserialize(TypeRecordKind Kind, Name, Names); } -ErrorOr OneMethodRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected +OneMethodRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; StringRef Name; int32_t VFTableOffset = -1; @@ -275,11 +279,12 @@ ErrorOr OneMethodRecord::deserialize(TypeRecordKind Kind, Name); // Validate the vftable offset. if (Method.isIntroducingVirtual() && Method.getVFTableOffset() < 0) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error(cv_error_code::corrupt_record, + "Invalid VFTableOffset"); return Method; } -ErrorOr +Expected MethodOverloadListRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { std::vector Methods; @@ -299,12 +304,13 @@ MethodOverloadListRecord::deserialize(TypeRecordKind Kind, // Validate the vftable offset. auto &Method = Methods.back(); if (Method.isIntroducingVirtual() && Method.getVFTableOffset() < 0) - return std::make_error_code(std::errc::illegal_byte_sequence); + return make_error(cv_error_code::corrupt_record, + "Invalid VFTableOffset"); } return MethodOverloadListRecord(Methods); } -ErrorOr +Expected OverloadedMethodRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; @@ -313,7 +319,7 @@ OverloadedMethodRecord::deserialize(TypeRecordKind Kind, return OverloadedMethodRecord(L->MethodCount, L->MethList, Name); } -ErrorOr +Expected DataMemberRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; uint64_t Offset; @@ -322,7 +328,7 @@ DataMemberRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { return DataMemberRecord(L->Attrs.getAccess(), L->Type, Offset, Name); } -ErrorOr +Expected StaticDataMemberRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; @@ -331,7 +337,7 @@ StaticDataMemberRecord::deserialize(TypeRecordKind Kind, return StaticDataMemberRecord(L->Attrs.getAccess(), L->Type, Name); } -ErrorOr +Expected EnumeratorRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; APSInt Value; @@ -340,23 +346,23 @@ EnumeratorRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { return EnumeratorRecord(L->Attrs.getAccess(), Value, Name); } -ErrorOr VFPtrRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected VFPtrRecord::deserialize(TypeRecordKind Kind, + ArrayRef &Data) { const Layout *L = nullptr; if (auto EC = consumeObject(Data, L)) - return EC; + return std::move(EC); return VFPtrRecord(L->Type); } -ErrorOr BaseClassRecord::deserialize(TypeRecordKind Kind, - ArrayRef &Data) { +Expected +BaseClassRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; uint64_t Offset; CV_DESERIALIZE(Data, L, CV_NUMERIC_FIELD(Offset)); return BaseClassRecord(L->Attrs.getAccess(), L->BaseType, Offset); } -ErrorOr +Expected VirtualBaseClassRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; @@ -367,7 +373,7 @@ VirtualBaseClassRecord::deserialize(TypeRecordKind Kind, Offset, Index); } -ErrorOr +Expected ListContinuationRecord::deserialize(TypeRecordKind Kind, ArrayRef &Data) { const Layout *L = nullptr; diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp index 032283e..7ccc2c7 100644 --- a/llvm/tools/llvm-readobj/llvm-readobj.cpp +++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -272,6 +272,14 @@ LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { exit(1); } +void error(llvm::Error EC) { + if (!EC) + return; + + handleAllErrors(std::move(EC), + [&](ErrorInfoBase &EI) { reportError(EI.message()); }); +} + void error(std::error_code EC) { if (!EC) return; diff --git a/llvm/tools/llvm-readobj/llvm-readobj.h b/llvm/tools/llvm-readobj/llvm-readobj.h index b169c00..862fd13 100644 --- a/llvm/tools/llvm-readobj/llvm-readobj.h +++ b/llvm/tools/llvm-readobj/llvm-readobj.h @@ -24,6 +24,7 @@ namespace llvm { // Various helper functions. LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg); void error(std::error_code ec); + void error(llvm::Error EC); template T unwrapOrError(ErrorOr EO) { if (EO) return *EO;