ErrorCollector.h 2.8 KB

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