Skip to content

Commit 49b26af

Browse files
committed
[readtapi] Use consistent error handling diagnostics (llvm#73419)
(cherry picked from commit bc191ac)
1 parent b862ab1 commit 49b26af

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

llvm/tools/llvm-readtapi/llvm-readtapi.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "llvm/Support/Error.h"
1818
#include "llvm/Support/InitLLVM.h"
1919
#include "llvm/Support/MemoryBuffer.h"
20-
#include "llvm/Support/WithColor.h"
2120
#include "llvm/Support/raw_ostream.h"
2221
#include "llvm/TextAPI/TextAPIError.h"
2322
#include "llvm/TextAPI/TextAPIReader.h"
@@ -57,9 +56,16 @@ class TAPIOptTable : public opt::GenericOptTable {
5756
}
5857
};
5958

59+
// Use unique exit code to differentiate failures not directly caused from
60+
// TextAPI operations. This is used for wrapping `compare` operations in
61+
// automation and scripting.
62+
const int NON_TAPI_EXIT_CODE = 2;
63+
const std::string TOOLNAME = "llvm-readtapi";
64+
ExitOnError ExitOnErr;
65+
6066
// Handle error reporting in cases where `ExitOnError` is not used.
6167
void reportError(Twine Message, int ExitCode = EXIT_FAILURE) {
62-
WithColor::error(errs()) << Message << "\n";
68+
errs() << TOOLNAME << ": error: " << Message << "\n";
6369
errs().flush();
6470
exit(ExitCode);
6571
}
@@ -71,9 +77,8 @@ struct Context {
7177
bool Compact = false;
7278
};
7379

74-
std::unique_ptr<InterfaceFile> getInterfaceFile(const StringRef Filename,
75-
ExitOnError &ExitOnErr) {
76-
ExitOnErr.setBanner("error: '" + Filename.str() + "' ");
80+
std::unique_ptr<InterfaceFile> getInterfaceFile(const StringRef Filename) {
81+
ExitOnErr.setBanner(TOOLNAME + ": error: '" + Filename.str() + "' ");
7782
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
7883
MemoryBuffer::getFile(Filename);
7984
if (BufferOrErr.getError())
@@ -82,34 +87,32 @@ std::unique_ptr<InterfaceFile> getInterfaceFile(const StringRef Filename,
8287
TextAPIReader::get((*BufferOrErr)->getMemBufferRef());
8388
if (!IF)
8489
ExitOnErr(IF.takeError());
90+
// Set Banner back.
91+
ExitOnErr.setBanner(TOOLNAME + ": error: ");
8592
return std::move(*IF);
8693
}
8794

88-
// Use unique exit code to differentiate failures not directly caused from
89-
// TextAPI operations. This is used for wrapping `compare` operations in
90-
// automation and scripting.
91-
const int NON_TAPI_EXIT_CODE = 2;
92-
9395
bool handleCompareAction(const Context &Ctx) {
9496
if (Ctx.Inputs.size() != 2)
9597
reportError("compare only supports two input files",
9698
/*ExitCode=*/NON_TAPI_EXIT_CODE);
9799

98-
ExitOnError ExitOnErr("error: ", /*DefaultErrorExitCode=*/NON_TAPI_EXIT_CODE);
99-
auto LeftIF = getInterfaceFile(Ctx.Inputs.front(), ExitOnErr);
100-
auto RightIF = getInterfaceFile(Ctx.Inputs.at(1), ExitOnErr);
100+
// Override default exit code.
101+
ExitOnErr = ExitOnError(TOOLNAME + ": error: ",
102+
/*DefaultErrorExitCode=*/NON_TAPI_EXIT_CODE);
103+
auto LeftIF = getInterfaceFile(Ctx.Inputs.front());
104+
auto RightIF = getInterfaceFile(Ctx.Inputs.at(1));
101105

102106
raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs();
103107
return DiffEngine(LeftIF.get(), RightIF.get()).compareFiles(OS);
104108
}
105109

106110
bool handleWriteAction(const Context &Ctx,
107111
std::unique_ptr<InterfaceFile> Out = nullptr) {
108-
ExitOnError ExitOnErr("error: ");
109112
if (!Out) {
110113
if (Ctx.Inputs.size() != 1)
111114
reportError("write only supports one input file");
112-
Out = getInterfaceFile(Ctx.Inputs.front(), ExitOnErr);
115+
Out = getInterfaceFile(Ctx.Inputs.front());
113116
}
114117
raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs();
115118
ExitOnErr(TextAPIWriter::writeToStream(OS, *Out, Ctx.WriteFT, Ctx.Compact));
@@ -120,10 +123,9 @@ bool handleMergeAction(const Context &Ctx) {
120123
if (Ctx.Inputs.size() < 2)
121124
reportError("merge requires at least two input files");
122125

123-
ExitOnError ExitOnErr("error: ");
124126
std::unique_ptr<InterfaceFile> Out;
125127
for (StringRef FileName : Ctx.Inputs) {
126-
auto IF = getInterfaceFile(FileName, ExitOnErr);
128+
auto IF = getInterfaceFile(FileName);
127129
// On the first iteration copy the input file and skip merge.
128130
if (!Out) {
129131
Out = std::move(IF);
@@ -145,11 +147,9 @@ int main(int Argc, char **Argv) {
145147
StringSaver Saver(A);
146148
TAPIOptTable Tbl;
147149
Context Ctx;
148-
opt::InputArgList Args =
149-
Tbl.parseArgs(Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
150-
WithColor::error(errs(), "llvm-readtapi") << Msg << "\n";
151-
exit(1);
152-
});
150+
ExitOnErr.setBanner(TOOLNAME + ": error:");
151+
opt::InputArgList Args = Tbl.parseArgs(
152+
Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) { reportError(Msg); });
153153
if (Args.hasArg(OPT_help)) {
154154
Tbl.printHelp(outs(), "llvm-readtapi [options] <inputs>",
155155
"LLVM TAPI file reader and manipulator");
@@ -163,11 +163,9 @@ int main(int Argc, char **Argv) {
163163
std::string OutputLoc = std::move(A->getValue());
164164
std::error_code EC;
165165
Ctx.OutStream = std::make_unique<llvm::raw_fd_stream>(OutputLoc, EC);
166-
if (EC) {
167-
llvm::errs() << "error opening the file '" << OutputLoc
168-
<< "': " << EC.message() << "\n";
169-
return NON_TAPI_EXIT_CODE;
170-
}
166+
if (EC)
167+
reportError("error opening the file '" + OutputLoc + EC.message(),
168+
NON_TAPI_EXIT_CODE);
171169
}
172170

173171
Ctx.Compact = Args.hasArg(OPT_compact);

0 commit comments

Comments
 (0)