| Armando Montanez | 31f0f65 | 2019-01-03 18:32:36 +0000 | [diff] [blame] | 1 | //===- ErrorCollector.cpp -------------------------------------------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Armando Montanez | 31f0f65 | 2019-01-03 18:32:36 +0000 | [diff] [blame] | 6 | // |
| 7 | //===-----------------------------------------------------------------------===/ |
| 8 | |
| 9 | #include "ErrorCollector.h" |
| 10 | #include "llvm/Support/Errc.h" |
| 11 | #include "llvm/Support/Error.h" |
| Armando Montanez | 31f0f65 | 2019-01-03 18:32:36 +0000 | [diff] [blame] | 12 | #include "llvm/Support/WithColor.h" |
| Fangrui Song | ef59875 | 2019-02-21 07:42:31 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
| Armando Montanez | 31f0f65 | 2019-01-03 18:32:36 +0000 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace llvm::elfabi; |
| 18 | |
| 19 | void ErrorCollector::escalateToFatal() { |
| 20 | ErrorsAreFatal = true; |
| 21 | } |
| 22 | |
| 23 | void ErrorCollector::addError(Error &&Err, StringRef Tag) { |
| 24 | if (Err) { |
| 25 | Errors.push_back(std::move(Err)); |
| 26 | Tags.push_back(Tag.str()); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | Error ErrorCollector::makeError() { |
| 31 | // TODO: Make this return something (an AggregateError?) that gives more |
| 32 | // individual control over each error and which might be of interest. |
| 33 | Error JoinedErrors = Error::success(); |
| 34 | for (Error &E : Errors) { |
| 35 | JoinedErrors = joinErrors(std::move(JoinedErrors), std::move(E)); |
| 36 | } |
| 37 | Errors.clear(); |
| 38 | Tags.clear(); |
| 39 | return JoinedErrors; |
| 40 | } |
| 41 | |
| 42 | void ErrorCollector::log(raw_ostream &OS) { |
| 43 | OS << "Encountered multiple errors:\n"; |
| 44 | for (size_t i = 0; i < Errors.size(); ++i) { |
| 45 | WithColor::error(OS) << "(" << Tags[i] << ") " << Errors[i]; |
| 46 | if (i != Errors.size() - 1) |
| 47 | OS << "\n"; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | bool ErrorCollector::allErrorsHandled() const { |
| 52 | return Errors.empty(); |
| 53 | } |
| 54 | |
| 55 | ErrorCollector::~ErrorCollector() { |
| 56 | if (ErrorsAreFatal && !allErrorsHandled()) |
| 57 | fatalUnhandledError(); |
| 58 | |
| 59 | for (Error &E : Errors) { |
| 60 | consumeError(std::move(E)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | LLVM_ATTRIBUTE_NORETURN void ErrorCollector::fatalUnhandledError() { |
| 65 | errs() << "Program aborted due to unhandled Error(s):\n"; |
| 66 | log(errs()); |
| 67 | errs() << "\n"; |
| 68 | abort(); |
| 69 | } |