File: assert.h

package info (click to toggle)
optee-os 4.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,960 kB
  • sloc: ansic: 444,388; asm: 12,922; python: 3,719; makefile: 1,681; sh: 238
file content (77 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
#ifndef __ASSERT_H
#define __ASSERT_H

#include <compiler.h>
#include <trace.h>

void __noreturn _assert_break(void);
void _assert_log(const char *expr, const char *file, const int line,
			const char *func);

static inline void __noreturn _assert_trap(const char *expr_str,
					   const char *file, const int line,
					   const char *func)
{
	_assert_log(expr_str, file, line, func);
	_assert_break();
}

static inline void _runtime_assert_trap(const char *expr_str, const char *file,
					const int line, const char *func)
{
	volatile bool do_break = true;

	_assert_log(expr_str, file, line, func);
	if (do_break)
		_assert_break();
}

/*
 * runtime_assert() behaves as assert() except that it doesn't tell the
 * compiler it will never return. This can be used to avoid the warning:
 * error: function might be candidate for attribute ‘noreturn’
 */
#ifdef NDEBUG
#define assert(expr)	((void)0)
#define runtime_assert(expr)	((void)0)
#else
#define assert(expr)	\
	((expr) ? (void)0 : _assert_trap(#expr, __FILE__, __LINE__, __func__))
#define runtime_assert(expr)	\
	((expr) ? (void)0 : \
		_runtime_assert_trap(#expr, __FILE__, __LINE__, __func__))
#endif

/* This macro is deprecated, please use static_assert instead */
#define COMPILE_TIME_ASSERT(x) \
	do { \
		switch (0) { case 0: case ((x) ? 1: 0): default : break; } \
	} while (0)

#endif

#if !defined(__cplusplus) || (__cplusplus < 201103L)
#if defined(__HAVE_SINGLE_ARGUMENT_STATIC_ASSERT)
#define static_assert _Static_assert
#else
/*
 * In gcc prior to 9.1 _Static_assert requires two arguments. To allow
 * passing a single argument to static_assert() add a workaround with
 * macros.
 */
#define ___args_count(_0, _1, x, ...) x
#define __args_count(...) ___args_count(__VA_ARGS__, 2, 1, 0)

#define __static_assert_1(expr)		_Static_assert(expr, "")
#define __static_assert_2(expr, msg)	_Static_assert(expr, msg)
#define ___static_assert(count, ...)	__static_assert_ ## count(__VA_ARGS__)
#define __static_assert(count, ...)	___static_assert(count, __VA_ARGS__)

#define static_assert(...) \
	__static_assert(__args_count(__VA_ARGS__), __VA_ARGS__)
#endif
#endif /* __ASSERT_H */