1

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"

Matthias
  • 1,200
  • 2
  • 13
  • 33
  • 1
    If you see e.g. [this GCC online manual about warning flags](https://gcc.gnu.org/onlinedocs/gcc-4.9.3/gcc/Warning-Options.html#Warning-Options), you will pretty early see `-Werror=`, where you can tell GCC to turn a specific warning into an error. – Some programmer dude Mar 09 '16 at 08:07
  • I have already seen the -Werror= functionality. But I thought it can only switch a group of warnings controlled by a flag (put in []-brackets after warning, like: -Werror=undef for "XXX is not defined [-Wundef]"). But how to switch my specific warning to an error? – Matthias Mar 09 '16 at 08:12
  • While I don't know what warning option is used for the "excess elements" warning, you turn a single warning into an error by doing `-Werror=undef` (for the `-Wundef` warning option). – Some programmer dude Mar 09 '16 at 08:17
  • Even in GCC 5.3.0, there doesn't seem to be an independent option to turn that warning into an error. It looks like you'll have to write good C and ensure that there are no warnings, so you can use `-Werror`. Or you'll have to search the build logs to find that warning. I tried `-std=c11 -pedantic -Wall -Wextra` and even that didn't seem to trigger an error — still just a warning. That is still standard compliant; a diagnostic (the warning) is issued — what else happens is implementation defined. – Jonathan Leffler Mar 09 '16 at 08:28
  • The relevant section of the standard is ISO/IEC 9899:2011 §6.7.9 Initialization. The first constraint is: _No initializer shall attempt to provide a value for an object not contained within the entity being initialized._ Too many initializers for an array violates that constraint. If you have a problem of not being able to count your initializers, you need to learn how to do it. If your colleagues can't count, then you need to train them. If you have a code generator that can't count, fix the code generator. It's something that should only occur very seldom — and should be spotted by devs. – Jonathan Leffler Mar 09 '16 at 08:31
  • The problem is, we have a multi-staging continuous integration system, which only accepts commits, that build with GCC. Due to time problems (too long feedback time) we can only test with this one compiler. In the end it can mean that the software inside the repository is not really valid. Assuming every developer sees every compilation issue is not really what I want. I think that's the job of the compiler, not the dev. While doing huge merge activities with hundreds of preprocessor statements is not really good to overlook. That's why we have tools after all :) – Matthias Mar 09 '16 at 08:39
  • And having no warnings and switch all warnings to errors is somehow our goal, but cannot be done right now, or development would pause for several weeks (very likely) – Matthias Mar 09 '16 at 08:39

1 Answers1

0

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.

Yasushi Shoji
  • 4,028
  • 1
  • 26
  • 47