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?
This works (GCC version 4.6.1):
GCC v4.2.4 doc. 5.32 Specifying Attributes of Variables:
GCC v4.2.4 doc. 5.25 Declaring Attributes of Functions (a more correct link, functions, sorry about that.)
When it comes to
pragma push,pop, etc,, the documentation say:GCC doc. 6.57.10 Diagnostic Pragmas:
It states; "and not all of them",
-Wunused-functionseems to be one of the ones not willing to be suppressed. The optionfdiagnostics-show-optionseems to be enabled by default. (Gives the flag from which the error/warning enabled.).Using
-Wno-unused-functionat command line disables it, but then of course for all, not just the ones directly specified in the code.If you say:
You’ll still get the warning, – so it does not have anything to do with, e.g., the
-Wallsetting for the compile process in an special state where it won’t work.As an example, this works: