Consider the following code in linux/drivers/usb/hid-core.c:
static void hid_process_event (struct hid_device *hid,
struct hid_field *field,
struct hid_usage *usage,
__s32 value)
{
hid_dump_input(usage, value);
if (hid->claimed & HID_CLAIMED_INPUT)
hidinput_hid_event(hid, field, usage, value);
#ifdef CONFIG_USB_HIDDEV
if (hid->claimed & HID_CLAIMED_HIDDEV)
hiddev_hid_event(hid, usage->hid, value);
#endif
}
Here the author does not want to call hiddev_hid_event() if a specific configuration option is not enabled. This is because that function will not even be present if the configuration option is not enabled.
To remove this #ifdef, the following change was made to include/linux/hiddev.h:
#ifdef CONFIG_USB_HIDDEV
extern void hiddev_hid_event (struct hid_device *,
unsigned int usage,
int value);
#else
static inline void
hiddev_hid_event (struct hid_device
*hid,
unsigned int usage,
int value) { }
#endif
Then drivers/usb/hid-core.c was changed to:
static void hid_process_event
(struct hid_device *hid,
struct hid_field *field,
struct hid_usage *usage,
__s32 value)
{
hid_dump_input(usage, value);
if (hid->claimed & HID_CLAIMED_INPUT)
hidinput_hid_event(hid, field, usage, value);
if (hid->claimed & HID_CLAIMED_HIDDEV)
hiddev_hid_event(hid, usage->hid, value);
}
If CONFIG_USB_HIDDEV is not enabled, the compiler will replace the call to hiddev_hid_event() with a null function call and then optimize away the if statement entirely.
What I can’t understand is how the call to hiddev_hid_event() is replaced with a null function by the compiler. The only difference I see is that return type extern void has been replaced with static inline void. Does this means that all extern functions if not defined will automatically become null function?
The function actually is defined, but has an empty body:
Optimizing away inline functions with empty bodies is trivial, I guess.