C++ 选择性地仅对翻译单元的一部分禁用 GCC 警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/965093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selectively disable GCC warnings for only part of a translation unit?
提问by Jon-Eric
What's the closest GCC equivalent to this MSVC preprocessor code?
与此 MSVC 预处理器代码最接近的 GCC 是什么?
#pragma warning( push ) // Save the current warning state.
#pragma warning( disable : 4723 ) // C4723: potential divide by 0
// Code which would generate warning 4723.
#pragma warning( pop ) // Restore warnings to previous state.
We have code in commonly included headers which we do not want to generate a specific warning. However we want files which include those headers to continue to generate that warning (if the project has that warning enabled).
我们在通常包含的标头中有代码,我们不想生成特定警告。但是,我们希望包含这些标题的文件继续生成该警告(如果项目启用了该警告)。
回答by Matt Joiner
This is possible in GCCsince version 4.6, or around June 2010 in the trunk.
自 4.6 版或 2010 年 6 月左右在主干中,这在 GCC 中是可能的。
Here's an example:
下面是一个例子:
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wuninitialized"
foo(a); /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
foo(b); /* no diagnostic for this one */
#pragma GCC diagnostic pop
foo(c); /* error is given for this one */
#pragma GCC diagnostic pop
foo(d); /* depends on command line options */
回答by chaos
The closest thing is the GCC diagnostic pragma, #pragma GCC diagnostic [warning|error|ignored] "-Wwhatever"
. It isn't very close to what you want, and see the link for details and caveats.
最接近的是GCC 诊断编译指示, #pragma GCC diagnostic [warning|error|ignored] "-Wwhatever"
. 它与您想要的不是很接近,有关详细信息和注意事项,请参阅链接。
回答by Tom
I've done something similar. For third-party code, I didn't want to see any warnings at all. So, rather than specify -I/path/to/libfoo/include
, I used -isystem /path/to/libfoo/include
. This makes the compiler treat those header files as "system headers" for the purpose of warnings, and so long as you don't enable -Wsystem-headers
, you're mostly safe. I've still seen a few warnings leak out of there, but it cuts down on most of the junk.
我做过类似的事情。对于第三方代码,我根本不想看到任何警告。因此,-I/path/to/libfoo/include
我没有指定,而是使用-isystem /path/to/libfoo/include
. 这使得编译器出于警告目的将这些头文件视为“系统头文件”,只要您不启用-Wsystem-headers
,您就基本上是安全的。我仍然看到一些警告从那里泄漏,但它减少了大部分垃圾。
Note that this onlyhelps you if you can isolate the offending code by include-directory. If it's just a subset of your own project, or intermixed with other code, you're out of luck.
请注意,如果您可以通过包含目录隔离有问题的代码,这只会对您有所帮助。如果它只是您自己项目的一个子集,或者与其他代码混合,那么您就不走运了。
回答by Cássio Renan
This is an expansion to Matt Joiner's answer.
这是对Matt Joiner 的回答的扩展。
If you don't want to spawn pragmas all over your code, you can use the _Pragma operator:
如果您不想在整个代码中生成 pragma,您可以使用_Pragma 运算符:
#ifdef __GNUC__
# define DIAGNOSTIC_ERROR(w) _Pragma("GCC diagnostic error \"" w "\"")
# define DIAGNOSTIC_IGNORE(w) _Pragma("GCC diagnostic ignore \"" w "\"")
# define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
# define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
#endif
// (...)
DIAGNOSTIC_ERROR("-Wuninitialized")
foo(a); // Error
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE("-Wuninitialized")
foo(a); // No error
DIAGNOSTIC_POP
foo(a); // Error