One way to implement deprecation warnings is to produce warnings on calls to deprecated functions, unless you are calling from a deprecated context. This way legacy code can call legacy code without producing warnings that only amount to noise.
This is a reasonable line of thinking, and it is reflected in the implementations I see in GCC 4.2 (1) and Clang 4.0 (2) on OS X as well as Clang 3.0 (3) on Ubuntu.
However, when I compile with GCC 4.6 (4) on Ubuntu, I get deprecated warnings for all invocations of deprecated functions, independently of context. Is this a regression in functionality? Are there compiler options I can use to get the other behavior?
Example program:
int __attribute__((deprecated)) a() { return 10; } int __attribute__((deprecated)) b() { return a() * 2; //< I want to get rid of warnings from this line } int main() { return b(); //< I expect a warning on this line only }
Output from GCC 4.2 (Yes, I do get the same warning twice. I don't care about that, though):
main.cpp: In function ‘int main()’: main.cpp:10: warning: ‘b’ is deprecated (declared at main.cpp:5) main.cpp:10: warning: ‘b’ is deprecated (declared at main.cpp:5)
Output from GCC 4.6:
main.cpp: In function 'int b()': main.cpp:6:9: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations] main.cpp:6:11: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations] main.cpp: In function 'int main()': main.cpp:10:9: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations] main.cpp:10:11: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations]
How can I convince GCC 4.6 that it should give me the same output as GCC 4.2?
When nothing else works: $ pip install shutup . Then at the top of the code import shutup;shutup. please() . This will disable all warnings.
Deprecation warnings are a common thing in our industry. They are warnings that notify us that a specific feature (e.g. a method) will be removed soon (usually in the next minor or major version) and should be replaced with something else.
The deprecated declaration lets you specify particular forms of function overloads as deprecated, whereas the pragma form applies to all overloaded forms of a function name. The deprecated declaration lets you specify a message that will display at compile time. The text of the message can be from a macro.
-Wno-deprecated
will remove all deprecated warnings
gcc 4.6 added diagnostic pragmas that will help solve this problem:
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" int __attribute__((deprecated)) b() { return a() * 2; //< I want to get rid of warnings from this line } #pragma GCC diagnostic pop
Note: This only works in gcc 4.6 and higher. The push
and pop
are 4.6 extensions. With gcc 4.5, the #pragma GCC diagnostic push
and pop
will be ignored (with warnings). What won't be ignored is the #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-- but now this has effect until end of file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With