17
17
#include " llvm/Support/Error.h"
18
18
#include " llvm/Support/InitLLVM.h"
19
19
#include " llvm/Support/MemoryBuffer.h"
20
- #include " llvm/Support/WithColor.h"
21
20
#include " llvm/Support/raw_ostream.h"
22
21
#include " llvm/TextAPI/TextAPIError.h"
23
22
#include " llvm/TextAPI/TextAPIReader.h"
@@ -57,9 +56,16 @@ class TAPIOptTable : public opt::GenericOptTable {
57
56
}
58
57
};
59
58
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
+
60
66
// Handle error reporting in cases where `ExitOnError` is not used.
61
67
void reportError (Twine Message, int ExitCode = EXIT_FAILURE) {
62
- WithColor::error ( errs ()) << Message << " \n " ;
68
+ errs () << TOOLNAME << " : error: " << Message << " \n " ;
63
69
errs ().flush ();
64
70
exit (ExitCode);
65
71
}
@@ -71,9 +77,8 @@ struct Context {
71
77
bool Compact = false ;
72
78
};
73
79
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 () + " ' " );
77
82
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
78
83
MemoryBuffer::getFile (Filename);
79
84
if (BufferOrErr.getError ())
@@ -82,34 +87,32 @@ std::unique_ptr<InterfaceFile> getInterfaceFile(const StringRef Filename,
82
87
TextAPIReader::get ((*BufferOrErr)->getMemBufferRef ());
83
88
if (!IF)
84
89
ExitOnErr (IF.takeError ());
90
+ // Set Banner back.
91
+ ExitOnErr.setBanner (TOOLNAME + " : error: " );
85
92
return std::move (*IF);
86
93
}
87
94
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
-
93
95
bool handleCompareAction (const Context &Ctx) {
94
96
if (Ctx.Inputs .size () != 2 )
95
97
reportError (" compare only supports two input files" ,
96
98
/* ExitCode=*/ NON_TAPI_EXIT_CODE);
97
99
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 ));
101
105
102
106
raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs ();
103
107
return DiffEngine (LeftIF.get (), RightIF.get ()).compareFiles (OS);
104
108
}
105
109
106
110
bool handleWriteAction (const Context &Ctx,
107
111
std::unique_ptr<InterfaceFile> Out = nullptr ) {
108
- ExitOnError ExitOnErr (" error: " );
109
112
if (!Out) {
110
113
if (Ctx.Inputs .size () != 1 )
111
114
reportError (" write only supports one input file" );
112
- Out = getInterfaceFile (Ctx.Inputs .front (), ExitOnErr );
115
+ Out = getInterfaceFile (Ctx.Inputs .front ());
113
116
}
114
117
raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs ();
115
118
ExitOnErr (TextAPIWriter::writeToStream (OS, *Out, Ctx.WriteFT , Ctx.Compact ));
@@ -120,10 +123,9 @@ bool handleMergeAction(const Context &Ctx) {
120
123
if (Ctx.Inputs .size () < 2 )
121
124
reportError (" merge requires at least two input files" );
122
125
123
- ExitOnError ExitOnErr (" error: " );
124
126
std::unique_ptr<InterfaceFile> Out;
125
127
for (StringRef FileName : Ctx.Inputs ) {
126
- auto IF = getInterfaceFile (FileName, ExitOnErr );
128
+ auto IF = getInterfaceFile (FileName);
127
129
// On the first iteration copy the input file and skip merge.
128
130
if (!Out) {
129
131
Out = std::move (IF);
@@ -145,11 +147,9 @@ int main(int Argc, char **Argv) {
145
147
StringSaver Saver (A);
146
148
TAPIOptTable Tbl;
147
149
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); });
153
153
if (Args.hasArg (OPT_help)) {
154
154
Tbl.printHelp (outs (), " llvm-readtapi [options] <inputs>" ,
155
155
" LLVM TAPI file reader and manipulator" );
@@ -163,11 +163,9 @@ int main(int Argc, char **Argv) {
163
163
std::string OutputLoc = std::move (A->getValue ());
164
164
std::error_code EC;
165
165
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);
171
169
}
172
170
173
171
Ctx.Compact = Args.hasArg (OPT_compact);
0 commit comments