[llvm] r269339 - llvm-dwp: Use llvm::Error to improve diagnostic quality/error handling in llvm-dwp

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Thu May 12 12:59:54 PDT 2016


Author: dblaikie
Date: Thu May 12 14:59:54 2016
New Revision: 269339

URL: http://llvm.org/viewvc/llvm-project?rev=269339&view=rev
Log:
llvm-dwp: Use llvm::Error to improve diagnostic quality/error handling in llvm-dwp

Added:
    llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/
    llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/a.dwo
    llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.dwo
    llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.o
    llvm/trunk/test/tools/llvm-dwp/Inputs/invalid_cu_index/
    llvm/trunk/test/tools/llvm-dwp/Inputs/invalid_cu_index/x.dwp
    llvm/trunk/test/tools/llvm-dwp/Inputs/missing_tu_index/
    llvm/trunk/test/tools/llvm-dwp/Inputs/missing_tu_index/x.dwp
    llvm/trunk/test/tools/llvm-dwp/X86/compressfail.test
    llvm/trunk/test/tools/llvm-dwp/X86/nocompress.test
    llvm/trunk/tools/llvm-dwp/DWPError.cpp
    llvm/trunk/tools/llvm-dwp/DWPError.h
Modified:
    llvm/trunk/tools/llvm-dwp/CMakeLists.txt
    llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp

Added: llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/a.dwo
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/a.dwo?rev=269339&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/a.dwo (added) and llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/a.dwo Thu May 12 14:59:54 2016 differ

Added: llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.dwo
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.dwo?rev=269339&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.dwo (added) and llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.dwo Thu May 12 14:59:54 2016 differ

Added: llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.o
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.o?rev=269339&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.o (added) and llvm/trunk/test/tools/llvm-dwp/Inputs/compressfail/compress.o Thu May 12 14:59:54 2016 differ

Added: llvm/trunk/test/tools/llvm-dwp/Inputs/invalid_cu_index/x.dwp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/Inputs/invalid_cu_index/x.dwp?rev=269339&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-dwp/Inputs/invalid_cu_index/x.dwp (added) and llvm/trunk/test/tools/llvm-dwp/Inputs/invalid_cu_index/x.dwp Thu May 12 14:59:54 2016 differ

Added: llvm/trunk/test/tools/llvm-dwp/Inputs/missing_tu_index/x.dwp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/Inputs/missing_tu_index/x.dwp?rev=269339&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-dwp/Inputs/missing_tu_index/x.dwp (added) and llvm/trunk/test/tools/llvm-dwp/Inputs/missing_tu_index/x.dwp Thu May 12 14:59:54 2016 differ

Added: llvm/trunk/test/tools/llvm-dwp/X86/compressfail.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/X86/compressfail.test?rev=269339&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-dwp/X86/compressfail.test (added)
+++ llvm/trunk/test/tools/llvm-dwp/X86/compressfail.test Thu May 12 14:59:54 2016
@@ -0,0 +1,5 @@
+RUN: not llvm-dwp %p/../Inputs/compressfail/a.dwo -o %t 2>&1 | FileCheck %s
+
+REQUIRES: zlib
+
+CHECK: error: failure while decompressing compressed section: 'zdebug_info.dwo'

Added: llvm/trunk/test/tools/llvm-dwp/X86/nocompress.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwp/X86/nocompress.test?rev=269339&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-dwp/X86/nocompress.test (added)
+++ llvm/trunk/test/tools/llvm-dwp/X86/nocompress.test Thu May 12 14:59:54 2016
@@ -0,0 +1,5 @@
+RUN: not llvm-dwp %p/../Inputs/compress/a.dwo -o %t 2>&1 | FileCheck %s
+
+REQUIRES: nozlib
+
+CHECK: error: zlib not available

Modified: llvm/trunk/tools/llvm-dwp/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwp/CMakeLists.txt?rev=269339&r1=269338&r2=269339&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-dwp/CMakeLists.txt (original)
+++ llvm/trunk/tools/llvm-dwp/CMakeLists.txt Thu May 12 14:59:54 2016
@@ -10,4 +10,5 @@ set(LLVM_LINK_COMPONENTS
 
 add_llvm_tool(llvm-dwp
   llvm-dwp.cpp
+  DWPError.cpp
   )

Added: llvm/trunk/tools/llvm-dwp/DWPError.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwp/DWPError.cpp?rev=269339&view=auto
==============================================================================
--- llvm/trunk/tools/llvm-dwp/DWPError.cpp (added)
+++ llvm/trunk/tools/llvm-dwp/DWPError.cpp Thu May 12 14:59:54 2016
@@ -0,0 +1,3 @@
+#include "DWPError.h"
+using namespace llvm;
+char DWPError::ID;

Added: llvm/trunk/tools/llvm-dwp/DWPError.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwp/DWPError.h?rev=269339&view=auto
==============================================================================
--- llvm/trunk/tools/llvm-dwp/DWPError.h (added)
+++ llvm/trunk/tools/llvm-dwp/DWPError.h Thu May 12 14:59:54 2016
@@ -0,0 +1,17 @@
+#include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorHandling.h"
+#include <string>
+namespace llvm {
+class DWPError : public ErrorInfo<DWPError> {
+public:
+  DWPError(std::string Info) : Info(std::move(Info)) {}
+  void log(raw_ostream &OS) const override { OS << Info; }
+  std::error_code convertToErrorCode() const override {
+    llvm_unreachable("Not implemented");
+  }
+  static char ID;
+
+private:
+  std::string Info;
+};
+}

Modified: llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp?rev=269339&r1=269338&r2=269339&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp (original)
+++ llvm/trunk/tools/llvm-dwp/llvm-dwp.cpp Thu May 12 14:59:54 2016
@@ -35,10 +35,11 @@
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Target/TargetMachine.h"
+#include "DWPError.h"
 #include <iostream>
 #include <memory>
-#include <system_error>
 
 using namespace llvm;
 using namespace llvm::object;
@@ -59,7 +60,7 @@ static int error(const Twine &Error, con
   return 1;
 }
 
-static std::error_code
+static Error
 writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
                        uint32_t &StringOffset, MCSection *StrSection,
                        MCSection *StrOffsetSection, StringRef CurStrSection,
@@ -67,7 +68,7 @@ writeStringsAndOffsets(MCStreamer &Out,
   // Could possibly produce an error or warning if one of these was non-null but
   // the other was null.
   if (CurStrSection.empty() || CurStrOffsetSection.empty())
-    return std::error_code();
+    return Error();
 
   DenseMap<uint32_t, uint32_t> OffsetRemapping;
 
@@ -99,7 +100,7 @@ writeStringsAndOffsets(MCStreamer &Out,
     Out.EmitIntValue(NewOffset, 4);
   }
 
-  return std::error_code();
+  return Error();
 }
 
 static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
@@ -364,7 +365,7 @@ void printDuplicateError(const std::pair
   }
   errs() << '\n';
 }
-static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
+static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
   const auto &MCOFI = *Out.getContext().getObjectFileInfo();
   MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
   MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
@@ -393,7 +394,7 @@ static std::error_code write(MCStreamer
   for (const auto &Input : Inputs) {
     auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
     if (!ErrOrObj)
-      return errorToErrorCode(ErrOrObj.takeError());
+      return ErrOrObj.takeError();
 
     UnitIndexEntry CurEntry = {};
 
@@ -415,19 +416,22 @@ static std::error_code write(MCStreamer
 
       StringRef Name;
       if (std::error_code Err = Section.getName(Name))
-        return Err;
+        return errorCodeToError(Err);
 
       Name = Name.substr(Name.find_first_not_of("._"));
 
       StringRef Contents;
       if (auto Err = Section.getContents(Contents))
-        return Err;
+        return errorCodeToError(Err);
 
       if (Name.startswith("zdebug_")) {
         uint64_t OriginalSize;
-        if (!zlib::isAvailable() ||
-            !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
-          return make_error_code(std::errc::invalid_argument);
+        if (!zlib::isAvailable())
+          return make_error<DWPError>("zlib not available");
+        if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize))
+          return make_error<DWPError>(
+              ("failure while decompressing compressed section: '" + Name +
+               "\'").str());
         UncompressedSections.resize(UncompressedSections.size() + 1);
         if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
             zlib::StatusOK) {
@@ -487,7 +491,7 @@ static std::error_code write(MCStreamer
       DataExtractor CUIndexData(CurCUIndexSection,
                                 ErrOrObj->getBinary()->isLittleEndian(), 0);
       if (!CUIndex.parse(CUIndexData))
-        return make_error_code(std::errc::invalid_argument);
+        return make_error<DWPError>("Failed to parse cu_index");
 
       for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
         auto *I = E.getOffsets();
@@ -502,7 +506,7 @@ static std::error_code write(MCStreamer
             CurStrSection);
         if (!P.second) {
           printDuplicateError(*P.first, ID, Input);
-          return make_error_code(std::errc::invalid_argument);
+          return make_error<DWPError>("Duplicate DWO ID");
         }
         auto &NewEntry = P.first->second;
         NewEntry.Name = ID.Name;
@@ -518,13 +522,11 @@ static std::error_code write(MCStreamer
 
       if (!CurTypesSection.empty()) {
         assert(CurTypesSection.size() == 1);
-        if (CurTUIndexSection.empty())
-          return make_error_code(std::errc::invalid_argument);
         DWARFUnitIndex TUIndex(DW_SECT_TYPES);
         DataExtractor TUIndexData(CurTUIndexSection,
                                   ErrOrObj->getBinary()->isLittleEndian(), 0);
         if (!TUIndex.parse(TUIndexData))
-          return make_error_code(std::errc::invalid_argument);
+          return make_error<DWPError>("Failed to parse tu_index");
         addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
                            CurTypesSection.front(), CurEntry,
                            ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
@@ -535,7 +537,7 @@ static std::error_code write(MCStreamer
       auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
       if (!P.second) {
         printDuplicateError(*P.first, ID, "");
-        return make_error_code(std::errc::invalid_argument);
+        return make_error<DWPError>("Duplicate DWO ID");
       }
       P.first->second.Name = ID.Name;
       P.first->second.DWOName = ID.DWOName;
@@ -563,7 +565,7 @@ static std::error_code write(MCStreamer
   writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
              IndexEntries);
 
-  return std::error_code();
+  return Error();
 }
 
 int main(int argc, char **argv) {
@@ -631,8 +633,10 @@ int main(int argc, char **argv) {
   if (!MS)
     return error("no object streamer for target " + TripleName, Context);
 
-  if (auto Err = write(*MS, InputFiles))
-    return error(Err.message(), "Writing DWP file");
+  if (auto Err = write(*MS, InputFiles)) {
+    logAllUnhandledErrors(std::move(Err), errs(), "error: ");
+    return 1;
+  }
 
   MS->Finish();
 }




More information about the llvm-commits mailing list