__assert 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ASSERT
  10. #define _LIBCPP___ASSERT
  11. #include <__config>
  12. #include <__verbose_abort>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. #define _LIBCPP_ASSERT(expression, message) \
  17. (__builtin_expect(static_cast<bool>(expression), 1) \
  18. ? (void)0 \
  19. : _LIBCPP_VERBOSE_ABORT( \
  20. "%s:%d: assertion %s failed: %s\n", __builtin_FILE(), __builtin_LINE(), #expression, message))
  21. // TODO: __builtin_assume can currently inhibit optimizations. Until this has been fixed and we can add
  22. // assumptions without a clear optimization intent, disable that to avoid worsening the code generation.
  23. // See https://discourse.llvm.org/t/llvm-assume-blocks-optimization/71609 for a discussion.
  24. #if 0 && __has_builtin(__builtin_assume)
  25. # define _LIBCPP_ASSUME(expression) \
  26. (_LIBCPP_DIAGNOSTIC_PUSH _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume") \
  27. __builtin_assume(static_cast<bool>(expression)) _LIBCPP_DIAGNOSTIC_POP)
  28. #else
  29. # define _LIBCPP_ASSUME(expression) ((void)0)
  30. #endif
  31. #endif // _LIBCPP___ASSERT