Mag*_*off 31 c++ gcc deprecated gcc-warning
实现弃用警告的一种方法是在调用已弃用的函数时生成警告,除非您从已弃用的上下文中调用.这样,遗留代码可以调用遗留代码,而不会产生仅相当于噪声的警告.
这是一个合理的思路,它反映在我在OS X上的GCC 4.2(1)和Clang 4.0(2)以及Ubuntu上的Clang 3.0(3)中看到的实现中.
但是,当我在Ubuntu上使用GCC 4.6(4)进行编译时,我对所有已弃用函数的调用都会被弃用警告,而与上下文无关.这是功能的回归吗?是否有可用于获取其他行为的编译器选项?
示例程序:
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
}
Run Code Online (Sandbox Code Playgroud)
GCC 4.2的输出(是的,我确实得到了两次相同的警告.但我并不关心这一点):
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)
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
我怎样才能说服GCC 4.6它应该给我与GCC 4.2相同的输出?
dor*_*ron 42
-Wno-deprecated
将删除所有已弃用的警告
Dav*_*men 42
gcc 4.6添加了有助于解决此问题的诊断编译指示:
#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
Run Code Online (Sandbox Code Playgroud)
注意:这仅适用于gcc 4.6及更高版本.这是push
和pop
4.6扩展.用GCC 4.5,在#pragma GCC diagnostic push
和pop
会(有警告)被忽略.什么是不会被忽略的#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- 但现在这有效直到文件结束.
小智 15
您在GCC 4.2中看到的行为是由针对GCC的Apple特定补丁引起的.FSF GCC 4.2.4警告使用a
.Apple GCC对FSF GCC的具体位不是:
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -902,6 +902,9 @@ warn_deprecated_use (tree node)
if (node == 0 || !warn_deprecated_decl)
return;
+ if (current_function_decl && TREE_DEPRECATED (current_function_decl))
+ return;
+
if (DECL_P (node))
{
expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
Run Code Online (Sandbox Code Playgroud)
(根据GPLv2或更高版本提供)
您可能希望将此修补程序调整为更高版本的GCC(可能不需要进行任何更改,可能需要进行重大更改),并且应用此修补程序从源代码构建GCC.或者您可以在FSF GCC bugzilla上将此报告为功能请求.