1 //===- ErrorCollector.h -----------------------------------------*- C++ -*-===//
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
7 //===-----------------------------------------------------------------------===/
9 /// This class collects errors that should be reported or ignored in aggregate.
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
19 //===-----------------------------------------------------------------------===/
21 #ifndef LLVM_TOOLS_LLVM_IFS_ERRORCOLLECTOR_H
22 #define LLVM_TOOLS_LLVM_IFS_ERRORCOLLECTOR_H
24 #include "llvm/Support/raw_ostream.h"
33 class ErrorCollector
{
35 /// Upon destruction, an ErrorCollector will crash if UseFatalErrors=true and
36 /// there are remaining errors that haven't been fetched by makeError().
37 ErrorCollector(bool UseFatalErrors
= true) : ErrorsAreFatal(UseFatalErrors
) {}
38 // Don't allow copying.
39 ErrorCollector(const ErrorCollector
&Stub
) = delete;
40 ErrorCollector
&operator=(const ErrorCollector
&Other
) = delete;
43 // TODO: Add move constructor and operator= when a testable situation arises.
45 /// Returns a single error that contains messages for all stored Errors.
48 /// Adds an error with a descriptive tag that helps with identification.
49 /// If the error is an Error::success(), it is checked and discarded.
50 void addError(Error
&&E
, StringRef Tag
);
52 /// This ensures an ErrorCollector will treat unhandled errors as fatal.
53 /// This function should be called if errors that usually can be ignored
54 /// are suddenly of concern (i.e. attempt multiple things that return Error,
55 /// but only care about the Errors if no attempt succeeds).
56 void escalateToFatal();
59 /// Logs all errors to a raw_ostream.
60 void log(raw_ostream
&OS
);
62 /// Returns true if all errors have been retrieved through makeError(), or
63 /// false if errors have been added since the last makeError() call.
64 bool allErrorsHandled() const;
66 /// Dump output and crash.
67 [[noreturn
]] void fatalUnhandledError();
70 std::vector
<Error
> Errors
;
71 std::vector
<std::string
> Tags
;
74 } // end namespace ifs
75 } // end namespace llvm
77 #endif // LLVM_TOOLS_LLVM_IFS_ERRORCOLLECTOR_H