[英]GCC ignores diagnostic pragmas while clang doesn't
GCC 10.2.0, CLang 11.1.0 GCC 10.2.0,CLang 11.1.0
I have the following piece of code:我有以下代码:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
uint8_t hour = p[8];
uint8_t min = p[9];
uint8_t sec = p[10];
uint16_t msec = (p[11] * 4);
#pragma GCC diagnostic pop
CLang (which is used by YouCompleteMe vim plugin) honors these pragmas and doesn't show any warning during file processing. CLang(YouCompleteMe vim 插件使用)遵循这些编译指示,并且在文件处理期间不显示任何警告。 I can confirm that by commenting these pragmas out - YCM diagnostics immediately show me warnings on corresponding lines.
我可以通过注释掉这些 pragma 来确认 - YCM 诊断程序会立即在相应的行上向我显示警告。
However, GCC ignores them and I get the following in console log:但是,GCC 忽略了它们,我在控制台日志中得到以下信息:
packet.c:111:26: warning: unused variable ‘msec’ [-Wunused-variable]
111 | uint16_t msec = (p[11] * 4);
| ^~~~
packet.c:110:25: warning: unused variable ‘sec’ [-Wunused-variable]
110 | uint8_t sec = p[10];
| ^~~
packet.c:109:25: warning: unused variable ‘min’ [-Wunused-variable]
109 | uint8_t min = p[9];
| ^~~
packet.c:108:25: warning: unused variable ‘hour’ [-Wunused-variable]
108 | uint8_t hour = p[8];
| ^~~~
Flags used for build:用于构建的标志:
CFLAGS="-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -Wall -pedantic -Werror=format-security" CXXFLAGS="-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -Wall -pedantic -Werror=format-security" LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now"
What is going on here?这里发生了什么? Seems like a bug in GCC
似乎是 GCC 中的错误
ADDITION 1 It seems like this is the case even for this simplest program: ADDITION 1即使对于这个最简单的程序,情况似乎也是如此:
int main(int argc, char *argv[]) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
int x;
#pragma GCC diagnostic pop
return 0;
}
Tests:测试:
[23:41 viktor@desolve-desktop /tmp]$ clang -Wall main.c
[23:41 viktor@desolve-desktop /tmp]$ gcc -Wall main.c
main.c: In function ‘main’:
main.c:4:9: warning: unused variable ‘x’ [-Wunused-variable]
4 | int x;
| ^
ADDITION 2 Some similar bug is described for g++ and still opened for about 9 years!补充 2 g++ 描述了一些类似的错误,并且仍然存在大约 9 年!
Seems like it's related to the scoping.似乎它与范围界定有关。 When I move push/pop
#pragma
s outside of a variable's block GCC behaves just well.当我将 push/pop
#pragma
移动到变量块之外时,GCC 表现得很好。 For example:例如:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
uint8_t hour = p[8];
uint8_t min = p[9];
uint8_t sec = p[10];
uint16_t msec = (p[11] * 4);
#pragma GCC diagnostic pop
should be changed to the:应改为:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
{
uint8_t hour = p[8];
uint8_t min = p[9];
uint8_t sec = p[10];
uint16_t msec = (p[11] * 4);
}
#pragma GCC diagnostic pop
Then both clang and gcc works fine然后 clang 和 gcc 工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.