除最后一个外,所有#pragma GCC 诊断均被忽略
All #pragma GCC diagnostics ignored except the last one
当使用#pragma GCC diagnostic push/pop/warning/ignored
...时,似乎只有最后的#pragma
- 行生效!为什么?
例如,我从 here
复制并修改了 gcc 7.3.0 的示例
#include <stdio.h>
#include <stdint.h>
static void foo();
static void bar();
static void car();
static void dar();
int main() {
#pragma GCC diagnostic warning "-Wunused-variable"
foo(); /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
bar(); /* no diagnostic for this one */
#pragma GCC diagnostic pop
car(); /* error is given for this one */
#pragma GCC diagnostic pop
dar(); /* depends on command line options */
return 0;
}
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void bar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void car() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void dar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
通过两种方式编译上面的代码
在命令行中添加-Wall
在命令行中省略 -Wall
将导致 1. 对 foo()
、bar()
、car()
和 dar()
的所有调用发出警告,而 2. 不会发出警告对于任何.. 暗示最后一个 #pragma GCC diagnostic pop
是唯一生效的,并且是遵循命令行规则的那个。
我当然不是仅通过这个例子得出这个结论,而是我在这里代表的那个。
关于这是为什么的任何想法?我做错了吗?
编辑:
接受的答案导致以下工作代码:
#include <stdio.h>
#include <stdint.h>
static void foo();
static void bar();
static void car();
static void dar();
int main() {
foo(); /* error is given for this one */
bar(); /* no diagnostic for this one */
car(); /* error is given for this one */
dar(); /* depends on command line options */
return 0;
}
#pragma GCC diagnostic warning "-Wunused-variable"
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
static void bar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic pop
static void car() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic pop
static void dar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
最后一个 #pragma
被使用,因为 foo
和 bar
被放置在你的代码中所有 pragma 行之后。试试这个:
#pragma GCC diagnostic warning "-Wunused-variable"
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
pragma
影响此行之后的代码,并且不会像您预期的那样跟随函数调用。
当使用#pragma GCC diagnostic push/pop/warning/ignored
...时,似乎只有最后的#pragma
- 行生效!为什么?
例如,我从 here
#include <stdio.h>
#include <stdint.h>
static void foo();
static void bar();
static void car();
static void dar();
int main() {
#pragma GCC diagnostic warning "-Wunused-variable"
foo(); /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
bar(); /* no diagnostic for this one */
#pragma GCC diagnostic pop
car(); /* error is given for this one */
#pragma GCC diagnostic pop
dar(); /* depends on command line options */
return 0;
}
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void bar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void car() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void dar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
通过两种方式编译上面的代码
在命令行中添加
-Wall
在命令行中省略
-Wall
将导致 1. 对 foo()
、bar()
、car()
和 dar()
的所有调用发出警告,而 2. 不会发出警告对于任何.. 暗示最后一个 #pragma GCC diagnostic pop
是唯一生效的,并且是遵循命令行规则的那个。
我当然不是仅通过这个例子得出这个结论,而是我在这里代表的那个。
关于这是为什么的任何想法?我做错了吗?
编辑: 接受的答案导致以下工作代码:
#include <stdio.h>
#include <stdint.h>
static void foo();
static void bar();
static void car();
static void dar();
int main() {
foo(); /* error is given for this one */
bar(); /* no diagnostic for this one */
car(); /* error is given for this one */
dar(); /* depends on command line options */
return 0;
}
#pragma GCC diagnostic warning "-Wunused-variable"
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
static void bar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic pop
static void car() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic pop
static void dar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
最后一个 #pragma
被使用,因为 foo
和 bar
被放置在你的代码中所有 pragma 行之后。试试这个:
#pragma GCC diagnostic warning "-Wunused-variable"
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
pragma
影响此行之后的代码,并且不会像您预期的那样跟随函数调用。