check.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 CARBON_COMMON_CHECK_H_
  5. #define CARBON_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 CARBON_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. // CARBON_CHECK(is_valid) << "Data is not valid!";
  19. #define CARBON_CHECK(condition) \
  20. (condition) ? (void)0 \
  21. : CARBON_RAW_EXITING_STREAM() \
  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 CARBON_DCHECK(condition) CARBON_CHECK(condition)
  28. #else
  29. #define CARBON_DCHECK(condition) CARBON_CHECK(true || (condition))
  30. #endif
  31. // This is similar to CHECK, but is unconditional. Writing CARBON_FATAL() is
  32. // clearer than CARBON_CHECK(false) because it avoids confusion about control
  33. // flow.
  34. //
  35. // For example:
  36. // CARBON_FATAL() << "Unreachable!";
  37. #define CARBON_FATAL() \
  38. CARBON_RAW_EXITING_STREAM() \
  39. << "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": "
  40. } // namespace Carbon
  41. #endif // CARBON_COMMON_CHECK_H_