Two type check macros in the Linux kernel
18 Jan 2021 - John Z. Li
There are two macros for type checking in the Linux kernel. The header “typecheck.h” (in “include/linux”) is quite short, only containing two macros, which are straightforward:
As the comments say, the two macros are intended to check if
a variable or function is of the desired types,
both being dependent on GCC’s typeof
extension.
Usually, if a variable is defined but not used,
the compiler will complain about it.
By explicitly converting it to the type of void
, according to the standard,
the value of such a variable will just be silently discarded without issuing a warning.
In the first example, comparing two pointers of incompatible types leads to a compiler warning. In the second example, trying to assign a function (pointer) to another function pointer of an incompatible type will make the compiler complain. Maybe a better signature for the macro could be
typecheck_fn(fun_ptr_type, fun)
One interesting is, when I searched the source tree with
ag -w "typecheck_fn" path_to_source_tree_root
I found nothing except the macro definition. It seems that the second macro has not been put into use.