On 06/30/2012 05:47 PM, Pádraig Brady wrote: >>> +#ifdef CIBAUD >>> bool speed_was_set; >>> +#else >>> + bool speed_was_set ATTRIBUTE_UNUSED; >>> +#endif >> >> Three lines too many. ATTRIBUTE_UNUSED is defined by gcc to mean 'might >> be unused, therefore don't warn if it was not used', and not 'must not >> be used, and therefore warn if it is used'. Therefore, it is always >> safe to use the one-liner: >> >> bool speed_was_set ATTRIBUTE_UNUSED; >> >> even if, when CIBAUD is defined, it was actually used. No need for >> extra #ifdef. >> > > But then you would never get such warnings for this variable. > I was trying to disable the warning only where it's incorrect, > and thought the single ifdef worth it?
My point is that there ARE no warnings if you unconditionally use the label. That is, gcc behaves the same with no warnings, on both the case where CIBAUD is undefined and where it is defined, given either: #ifdef CIBAUD bool speed_was_set; #else bool speed_was_set ATTRIBUTE_UNUSED; #endif ... #ifdef CIBAUD use(speed_was_set); #endif or whether you use the shorter: bool speed_was_set ATTRIBUTE_UNUSED; ... #ifdef CIBAUD use(speed_was_set); #endif and that's because the definition of ATTRIBUTE_UNUSED in gcc means only "this _might_ be unused on some conditional compilations, so suppress unused warnings". Had gcc defined ATTRIBUTE_UNUSED as "warn if we used it after all", then yes, you'd need the #ifdef. Thankfully, gcc did not use that formulation. -- Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
