I have a C program with a couple of static functions that are not (yet) used. I want to disable warnings for those specific functions. I do not want to disable all -Wunused-function
warnings. I am using GCC 4.6. Specifically:
gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I am following the advice in the documentation (to use push
and pop
), but I have not been able to get it to work.
I have created some simplified source code to investigate the problem. I am compiling them with gcc -Wall -o pragma pragma.c
(where pragma.c
). My first version of pragma.c
looks like this:
void foo(int i) { }
static void bar() { }
int main() { return 0; }
As expected, I get this when I compile it:
pragma.c:3:13: warning: ‘bar’ defined but not used [-Wunused-function]
Also as expected, I am able to disable the warning like this (then the compile succeeds silently):
#pragma GCC diagnostic ignored "-Wunused-function"
void foo(int i) { }
static void bar() { }
int main() { return 0; }
But then, I tried this:
void foo(int i) { }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static void bar() { }
#pragma GCC diagnostic pop
int main() { return 0; }
When I compiled that, I got the original warning:
pragma.c:4:13: warning: ‘bar’ defined but not used [-Wunused-function]
Removing the pop
gets rid of the warning:
void foo(int i) { }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static void bar() { }
int main() { return 0; }
But I need a way to disable the warning just for a specific section of code. I have not been able to do that.
It is hard for me to imagine how this could be intended behavior...but many others have used this version of GCC and it seems unlikely that, if this were a bug, it would make it into the release version.
Still, I have trouble seeing how this behavior is consistent with the documentation, which says that "pragmas occurring after a line do not affect diagnostics caused by that line."
What am I doing wrong? Is there more information about the problem, such as a bug report and/or information about possible workarounds?