check.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 COMMON_CHECK_H_
  5. #define COMMON_CHECK_H_
  6. #include "llvm/Support/ErrorHandling.h"
  7. #include "llvm/Support/Signals.h"
  8. #include "llvm/Support/raw_ostream.h"
  9. namespace Carbon {
  10. // Wraps a stream and exiting for fatal errors.
  11. class ExitingStream {
  12. public:
  13. LLVM_ATTRIBUTE_NORETURN ~ExitingStream() {
  14. // Finish with a newline.
  15. llvm::errs() << "\n";
  16. exit(-1);
  17. }
  18. // Indicates that initial input is in, so this is where a ": " should be added
  19. // before user input.
  20. ExitingStream& add_separator() {
  21. separator = true;
  22. return *this;
  23. }
  24. // Prints a stack traces.
  25. ExitingStream& print_stack() {
  26. llvm::sys::PrintStackTrace(llvm::errs());
  27. return *this;
  28. }
  29. // If the bool cast occurs, it's because the condition is false. This supports
  30. // && short-circuiting the creation of ExitingStream.
  31. explicit operator bool() const { return true; }
  32. // Forward output to llvm::errs.
  33. template <typename T>
  34. ExitingStream& operator<<(const T& message) {
  35. if (separator) {
  36. llvm::errs() << ": ";
  37. separator = false;
  38. }
  39. llvm::errs() << message;
  40. return *this;
  41. }
  42. private:
  43. // Whether a separator should be printed if << is used again.
  44. bool separator = false;
  45. };
  46. // Checks the given condition, and if it's false, prints a stack, streams the
  47. // error message, then exits. This should be used for unexpected errors, such as
  48. // a bug in the application.
  49. //
  50. // For example:
  51. // CHECK(is_valid) << "Data is not valid!";
  52. #define CHECK(condition) \
  53. (!(condition)) && \
  54. (Carbon::ExitingStream().print_stack() << "CHECK failure: " #condition) \
  55. .add_separator()
  56. // This is similar to CHECK, but is unconditional.
  57. //
  58. // For example:
  59. // FATAL() << "Unreachable!";
  60. #define FATAL() Carbon::ExitingStream().print_stack() << "FATAL: "
  61. } // namespace Carbon
  62. #endif // COMMON_CHECK_H_