Search code examples
gcccompiler-errorsgcc-warninggcc4.7

GCC force warning to be an error: excess elements in array initializer


My projects have to compile with two different compilers. One that creates a DLL (for a PC simulation, Mingw32-gcc-4.7.2) and another one that creates an ELF (for real hardware). Both compilers have partially different behaviors, but we want them to be as similar as possible, at least when it comes to errors. There is one particular error, that I want to activate in GCC, which is only a warning there:

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

Is there a way to only promote this warning to an error? I only found solutions to activate a group of warnings, but not just this one. And if I am not mistaken the group flag should be behind the warning message in brackets: [enabled by default] ... I don't want all default warnings as error.

EDIT: what would be the flag to make it an active error with "-Werror=XXX"


Solution

  • 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.