[llvm-tapi-diff] Replicate diff utility error handling

For scripting purposes, use different error code for cases that aren't a
result of different tbd files, e.g. bad input like a path to a broken
symlink. This behaves more similiarly to how the unix `diff` command behaves.
This also includes minor tweaks on error messages.

Reviewed By: ributzka, JDevlieghere

Differential Revision: https://reviews.llvm.org/D115905
This commit is contained in:
Cyndy Ishida 2021-12-18 09:42:40 -08:00
parent 9927a06f74
commit 982604cc08
3 changed files with 12 additions and 30 deletions

View File

@ -2,6 +2,6 @@
; RUN: yaml2obj %S/Inputs/macho.yaml -o %t/macho.dylib
; RUN: not llvm-tapi-diff %S/Inputs/v4A.tbd %t/macho.dylib 2>&1 | FileCheck %s
; CHECK: {{.*}}: error: {{.*}}macho.dylib: Error when parsing file, unsupported file format
; CHECK: error: {{.*}}macho.dylib' unsupported file format
; CHECK-NOT: error:
; CHECK-NOT: warning:

View File

@ -1,5 +1,3 @@
; RUN: not llvm-tapi-diff %S/Inputs/v4A.tbd %S/Inputs/v4.tbd 2>&1 | FileCheck %s -DMSG=%errc_ENOENT
; RUN: not llvm-tapi-diff %S/Inputs/v4A.tbd %S/Inputs/v4.tbd 2>&1 | FileCheck %s
; CHECK: {{.*}}: error: {{.*}}v4.tbd: [[MSG]]
; CHECK-NOT: error:
; CHECK-NOT: warning:
; CHECK: error: {{.*}}v4.tbd' {{[Nn]}}o such file or directory

View File

@ -1,6 +1,4 @@
//===-- llvm-tapi-diff.cpp - tbd comparator command-line driver ---*-
// C++
//-*-===//
//===-- llvm-tapi-diff.cpp - tbd comparator command-line driver --*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@ -31,16 +29,8 @@ cl::opt<std::string> InputFileNameLHS(cl::Positional, cl::desc("<first file>"),
cl::cat(NMCat));
cl::opt<std::string> InputFileNameRHS(cl::Positional, cl::desc("<second file>"),
cl::cat(NMCat));
std::string ToolName;
} // anonymous namespace
ExitOnError ExitOnErr;
void setErrorBanner(ExitOnError &ExitOnErr, std::string InputFile) {
ExitOnErr.setBanner(ToolName + ": error: " + InputFile + ": ");
}
Expected<std::unique_ptr<Binary>> convertFileToBinary(std::string &Filename) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFileOrSTDIN(Filename);
@ -52,35 +42,29 @@ Expected<std::unique_ptr<Binary>> convertFileToBinary(std::string &Filename) {
int main(int Argc, char **Argv) {
InitLLVM X(Argc, Argv);
cl::HideUnrelatedOptions(NMCat);
cl::ParseCommandLineOptions(
Argc, Argv,
"This tool will compare two tbd files and return the "
"differences in those files.");
cl::ParseCommandLineOptions(Argc, Argv, "Text-based Stubs Comparison Tool");
if (InputFileNameLHS.empty() || InputFileNameRHS.empty()) {
cl::PrintHelpMessage();
return EXIT_FAILURE;
}
ToolName = Argv[0];
setErrorBanner(ExitOnErr, InputFileNameLHS);
ExitOnError ExitOnErr("error: '" + InputFileNameLHS + "' ",
/*DefaultErrorExitCode=*/2);
auto BinLHS = ExitOnErr(convertFileToBinary(InputFileNameLHS));
TapiUniversal *FileLHS = dyn_cast<TapiUniversal>(BinLHS.get());
if (!FileLHS) {
ExitOnErr(
createStringError(std::errc::executable_format_error,
"Error when parsing file, unsupported file format"));
ExitOnErr(createStringError(std::errc::executable_format_error,
"unsupported file format"));
}
setErrorBanner(ExitOnErr, InputFileNameRHS);
ExitOnErr.setBanner("error: '" + InputFileNameRHS + "' ");
auto BinRHS = ExitOnErr(convertFileToBinary(InputFileNameRHS));
TapiUniversal *FileRHS = dyn_cast<TapiUniversal>(BinRHS.get());
if (!FileRHS) {
ExitOnErr(
createStringError(std::errc::executable_format_error,
"Error when parsing file, unsupported file format"));
ExitOnErr(createStringError(std::errc::executable_format_error,
"unsupported file format"));
}
raw_ostream &OS = outs();