我的项目必须使用两个不同的编译器进行编译。一个创建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_PEDWARN
。 DK_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/