gcc - GCC强制警告为错误: excess elements in array initializer

标签 gcc compiler-errors gcc-warning gcc4.7

我的项目必须使用两个不同的编译器进行编译。一个创建DLL(用于PC模拟,Mingw32-gcc-4.7.2),另一个创建ELF(用于真实硬件)。两种编译器的行为都有部分不同,但是我们希望它们至少在出现错误时尽可能相似。
我想在GCC中激活一个特定的错误,这只是一个警告:

warning: excess elements in array initializer [enabled by default]

有没有一种方法只能将此警告升级为错误?我只找到了激活一组警告的解决方案,而不仅仅是这个警告。如果我没记错的话,组标志应该放在方括号内的警告消息后面:[默认情况下启用] ...我不希望所有默认警告都作为错误。

编辑:什么是标志,使其成为 Activity 错误,“-Werror = XXX”

最佳答案

tl;博士
-pedantic-errors似乎有效。

https://godbolt.org/z/BD2XTr

搜索路径

我有同样的需求,但一直没有找到答案。当我发现这个问题还没有任何答案时,我决定深入研究GCC源代码。

一个简单的git grep 'excess elements in array initializer'告诉我,警告是从pedwarn_init()中的 gcc/c/c-typeck.c 生成的。

该函数在同一文件的6375行中定义。

/* Issue a pedantic warning for a bad initializer component.  OPT is
   the option OPT_* (from options.h) controlling this warning or 0 if
   it is unconditionally given.  GMSGID identifies the message.  The
   component name is taken from the spelling stack.  */

static void ATTRIBUTE_GCC_DIAG (3,0)
pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
{
  /* Use the location where a macro was expanded rather than where
     it was defined to make sure macros defined in system headers
     but used incorrectly elsewhere are diagnosed.  */
  location_t exploc = expansion_point_location_if_in_system_header (loc);
  auto_diagnostic_group d;
  va_list ap;
  va_start (ap, gmsgid);
  bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
  va_end (ap);
  char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
  if (*ofwhat && warned)
    inform (exploc, "(near initialization for %qs)", ofwhat);
}

调用者将0传递为OPT。所以我认为没有办法将这个警告变成一个错误。但是我注意到函数中使用了DK_PEDWARNDK_PEDWARN的定义如下:
DEFINE_DIAGNOSTIC_KIND (DK_PEDWARN, "pedwarn", NULL)

但是没有-Wpedwarn。我知道有-pedantic。因此,以防万一我在GCC信息中搜索pedantic和voilà,它在2.1节中。 (强调我的)

2.1 C Language

[...] To select this standard in GCC, use one of the options '-ansi', '-std=c90' or '-std=iso9899:1990'; to obtain all the diagnostics required by the standard, you should also specify '-pedantic' (or '-pedantic-errors' if you want them to be errors rather than warnings). *Note Options Controlling C Dialect: C Dialect Options.



它将所有学究式警告转换为错误,但我认为它比-Werror更好。

关于gcc - GCC强制警告为错误: excess elements in array initializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35886045/

相关文章:

linux -/usr/lib/libpthread.so : No such file or directory &&/usr/lib/libm. so: No such file or directory 已解决

c - 位移位和整数提升?

c - 警告 : format %s expects type char * but argument 2 has type int

linux - 无法禁用 gcc 警告 - 从整数创建指针而不进行强制转换

c - macOS 和 Linux 下 qsort() 结果存在无法解释的差异

c - 修改gcc的补丁认证程序

c++ - boost::spirit::qi 编译器错误:没有转换运算符无法将 'const some_stuff::return_type' 转换为 'unsigned int'

python - python-源代码字符串不能包含空字节

STM32驱动开发中的C编译错误

c - 如何让 gcc 提示 char 与 256 的比较