qassert.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * @file
  3. * @brief Customizable and memory-efficient assertions for embedded systems
  4. * @cond
  5. ******************************************************************************
  6. * Last updated for version 5.4.0
  7. * Last updated on 2015-03-14
  8. *
  9. * Q u a n t u m L e a P s
  10. * ---------------------------
  11. * innovating embedded systems
  12. *
  13. * Copyright (C) Quantum Leaps, www.state-machine.com.
  14. *
  15. * This program is open source software: you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as published
  17. * by the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * Alternatively, this program may be distributed and modified under the
  21. * terms of Quantum Leaps commercial licenses, which expressly supersede
  22. * the GNU General Public License and are specifically designed for
  23. * licensees interested in retaining the proprietary status of their code.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. * Contact information:
  34. * Web: www.state-machine.com
  35. * Email: info@state-machine.com
  36. ******************************************************************************
  37. * @endcond
  38. */
  39. #ifndef qassert_h
  40. #define qassert_h
  41. /**
  42. * @note
  43. * This header file can be used in C, C++, and mixed C/C++ programs.
  44. *
  45. * @note The preprocessor switch #Q_NASSERT disables checking assertions.
  46. * However, it is generally __not__ advisable to disable assertions,
  47. * __especially__ in the production code. Instead, the assertion handler
  48. * Q_onAssert() should be very carefully designed and tested.
  49. */
  50. #ifdef Q_NASSERT /* Q_NASSERT defined--assertion checking disabled */
  51. #define Q_DEFINE_THIS_FILE
  52. #define Q_DEFINE_THIS_MODULE(name_)
  53. #define Q_ASSERT(test_) ((void)0)
  54. #define Q_ASSERT_ID(id_, test_) ((void)0)
  55. #define Q_ALLEGE(test_) ((void)(test_))
  56. #define Q_ALLEGE_ID(id_, test_) ((void)(test_))
  57. #define Q_ERROR() ((void)0)
  58. #define Q_ERROR_ID(id_) ((void)0)
  59. #else /* Q_NASSERT not defined--assertion checking enabled */
  60. /*! Define the file name (with `__FILE__`) for assertions in this file. */
  61. /**
  62. * @description
  63. * Macro to be placed at the top of each C/C++ module to define the
  64. * single instance of the file name string to be used in reporting
  65. * assertions in this module.
  66. *
  67. * @note The file name string literal is defined by means of the standard
  68. * preprocessor macro `__FILE__`. However, please note that, depending
  69. * on the compiler, the `__FILE__` macro might contain the whole path name
  70. * to the file, which might be inconvenient to log assertions.
  71. * @note This macro should __not__ be terminated by a semicolon.
  72. * @sa Q_DEFINE_THIS_MODULE()
  73. */
  74. #define Q_DEFINE_THIS_FILE \
  75. static char_t const Q_ROM Q_this_module_[] = __FILE__;
  76. /*! Define the user-specified module name for assertions in this file. */
  77. /**
  78. * @description
  79. * Macro to be placed at the top of each C/C++ module to define the
  80. * single instance of the module name string to be used in reporting
  81. * assertions in this module. This macro takes the user-supplied parameter
  82. * @p name_ instead of `__FILE__` to precisely control the name of the
  83. * module.
  84. *
  85. * @param[in] name_ string constant representing the module name
  86. *
  87. * @note This macro should __not__ be terminated by a semicolon.
  88. */
  89. #define Q_DEFINE_THIS_MODULE(name_) \
  90. static char_t const Q_ROM Q_this_module_[] = name_;
  91. /*! General purpose assertion. */
  92. /**
  93. * @description
  94. * Makes sure the @p test_ parameter is TRUE. Calls the Q_onAssert()
  95. * callback if the @p test_ expression evaluates to FALSE. This
  96. * macro identifies the assertion location within the file by means
  97. * of the standard `__LINE__` macro.
  98. *
  99. * @param[in] test_ Boolean expression
  100. *
  101. * @note the @p test_ is __not__ evaluated if assertions are disabled
  102. * with the #Q_NASSERT switch.
  103. */
  104. #define Q_ASSERT(test_) ((test_) \
  105. ? (void)0 : Q_onAssert(&Q_this_module_[0], (int_t)__LINE__))
  106. /*! General purpose assertion with user-specified assertion-id. */
  107. /**
  108. * @description
  109. * Makes sure the @p test_ parameter is TRUE. Calls the Q_onAssert()
  110. * callback if the @p test_ evaluates to FALSE. This assertion takes the
  111. * user-supplied parameter @p id_ to identify the location of this
  112. * assertion within the file. This avoids the volatility of using line
  113. * numbers, which change whenever a line of code is added or removed
  114. * upstream from the assertion.
  115. *
  116. * @param[in] id_ ID number (unique within the module) of the assertion
  117. * @param[in] test_ Boolean expression
  118. *
  119. * @note the @p test_ expression is __not__ evaluated if assertions are
  120. * disabled with the #Q_NASSERT switch.
  121. */
  122. #define Q_ASSERT_ID(id_, test_) ((test_) \
  123. ? (void)0 : Q_onAssert(&Q_this_module_[0], (int_t)(id_)))
  124. /*! General purpose assertion that __always__ evaluates the @p test_
  125. * expression. */
  126. /**
  127. * @description
  128. * Like the Q_ASSERT() macro, except it __always__ evaluates the @p test_
  129. * expression even when assertions are disabled with the #Q_NASSERT macro.
  130. * However, when the #Q_NASSERT macro is defined, the Q_onAssert()
  131. * callback is __not__ called, even if @p test_ evaluates to FALSE.
  132. *
  133. * @param[in] test_ Boolean expression (__always__ evaluated)
  134. *
  135. * @sa #Q_ALLEGE_ID
  136. */
  137. #define Q_ALLEGE(test_) Q_ASSERT(test_)
  138. /*! General purpose assertion with user-specified assertion-id that
  139. * __always__ evaluates the @p test_ expression. */
  140. /**
  141. * @description
  142. * Like the Q_ASSERT_ID() macro, except it __always__ evaluates the
  143. * @p test_ expression even when assertions are disabled with the
  144. * #Q_NASSERT macro. However, when the #Q_NASSERT macro is defined, the
  145. * Q_onAssert() callback is __not__ called, even if @p test_ evaluates
  146. * to FALSE.
  147. *
  148. * @param[in] id_ ID number (unique within the module) of the assertion
  149. * @param[in] test_ Boolean expression
  150. */
  151. #define Q_ALLEGE_ID(id_, test_) Q_ASSERT_ID((id_), (test_))
  152. /*! Assertion for a wrong path through the code. */
  153. /**
  154. * @description
  155. * Calls the Q_onAssert() callback if ever executed.
  156. *
  157. * @note Does noting if assertions are disabled with the #Q_NASSERT switch.
  158. */
  159. #define Q_ERROR() \
  160. Q_onAssert(&Q_this_module_[0], (int_t)__LINE__)
  161. /*! Assertion with user-specified assertion-id for a wrong path. */
  162. /**
  163. * @description
  164. * Calls the Q_onAssert() callback if ever executed. This assertion
  165. * takes the user-supplied parameter @p id_ to identify the location of
  166. * this assertion within the file. This avoids the volatility of using
  167. * line numbers, which change whenever a line of code is added or removed
  168. * upstream from the assertion.
  169. *
  170. * @param[in] id_ ID number (unique within the module) of the assertion
  171. *
  172. * @note Does noting if assertions are disabled with the #Q_NASSERT switch.
  173. */
  174. #define Q_ERROR_ID(id_) \
  175. Q_onAssert(&Q_this_module_[0], (int_t)(id_))
  176. #endif /* Q_NASSERT */
  177. #ifdef __cplusplus
  178. extern "C" {
  179. #endif
  180. /*! Callback function invoked in case of any assertion failure. */
  181. /**
  182. * @description
  183. * This is an application-specific callback function needs to be defined in
  184. * the application to perform the clean system shutdown and perhaps a reset.
  185. *
  186. * @param[in] module name of the file/module in which the assertion failed
  187. * (constant ROM-based, zero-terminated C string)
  188. * @param[in] line line number or user-specified ID-number to identify
  189. * the location of the failing assertion within the file.
  190. *
  191. * @note This callback function should _not_ return, as continuation after
  192. * an assertion failure does not make sense.
  193. *
  194. * @note The Q_onAssert() function is the last line of defense after the
  195. * system failure and its implementation shouild be very __carefully__
  196. * designed and __tested__ under various fault conditions, including but
  197. * not limited to: stack overflow, stack corruption, or calling Q_onAssert()
  198. * from an interrupt.
  199. *
  200. * @note It is typically a __bad idea__ to implement Q_onAssert() as an
  201. * endless loop that ties up the CPU. During debuggin, Q_onAssert() is an
  202. * ideal place to put a breakpoint.
  203. *
  204. * Called by the following macros: #Q_ASSERT, #Q_REQUIRE, #Q_ENSURE,
  205. * #Q_ERROR, #Q_ALLEGE as well as #Q_ASSERT_ID, #Q_REQUIRE_ID, #Q_ENSURE_ID,
  206. * #Q_ERROR_ID, and #Q_ALLEGE_ID.
  207. */
  208. void Q_onAssert(char_t const Q_ROM * const file, int_t line);
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. /*! Assertion for checking preconditions. */
  213. /**
  214. * @description
  215. * This macro is equivalent to #Q_ASSERT, except the name provides a better
  216. * documentation of the intention of this assertion.
  217. *
  218. * @param[in] test_ Boolean expression
  219. */
  220. #define Q_REQUIRE(test_) Q_ASSERT(test_)
  221. /*! Assertion for checking preconditions with user-specified assertion-id. */
  222. /**
  223. * @description
  224. * Equivalent to #Q_ASSERT_ID, except the macro name provides a better
  225. * documentation of the intention of this assertion.
  226. *
  227. * @param[in] id_ ID number (unique within the module) of the assertion
  228. * @param[in] test_ Boolean expression
  229. */
  230. #define Q_REQUIRE_ID(id_, test_) Q_ASSERT_ID((id_), (test_))
  231. /*! Assertion for checking postconditions. */
  232. /** Equivalent to #Q_ASSERT, except the macro name provides a better
  233. * documentation of the intention of this assertion.
  234. *
  235. * @param[in] test_ Boolean expression
  236. */
  237. #define Q_ENSURE(test_) Q_ASSERT(test_)
  238. /*! Assertion for checking postconditions with user-specified assertion-id. */
  239. /**
  240. * @description
  241. * Equivalent to #Q_ASSERT_ID, except the name provides a better documentation
  242. * of the intention of this assertion.
  243. *
  244. * @param[in] id_ ID number (unique within the module) of the assertion
  245. * @param[in] test_ Boolean expression
  246. */
  247. #define Q_ENSURE_ID(id_, test_) Q_ASSERT_ID((id_), (test_))
  248. /*! Assertion for checking invariants. */
  249. /**
  250. * @description
  251. * Equivalent to #Q_ASSERT, except the macro name provides a better
  252. * documentation of the intention of this assertion.
  253. *
  254. * @param[in] test_ Boolean expression
  255. */
  256. #define Q_INVARIANT(test_) Q_ASSERT(test_)
  257. /*! Assertion for checking invariants with user-specified assertion-id. */
  258. /**
  259. * @description
  260. * Equivalent to #Q_ASSERT_ID, except the macro name provides a better
  261. * documentation of the intention of this assertion.
  262. *
  263. * @param[in] id_ ID number (unique within the module) of the assertion
  264. * @param[in] test_ Boolean expression
  265. */
  266. #define Q_INVARIANT_ID(id_, test_) Q_ASSERT_ID((id_), (test_))
  267. /*! Compile-time assertion. */
  268. /**
  269. * @description
  270. * This type of assertion deliberately causes a compile-time error when
  271. * the @p test_ evaluates to FALSE. The macro exploits the fact that in C/C++
  272. * a dimension of an array cannot be negative. The compile-time assertion has
  273. * no runtime side effects.
  274. *
  275. * @param[in] test_ Compile-time Boolean expression
  276. */
  277. #define Q_ASSERT_COMPILE(test_) \
  278. extern int_t Q_assert_compile[(test_) ? 1 : -1]
  279. #endif /* qassert_h */