check.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 "common/check_internal.h"
  7. namespace Carbon {
  8. // Raw exiting stream. This should be used when building other forms of exiting
  9. // macros like those below. It evaluates to a temporary `ExitingStream` object
  10. // that can be manipulated, streamed into, and then will exit the program.
  11. #define RAW_EXITING_STREAM() \
  12. Carbon::Internal::ExitingStream::Helper() | Carbon::Internal::ExitingStream()
  13. // Checks the given condition, and if it's false, prints a stack, streams the
  14. // error message, then exits. This should be used for unexpected errors, such as
  15. // a bug in the application.
  16. //
  17. // For example:
  18. // CHECK(is_valid) << "Data is not valid!";
  19. #define CHECK(condition) \
  20. (condition) ? (void)0 \
  21. : RAW_EXITING_STREAM().TreatAsBug() \
  22. << "CHECK failure at " << __FILE__ << ":" << __LINE__ \
  23. << ": " #condition \
  24. << Carbon::Internal::ExitingStream::AddSeparator()
  25. // This is similar to CHECK, but is unconditional. Writing FATAL() is clearer
  26. // than CHECK(false) because it avoids confusion about control flow.
  27. //
  28. // For example:
  29. // FATAL() << "Unreachable!";
  30. #define FATAL() \
  31. RAW_EXITING_STREAM().TreatAsBug() \
  32. << "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": "
  33. } // namespace Carbon
  34. #endif // COMMON_CHECK_H_