Skip to content

Commit cba595d

Browse files
author
Victor Leschuk
committed
[DWARF] Refactor DWARF classes to use unified error reporting. NFC.
DWARF-related classes in lib/DebugInfo/DWARF contained duplicating code for creating StringError instances, like: template <typename... Ts> static Error createError(char const *Fmt, const Ts &... Vals) { std::string Buffer; raw_string_ostream Stream(Buffer); Stream << format(Fmt, Vals...); return make_error<StringError>(Stream.str(), inconvertibleErrorCode()); } Similar function was placed in Support lib in https://reviews.llvm.org/D49824 This revision makes DWARF classes use this function instead of their local implementation of it. Reviewers: aprantl, dblaikie, probinson, wolfgangp, JDevlieghere, jhenderson Reviewed By: JDevlieghere, jhenderson Differential Revision: https://reviews.llvm.org/D49964 llvm-svn: 340163
1 parent bbd2d15 commit cba595d

9 files changed

+106
-134
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFListTable.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/BinaryFormat/Dwarf.h"
1414
#include "llvm/DebugInfo/DIContext.h"
1515
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
16+
#include "llvm/Support/Errc.h"
1617
#include "llvm/Support/Error.h"
1718
#include "llvm/Support/Format.h"
1819
#include "llvm/Support/raw_ostream.h"
@@ -43,10 +44,6 @@ template <typename ListEntryType> class DWARFListType {
4344
ListEntries Entries;
4445

4546
public:
46-
// FIXME: We need to consolidate the various verions of "createError"
47-
// that are used in the DWARF consumer. Until then, this is a workaround.
48-
Error createError(const char *, const char *, uint32_t);
49-
5047
const ListEntries &getEntries() const { return Entries; }
5148
bool empty() const { return Entries.empty(); }
5249
void clear() { Entries.clear(); }
@@ -213,7 +210,8 @@ Error DWARFListType<ListEntryType>::extract(DWARFDataExtractor Data,
213210
StringRef SectionName,
214211
StringRef ListTypeString) {
215212
if (*OffsetPtr < HeaderOffset || *OffsetPtr >= End)
216-
return createError("invalid %s list offset 0x%" PRIx32,
213+
return createStringError(errc::invalid_argument,
214+
"invalid %s list offset 0x%" PRIx32,
217215
ListTypeString.data(), *OffsetPtr);
218216
Entries.clear();
219217
while (*OffsetPtr < End) {
@@ -224,7 +222,8 @@ Error DWARFListType<ListEntryType>::extract(DWARFDataExtractor Data,
224222
if (Entry.isSentinel())
225223
return Error::success();
226224
}
227-
return createError("no end of list marker detected at end of %s table "
225+
return createStringError(errc::illegal_byte_sequence,
226+
"no end of list marker detected at end of %s table "
228227
"starting at offset 0x%" PRIx32,
229228
SectionName.data(), HeaderOffset);
230229
}

llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp

+26-28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
1515
#include "llvm/Support/Compiler.h"
1616
#include "llvm/Support/DJB.h"
17+
#include "llvm/Support/Errc.h"
1718
#include "llvm/Support/Format.h"
1819
#include "llvm/Support/FormatVariadic.h"
1920
#include "llvm/Support/ScopedPrinter.h"
@@ -45,9 +46,9 @@ llvm::Error AppleAcceleratorTable::extract() {
4546
uint32_t Offset = 0;
4647

4748
// Check that we can at least read the header.
48-
if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
49-
return make_error<StringError>("Section too small: cannot read header.",
50-
inconvertibleErrorCode());
49+
if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength) + 4))
50+
return createStringError(errc::illegal_byte_sequence,
51+
"Section too small: cannot read header.");
5152

5253
Hdr.Magic = AccelSection.getU32(&Offset);
5354
Hdr.Version = AccelSection.getU16(&Offset);
@@ -62,9 +63,9 @@ llvm::Error AppleAcceleratorTable::extract() {
6263
// equal to the size for an empty table and hence pointer after the section.
6364
if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
6465
Hdr.BucketCount * 4 + Hdr.HashCount * 8 - 1))
65-
return make_error<StringError>(
66-
"Section too small: cannot read buckets and hashes.",
67-
inconvertibleErrorCode());
66+
return createStringError(
67+
errc::illegal_byte_sequence,
68+
"Section too small: cannot read buckets and hashes.");
6869

6970
HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
7071
uint32_t NumAtoms = AccelSection.getU32(&Offset);
@@ -380,8 +381,8 @@ llvm::Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS,
380381
uint32_t *Offset) {
381382
// Check that we can read the fixed-size part.
382383
if (!AS.isValidOffset(*Offset + sizeof(HeaderPOD) - 1))
383-
return make_error<StringError>("Section too small: cannot read header.",
384-
inconvertibleErrorCode());
384+
return createStringError(errc::illegal_byte_sequence,
385+
"Section too small: cannot read header.");
385386

386387
UnitLength = AS.getU32(Offset);
387388
Version = AS.getU16(Offset);
@@ -395,9 +396,9 @@ llvm::Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS,
395396
AugmentationStringSize = alignTo(AS.getU32(Offset), 4);
396397

397398
if (!AS.isValidOffsetForDataOfSize(*Offset, AugmentationStringSize))
398-
return make_error<StringError>(
399-
"Section too small: cannot read header augmentation.",
400-
inconvertibleErrorCode());
399+
return createStringError(
400+
errc::illegal_byte_sequence,
401+
"Section too small: cannot read header augmentation.");
401402
AugmentationString.resize(AugmentationStringSize);
402403
AS.getU8(Offset, reinterpret_cast<uint8_t *>(AugmentationString.data()),
403404
AugmentationStringSize);
@@ -439,8 +440,8 @@ DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getTombstoneKey() {
439440
Expected<DWARFDebugNames::AttributeEncoding>
440441
DWARFDebugNames::NameIndex::extractAttributeEncoding(uint32_t *Offset) {
441442
if (*Offset >= EntriesBase) {
442-
return make_error<StringError>("Incorrectly terminated abbreviation table.",
443-
inconvertibleErrorCode());
443+
return createStringError(errc::illegal_byte_sequence,
444+
"Incorrectly terminated abbreviation table.");
444445
}
445446

446447
uint32_t Index = Section.AccelSection.getULEB128(Offset);
@@ -465,8 +466,8 @@ DWARFDebugNames::NameIndex::extractAttributeEncodings(uint32_t *Offset) {
465466
Expected<DWARFDebugNames::Abbrev>
466467
DWARFDebugNames::NameIndex::extractAbbrev(uint32_t *Offset) {
467468
if (*Offset >= EntriesBase) {
468-
return make_error<StringError>("Incorrectly terminated abbreviation table.",
469-
inconvertibleErrorCode());
469+
return createStringError(errc::illegal_byte_sequence,
470+
"Incorrectly terminated abbreviation table.");
470471
}
471472

472473
uint32_t Code = Section.AccelSection.getULEB128(Offset);
@@ -501,9 +502,8 @@ Error DWARFDebugNames::NameIndex::extract() {
501502
Offset += Hdr.NameCount * 4;
502503

503504
if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize))
504-
return make_error<StringError>(
505-
"Section too small: cannot read abbreviations.",
506-
inconvertibleErrorCode());
505+
return createStringError(errc::illegal_byte_sequence,
506+
"Section too small: cannot read abbreviations.");
507507

508508
EntriesBase = Offset + Hdr.AbbrevTableSize;
509509

@@ -514,10 +514,9 @@ Error DWARFDebugNames::NameIndex::extract() {
514514
if (isSentinel(*AbbrevOr))
515515
return Error::success();
516516

517-
if (!Abbrevs.insert(std::move(*AbbrevOr)).second) {
518-
return make_error<StringError>("Duplicate abbreviation code.",
519-
inconvertibleErrorCode());
520-
}
517+
if (!Abbrevs.insert(std::move(*AbbrevOr)).second)
518+
return createStringError(errc::invalid_argument,
519+
"Duplicate abbreviation code.");
521520
}
522521
}
523522
DWARFDebugNames::Entry::Entry(const NameIndex &NameIdx, const Abbrev &Abbr)
@@ -600,25 +599,24 @@ Expected<DWARFDebugNames::Entry>
600599
DWARFDebugNames::NameIndex::getEntry(uint32_t *Offset) const {
601600
const DWARFDataExtractor &AS = Section.AccelSection;
602601
if (!AS.isValidOffset(*Offset))
603-
return make_error<StringError>("Incorrectly terminated entry list.",
604-
inconvertibleErrorCode());
602+
return createStringError(errc::illegal_byte_sequence,
603+
"Incorrectly terminated entry list.");
605604

606605
uint32_t AbbrevCode = AS.getULEB128(Offset);
607606
if (AbbrevCode == 0)
608607
return make_error<SentinelError>();
609608

610609
const auto AbbrevIt = Abbrevs.find_as(AbbrevCode);
611610
if (AbbrevIt == Abbrevs.end())
612-
return make_error<StringError>("Invalid abbreviation.",
613-
inconvertibleErrorCode());
611+
return createStringError(errc::invalid_argument, "Invalid abbreviation.");
614612

615613
Entry E(*this, *AbbrevIt);
616614

617615
dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32};
618616
for (auto &Value : E.Values) {
619617
if (!Value.extractValue(AS, Offset, FormParams))
620-
return make_error<StringError>("Error extracting index attribute values.",
621-
inconvertibleErrorCode());
618+
return createStringError(errc::io_error,
619+
"Error extracting index attribute values.");
622620
}
623621
return std::move(E);
624622
}

llvm/lib/DebugInfo/DWARF/DWARFContext.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,8 @@ Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
15981598
const Target *TheTarget =
15991599
TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
16001600
if (!TargetLookupError.empty())
1601-
return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1601+
return createStringError(errc::invalid_argument,
1602+
TargetLookupError.c_str());
16021603
RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
16031604
return Error::success();
16041605
}

llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/Support/Casting.h"
1717
#include "llvm/Support/Compiler.h"
1818
#include "llvm/Support/DataExtractor.h"
19+
#include "llvm/Support/Errc.h"
1920
#include "llvm/Support/ErrorHandling.h"
2021
#include "llvm/Support/Format.h"
2122
#include "llvm/Support/raw_ostream.h"
@@ -47,9 +48,9 @@ Error CFIProgram::parse(DataExtractor Data, uint32_t *Offset,
4748
uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
4849
switch (Primary) {
4950
default:
50-
return make_error<StringError>(
51-
"Invalid primary CFI opcode",
52-
std::make_error_code(std::errc::illegal_byte_sequence));
51+
return createStringError(errc::illegal_byte_sequence,
52+
"Invalid primary CFI opcode 0x%" PRIx8,
53+
Primary);
5354
case DW_CFA_advance_loc:
5455
case DW_CFA_restore:
5556
addInstruction(Primary, Op1);
@@ -62,9 +63,9 @@ Error CFIProgram::parse(DataExtractor Data, uint32_t *Offset,
6263
// Extended opcode - its value is Opcode itself.
6364
switch (Opcode) {
6465
default:
65-
return make_error<StringError>(
66-
"Invalid extended CFI opcode",
67-
std::make_error_code(std::errc::illegal_byte_sequence));
66+
return createStringError(errc::illegal_byte_sequence,
67+
"Invalid extended CFI opcode 0x%" PRIx8,
68+
Opcode);
6869
case DW_CFA_nop:
6970
case DW_CFA_remember_state:
7071
case DW_CFA_restore_state:

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp

+14-26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/BinaryFormat/Dwarf.h"
1616
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
1717
#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
18+
#include "llvm/Support/Errc.h"
1819
#include "llvm/Support/Format.h"
1920
#include "llvm/Support/Path.h"
2021
#include "llvm/Support/WithColor.h"
@@ -273,24 +274,6 @@ parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
273274
return true;
274275
}
275276

276-
template <typename... Ts>
277-
static std::string formatErrorString(char const *Fmt, const Ts &... Vals) {
278-
std::string Buffer;
279-
raw_string_ostream Stream(Buffer);
280-
Stream << format(Fmt, Vals...);
281-
return Stream.str();
282-
}
283-
284-
template <typename... Ts>
285-
static Error createError(char const *Fmt, const Ts &... Vals) {
286-
return make_error<StringError>(formatErrorString(Fmt, Vals...),
287-
inconvertibleErrorCode());
288-
}
289-
290-
static Error createError(char const *Msg) {
291-
return make_error<StringError>(Msg, inconvertibleErrorCode());
292-
}
293-
294277
Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
295278
uint32_t *OffsetPtr,
296279
const DWARFContext &Ctx,
@@ -303,14 +286,15 @@ Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
303286
FormParams.Format = dwarf::DWARF64;
304287
TotalLength = DebugLineData.getU64(OffsetPtr);
305288
} else if (TotalLength >= 0xffffff00) {
306-
return createError(
289+
return createStringError(errc::invalid_argument,
307290
"parsing line table prologue at offset 0x%8.8" PRIx64
308291
" unsupported reserved unit length found of value 0x%8.8" PRIx64,
309292
PrologueOffset, TotalLength);
310293
}
311294
FormParams.Version = DebugLineData.getU16(OffsetPtr);
312295
if (getVersion() < 2)
313-
return createError("parsing line table prologue at offset 0x%8.8" PRIx64
296+
return createStringError(errc::not_supported,
297+
"parsing line table prologue at offset 0x%8.8" PRIx64
314298
" found unsupported version 0x%2.2" PRIx16,
315299
PrologueOffset, getVersion());
316300

@@ -342,7 +326,7 @@ Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
342326
if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
343327
FormParams, Ctx, U, ContentTypes,
344328
IncludeDirectories, FileNames)) {
345-
return createError(
329+
return createStringError(errc::invalid_argument,
346330
"parsing line table prologue at 0x%8.8" PRIx64
347331
" found an invalid directory or file table description at"
348332
" 0x%8.8" PRIx64,
@@ -353,7 +337,8 @@ Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
353337
ContentTypes, IncludeDirectories, FileNames);
354338

355339
if (*OffsetPtr != EndPrologueOffset)
356-
return createError("parsing line table prologue at 0x%8.8" PRIx64
340+
return createStringError(errc::invalid_argument,
341+
"parsing line table prologue at 0x%8.8" PRIx64
357342
" should have ended at 0x%8.8" PRIx64
358343
" but it ended at 0x%8.8" PRIx64,
359344
PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
@@ -470,7 +455,7 @@ Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
470455
DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx,
471456
const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
472457
if (!DebugLineData.isValidOffset(Offset))
473-
return createError("offset 0x%8.8" PRIx32
458+
return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx32
474459
" is not a valid debug line section offset",
475460
Offset);
476461

@@ -575,7 +560,8 @@ Error DWARFDebugLine::LineTable::parse(
575560
if (DebugLineData.getAddressSize() == 0)
576561
DebugLineData.setAddressSize(Len - 1);
577562
else if (DebugLineData.getAddressSize() != Len - 1) {
578-
return createError("mismatching address size at offset 0x%8.8" PRIx32
563+
return createStringError(errc::invalid_argument,
564+
"mismatching address size at offset 0x%8.8" PRIx32
579565
" expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
580566
ExtOffset, DebugLineData.getAddressSize(),
581567
Len - 1);
@@ -640,7 +626,8 @@ Error DWARFDebugLine::LineTable::parse(
640626
// Make sure the stated and parsed lengths are the same.
641627
// Otherwise we have an unparseable line-number program.
642628
if (*OffsetPtr - ExtOffset != Len)
643-
return createError("unexpected line op length at offset 0x%8.8" PRIx32
629+
return createStringError(errc::illegal_byte_sequence,
630+
"unexpected line op length at offset 0x%8.8" PRIx32
644631
" expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32,
645632
ExtOffset, Len, *OffsetPtr - ExtOffset);
646633
} else if (Opcode < Prologue.OpcodeBase) {
@@ -847,7 +834,8 @@ Error DWARFDebugLine::LineTable::parse(
847834

848835
if (!State.Sequence.Empty)
849836
RecoverableErrorCallback(
850-
createError("last sequence in debug line table is not terminated!"));
837+
createStringError(errc::illegal_byte_sequence,
838+
"last sequence in debug line table is not terminated!"));
851839

852840
// Sort all sequences so that address lookup will work faster.
853841
if (!Sequences.empty()) {

llvm/lib/DebugInfo/DWARF/DWARFDebugRangeList.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@
99

1010
#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
1111
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
12+
#include "llvm/Support/Errc.h"
1213
#include "llvm/Support/Format.h"
1314
#include "llvm/Support/raw_ostream.h"
1415
#include <cinttypes>
1516
#include <cstdint>
1617

1718
using namespace llvm;
1819

19-
// FIXME: There are several versions of this. Consolidate them.
20-
template <typename... Ts>
21-
static Error createError(char const *Fmt, const Ts &... Vals) {
22-
std::string Buffer;
23-
raw_string_ostream Stream(Buffer);
24-
Stream << format(Fmt, Vals...);
25-
return make_error<StringError>(Stream.str(), inconvertibleErrorCode());
26-
}
27-
2820
void DWARFDebugRangeList::clear() {
2921
Offset = -1U;
3022
AddressSize = 0;
@@ -35,11 +27,13 @@ Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data,
3527
uint32_t *offset_ptr) {
3628
clear();
3729
if (!data.isValidOffset(*offset_ptr))
38-
return createError("invalid range list offset 0x%" PRIx32, *offset_ptr);
30+
return createStringError(errc::invalid_argument,
31+
"invalid range list offset 0x%" PRIx32, *offset_ptr);
3932

4033
AddressSize = data.getAddressSize();
4134
if (AddressSize != 4 && AddressSize != 8)
42-
return createError("invalid address size: %d", AddressSize);
35+
return createStringError(errc::invalid_argument,
36+
"invalid address size: %" PRIu8, AddressSize);
4337
Offset = *offset_ptr;
4438
while (true) {
4539
RangeListEntry Entry;
@@ -53,7 +47,8 @@ Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data,
5347
// Check that both values were extracted correctly.
5448
if (*offset_ptr != prev_offset + 2 * AddressSize) {
5549
clear();
56-
return createError("invalid range list entry at offset 0x%" PRIx32,
50+
return createStringError(errc::invalid_argument,
51+
"invalid range list entry at offset 0x%" PRIx32,
5752
prev_offset);
5853
}
5954
if (Entry.isEndOfListEntry())

0 commit comments

Comments
 (0)