SafeAssert.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/Portability.h>
  18. #include <folly/Preprocessor.h>
  19. /**
  20. * Verify that the expression is true. If not, prints an error message
  21. * (containing msg) to stderr and abort()s. Just like CHECK(), but only
  22. * logs to stderr and only does async-signal-safe calls.
  23. */
  24. #define FOLLY_SAFE_CHECK_IMPL(expr, expr_s, msg) \
  25. ((expr) ? static_cast<void>(0) \
  26. : ::folly::detail::assertionFailure( \
  27. FB_STRINGIZE(expr_s), \
  28. (msg), \
  29. __FILE__, \
  30. __LINE__, \
  31. __PRETTY_FUNCTION__))
  32. #define FOLLY_SAFE_CHECK(expr, msg) FOLLY_SAFE_CHECK_IMPL((expr), (expr), (msg))
  33. /**
  34. * In debug mode, verify that the expression is true. Otherwise, do nothing
  35. * (do not even evaluate expr). Just like DCHECK(), but only logs to stderr and
  36. * only does async-signal-safe calls.
  37. */
  38. #define FOLLY_SAFE_DCHECK(expr, msg) \
  39. FOLLY_SAFE_CHECK_IMPL(!folly::kIsDebug || (expr), (expr), (msg))
  40. namespace folly {
  41. namespace detail {
  42. [[noreturn]] void assertionFailure(
  43. const char* expr,
  44. const char* msg,
  45. const char* file,
  46. unsigned int line,
  47. const char* function);
  48. } // namespace detail
  49. } // namespace folly