GCC Compiler Warnings
Saturday, November 28th, 2009It’s often a good strategy when developing software to ensure that a number of compiler warnings are switched on. For example, the GCC compiler flag ‘-Wall‘ is often used to provide warnings about all ‘questionable constructions’ (things like omitting to include a return statement in a function that should return a non-void type). Enabling warnings such as this can speed up development as more time is spend debugging application logic rather than silly errors. However the downside is that the compiler may provide warnings about things that we know about and are happy with. This post revisits GCC attributes to see how we can supress such warnings.
Let’s use the following straight forward source as an example:
1: void test(int a)
2: {
3: }
If we compile this using GCC with ‘gcc test.c -c -Wall -Wextra’ we would get the following output:
test.c:1: warning: unused parameter "a"
In this scenario we are aware that we don’t use the ‘a’ variable and that is OK – however we don’t want the compiler to keep reminding us. We want to suppress this warning as otherwise warnings that we are aware of and happy with may make it harder to see other warnings in the compiler output that we do care about.
There are of course a number of solutions to suppress the warning – the obvious is removing the ‘int a’ from the function definition. However there may be circumstances where this isn’t a good idea – perhaps we’re published this interface and don’t wish to make changes.
A common approach is to just cast the variable to void to make the warnings go away, i.e.
1: void test(int a)
2: {
3: (void)a; //suppress compiler warnings
4: }
This works and has no side effects – however I think this clutters the code and makes the code harder to maintain.
Another alternative may be to use GCC’s pragma diagnostic – however this only really applies to one unit (file) of source at a time – though see here for more information.
So what about modifying the GCC compiler flags? We could suppress this type of warning using the ‘-Wno-unused-parameter’ flag. (This is the opposite of the ‘-Wunused-parameter‘ flag) – however this negates the reason for adding the flag in the first place!
The other approach would be to use GCC attributes. How about this?
1: void test(int __attribute__ ((unused)) a)
2: {
3: }
This tells GCC during compile time that the ‘a’ variable is not used and ‘unused’ warnings about this variable should be suppressed. (Unfortunately it doesn’t complain if you do go on and use the variable even though you’ve marked it as unused). This approach works well as it allows you to suppress specific warnings on a function basis.
Of course the downside with this approach is that your code is slightly less portable – only GCC compilers may understand this attribute. There may be workarounds though- you could use a macro and how that macro expands would depend on which compiler is used. The kernel uses a similar approach in it’s design – see compiler.h and compiler-gcc3.h to see what I mean.
Using GCC attributes to suppress specific warnings can also be applied to variables, labels and omitting a return amongst many more – though it does have it’s pros and cons. I’d like to hear your views on this – worth using?