https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62194
Bug ID: 62194 Summary: Add deadfield attribute to ignore initializers for a structure field Product: gcc Version: unknown Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: josh at joshtriplett dot org This request is the result of a discussion at Kernel Summit 2014, about handling conditional code without massive transitions or excessive preprocessor abuse. Frequently, a structure will have a field that is only used with specific configurations. For instance, a driver may have an "ops" structure with suspend/resume methods, but those methods will only be called if the kernel configuration supports suspend/resume. If we configure suspend/resume out of the kernel, nothing will call or otherwise read foo->suspend(...). However, drivers will still have designated assignments to that structure field: struct something_ops { ... .suspend = foo_suspend, .resume = foo_resume, ... } Currently, this has to work in one of two ways: either the structure fields unconditionally exist in all cases so that the designated assignments don't break (keeping a reference to the functions and preventing them from being compiled out), or the structure field and corresponding designated assignments must be #ifdef'd out (requiring a massive transition and a pile of ugly preprocessor directives proportional to the number of structures). Discussion at Kernel Summit came up with the following alternative: Create a new attribute "deadfield". A field with that attribute is not actually included in the layout of the structure. Writes or designated assignments to the field get ignored. Attempting to read from the field produces an error. Values assigned to dead fields are treated as __attribute__((unused)). That would then allow definitions of instances of the structure to remain unmodified in all configurations. Optionally, the "deadfield" attribute could take a single parameter, which is a constant value to substitute for any read from the field rather than producing an error. For instance, a function pointer field may have a do-nothing stub function (which GCC can then inline and turn into no code), or a flag field may have a constant value (allowing subsequent constant-folding). I'm willing to work on a patch for this.