我试图在从宏扩展的代码中禁用g ++警告。据我了解,_Pragma应该遵循宏用法,并且在使用Wparentheses编译时不应触发g++

#include <stdio.h>

#define TEST(expr) \
    int a = 1; \
    _Pragma( "GCC diagnostic push" ) \
    _Pragma( "GCC diagnostic ignored \"-Wparentheses\"" ) \
    if (a <= expr) { \
        printf("filler\n"); \
    } \
    _Pragma( "GCC diagnostic pop" )

int main(){
    int b = 2, c = 3;
    TEST(b == c);
}


当我使用g++进行编译时,收到Wparentheses警告,我正在尝试将其禁用。

xarn@DESKTOP-B2A3CNC:/mnt/c/ubuntu$ g++ -Wall -Wextra test3.c
test3.c: In function ‘int main()’:
test3.c:8:11: warning: suggest parentheses around comparison in operand of ‘==’ [-Wparentheses]
     if (a <= expr) { \
           ^
test3.c:15:5: note: in expansion of macro ‘TEST’
     TEST(b == c);
     ^


但是,使用gcc时,它可以按预期工作:

xarn@DESKTOP-B2A3CNC:/mnt/c/ubuntu$ gcc -Wall -Wextra test3.c
test3.c: In function ‘main’:
test3.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^


我正在使用g++版本4.8.5。

最佳答案

使用gcc前端时,gcc处理_Pragma中存在一些长期存在的错误。唯一的解决方案是要么升级到足够现代的g ++版本(IIRC 6+),要么对整个TU禁用警告。

关于c++ - G++忽略_Pragma诊断忽略,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43063596/

10-11 23:11
查看更多