mxc_assert.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @file
  3. * @brief Assertion checks for debugging.
  4. */
  5. /* ****************************************************************************
  6. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. *
  37. * $Date: 2018-08-09 18:45:02 -0500 (Thu, 09 Aug 2018) $
  38. * $Revision: 36818 $
  39. *
  40. *************************************************************************** */
  41. /* Define to prevent redundant inclusion */
  42. #ifndef _MXC_ASSERT_H_
  43. #define _MXC_ASSERT_H_
  44. /* **** Includes **** */
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @ingroup syscfg
  50. * @defgroup mxc_assertions Assertion Checks for Debugging
  51. * @brief Assertion checks for debugging.
  52. * @{
  53. */
  54. /* **** Definitions **** */
  55. /**
  56. * @note To use debug assertions, the symbol @c MXC_ASSERT_ENABLE must be
  57. * defined.
  58. */
  59. ///@cond
  60. #ifdef MXC_ASSERT_ENABLE
  61. /**
  62. * Macro that checks the expression for true and generates an assertion.
  63. * @note To use debug assertions, the symbol @c MXC_ASSERT_ENABLE must be
  64. * defined.
  65. */
  66. #define MXC_ASSERT(expr) \
  67. if (!(expr)) \
  68. { \
  69. mxc_assert(#expr, __FILE__, __LINE__); \
  70. }
  71. /**
  72. * Macro that generates an assertion with the message "FAIL".
  73. * @note To use debug assertions, the symbol @c MXC_ASSERT_ENABLE must be
  74. * defined.
  75. */
  76. #define MXC_ASSERT_FAIL() mxc_assert("FAIL", __FILE__, __LINE__);
  77. #else
  78. #define MXC_ASSERT(expr)
  79. #define MXC_ASSERT_FAIL()
  80. #endif
  81. ///@endcond
  82. /* **** Globals **** */
  83. /* **** Function Prototypes **** */
  84. /**
  85. * @brief Assert an error when the given expression fails during debugging.
  86. * @param expr String with the expression that failed the assertion.
  87. * @param file File containing the failed assertion.
  88. * @param line Line number for the failed assertion.
  89. * @note This is defined as a weak function and can be overridden at the
  90. * application layer to print the debugging information.
  91. * @code
  92. * printf("%s, file: %s, line %d\n", expr, file, line);
  93. * @endcode
  94. * @note To use debug assertions, the symbol @c MXC_ASSERT_ENABLE must be
  95. * defined.
  96. */
  97. void mxc_assert(const char *expr, const char *file, int line);
  98. /**@} end of group MXC_Assertions*/
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif /* _MXC_ASSERT_H_ */