感到困惑的是“#pragma GCC诊断推/弹”

时间:2013-02-22 00:11:11

标签: c++ gcc pragma

我正在尝试使用“#pragma GCC诊断推送”和“#pragma GCC诊断弹出”来为我的代码打开警告然后退出(例如,标题结束后)。但是当我弹出时,警告并没有像预期的那样被关闭。作为一个简单的例子:

int main(int, char*[]) {
  int i;
  unsigned int ui;
  i==ui;  // no warning as expected
// this push doesn't make any difference
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wsign-compare"
  i==ui;  // warns as expected

// this push doesn't make any difference in warning output
#pragma GCC diagnostic push

#pragma GCC diagnostic ignored "-Wsign-compare"
  i==ui; // no warning as expected

#pragma GCC diagnostic pop
  i==ui; // warns as expected

#pragma GCC diagnostic pop
  i==ui; // warns unexpectedly

// I can put as many pops as I want here, nothing changes
#pragma GCC diagnostic pop
  i==ui; // warns unexpected
  return 0;
}

使用gcc 4.6(和4.7)并且没有从命令行启用警告(或者甚至在命令行上传递-Wno-sign-compare),警告在文本中标记为。也就是说,人们似乎无法将东西带回命令行值。我是否误解了这应该如何运作?

关于如何让事情发挥作用的任何指示?

我在gcc手册中看到了与示例相同的行为:

int test() {
  int a,b,c,d;
#pragma GCC diagnostic warning "-Wuninitialized"
  foo(a);                  /* warning 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);                  /* warning is given for this one */
#pragma GCC diagnostic pop
  foo(d);                  /* warning is given here too, for some reason */
}

0 个答案:

没有答案