[llvm] [readtapi] Use consistent error handling diagnostics (PR #73419)

Cyndy Ishida via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 25 17:38:10 PST 2023


https://github.com/cyndyishida created https://github.com/llvm/llvm-project/pull/73419

None

>From 37a48ac63cfa065425b46b6b42f78ffed4ef56c0 Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ishida at apple.com>
Date: Sat, 25 Nov 2023 17:32:40 -0800
Subject: [PATCH] [readtapi] Use consistent error handling diagnostics

---
 llvm/tools/llvm-readtapi/llvm-readtapi.cpp | 52 +++++++++++-----------
 1 file changed, 25 insertions(+), 27 deletions(-)

diff --git a/llvm/tools/llvm-readtapi/llvm-readtapi.cpp b/llvm/tools/llvm-readtapi/llvm-readtapi.cpp
index 3e0bcc49d19cc4c..44419e8c5c2fceb 100644
--- a/llvm/tools/llvm-readtapi/llvm-readtapi.cpp
+++ b/llvm/tools/llvm-readtapi/llvm-readtapi.cpp
@@ -17,7 +17,6 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TextAPI/TextAPIError.h"
 #include "llvm/TextAPI/TextAPIReader.h"
@@ -57,9 +56,16 @@ class TAPIOptTable : public opt::GenericOptTable {
   }
 };
 
+// Use unique exit code to differentiate failures not directly caused from
+// TextAPI operations. This is used for wrapping `compare` operations in
+// automation and scripting.
+const int NON_TAPI_EXIT_CODE = 2;
+const std::string TOOLNAME = "llvm-readtapi";
+ExitOnError ExitOnErr;
+
 // Handle error reporting in cases where `ExitOnError` is not used.
 void reportError(Twine Message, int ExitCode = EXIT_FAILURE) {
-  WithColor::error(errs()) << Message << "\n";
+  errs() << TOOLNAME << ": error: " << Message << "\n";
   errs().flush();
   exit(ExitCode);
 }
@@ -71,9 +77,8 @@ struct Context {
   bool Compact = false;
 };
 
-std::unique_ptr<InterfaceFile> getInterfaceFile(const StringRef Filename,
-                                                ExitOnError &ExitOnErr) {
-  ExitOnErr.setBanner("error: '" + Filename.str() + "' ");
+std::unique_ptr<InterfaceFile> getInterfaceFile(const StringRef Filename) {
+  ExitOnErr.setBanner(TOOLNAME + ": error: '" + Filename.str() + "' ");
   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
       MemoryBuffer::getFile(Filename);
   if (BufferOrErr.getError())
@@ -82,22 +87,21 @@ std::unique_ptr<InterfaceFile> getInterfaceFile(const StringRef Filename,
       TextAPIReader::get((*BufferOrErr)->getMemBufferRef());
   if (!IF)
     ExitOnErr(IF.takeError());
+  // Set Banner back.
+  ExitOnErr.setBanner(TOOLNAME + ": error: ");
   return std::move(*IF);
 }
 
-// Use unique exit code to differentiate failures not directly caused from
-// TextAPI operations. This is used for wrapping `compare` operations in
-// automation and scripting.
-const int NON_TAPI_EXIT_CODE = 2;
-
 bool handleCompareAction(const Context &Ctx) {
   if (Ctx.Inputs.size() != 2)
     reportError("compare only supports two input files",
                 /*ExitCode=*/NON_TAPI_EXIT_CODE);
 
-  ExitOnError ExitOnErr("error: ", /*DefaultErrorExitCode=*/NON_TAPI_EXIT_CODE);
-  auto LeftIF = getInterfaceFile(Ctx.Inputs.front(), ExitOnErr);
-  auto RightIF = getInterfaceFile(Ctx.Inputs.at(1), ExitOnErr);
+  // Override default exit code.
+  ExitOnErr = ExitOnError(TOOLNAME + ": error: ",
+                          /*DefaultErrorExitCode=*/NON_TAPI_EXIT_CODE);
+  auto LeftIF = getInterfaceFile(Ctx.Inputs.front());
+  auto RightIF = getInterfaceFile(Ctx.Inputs.at(1));
 
   raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs();
   return DiffEngine(LeftIF.get(), RightIF.get()).compareFiles(OS);
@@ -105,11 +109,10 @@ bool handleCompareAction(const Context &Ctx) {
 
 bool handleWriteAction(const Context &Ctx,
                        std::unique_ptr<InterfaceFile> Out = nullptr) {
-  ExitOnError ExitOnErr("error: ");
   if (!Out) {
     if (Ctx.Inputs.size() != 1)
       reportError("write only supports one input file");
-    Out = getInterfaceFile(Ctx.Inputs.front(), ExitOnErr);
+    Out = getInterfaceFile(Ctx.Inputs.front());
   }
   raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs();
   ExitOnErr(TextAPIWriter::writeToStream(OS, *Out, Ctx.WriteFT, Ctx.Compact));
@@ -120,10 +123,9 @@ bool handleMergeAction(const Context &Ctx) {
   if (Ctx.Inputs.size() < 2)
     reportError("merge requires at least two input files");
 
-  ExitOnError ExitOnErr("error: ");
   std::unique_ptr<InterfaceFile> Out;
   for (StringRef FileName : Ctx.Inputs) {
-    auto IF = getInterfaceFile(FileName, ExitOnErr);
+    auto IF = getInterfaceFile(FileName);
     // On the first iteration copy the input file and skip merge.
     if (!Out) {
       Out = std::move(IF);
@@ -145,11 +147,9 @@ int main(int Argc, char **Argv) {
   StringSaver Saver(A);
   TAPIOptTable Tbl;
   Context Ctx;
-  opt::InputArgList Args =
-      Tbl.parseArgs(Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
-        WithColor::error(errs(), "llvm-readtapi") << Msg << "\n";
-        exit(1);
-      });
+  ExitOnErr.setBanner(TOOLNAME + ": error:");
+  opt::InputArgList Args = Tbl.parseArgs(
+      Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) { reportError(Msg); });
   if (Args.hasArg(OPT_help)) {
     Tbl.printHelp(outs(), "llvm-readtapi [options] <inputs>",
                   "LLVM TAPI file reader and manipulator");
@@ -163,11 +163,9 @@ int main(int Argc, char **Argv) {
     std::string OutputLoc = std::move(A->getValue());
     std::error_code EC;
     Ctx.OutStream = std::make_unique<llvm::raw_fd_stream>(OutputLoc, EC);
-    if (EC) {
-      llvm::errs() << "error opening the file '" << OutputLoc
-                   << "': " << EC.message() << "\n";
-      return NON_TAPI_EXIT_CODE;
-    }
+    if (EC)
+      reportError("error opening the file '" + OutputLoc + EC.message(),
+                  NON_TAPI_EXIT_CODE);
   }
 
   Ctx.Compact = Args.hasArg(OPT_compact);



More information about the llvm-commits mailing list