ErrorCollector.h 2.8 KB

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