Skip to content

[readtapi] Use consistent error handling diagnostics #73419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions llvm/tools/llvm-readtapi/llvm-readtapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}
Expand All @@ -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())
Expand All @@ -82,34 +87,32 @@ 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);
}

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));
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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);
Expand Down