check.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // DCHECK calls CHECK in debug mode, and does nothing otherwise.
  26. #ifndef NDEBUG
  27. #define DCHECK(condition) CHECK(condition)
  28. #else
  29. #define DCHECK(condition) CHECK(true || (condition))
  30. #endif
  31. // This is similar to CHECK, but is unconditional. Writing FATAL() is clearer
  32. // than CHECK(false) because it avoids confusion about control flow.
  33. //
  34. // For example:
  35. // FATAL() << "Unreachable!";
  36. #define FATAL() \
  37. RAW_EXITING_STREAM().TreatAsBug() \
  38. << "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": "
  39. } // namespace Carbon
  40. #endif // COMMON_CHECK_H_