Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "__maybe_unused"?

Tags:

c

linux-kernel

While looking at the Linux kernel, I noticed the line:

static void *malloc(size_t size) __maybe_unused;

in Linux v3.2 /arch/frv/kernel/gdb_stub.c . I've never seen __maybe_unused used before. Is it specific to the Linux kernel? Or is it defined in the C spec? And what exactly does it do?

like image 951
Ivan Avatar asked Oct 17 '12 20:10

Ivan


2 Answers

In include/linux/compiler-gcc.h there is the definition of the __maybe_unused macro:

#define __maybe_unused  __attribute__((unused))

and in gcc manual you have the documentation of the unused attribute for functions:

unused "This attribute, attached to a function, means that the function is meant to be possibly unused. GCC will not produce a warning for this function."

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

and for variables:

unused "This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable."

http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html

like image 105
ouah Avatar answered Oct 18 '22 10:10

ouah


From the commit that introduced the attribute:

__maybe_unused is defined to be __attribute__((unused)) for both function and variable use if it could possibly be unreferenced due to the evaluation of preprocessor macros. Function prototypes shall be marked with __maybe_unused if the actual definition of the function is dependant on preprocessor macros.

like image 30
David Ranieri Avatar answered Oct 18 '22 11:10

David Ranieri