简体   繁体   English

GCC 忽略诊断编译指示,而 clang 不会

[英]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.

相关问题 Clang编译工作,而gcc不用于钻石继承 - Clang compilation works while gcc doesn't for diamond inheritance 为什么GCC会为结构化绑定诊断一个未使用的变量,而Clang却没有? - Why does GCC diagnose a unused variable for structured bindings while Clang doesn't? 当我聚合初始化数组而 gcc 没有时,Clang 会警告我 - Clang warns me when I aggregate initialize an array while gcc doesn't gcc和clang预处理器不了解L前缀 - gcc and clang preprocessor doesn't understand L prefix Clang 不编译代码,但 gcc 和 msvc 编译了它 - Clang doesn't compile code but gcc and msvc compiled it 为什么GCC和Clang不进行混叠优化? - Why doesn't GCC and Clang do this aliasing-optimization? Mac 上的未知 GCC 编译指示 - Unknown GCC pragmas on Mac 当程序对 function 指针进行指针运算时,clang 和 gcc 是否应该产生诊断消息? - Should clang and gcc produce a diagnostic message when a program does pointer arithmetic on a function pointer? 为什么此模板constexpr函数不能在gcc上编译,但是在clang上能很好地工作? - Why this template constexpr function doesn't compile on gcc but works well on clang? 通过朋友访问类中的受保护类型 - gcc允许,clang不允许 - Accessing protected type in a class through a friend - gcc allows, clang doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM