1 //===- ErrorCollector.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===-----------------------------------------------------------------------===/ 8 9 #include "ErrorCollector.h" 10 #include "llvm/Support/Errc.h" 11 #include "llvm/Support/Error.h" 12 #include "llvm/Support/WithColor.h" 13 #include "llvm/Support/raw_ostream.h" 14 #include <vector> 15 16 using namespace llvm; 17 using namespace llvm::elfabi; 18 escalateToFatal()19void ErrorCollector::escalateToFatal() { ErrorsAreFatal = true; } 20 addError(Error && Err,StringRef Tag)21void ErrorCollector::addError(Error &&Err, StringRef Tag) { 22 if (Err) { 23 Errors.push_back(std::move(Err)); 24 Tags.push_back(Tag.str()); 25 } 26 } 27 makeError()28Error ErrorCollector::makeError() { 29 // TODO: Make this return something (an AggregateError?) that gives more 30 // individual control over each error and which might be of interest. 31 Error JoinedErrors = Error::success(); 32 for (Error &E : Errors) { 33 JoinedErrors = joinErrors(std::move(JoinedErrors), std::move(E)); 34 } 35 Errors.clear(); 36 Tags.clear(); 37 return JoinedErrors; 38 } 39 log(raw_ostream & OS)40void ErrorCollector::log(raw_ostream &OS) { 41 OS << "Encountered multiple errors:\n"; 42 for (size_t i = 0; i < Errors.size(); ++i) { 43 WithColor::error(OS) << "(" << Tags[i] << ") " << Errors[i]; 44 if (i != Errors.size() - 1) 45 OS << "\n"; 46 } 47 } 48 allErrorsHandled() const49bool ErrorCollector::allErrorsHandled() const { return Errors.empty(); } 50 ~ErrorCollector()51ErrorCollector::~ErrorCollector() { 52 if (ErrorsAreFatal && !allErrorsHandled()) 53 fatalUnhandledError(); 54 55 for (Error &E : Errors) { 56 consumeError(std::move(E)); 57 } 58 } 59 fatalUnhandledError()60LLVM_ATTRIBUTE_NORETURN void ErrorCollector::fatalUnhandledError() { 61 errs() << "Program aborted due to unhandled Error(s):\n"; 62 log(errs()); 63 errs() << "\n"; 64 abort(); 65 } 66