2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* include/assert.h
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2024-10-02 21:59:15 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-09-28 17:44:33 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-09-28 17:44:33 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-12 20:15:48 +08:00
|
|
|
*
|
2021-09-28 17:44:33 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
#ifndef __INCLUDE_ASSERT_H
|
|
|
|
#define __INCLUDE_ASSERT_H
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2022-01-07 21:44:58 +08:00
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-02-12 22:37:55 +08:00
|
|
|
#include <nuttx/compiler.h>
|
2023-11-15 17:44:06 +08:00
|
|
|
#include <sys/types.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
2009-12-15 07:32:23 +08:00
|
|
|
* Pre-processor Definitions
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2016-06-12 20:15:48 +08:00
|
|
|
/* Macro Name: PANIC, ASSERT, VERIFY, et al. */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2016-06-12 20:15:48 +08:00
|
|
|
#undef PANIC /* Unconditional abort */
|
2013-05-02 20:28:59 +08:00
|
|
|
#undef ASSERT /* Assert if the condition is not true */
|
|
|
|
#undef VERIFY /* Assert if a function returns a negative value */
|
2016-06-12 20:15:48 +08:00
|
|
|
#undef DEBUGPANIC /* Like PANIC, but only if CONFIG_DEBUG_ASSERTIONS is defined */
|
2016-06-12 02:49:21 +08:00
|
|
|
#undef DEBUGASSERT /* Like ASSERT, but only if CONFIG_DEBUG_ASSERTIONS is defined */
|
|
|
|
#undef DEBUGVERIFY /* Like VERIFY, but only if CONFIG_DEBUG_ASSERTIONS is defined */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2023-10-19 14:37:48 +08:00
|
|
|
/* Macro to define the assertions file name and file line
|
|
|
|
* | Function |CONFIG | Show name/line |
|
|
|
|
* | --- | --- | --- |
|
|
|
|
* |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=y | Yes |
|
|
|
|
* |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=n | No |
|
|
|
|
* |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=y| Yes |
|
|
|
|
* |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=n| No |
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_FILENAME
|
|
|
|
# ifdef CONFIG_DEBUG_ASSERTIONS_FILENAME
|
|
|
|
# define __DEBUG_ASSERT_FILE__ __FILE__
|
|
|
|
# define __DEBUG_ASSERT_LINE__ __LINE__
|
|
|
|
# endif
|
|
|
|
# ifdef CONFIG_ASSERTIONS_FILENAME
|
|
|
|
# define __ASSERT_FILE__ __FILE__
|
|
|
|
# define __ASSERT_LINE__ __LINE__
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __DEBUG_ASSERT_FILE__
|
|
|
|
# define __DEBUG_ASSERT_FILE__ 0
|
|
|
|
# define __DEBUG_ASSERT_LINE__ 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __ASSERT_FILE__
|
2023-06-08 22:08:23 +08:00
|
|
|
# define __ASSERT_FILE__ 0
|
|
|
|
# define __ASSERT_LINE__ 0
|
2016-06-08 23:07:24 +08:00
|
|
|
#endif
|
|
|
|
|
2024-03-12 22:51:26 +08:00
|
|
|
#define PANIC() __assert(__ASSERT_FILE__, __ASSERT_LINE__, "panic")
|
|
|
|
#define PANIC_WITH_REGS(msg, regs) _assert(__ASSERT_FILE__, \
|
|
|
|
__ASSERT_LINE__, msg, regs)
|
|
|
|
|
2023-10-25 11:32:19 +08:00
|
|
|
#define __ASSERT__(f, file, line, _f) \
|
2023-10-18 22:24:21 +08:00
|
|
|
(predict_false(!(f))) ? __assert(file, line, _f) : ((void)0)
|
2023-03-20 14:54:47 +08:00
|
|
|
|
2023-10-25 11:32:19 +08:00
|
|
|
#define __VERIFY__(f, file, line, _f) \
|
2023-10-18 22:24:21 +08:00
|
|
|
(predict_false((f) < 0)) ? __assert(file, line, _f) : ((void)0)
|
2023-03-20 14:54:47 +08:00
|
|
|
|
2023-10-19 14:37:48 +08:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS_EXPRESSION
|
2023-10-25 11:32:19 +08:00
|
|
|
# define _ASSERT(f,file,line) __ASSERT__(f, file, line, #f)
|
|
|
|
# define _VERIFY(f,file,line) __VERIFY__(f, file, line, #f)
|
2023-10-19 14:37:48 +08:00
|
|
|
#else
|
2023-10-25 11:32:19 +08:00
|
|
|
# define _ASSERT(f,file,line) __ASSERT__(f, file, line, NULL)
|
|
|
|
# define _VERIFY(f,file,line) __VERIFY__(f, file, line, NULL)
|
2023-01-31 19:50:22 +08:00
|
|
|
#endif
|
2013-06-24 04:39:56 +08:00
|
|
|
|
2016-06-12 20:15:48 +08:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
2023-10-19 14:37:48 +08:00
|
|
|
# define DEBUGPANIC() __assert(__DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__, "panic")
|
|
|
|
# define DEBUGASSERT(f) _ASSERT(f, __DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__)
|
|
|
|
# define DEBUGVERIFY(f) _VERIFY(f, __DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__)
|
2016-06-08 23:07:24 +08:00
|
|
|
#else
|
2016-06-12 20:15:48 +08:00
|
|
|
# define DEBUGPANIC()
|
2024-02-22 09:32:37 +08:00
|
|
|
# define DEBUGASSERT(f) ((void)(1 || (f)))
|
2016-06-08 23:07:24 +08:00
|
|
|
# define DEBUGVERIFY(f) ((void)(f))
|
2016-06-12 20:15:48 +08:00
|
|
|
#endif
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2016-06-08 23:59:11 +08:00
|
|
|
/* The C standard states that if NDEBUG is defined, assert will do nothing.
|
|
|
|
* Users can define and undefine NDEBUG as they see fit to choose when assert
|
|
|
|
* does something or does not do anything.
|
2024-05-13 19:30:47 +08:00
|
|
|
*
|
|
|
|
* #define assert(ignore) ((void)0)
|
|
|
|
*
|
|
|
|
* Reference link:
|
|
|
|
* https://pubs.opengroup.org/onlinepubs/009695399/basedefs/assert.h.html
|
2016-06-08 23:59:11 +08:00
|
|
|
*/
|
|
|
|
|
2016-06-08 23:07:24 +08:00
|
|
|
#ifdef NDEBUG
|
2024-05-13 19:30:47 +08:00
|
|
|
# define assert(f) ((void)0)
|
2016-06-08 23:07:24 +08:00
|
|
|
#else
|
2023-10-19 14:37:48 +08:00
|
|
|
# define assert(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__)
|
2012-08-31 04:13:50 +08:00
|
|
|
#endif
|
|
|
|
|
2024-09-06 11:23:00 +08:00
|
|
|
/* ASSERT/VERIFY are NuttX-specific APIs.
|
|
|
|
* They are always enabled, regardless of NDEBUG/CONFIG_DEBUG_ASSERTIONS.
|
|
|
|
* The argument is evaluated exactly once.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ASSERT(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__)
|
|
|
|
#define VERIFY(f) _VERIFY(f, __ASSERT_FILE__, __ASSERT_LINE__)
|
|
|
|
|
2023-02-06 02:43:39 +08:00
|
|
|
/* Suppress 3rd party library redefine _assert/__assert */
|
|
|
|
|
|
|
|
#define _assert _assert
|
|
|
|
#define __assert __assert
|
|
|
|
|
2016-06-21 19:03:31 +08:00
|
|
|
/* Definition required for C11 compile-time assertion checking. The
|
2016-06-21 04:26:13 +08:00
|
|
|
* static_assert macro simply expands to the _Static_assert keyword.
|
|
|
|
*/
|
|
|
|
|
2016-06-21 19:03:31 +08:00
|
|
|
#ifndef __cplusplus
|
2022-04-19 20:31:34 +08:00
|
|
|
# if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
|
|
|
|
# define static_assert _Static_assert
|
|
|
|
# else
|
|
|
|
# define static_assert(cond, msg) \
|
2023-10-18 22:24:21 +08:00
|
|
|
extern int (*__static_assert_function(void)) \
|
2022-04-19 20:31:34 +08:00
|
|
|
[!!sizeof (struct { int __error_if_negative: (cond) ? 2 : -1; })]
|
|
|
|
# endif
|
2016-06-21 19:03:31 +08:00
|
|
|
#endif
|
2016-06-20 22:29:41 +08:00
|
|
|
|
2024-08-19 22:49:30 +08:00
|
|
|
#ifndef COMPILE_TIME_ASSERT
|
|
|
|
# define COMPILE_TIME_ASSERT(x) static_assert(x, "compile time assert failed")
|
|
|
|
#endif
|
2023-10-10 19:49:49 +08:00
|
|
|
|
2023-04-26 23:16:18 +08:00
|
|
|
/* Force a compilation error if condition is true, but also produce a
|
|
|
|
* result (of value 0 and type int), so the expression can be used
|
|
|
|
* e.g. in a structure initializer (or where-ever else comma expressions
|
|
|
|
* aren't permitted).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
|
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
2013-02-01 07:29:34 +08:00
|
|
|
* Public Data
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#define EXTERN extern "C"
|
2013-02-01 07:29:34 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
2007-02-18 07:21:28 +08:00
|
|
|
#else
|
|
|
|
#define EXTERN extern
|
|
|
|
#endif
|
|
|
|
|
2013-02-01 07:29:34 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-22 18:21:56 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: _assert
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the assert system call that performs the core dump etc. Function
|
|
|
|
* might not return if it is not safe to do so (in IRQ or in IDLE task).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-10 21:19:12 +08:00
|
|
|
void _assert(FAR const char *filename, int linenum,
|
|
|
|
FAR const char *msg, FAR void *regs);
|
2022-12-22 18:21:56 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: __assert
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the user space assert procedure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-22 19:31:32 +08:00
|
|
|
void __assert(FAR const char *filename, int linenum,
|
|
|
|
FAR const char *msg) noreturn_function;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
#undef EXTERN
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-07 21:44:58 +08:00
|
|
|
#endif /* __ASSEMBLY__ */
|
2008-11-17 00:36:30 +08:00
|
|
|
#endif /* __INCLUDE_ASSERT_H */
|