@@ -200,6 +200,25 @@ static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass(
200
200
201
201
static int compileModule (char **, LLVMContext &);
202
202
203
+ LLVM_ATTRIBUTE_NORETURN static void reportError (Twine Msg,
204
+ StringRef Filename = " " ) {
205
+ SmallString<256 > Prefix;
206
+ if (!Filename.empty ()) {
207
+ if (Filename == " -" )
208
+ Filename = " <stdin>" ;
209
+ (" '" + Twine (Filename) + " ': " ).toStringRef (Prefix);
210
+ }
211
+ WithColor::error (errs (), " llc" ) << Prefix << Msg << " \n " ;
212
+ exit (1 );
213
+ }
214
+
215
+ LLVM_ATTRIBUTE_NORETURN static void reportError (Error Err, StringRef Filename) {
216
+ assert (Err);
217
+ handleAllErrors (createFileError (Filename, std::move (Err)),
218
+ [&](const ErrorInfoBase &EI) { reportError (EI.message ()); });
219
+ llvm_unreachable (" reportError() should not return" );
220
+ }
221
+
203
222
static std::unique_ptr<ToolOutputFile> GetOutputStream (const char *TargetName,
204
223
Triple::OSType OS,
205
224
const char *ProgName) {
@@ -260,7 +279,7 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
260
279
OpenFlags |= sys::fs::OF_Text;
261
280
auto FDOut = std::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags);
262
281
if (EC) {
263
- WithColor::error () << EC.message () << ' \n ' ;
282
+ reportError ( EC.message ()) ;
264
283
return nullptr ;
265
284
}
266
285
@@ -353,18 +372,12 @@ int main(int argc, char **argv) {
353
372
setupLLVMOptimizationRemarks (Context, RemarksFilename, RemarksPasses,
354
373
RemarksFormat, RemarksWithHotness,
355
374
RemarksHotnessThreshold);
356
- if (Error E = RemarksFileOrErr.takeError ()) {
357
- WithColor::error (errs (), argv[0 ]) << toString (std::move (E)) << ' \n ' ;
358
- return 1 ;
359
- }
375
+ if (Error E = RemarksFileOrErr.takeError ())
376
+ reportError (std::move (E), RemarksFilename);
360
377
std::unique_ptr<ToolOutputFile> RemarksFile = std::move (*RemarksFileOrErr);
361
378
362
- if (InputLanguage != " " && InputLanguage != " ir" &&
363
- InputLanguage != " mir" ) {
364
- WithColor::error (errs (), argv[0 ])
365
- << " input language must be '', 'IR' or 'MIR'\n " ;
366
- return 1 ;
367
- }
379
+ if (InputLanguage != " " && InputLanguage != " ir" && InputLanguage != " mir" )
380
+ reportError (" input language must be '', 'IR' or 'MIR'" );
368
381
369
382
// Compile the module TimeCompilations times to give better compile time
370
383
// metrics.
@@ -490,11 +503,9 @@ static int compileModule(char **argv, LLVMContext &Context) {
490
503
491
504
// On AIX, setting the relocation model to anything other than PIC is
492
505
// considered a user error.
493
- if (TheTriple.isOSAIX () && RM.hasValue () && *RM != Reloc::PIC_) {
494
- WithColor::error (errs (), argv[0 ])
495
- << " invalid relocation model, AIX only supports PIC.\n " ;
496
- exit (1 );
497
- }
506
+ if (TheTriple.isOSAIX () && RM.hasValue () && *RM != Reloc::PIC_)
507
+ reportError (" invalid relocation model, AIX only supports PIC" ,
508
+ InputFilename);
498
509
499
510
InitializeOptions (TheTriple);
500
511
Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine (
@@ -567,10 +578,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
567
578
std::error_code EC;
568
579
DwoOut = std::make_unique<ToolOutputFile>(SplitDwarfOutputFile, EC,
569
580
sys::fs::OF_None);
570
- if (EC) {
571
- WithColor::error (errs (), argv[0 ]) << EC.message () << ' \n ' ;
572
- return 1 ;
573
- }
581
+ if (EC)
582
+ reportError (EC.message (), SplitDwarfOutputFile);
574
583
}
575
584
576
585
// Build up all of the passes that we want to do to the module.
@@ -586,12 +595,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
586
595
587
596
// Verify module immediately to catch problems before doInitialization() is
588
597
// called on any passes.
589
- if (!NoVerify && verifyModule (*M, &errs ())) {
590
- std::string Prefix =
591
- (Twine (argv[0 ]) + Twine (" : " ) + Twine (InputFilename)).str ();
592
- WithColor::error (errs (), Prefix) << " input module is broken!\n " ;
593
- return 1 ;
594
- }
598
+ if (!NoVerify && verifyModule (*M, &errs ()))
599
+ reportError (" input module cannot be verified" , InputFilename);
595
600
596
601
// Override function attributes based on CPUStr, FeaturesStr, and command line
597
602
// flags.
@@ -650,10 +655,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
650
655
} else if (Target->addPassesToEmitFile (
651
656
PM, *OS, DwoOut ? &DwoOut->os () : nullptr ,
652
657
codegen::getFileType (), NoVerify, MMIWP)) {
653
- WithColor::warning (errs (), argv[0 ])
654
- << " target does not support generation of this"
655
- << " file type!\n " ;
656
- return 1 ;
658
+ reportError (" target does not support generation of this file type" );
657
659
}
658
660
659
661
const_cast <TargetLoweringObjectFile *>(LLVMTM.getObjFileLowering ())
0 commit comments