mbed_assert.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /** \addtogroup platform */
  2. /** @{*/
  3. /**
  4. * \defgroup platform_Assert Assert macros
  5. * @{
  6. */
  7. /* mbed Microcontroller Library
  8. * Copyright (c) 2006-2013 ARM Limited
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef MBED_ASSERT_H
  23. #define MBED_ASSERT_H
  24. #include "mbed_preprocessor.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /** Internal mbed assert function which is invoked when MBED_ASSERT macro failes.
  29. * This function is active only if NDEBUG is not defined prior to including this
  30. * assert header file.
  31. * In case of MBED_ASSERT failing condition, error() is called with the assertation message.
  32. * @param expr Expresion to be checked.
  33. * @param file File where assertation failed.
  34. * @param line Failing assertation line number.
  35. */
  36. void mbed_assert_internal(const char *expr, const char *file, int line);
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40. /** MBED_ASSERT
  41. * Declare runtime assertions: results in runtime error if condition is false
  42. *
  43. * @note
  44. * Use of MBED_ASSERT is limited to Debug and Develop builds.
  45. *
  46. * @code
  47. *
  48. * int Configure(serial_t *obj) {
  49. * MBED_ASSERT(obj);
  50. * }
  51. * @endcode
  52. */
  53. #ifdef NDEBUG
  54. #define MBED_ASSERT(expr) ((void)0)
  55. #else
  56. #define MBED_ASSERT(expr) \
  57. do { \
  58. if (!(expr)) { \
  59. mbed_assert_internal(#expr, __FILE__, __LINE__); \
  60. } \
  61. } while (0)
  62. #endif
  63. /** MBED_STATIC_ASSERT
  64. * Declare compile-time assertions, results in compile-time error if condition is false
  65. *
  66. * The assertion acts as a declaration that can be placed at file scope, in a
  67. * code block (except after a label), or as a member of a C++ class/struct/union.
  68. *
  69. * @note
  70. * Use of MBED_STATIC_ASSERT as a member of a struct/union is limited:
  71. * - In C++, MBED_STATIC_ASSERT is valid in class/struct/union scope.
  72. * - In C, MBED_STATIC_ASSERT is not valid in struct/union scope, and
  73. * MBED_STRUCT_STATIC_ASSERT is provided as an alternative that is valid
  74. * in C and C++ class/struct/union scope.
  75. *
  76. * @code
  77. * MBED_STATIC_ASSERT(MBED_LIBRARY_VERSION >= 120,
  78. * "The mbed library must be at least version 120");
  79. *
  80. * int main() {
  81. * MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
  82. * "An int must be larger than a char");
  83. * }
  84. * @endcode
  85. */
  86. #if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L)
  87. #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
  88. #elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
  89. #define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
  90. #elif defined(__cplusplus) && defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__) \
  91. && (__GNUC__*100 + __GNUC_MINOR__) > 403L
  92. #define MBED_STATIC_ASSERT(expr, msg) __extension__ static_assert(expr, msg)
  93. #elif !defined(__cplusplus) && defined(__GNUC__) && !defined(__CC_ARM) \
  94. && (__GNUC__*100 + __GNUC_MINOR__) > 406L
  95. #define MBED_STATIC_ASSERT(expr, msg) __extension__ _Static_assert(expr, msg)
  96. #elif defined(__ICCARM__)
  97. #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
  98. #else
  99. #define MBED_STATIC_ASSERT(expr, msg) \
  100. enum {MBED_CONCAT(MBED_ASSERTION_AT_, __LINE__) = sizeof(char[(expr) ? 1 : -1])}
  101. #endif
  102. /** MBED_STRUCT_STATIC_ASSERT
  103. * Declare compile-time assertions, results in compile-time error if condition is false
  104. *
  105. * Unlike MBED_STATIC_ASSERT, MBED_STRUCT_STATIC_ASSERT can and must be used
  106. * as a member of a C/C++ class/struct/union.
  107. *
  108. * @code
  109. * struct thing {
  110. * MBED_STATIC_ASSERT(2 + 2 == 4,
  111. * "Hopefully the universe is mathematically consistent");
  112. * };
  113. * @endcode
  114. */
  115. #define MBED_STRUCT_STATIC_ASSERT(expr, msg) int : (expr) ? 0 : -1
  116. #endif
  117. /**@}*/
  118. /**@}*/