xref: /linux-6.15/include/linux/typecheck.h (revision cb0f8003)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2e0deaff4SAndrew Morton #ifndef TYPECHECK_H_INCLUDED
3e0deaff4SAndrew Morton #define TYPECHECK_H_INCLUDED
4e0deaff4SAndrew Morton 
5e0deaff4SAndrew Morton /*
6e0deaff4SAndrew Morton  * Check at compile time that something is of a particular type.
7e0deaff4SAndrew Morton  * Always evaluates to 1 so you may use it easily in comparisons.
8e0deaff4SAndrew Morton  */
9e0deaff4SAndrew Morton #define typecheck(type,x) \
10e0deaff4SAndrew Morton ({	type __dummy; \
11e0deaff4SAndrew Morton 	typeof(x) __dummy2; \
12e0deaff4SAndrew Morton 	(void)(&__dummy == &__dummy2); \
13e0deaff4SAndrew Morton 	1; \
14e0deaff4SAndrew Morton })
15e0deaff4SAndrew Morton 
16e0deaff4SAndrew Morton /*
17e0deaff4SAndrew Morton  * Check at compile time that 'function' is a certain type, or is a pointer
18e0deaff4SAndrew Morton  * to that type (needs to use typedef for the function type.)
19e0deaff4SAndrew Morton  */
20e0deaff4SAndrew Morton #define typecheck_fn(type,function) \
21e0deaff4SAndrew Morton ({	typeof(type) __tmp = function; \
22e0deaff4SAndrew Morton 	(void)__tmp; \
23e0deaff4SAndrew Morton })
24e0deaff4SAndrew Morton 
25*cb0f8003SKumar Kartikeya Dwivedi /*
26*cb0f8003SKumar Kartikeya Dwivedi  * Check at compile time that something is a pointer type.
27*cb0f8003SKumar Kartikeya Dwivedi  */
28*cb0f8003SKumar Kartikeya Dwivedi #define typecheck_pointer(x) \
29*cb0f8003SKumar Kartikeya Dwivedi ({	typeof(x) __dummy; \
30*cb0f8003SKumar Kartikeya Dwivedi 	(void)sizeof(*__dummy); \
31*cb0f8003SKumar Kartikeya Dwivedi 	1; \
32*cb0f8003SKumar Kartikeya Dwivedi })
33*cb0f8003SKumar Kartikeya Dwivedi 
34e0deaff4SAndrew Morton #endif		/* TYPECHECK_H_INCLUDED */
35