[llvm/Object] - Convert SectionRef::getName() to return Expected<>
[llvm-complete.git] / tools / llvm-elfabi / ErrorCollector.h
blobe2eb1c5ed9f3ba18f42a2d3f5b954a4aa93712ac
1 //===- ErrorCollector.h -----------------------------------------*- C++ -*-===//
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 /// This class collects errors that should be reported or ignored in aggregate.
10 ///
11 /// Like llvm::Error, an ErrorCollector cannot be copied. Unlike llvm::Error,
12 /// an ErrorCollector may be destroyed if it was originally constructed to treat
13 /// errors as non-fatal. In this case, all Errors are consumed upon destruction.
14 /// An ErrorCollector may be initially constructed (or escalated) such that
15 /// errors are treated as fatal. This causes a crash if an attempt is made to
16 /// delete the ErrorCollector when some Errors have not been retrieved via
17 /// makeError().
18 ///
19 //===-----------------------------------------------------------------------===/
21 #ifndef LLVM_TOOLS_ELFABI_ERRORCOLLECTOR_H
22 #define LLVM_TOOLS_ELFABI_ERRORCOLLECTOR_H
24 #include "llvm/Support/Error.h"
25 #include <vector>
27 namespace llvm {
28 namespace elfabi {
30 class ErrorCollector {
31 public:
32 /// Upon destruction, an ErrorCollector will crash if UseFatalErrors=true and
33 /// there are remaining errors that haven't been fetched by makeError().
34 ErrorCollector(bool UseFatalErrors = true) : ErrorsAreFatal(UseFatalErrors) {}
35 // Don't allow copying.
36 ErrorCollector(const ErrorCollector &Stub) = delete;
37 ErrorCollector &operator=(const ErrorCollector &Other) = delete;
38 ~ErrorCollector();
40 // TODO: Add move constructor and operator= when a testable situation arises.
42 /// Returns a single error that contains messages for all stored Errors.
43 Error makeError();
45 /// Adds an error with a descriptive tag that helps with identification.
46 /// If the error is an Error::success(), it is checked and discarded.
47 void addError(Error &&E, StringRef Tag);
49 /// This ensures an ErrorCollector will treat unhandled errors as fatal.
50 /// This function should be called if errors that usually can be ignored
51 /// are suddenly of concern (i.e. attempt multiple things that return Error,
52 /// but only care about the Errors if no attempt succeeds).
53 void escalateToFatal();
55 private:
56 /// Logs all errors to a raw_ostream.
57 void log(raw_ostream &OS);
59 /// Returns true if all errors have been retrieved through makeError(), or
60 /// false if errors have been added since the last makeError() call.
61 bool allErrorsHandled() const;
63 /// Dump output and crash.
64 LLVM_ATTRIBUTE_NORETURN void fatalUnhandledError();
66 bool ErrorsAreFatal;
67 std::vector<Error> Errors;
68 std::vector<std::string> Tags;
71 } // end namespace elfabi
72 } // end namespace llvm
74 #endif // LLVM_TOOLS_ELFABI_ERRORCOLLECTOR_H