[llvm] r327411 - [dsymutil] Unify error handling outside DwarfLinker.

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 13 08:47:38 PDT 2018


Author: jdevlieghere
Date: Tue Mar 13 08:47:38 2018
New Revision: 327411

URL: http://llvm.org/viewvc/llvm-project?rev=327411&view=rev
Log:
[dsymutil] Unify error handling outside DwarfLinker.

This is a follow-up to r327137 where we unified error handling for the
DwarfLinker. This replaces calls to errs() and outs() with the
appropriate ostream wrapper everywhere in dsymutil.

Added:
    llvm/trunk/tools/dsymutil/ErrorReporting.h
Modified:
    llvm/trunk/test/tools/dsymutil/arch-option.test
    llvm/trunk/test/tools/dsymutil/debug-map-parsing.test
    llvm/trunk/tools/dsymutil/DebugMap.cpp
    llvm/trunk/tools/dsymutil/DwarfLinker.cpp
    llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp
    llvm/trunk/tools/dsymutil/MachOUtils.cpp
    llvm/trunk/tools/dsymutil/dsymutil.cpp

Modified: llvm/trunk/test/tools/dsymutil/arch-option.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/arch-option.test?rev=327411&r1=327410&r2=327411&view=diff
==============================================================================
--- llvm/trunk/test/tools/dsymutil/arch-option.test (original)
+++ llvm/trunk/test/tools/dsymutil/arch-option.test Tue Mar 13 08:47:38 2018
@@ -35,5 +35,5 @@ ARM64-NOT: ---
 
 CHECK: ...
 
-BADARCH: error: Unsupported cpu architecture: 'arm42'
+BADARCH: error: unsupported cpu architecture: 'arm42'
 EMPTY: error: no architecture to link

Modified: llvm/trunk/test/tools/dsymutil/debug-map-parsing.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/debug-map-parsing.test?rev=327411&r1=327410&r2=327411&view=diff
==============================================================================
--- llvm/trunk/test/tools/dsymutil/debug-map-parsing.test (original)
+++ llvm/trunk/test/tools/dsymutil/debug-map-parsing.test Tue Mar 13 08:47:38 2018
@@ -71,9 +71,9 @@ CHECK-ARCHIVE: ...
 Check that we warn about missing object files (this presumes that the files aren't
 present in the machine's /Inputs/ folder, which should be a pretty safe bet).
 
-NOT-FOUND: cannot open{{.*}}"/Inputs/basic1.macho.x86_64.o": {{[Nn]o}} such file
-NOT-FOUND: cannot open{{.*}}"/Inputs/basic2.macho.x86_64.o": {{[Nn]o}} such file
-NOT-FOUND: cannot open{{.*}}"/Inputs/basic3.macho.x86_64.o": {{[Nn]o}} such file
+NOT-FOUND: cannot open{{.*}}'/Inputs/basic1.macho.x86_64.o': {{[Nn]o}} such file
+NOT-FOUND: cannot open{{.*}}'/Inputs/basic2.macho.x86_64.o': {{[Nn]o}} such file
+NOT-FOUND: cannot open{{.*}}'/Inputs/basic3.macho.x86_64.o': {{[Nn]o}} such file
 NOT-FOUND: ---
 NOT-FOUND-NEXT: triple: 'x86_64-apple-darwin'
 NOT-FOUND-NEXT: binary-path:{{.*}}/Inputs/basic.macho.x86_64
@@ -81,5 +81,5 @@ NOT-FOUND-NEXT: ...
 
 Check that we correctly error out on invalid executatble.
 
-NO-EXECUTABLE: cannot parse{{.*}}/inexistant": {{[Nn]o}} such file
+NO-EXECUTABLE: cannot parse{{.*}}/inexistant': {{[Nn]o}} such file
 NO-EXECUTABLE-NOT: ---

Modified: llvm/trunk/tools/dsymutil/DebugMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/DebugMap.cpp?rev=327411&r1=327410&r2=327411&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/DebugMap.cpp (original)
+++ llvm/trunk/tools/dsymutil/DebugMap.cpp Tue Mar 13 08:47:38 2018
@@ -9,6 +9,7 @@
 
 #include "DebugMap.h"
 #include "BinaryHolder.h"
+#include "ErrorReporting.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringMap.h"
@@ -241,7 +242,7 @@ MappingTraits<dsymutil::DebugMapObject>:
   sys::path::append(Path, Filename);
   auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path);
   if (auto EC = ErrOrObjectFiles.getError()) {
-    errs() << "warning: Unable to open " << Path << " " << EC.message() << '\n';
+    warn_ostream() << "Unable to open " << Path << " " << EC.message() << '\n';
   } else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) {
     // Rewrite the object file symbol addresses in the debug map. The
     // YAML input is mainly used to test llvm-dsymutil without

Modified: llvm/trunk/tools/dsymutil/DwarfLinker.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/DwarfLinker.cpp?rev=327411&r1=327410&r2=327411&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/DwarfLinker.cpp (original)
+++ llvm/trunk/tools/dsymutil/DwarfLinker.cpp Tue Mar 13 08:47:38 2018
@@ -9,6 +9,7 @@
 
 #include "BinaryHolder.h"
 #include "DebugMap.h"
+#include "ErrorReporting.h"
 #include "MachOUtils.h"
 #include "NonRelocatableStringpool.h"
 #include "dsymutil.h"
@@ -78,7 +79,6 @@
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/ToolOutputFile.h"
-#include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
@@ -580,19 +580,7 @@ static bool inFunctionScope(CompileUnit
   }
   return false;
 }
-
-static raw_ostream &error_ostream() {
-  return WithColor(errs(), HighlightColor::Error).get() << "error: ";
-}
-
-static raw_ostream &warn_ostream() {
-  return WithColor(errs(), HighlightColor::Warning).get() << "warning: ";
-}
-
-static raw_ostream &note_ostream() {
-  return WithColor(errs(), HighlightColor::Note).get() << "note: ";
-}
-} // end anonymous namespace
+} // namespace
 
 void warn(Twine Warning, Twine Context) {
   warn_ostream() << Warning + "\n";
@@ -4171,7 +4159,7 @@ bool DwarfLinker::link(const DebugMap &M
       StringRef File = LinkContext.DMO.getObjectFilename();
       auto ErrorOrMem = MemoryBuffer::getFile(File);
       if (!ErrorOrMem) {
-        errs() << "Warning: Could not open " << File << "\n";
+        warn("Could not open '" + File + "'\n");
         continue;
       }
       sys::fs::file_status Stat;
@@ -4353,5 +4341,5 @@ bool linkDwarf(raw_fd_ostream &OutFile,
   return Linker.link(DM);
 }
 
-} // end namespace dsymutil
-} // end namespace llvm
+} // namespace dsymutil
+} // namespace llvm

Added: llvm/trunk/tools/dsymutil/ErrorReporting.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/ErrorReporting.h?rev=327411&view=auto
==============================================================================
--- llvm/trunk/tools/dsymutil/ErrorReporting.h (added)
+++ llvm/trunk/tools/dsymutil/ErrorReporting.h Tue Mar 13 08:47:38 2018
@@ -0,0 +1,33 @@
+//===- ErrorReporting.h - dsymutil error reporting  -------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_DSYMUTIL_ERRORREPORTING_H
+#define LLVM_TOOLS_DSYMUTIL_ERRORREPORTING_H
+#include "llvm/Support/WithColor.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+namespace dsymutil {
+
+inline raw_ostream &error_ostream() {
+  return WithColor(errs(), HighlightColor::Error).get() << "error: ";
+}
+
+inline raw_ostream &warn_ostream() {
+  return WithColor(errs(), HighlightColor::Warning).get() << "warning: ";
+}
+
+inline raw_ostream &note_ostream() {
+  return WithColor(errs(), HighlightColor::Note).get() << "note: ";
+}
+
+} // namespace dsymutil
+} // end namespace llvm
+
+#endif // LLVM_TOOLS_DSYMUTIL_ERRORREPORTING_H

Modified: llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp?rev=327411&r1=327410&r2=327411&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp (original)
+++ llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp Tue Mar 13 08:47:38 2018
@@ -9,6 +9,7 @@
 
 #include "BinaryHolder.h"
 #include "DebugMap.h"
+#include "ErrorReporting.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Support/Path.h"
@@ -97,7 +98,7 @@ private:
                          StringRef BinaryPath);
 };
 
-static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; }
+static void Warning(const Twine &Msg) { warn_ostream() << Msg << '\n'; }
 } // anonymous namespace
 
 /// Reset the parser state corresponding to the current object
@@ -121,16 +122,17 @@ void MachODebugMapParser::switchToNewDeb
   auto MachOOrError =
       CurrentObjectHolder.GetFilesAs<MachOObjectFile>(Path, Timestamp);
   if (auto Error = MachOOrError.getError()) {
-    Warning(Twine("cannot open debug object \"") + Path.str() +
-            "\": " + Error.message() + "\n");
+    Warning(Twine("cannot open debug object '") + Path.str() +
+            "': " + Error.message());
     return;
   }
 
   auto ErrOrAchObj =
       CurrentObjectHolder.GetAs<MachOObjectFile>(Result->getTriple());
-  if (auto Err = ErrOrAchObj.getError()) {
-    return Warning(Twine("cannot open debug object \"") + Path.str() +
-                   "\": " + Err.message() + "\n");
+  if (auto Error = ErrOrAchObj.getError()) {
+    Warning(Twine("cannot open debug object '") + Path.str() +
+            "': " + Error.message());
+    return;
   }
 
   CurrentDebugMapObject =

Modified: llvm/trunk/tools/dsymutil/MachOUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/MachOUtils.cpp?rev=327411&r1=327410&r2=327411&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/MachOUtils.cpp (original)
+++ llvm/trunk/tools/dsymutil/MachOUtils.cpp Tue Mar 13 08:47:38 2018
@@ -10,6 +10,7 @@
 #include "MachOUtils.h"
 #include "BinaryHolder.h"
 #include "DebugMap.h"
+#include "ErrorReporting.h"
 #include "NonRelocatableStringpool.h"
 #include "dsymutil.h"
 #include "llvm/MC/MCAsmLayout.h"
@@ -38,7 +39,7 @@ static bool runLipo(StringRef SDKPath, S
     Path = sys::findProgramByName("lipo");
 
   if (!Path) {
-    errs() << "error: lipo: " << Path.getError().message() << "\n";
+    error_ostream() << "lipo: " << Path.getError().message() << "\n";
     return false;
   }
 
@@ -46,7 +47,7 @@ static bool runLipo(StringRef SDKPath, S
   int result =
       sys::ExecuteAndWait(*Path, Args.data(), nullptr, {}, 0, 0, &ErrMsg);
   if (result) {
-    errs() << "error: lipo: " << ErrMsg << "\n";
+    error_ostream() << "lipo: " << ErrMsg << "\n";
     return false;
   }
 
@@ -63,8 +64,8 @@ bool generateUniversalBinary(SmallVector
     StringRef From(ArchFiles.front().Path);
     if (sys::fs::rename(From, OutputFileName)) {
       if (std::error_code EC = sys::fs::copy_file(From, OutputFileName)) {
-        errs() << "error: while copying " << From << " to " << OutputFileName
-               << ": " << EC.message() << "\n";
+        error_ostream() << "while copying " << From << " to " << OutputFileName
+                        << ": " << EC.message() << "\n";
         return false;
       }
       sys::fs::remove(From);

Modified: llvm/trunk/tools/dsymutil/dsymutil.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/dsymutil.cpp?rev=327411&r1=327410&r2=327411&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/dsymutil.cpp (original)
+++ llvm/trunk/tools/dsymutil/dsymutil.cpp Tue Mar 13 08:47:38 2018
@@ -15,6 +15,7 @@
 #include "dsymutil.h"
 #include "CFBundle.h"
 #include "DebugMap.h"
+#include "ErrorReporting.h"
 #include "MachOUtils.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
@@ -156,8 +157,8 @@ static bool createPlistFile(llvm::String
   std::error_code EC;
   llvm::raw_fd_ostream PL(InfoPlist, EC, llvm::sys::fs::F_Text);
   if (EC) {
-    llvm::errs() << "error: cannot create plist file " << InfoPlist << ": "
-                 << EC.message() << '\n';
+    error_ostream() << "cannot create plist file " << InfoPlist << ": "
+                    << EC.message() << '\n';
     return false;
   }
 
@@ -214,8 +215,8 @@ static bool createBundleDir(llvm::String
   llvm::sys::path::append(Bundle, "Contents", "Resources", "DWARF");
   if (std::error_code EC = create_directories(Bundle.str(), true,
                                               llvm::sys::fs::perms::all_all)) {
-    llvm::errs() << "error: cannot create directory " << Bundle << ": "
-                 << EC.message() << "\n";
+    error_ostream() << "cannot create directory " << Bundle << ": "
+                    << EC.message() << "\n";
     return false;
   }
   return true;
@@ -223,8 +224,8 @@ static bool createBundleDir(llvm::String
 
 static bool verify(llvm::StringRef OutputFile, llvm::StringRef Arch) {
   if (OutputFile == "-") {
-    llvm::errs() << "warning: verification skipped for " << Arch
-                 << "because writing to stdout.\n";
+    warn_ostream() << "verification skipped for " << Arch
+                   << "because writing to stdout.\n";
     return true;
   }
 
@@ -242,7 +243,7 @@ static bool verify(llvm::StringRef Outpu
     DIDumpOptions DumpOpts;
     bool success = DICtx->verify(os, DumpOpts.noImplicitRecursion());
     if (!success)
-      errs() << "error: verification failed for " << Arch << '\n';
+      error_ostream() << "verification failed for " << Arch << '\n';
     return success;
   }
 
@@ -314,7 +315,7 @@ static Expected<LinkOptions> getOptions(
     // used a unique BinaryHolder object that could cache multiple
     // binaries this restriction would go away.
     return make_error<StringError>(
-        "error: standard input cannot be used as input for a dSYM update.",
+        "standard input cannot be used as input for a dSYM update.",
         inconvertibleErrorCode());
   }
 
@@ -404,7 +405,7 @@ int main(int argc, char **argv) {
 
   auto OptionsOrErr = getOptions();
   if (!OptionsOrErr) {
-    errs() << "error: " << toString(OptionsOrErr.takeError());
+    error_ostream() << toString(OptionsOrErr.takeError());
     return 1;
   }
 
@@ -415,24 +416,24 @@ int main(int argc, char **argv) {
 
   auto InputsOrErr = getInputs(OptionsOrErr->Update);
   if (!InputsOrErr) {
-    errs() << "error: " << toString(InputsOrErr.takeError()) << '\n';
+    error_ostream() << toString(InputsOrErr.takeError()) << '\n';
     return 1;
   }
 
   if (!FlatOut && OutputFileOpt == "-") {
-    llvm::errs() << "error: cannot emit to standard output without --flat\n";
+    error_ostream() << "cannot emit to standard output without --flat\n";
     return 1;
   }
 
   if (InputsOrErr->size() > 1 && FlatOut && !OutputFileOpt.empty()) {
-    llvm::errs() << "error: cannot use -o with multiple inputs in flat mode\n";
+    error_ostream() << "cannot use -o with multiple inputs in flat mode\n";
     return 1;
   }
 
   for (const auto &Arch : ArchFlags)
     if (Arch != "*" && Arch != "all" &&
         !llvm::object::MachOObjectFile::isValidArch(Arch)) {
-      llvm::errs() << "error: Unsupported cpu architecture: '" << Arch << "'\n";
+      error_ostream() << "unsupported cpu architecture: '" << Arch << "'\n";
       return 1;
     }
 
@@ -448,8 +449,8 @@ int main(int argc, char **argv) {
                                            Verbose, InputIsYAMLDebugMap);
 
     if (auto EC = DebugMapPtrsOrErr.getError()) {
-      llvm::errs() << "error: cannot parse the debug map for \"" << InputFile
-                   << "\": " << EC.message() << '\n';
+      error_ostream() << "cannot parse the debug map for '" << InputFile
+                      << "': " << EC.message() << '\n';
       return 1;
     }
 
@@ -463,7 +464,7 @@ int main(int argc, char **argv) {
 
     // Ensure that the debug map is not empty (anymore).
     if (DebugMapPtrsOrErr->empty()) {
-      llvm::errs() << "error: no architecture to link\n";
+      error_ostream() << "no architecture to link\n";
       return 1;
     }
 
@@ -492,9 +493,10 @@ int main(int argc, char **argv) {
         continue;
 
       if (Map->begin() == Map->end())
-        llvm::errs() << "warning: no debug symbols in executable (-arch "
-                     << MachOUtils::getArchName(Map->getTriple().getArchName())
-                     << ")\n";
+        warn_ostream() << "no debug symbols in executable (-arch "
+                       << MachOUtils::getArchName(
+                              Map->getTriple().getArchName())
+                       << ")\n";
 
       // Using a std::shared_ptr rather than std::unique_ptr because move-only
       // types don't work with std::bind in the ThreadPool implementation.




More information about the llvm-commits mailing list