uassert.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 2002-2011, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File uassert.h
  12. *
  13. * Contains the U_ASSERT and UPRV_UNREACHABLE_* macros
  14. *
  15. ******************************************************************************
  16. */
  17. #ifndef U_ASSERT_H
  18. #define U_ASSERT_H
  19. /* utypes.h is included to get the proper define for uint8_t */
  20. #include "unicode/utypes.h"
  21. /* for abort */
  22. #include <stdlib.h>
  23. /**
  24. * \def U_ASSERT
  25. * By default, U_ASSERT just wraps the C library assert macro.
  26. * By changing the definition here, the assert behavior for ICU can be changed
  27. * without affecting other non - ICU uses of the C library assert().
  28. */
  29. #if U_DEBUG
  30. # include <assert.h>
  31. # define U_ASSERT(exp) assert(exp)
  32. #elif U_CPLUSPLUS_VERSION
  33. # define U_ASSERT(exp) (void)0
  34. #else
  35. # define U_ASSERT(exp)
  36. #endif
  37. /**
  38. * \def UPRV_UNREACHABLE_ASSERT
  39. * This macro is used in places that we had believed were unreachable, but
  40. * experience has shown otherwise (possibly due to memory corruption, etc).
  41. * In this case we call assert() in debug versions as with U_ASSERT, instead
  42. * of unconditionally calling abort(). However we also allow redefinition as
  43. * with UPRV_UNREACHABLE_EXIT.
  44. * @internal
  45. */
  46. #if defined(UPRV_UNREACHABLE_ASSERT)
  47. // Use the predefined value.
  48. #elif U_DEBUG
  49. # include <assert.h>
  50. # define UPRV_UNREACHABLE_ASSERT assert(false)
  51. #elif U_CPLUSPLUS_VERSION
  52. # define UPRV_UNREACHABLE_ASSERT (void)0
  53. #else
  54. # define UPRV_UNREACHABLE_ASSERT
  55. #endif
  56. /**
  57. * \def UPRV_UNREACHABLE_EXIT
  58. * This macro is used to unconditionally abort if unreachable code is ever executed.
  59. * @internal
  60. */
  61. #if defined(UPRV_UNREACHABLE_EXIT)
  62. // Use the predefined value.
  63. #else
  64. # define UPRV_UNREACHABLE_EXIT abort()
  65. #endif
  66. #endif