blob: 0d74979c9a309cb6d981fcea563b759ce7f73475 [file] [log] [blame]
Armando Montanez31f0f652019-01-03 18:32:36 +00001//===- ErrorCollector.cpp -------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===-----------------------------------------------------------------------===/
9
10#include "ErrorCollector.h"
11#include "llvm/Support/Errc.h"
12#include "llvm/Support/Error.h"
13#include "llvm/Support/raw_ostream.h"
14#include "llvm/Support/WithColor.h"
15#include <vector>
16
17using namespace llvm;
18using namespace llvm::elfabi;
19
20void ErrorCollector::escalateToFatal() {
21 ErrorsAreFatal = true;
22}
23
24void ErrorCollector::addError(Error &&Err, StringRef Tag) {
25 if (Err) {
26 Errors.push_back(std::move(Err));
27 Tags.push_back(Tag.str());
28 }
29}
30
31Error ErrorCollector::makeError() {
32 // TODO: Make this return something (an AggregateError?) that gives more
33 // individual control over each error and which might be of interest.
34 Error JoinedErrors = Error::success();
35 for (Error &E : Errors) {
36 JoinedErrors = joinErrors(std::move(JoinedErrors), std::move(E));
37 }
38 Errors.clear();
39 Tags.clear();
40 return JoinedErrors;
41}
42
43void ErrorCollector::log(raw_ostream &OS) {
44 OS << "Encountered multiple errors:\n";
45 for (size_t i = 0; i < Errors.size(); ++i) {
46 WithColor::error(OS) << "(" << Tags[i] << ") " << Errors[i];
47 if (i != Errors.size() - 1)
48 OS << "\n";
49 }
50}
51
52bool ErrorCollector::allErrorsHandled() const {
53 return Errors.empty();
54}
55
56ErrorCollector::~ErrorCollector() {
57 if (ErrorsAreFatal && !allErrorsHandled())
58 fatalUnhandledError();
59
60 for (Error &E : Errors) {
61 consumeError(std::move(E));
62 }
63}
64
65LLVM_ATTRIBUTE_NORETURN void ErrorCollector::fatalUnhandledError() {
66 errs() << "Program aborted due to unhandled Error(s):\n";
67 log(errs());
68 errs() << "\n";
69 abort();
70}