简体   繁体   English

如何在GCC中弃用已弃用的函数中删除已弃用的警告?

[英]How can I get rid of deprecated warnings in deprecated functions in GCC?

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. 这是一个合理的思路,它反映在我在OS X上的GCC 4.2(1)和Clang 4.0(2)以及Ubuntu上的Clang 3.0(3)中看到的实现中。

  • (1): i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) (1):i686-apple-darwin11-llvm-g ++ - 4.2(GCC)4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.11.00)
  • (2): Apple clang version 4.0 (tags/Apple/clang-421.0.57) (based on LLVM 3.1svn) (2):Apple clang 4.0版(标签/ Apple / clang-421.0.57)(基于LLVM 3.1svn)
  • (3): Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0) (3):Ubuntu clang版本3.0-6ubuntu3(标签/ RELEASE_30 / final)(基于LLVM 3.0)

However, when I compile with GCC 4.6 (4) on Ubuntu, I get deprecated warnings for all invocations of deprecated functions, independently of context. 但是,当我在Ubuntu上使用GCC 4.6(4)进行编译时,我对所有已弃用函数的调用都会被弃用警告,而与上下文无关。 Is this a regression in functionality? 这是功能的回归吗? Are there compiler options I can use to get the other behavior? 是否有可用于获取其他行为的编译器选项?

  • (4): g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 (4):g ++(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3

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): 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)

Output from GCC 4.6: 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? 我怎样才能说服GCC 4.6它应该给我与GCC 4.2相同的输出?

-Wno-deprecated将删除所有已弃用的警告

gcc 4.6 added diagnostic pragmas that will help solve this problem: 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

Note: This only works in gcc 4.6 and higher. 注意:这仅适用于gcc 4.6及更高版本。 The push and pop are 4.6 extensions. pushpop是4.6扩展。 With gcc 4.5, the #pragma GCC diagnostic push and pop will be ignored (with warnings). 使用gcc 4.5, #pragma GCC diagnostic pushpop将被忽略(带警告)。 What won't be ignored is the #pragma GCC diagnostic ignored "-Wdeprecated-declarations" -- but now this has effect until end of file. 不会被忽略的是#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - 但现在这有效直到文件结束。

The behaviour you're seeing in GCC 4.2 is caused by an Apple-specific patch to GCC. 您在GCC 4.2中看到的行为是由针对GCC的Apple特定补丁引起的。 FSF GCC 4.2.4 warns about the use of a . FSF GCC 4.2.4警告使用a The specific bit that Apple GCC has that FSF GCC doesn't is: 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));

(Available under GPLv2 or later) (根据GPLv2或更高版本提供)

You may wish to adapt this patch to a later version of GCC (perhaps no changes are needed, perhaps major changes are needed), and build GCC from source with this patch applied. 您可能希望将此修补程序调整为更高版本的GCC(可能不需要进行任何更改,可能需要进行重大更改),并且应用此修补程序从源代码构建GCC。 Or you may report this as a feature request on FSF GCC bugzilla. 或者您可以在FSF GCC bugzilla上将此报告为功能请求。

I encounter the same issue. 我遇到了同样的问题。 The solution that came up is the following 提出的解决方案如下

typedef OLD_A_NOT_TO_BE_USED a __attribute__((deprecated));

int OLD_A_NOT_TO_BE_USED () {
    return 10;
}

int __attribute__((deprecated)) b() {
    return OLD_A_NOT_TO_BE_USED () * 2; //< I want to get rid of warnings from this line
}

int main() {
    return b(); //< I expect a warning on this line only
}

So I just rename my a class into OLD_A_NOT_TO_BE_USED class. 所以我只是将我的一个类重命名为OLD_A_NOT_TO_BE_USED类。 I get the warning only on return b(); 我只在返回b()时收到警告; and if someone was using a they will still get the deprecated warning. 如果有人在使用,他们仍会得到弃用的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM