gcc _Pragma 诊断忽略不工作

时间:2021-07-02 21:29:10

标签: gcc macros

此代码

#include <string.h>

char buf[4];

#define init() do{ \
  _Pragma("GCC diagnostic push") \
  _Pragma("GCC diagnostic ignored \"-Wstringop-truncation\"") \
  strncpy(buf, "abcdef", 4); \
  buf[3] = 0; \
  _Pragma("GCC diagnostic pop") \
  } while(0)

int main(int argc, char ** argv) {
  init();
}

应该在没有警告的情况下编译但是:

gcc -Wall -O pragma.c 在 /usr/include/string.h:495 包含的文件中, 来自 pragma.c:1: 在函数“strncpy”中, 从 pragma.c:14:3 处的“main”内联: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: 警告:'__builtin_strncpy' 输出被截断,从长度为 6 的字符串中复制 4 个字节 [-Wstringop-truncation] 106 |返回 __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~

删除 -O 删除警告.. 删除 #pragma pop 也会删除警告!

这听起来真的像是 gcc 中的一个错误;我正在寻找的是解决方法?

gcc 9.3.0 版

0 个答案:

没有答案