error.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. namespace ErrorInternal {
  11. // An error-printing stream that exits on destruction.
  12. class ExitingStream {
  13. public:
  14. // Ends the error with a newline and exits.
  15. LLVM_ATTRIBUTE_NORETURN virtual ~ExitingStream() {
  16. llvm::errs() << "\n";
  17. exit(-1);
  18. }
  19. // Forward output to llvm::errs.
  20. template <typename T>
  21. ExitingStream& operator<<(const T& message) {
  22. llvm::errs() << message;
  23. return *this;
  24. }
  25. };
  26. } // namespace ErrorInternal
  27. // Prints an error and exits. This should be used for non-recoverable errors
  28. // with user input.
  29. //
  30. // For example:
  31. // FATAL_USER_ERROR(line_num) << "Line is bad!";
  32. // FATAL_USER_ERROR_NO_LINE() << "Application is bad!";
  33. //
  34. // Where possible, try to identify the error as a compilation error or runtime
  35. // error. The generic user error option is provided as a fallback for cases that
  36. // don't fit either of those classifications.
  37. #define FATAL_USER_ERROR_NO_LINE() ErrorInternal::ExitingStream() << "ERROR: "
  38. #define FATAL_USER_ERROR(line) FATAL_USER_ERROR_NO_LINE() << line << ": "
  39. #define FATAL_COMPILATION_ERROR_NO_LINE() \
  40. ErrorInternal::ExitingStream() << "COMPILATION ERROR: "
  41. #define FATAL_COMPILATION_ERROR(line) \
  42. FATAL_COMPILATION_ERROR_NO_LINE() << line << ": "
  43. #define FATAL_RUNTIME_ERROR_NO_LINE() \
  44. ErrorInternal::ExitingStream() << "RUNTIME ERROR: "
  45. #define FATAL_RUNTIME_ERROR(line) FATAL_RUNTIME_ERROR_NO_LINE() << line << ": "
  46. } // namespace Carbon
  47. #endif // EXECUTABLE_SEMANTICS_COMMON_ERROR_H_