error.h 995 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
  5. #define EXECUTABLE_SEMANTICS_COMMON_ERROR_H_
  6. #include "llvm/Support/ErrorHandling.h"
  7. #include "llvm/Support/Signals.h"
  8. #include "llvm/Support/raw_ostream.h"
  9. namespace Carbon {
  10. // Prints an error and exits. This should be used for non-recoverable errors
  11. // with user input.
  12. //
  13. // For example:
  14. // FatalUserError() << "Input is not valid!";
  15. class FatalUserError {
  16. public:
  17. FatalUserError() { llvm::errs() << "ERROR: "; }
  18. ~FatalUserError() {
  19. // Finish with a newline.
  20. llvm::errs() << "\n";
  21. exit(-1);
  22. }
  23. // Forward output to llvm::errs.
  24. template <typename T>
  25. FatalUserError& operator<<(const T& message) {
  26. llvm::errs() << message;
  27. return *this;
  28. }
  29. };
  30. } // namespace Carbon
  31. #endif // EXECUTABLE_SEMANTICS_COMMON_ERROR_H_