tl;dr
-pedantic-errors
seems to work.
https://godbolt.org/z/BD2XTr
Searching path
I had the same need but had been failing to find an answer. When I found out that this question didn't have any answer yet, I decided to dive into GCC source code.
A simple git grep 'excess elements in array initializer'
told me that the warning is generated from pedwarn_init()
in gcc/c/c-typeck.c
.
The function is defined at line 6375 in the same file.
/* 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);
}
The caller passes 0
as OPT
. So I thought there was no way to turn this warning into an error. But I noticed that DK_PEDWARN
is used in the function. DK_PEDWARN
is define like,
DEFINE_DIAGNOSTIC_KIND (DK_PEDWARN, "pedwarn", NULL)
But there is no -Wpedwarn
. I knew there was -pedantic
, though. So just in case I've searched for pedantic
in the GCC info, and voilà, there it is in the section 2.1. (emphasis mine)
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.
It will convert all pedantic warnings to errors but I think it's better than -Werror
.