GCC强制警告为错误:数组初始化程序中的多余元素

问题描述 投票:1回答:1

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

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

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

编辑:使用“ -Werror = XXX”使其成为活动错误的标志是什么

gcc compiler-errors gcc-warning gcc4.7
1个回答
0
投票

tl; dr

-pedantic-errors似乎有效。

搜索路径

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

[简单的git grep 'excess elements in array initializer'告诉我警告是从pedwarn_init()中的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。所以我认为没有办法将这个警告变成一个错误。但是我注意到函数中使用了OPTDK_PEDWARN定义为,

DPK_PEDWARN

但是没有DEFINE_DIAGNOSTIC_KIND (DK_PEDWARN, "pedwarn", NULL) 。我知道有-Wpedwarn。因此,以防万一我在GCC信息和viora中搜索了-pedantic,它在2.1节中。 (强调我的)

2.1 C语言

[...]要在GCC中选择此标准,请使用以下选项之一'-ansi','-std = c90'或'-std = iso9899:1990';获得所有标准要求的诊断,还应指定'-pedantic'(或'-pedantic-errors',如果您希望它们是错误的而不是警告)。 *注意控制C语言的选项:C方言选项。

它将把所有的pedantic警告转换为错误,但我认为它比pedantic要好。

© www.soinside.com 2019 - 2024. All rights reserved.