assert.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /****************************************************************************
  2. * include/assert.h
  3. *
  4. * Copyright (C) 2007-2009, 2011-2013, 2015-2016 Gregory Nutt.
  5. * All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Copyright (C) 2016 Omni Hoverboards Inc. All rights reserved.
  9. * Author: Paul Alexander Patience <paul-a.patience@polymtl.ca>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. * 3. Neither the name NuttX nor the names of its contributors may be
  22. * used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. #ifndef __INCLUDE_ASSERT_H
  40. #define __INCLUDE_ASSERT_H
  41. /****************************************************************************
  42. * Included Files
  43. ****************************************************************************/
  44. #include <nuttx/compiler.h>
  45. #include <stdint.h>
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /* Macro Name: PANIC, ASSERT, VERIFY, et al. */
  50. #undef PANIC /* Unconditional abort */
  51. #undef ASSERT /* Assert if the condition is not true */
  52. #undef VERIFY /* Assert if a function returns a negative value */
  53. #undef DEBUGPANIC /* Like PANIC, but only if CONFIG_DEBUG_ASSERTIONS is defined */
  54. #undef DEBUGASSERT /* Like ASSERT, but only if CONFIG_DEBUG_ASSERTIONS is defined */
  55. #undef DEBUGVERIFY /* Like VERIFY, but only if CONFIG_DEBUG_ASSERTIONS is defined */
  56. #ifdef CONFIG_HAVE_FILENAME
  57. # define PANIC() _assert(__FILE__, __LINE__)
  58. #else
  59. # define PANIC() _assert("unknown", 0)
  60. #endif
  61. #define ASSERT(f) do { if (!(f)) PANIC(); } while (0)
  62. #define VERIFY(f) do { if ((f) < 0) PANIC(); } while (0)
  63. #ifdef CONFIG_DEBUG_ASSERTIONS
  64. # define DEBUGPANIC() PANIC()
  65. # define DEBUGASSERT(f) ASSERT(f)
  66. # define DEBUGVERIFY(f) VERIFY(f)
  67. #else
  68. # define DEBUGPANIC()
  69. # define DEBUGASSERT(f)
  70. # define DEBUGVERIFY(f) ((void)(f))
  71. #endif
  72. /* The C standard states that if NDEBUG is defined, assert will do nothing.
  73. * Users can define and undefine NDEBUG as they see fit to choose when assert
  74. * does something or does not do anything.
  75. */
  76. #ifdef NDEBUG
  77. # define assert(f)
  78. #else
  79. # define assert(f) ASSERT(f)
  80. #endif
  81. /* Definition required for C11 compile-time assertion checking. The
  82. * static_assert macro simply expands to the _Static_assert keyword.
  83. */
  84. #ifndef __cplusplus
  85. # define static_assert _Static_assert
  86. #endif
  87. /****************************************************************************
  88. * Included Files
  89. ****************************************************************************/
  90. /****************************************************************************
  91. * Public Data
  92. ****************************************************************************/
  93. #ifdef __cplusplus
  94. #define EXTERN extern "C"
  95. extern "C"
  96. {
  97. #else
  98. #define EXTERN extern
  99. #endif
  100. /****************************************************************************
  101. * Public Function Prototypes
  102. ****************************************************************************/
  103. void _assert(FAR const char *filename, int linenum) noreturn_function;
  104. #undef EXTERN
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* __INCLUDE_ASSERT_H */