check.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // Checks the given condition, and if it's false, prints a stack, streams the
  9. // error message, then exits. This should be used for unexpected errors, such as
  10. // a bug in the application.
  11. //
  12. // For example:
  13. // CARBON_CHECK(is_valid) << "Data is not valid!";
  14. #define CARBON_CHECK(...) \
  15. (__VA_ARGS__) ? (void)0 \
  16. : CARBON_CHECK_INTERNAL_STREAM() \
  17. << "CHECK failure at " << __FILE__ << ":" << __LINE__ \
  18. << ": " #__VA_ARGS__ \
  19. << Carbon::Internal::ExitingStream::AddSeparator()
  20. // DCHECK calls CHECK in debug mode, and does nothing otherwise.
  21. #ifndef NDEBUG
  22. #define CARBON_DCHECK(...) CARBON_CHECK(__VA_ARGS__)
  23. #else
  24. #define CARBON_DCHECK(...) CARBON_CHECK(true || (__VA_ARGS__))
  25. #endif
  26. // This is similar to CHECK, but is unconditional. Writing CARBON_FATAL() is
  27. // clearer than CARBON_CHECK(false) because it avoids confusion about control
  28. // flow.
  29. //
  30. // For example:
  31. // CARBON_FATAL() << "Unreachable!";
  32. #define CARBON_FATAL() \
  33. CARBON_CHECK_INTERNAL_STREAM() \
  34. << "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": "
  35. } // namespace Carbon
  36. #endif // CARBON_COMMON_CHECK_H_