Index: include/llvm/DebugInfo/DWARF/DWARFDebugAddr.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFDebugAddr.h +++ include/llvm/DebugInfo/DWARF/DWARFDebugAddr.h @@ -57,7 +57,7 @@ /// Extract an entire table, including all addresses. Error extract(DWARFDataExtractor Data, uint32_t *OffsetPtr, uint16_t Version, uint8_t AddrSize, - std::function WarnCallback); + std::function WarnCallback = DisplayWarning); uint32_t getHeaderOffset() const { return HeaderOffset; } uint8_t getAddrSize() const { return HeaderData.AddrSize; } Index: include/llvm/DebugInfo/DWARF/DWARFDebugLine.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFDebugLine.h +++ include/llvm/DebugInfo/DWARF/DWARFDebugLine.h @@ -249,7 +249,7 @@ /// Parse prologue and all rows. Error parse(DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, const DWARFContext &Ctx, const DWARFUnit *U, - std::function RecoverableErrorCallback = warn, + std::function RecoverableErrorCallback = DisplayWarning, raw_ostream *OS = nullptr); using RowVector = std::vector; @@ -273,7 +273,7 @@ Expected getOrParseLineTable( DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx, const DWARFUnit *U, - std::function RecoverableErrorCallback = warn); + std::function RecoverableErrorCallback = DisplayWarning); /// Helper to allow for parsing of an entire .debug_line section in sequence. class SectionParser { @@ -295,16 +295,17 @@ /// \param OS - if not null, the parser will print information about the /// table as it parses it. LineTable - parseNext(function_ref RecoverableErrorCallback = warn, - function_ref UnrecoverableErrorCallback = warn, - raw_ostream *OS = nullptr); + parseNext( + function_ref RecoverableErrorCallback = DisplayWarning, + function_ref UnrecoverableErrorCallback = DisplayWarning, + raw_ostream *OS = nullptr); /// Skip the current line table and go to the following line table (if /// present) immediately. /// /// \param ErrorCallback - report any prologue parsing issues via this /// callback. - void skip(function_ref ErrorCallback = warn); + void skip(function_ref ErrorCallback = DisplayWarning); /// Indicates if the parser has parsed as much as possible. /// @@ -327,12 +328,6 @@ bool Done = false; }; - /// Helper function for DWARFDebugLine parse functions, to report issues - /// identified during parsing. - /// - /// \param Err The Error to report. - static void warn(Error Err); - private: struct ParsingState { ParsingState(struct LineTable *LT); Index: include/llvm/Support/Error.h =================================================================== --- include/llvm/Support/Error.h +++ include/llvm/Support/Error.h @@ -909,6 +909,15 @@ cantFail(std::move(E)); } +/// Display error with color +void DisplayError(Error Error); + +/// Display warning with color +void DisplayWarning(Error Warning); + +/// Display note with color +void DisplayNote(Error Note); + /// Handle any errors (if present) in an Expected, then try a recovery path. /// /// If the incoming value is a success value it is returned unmodified. If it Index: lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFContext.cpp +++ lib/DebugInfo/DWARF/DWARFContext.cpp @@ -248,19 +248,11 @@ static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData, DIDumpOptions DumpOpts, uint16_t Version, uint8_t AddrSize) { - // TODO: Make this more general: add callback types to Error.h, create - // implementation and make all DWARF classes use them. - static auto WarnCallback = [](Error Warn) { - handleAllErrors(std::move(Warn), [](ErrorInfoBase &Info) { - WithColor::warning() << Info.message() << '\n'; - }); - }; uint32_t Offset = 0; while (AddrData.isValidOffset(Offset)) { DWARFDebugAddrTable AddrTable; uint32_t TableOffset = Offset; - if (Error Err = AddrTable.extract(AddrData, &Offset, Version, - AddrSize, WarnCallback)) { + if (Error Err = AddrTable.extract(AddrData, &Offset, Version, AddrSize)) { WithColor::error() << toString(std::move(Err)) << '\n'; // Keep going after an error, if we can, assuming that the length field // could be read. If it couldn't, stop reading the section. @@ -409,7 +401,7 @@ } OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n"; if (DumpOpts.Verbose) { - Parser.parseNext(DWARFDebugLine::warn, DWARFDebugLine::warn, &OS); + Parser.parseNext(DisplayWarning, DisplayWarning, &OS); } else { DWARFDebugLine::LineTable LineTable = Parser.parseNext(); LineTable.dump(OS, DumpOpts); @@ -799,9 +791,9 @@ const DWARFDebugLine::LineTable * DWARFContext::getLineTableForUnit(DWARFUnit *U) { Expected ExpectedLineTable = - getLineTableForUnit(U, DWARFDebugLine::warn); + getLineTableForUnit(U, DisplayWarning); if (!ExpectedLineTable) { - DWARFDebugLine::warn(ExpectedLineTable.takeError()); + DisplayWarning(ExpectedLineTable.takeError()); return nullptr; } return *ExpectedLineTable; Index: lib/DebugInfo/DWARF/DWARFDebugAddr.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFDebugAddr.cpp +++ lib/DebugInfo/DWARF/DWARFDebugAddr.cpp @@ -19,10 +19,8 @@ invalidateLength(); } -Error DWARFDebugAddrTable::extract(DWARFDataExtractor Data, - uint32_t *OffsetPtr, - uint16_t Version, - uint8_t AddrSize, +Error DWARFDebugAddrTable::extract(DWARFDataExtractor Data, uint32_t *OffsetPtr, + uint16_t Version, uint8_t AddrSize, std::function WarnCallback) { clear(); HeaderOffset = *OffsetPtr; Index: lib/DebugInfo/DWARF/DWARFDebugLine.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFDebugLine.cpp +++ lib/DebugInfo/DWARF/DWARFDebugLine.cpp @@ -1112,9 +1112,3 @@ Done = true; } } - -void DWARFDebugLine::warn(Error Err) { - handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) { - WithColor::warning() << Info.message() << '\n'; - }); -} Index: lib/Support/Error.cpp =================================================================== --- lib/Support/Error.cpp +++ lib/Support/Error.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Error.h" +#include "llvm/Support/WithColor.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" @@ -116,6 +117,24 @@ return make_error(Msg, EC); } +static void DisplayMessage(raw_ostream &OS, Error E) { + handleAllErrors(std::move(E), [&OS](ErrorInfoBase &Info) { + OS << Info.message() << '\n'; + }); +} + +void DisplayError(Error Error) { + DisplayMessage(WithColor::error(), std::move(Error)); +} + +void DisplayWarning(Error Warning) { + DisplayMessage(WithColor::warning(), std::move(Warning)); +} + +void DisplayNote(Error Note) { + DisplayMessage(WithColor::note(), std::move(Note)); +} + void report_fatal_error(Error Err, bool GenCrashDiag) { assert(Err && "report_fatal_error called with success value"); std::string ErrMsg; Index: tools/dsymutil/DwarfLinker.cpp =================================================================== --- tools/dsymutil/DwarfLinker.cpp +++ tools/dsymutil/DwarfLinker.cpp @@ -1696,7 +1696,7 @@ Error Err = LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf, &Unit.getOrigUnit()); - DWARFDebugLine::warn(std::move(Err)); + DisplayWarning(std::move(Err)); // This vector is the output line table. std::vector NewRows;