diff mbox series

[kbuild] Makefile.extrawarn: disable -Woverride-init in W=1

Message ID 20210407002450.10015-1-kabel@kernel.org (mailing list archive)
State Not Applicable
Delegated to: Netdev Maintainers
Headers show
Series [kbuild] Makefile.extrawarn: disable -Woverride-init in W=1 | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Marek Behún April 7, 2021, 12:24 a.m. UTC
The -Wextra flag enables -Woverride-init in newer versions of GCC.

This causes the compiler to warn when a value is written twice in a
designated initializer, for example:
  int x[1] = {
    [0] = 3,
    [0] = 3,
  };

Note that for clang, this was disabled from the beginning with
-Wno-initializer-overrides in commit a1494304346a3 ("kbuild: add all
Clang-specific flags unconditionally").

This prevents us from implementing complex macros for compile-time
initializers.

For example a macro of the form INITIALIZE_BITMAP(bits...) that can be
used as
  static DECLARE_BITMAP(bm, 64) = INITIALIZE_BITMAP(0, 1, 32, 33);
can only be implemented by allowing a designated initializer to
initialize the same members multiple times (because the compiler
complains even if the multiple initializations initialize to the same
value).

Disable the -Woverride-init flag.

Signed-off-by: Marek Behún <kabel@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Nathan Chancellor <natechancellor@gmail.com>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Andrew Lunn <andrew@lunn.ch>
Cc: netdev@vger.kernel.org
---
 scripts/Makefile.extrawarn | 1 +
 1 file changed, 1 insertion(+)

Comments

Arnd Bergmann April 7, 2021, 7:14 a.m. UTC | #1
On Wed, Apr 7, 2021 at 2:24 AM Marek Behún <kabel@kernel.org> wrote:
>
> The -Wextra flag enables -Woverride-init in newer versions of GCC.
>
> This causes the compiler to warn when a value is written twice in a
> designated initializer, for example:
>   int x[1] = {
>     [0] = 3,
>     [0] = 3,
>   };
>
> Note that for clang, this was disabled from the beginning with
> -Wno-initializer-overrides in commit a1494304346a3 ("kbuild: add all
> Clang-specific flags unconditionally").
>
> This prevents us from implementing complex macros for compile-time
> initializers.

I think this is generally a useful warning, and it has found a number
of real bugs. I would want this to be enabled in both gcc and clang
by default, and I have previously sent both bugfixes and patches to
disable it locally.

> For example a macro of the form INITIALIZE_BITMAP(bits...) that can be
> used as
>   static DECLARE_BITMAP(bm, 64) = INITIALIZE_BITMAP(0, 1, 32, 33);
> can only be implemented by allowing a designated initializer to
> initialize the same members multiple times (because the compiler
> complains even if the multiple initializations initialize to the same
> value).

We don't have this kind of macro at the moment, and this may just mean
you need to try harder to come up with a definition that only initializes
each member once if you want to add this.

How do you currently define it?

            Arnd
Marek Behún April 7, 2021, 2:49 p.m. UTC | #2
On Wed, 7 Apr 2021 09:14:29 +0200
Arnd Bergmann <arnd@arndb.de> wrote:

> On Wed, Apr 7, 2021 at 2:24 AM Marek Behún <kabel@kernel.org> wrote:
> >
> > The -Wextra flag enables -Woverride-init in newer versions of GCC.
> >
> > This causes the compiler to warn when a value is written twice in a
> > designated initializer, for example:
> >   int x[1] = {
> >     [0] = 3,
> >     [0] = 3,
> >   };
> >
> > Note that for clang, this was disabled from the beginning with
> > -Wno-initializer-overrides in commit a1494304346a3 ("kbuild: add all
> > Clang-specific flags unconditionally").
> >
> > This prevents us from implementing complex macros for compile-time
> > initializers.  
> 
> I think this is generally a useful warning, and it has found a number
> of real bugs. I would want this to be enabled in both gcc and clang
> by default, and I have previously sent both bugfixes and patches to
> disable it locally.
> 
> > For example a macro of the form INITIALIZE_BITMAP(bits...) that can be
> > used as
> >   static DECLARE_BITMAP(bm, 64) = INITIALIZE_BITMAP(0, 1, 32, 33);
> > can only be implemented by allowing a designated initializer to
> > initialize the same members multiple times (because the compiler
> > complains even if the multiple initializations initialize to the same
> > value).  
> 
> We don't have this kind of macro at the moment, and this may just mean
> you need to try harder to come up with a definition that only initializes
> each member once if you want to add this.
> 
> How do you currently define it?
> 
>             Arnd

You can look at the current definition in this patch
https://git.kernel.org/pub/scm/linux/kernel/git/kabel/linux.git/commit/?h=marvell10g-updates&id=a4ba5e6563ac4d9e352f55fbae8431339001acf1

And the previous patch, adding variadic-macro.h
https://git.kernel.org/pub/scm/linux/kernel/git/kabel/linux.git/commit/?h=marvell10g-updates&id=d5f8438024b688e96bdd16349f717e5469183362

I fear it won't be possible to expand a macro in such a way to
initialize each member only once, without giving it the number of array
members it has to fill as a constant, i.e. if the bitmap is 100 bits on
a 32 bit machine, it has to fill up to 4 longs, so we would need to
give 4 as an argument:
  ... = INITIALIZE_BITMAP(4, ...);
but DIV_ROUND_UP(100, BITS_PER_LONG) won't work.

Another way around this is to use _Pragma to disable this specific
warning for a specific part of code. Unfortunately it seems that this
_Pragma operator cannot be used withing the designated initializer, it
has to be outside the expression declaring the variable, i.e.
  _Pragma("GCC diagnostic ignored \"-Woverride-init\"")
  ... = INITIALIZE_BITMAP(...);

What I am frustrated about is why doesn't the compiler have the option
to warn only if designated initializer initializes the same member to a
different value...

Marek
Marek Behún April 7, 2021, 8:44 p.m. UTC | #3
On Wed, 7 Apr 2021 09:14:29 +0200
Arnd Bergmann <arnd@arndb.de> wrote:

> On Wed, Apr 7, 2021 at 2:24 AM Marek Behún <kabel@kernel.org> wrote:
> >
> > The -Wextra flag enables -Woverride-init in newer versions of GCC.
> >
> > This causes the compiler to warn when a value is written twice in a
> > designated initializer, for example:
> >   int x[1] = {
> >     [0] = 3,
> >     [0] = 3,
> >   };
> >
> > Note that for clang, this was disabled from the beginning with
> > -Wno-initializer-overrides in commit a1494304346a3 ("kbuild: add all
> > Clang-specific flags unconditionally").
> >
> > This prevents us from implementing complex macros for compile-time
> > initializers.  
> 
> I think this is generally a useful warning, and it has found a number
> of real bugs. I would want this to be enabled in both gcc and clang
> by default, and I have previously sent both bugfixes and patches to
> disable it locally.
> 
> > For example a macro of the form INITIALIZE_BITMAP(bits...) that can be
> > used as
> >   static DECLARE_BITMAP(bm, 64) = INITIALIZE_BITMAP(0, 1, 32, 33);
> > can only be implemented by allowing a designated initializer to
> > initialize the same members multiple times (because the compiler
> > complains even if the multiple initializations initialize to the same
> > value).  
> 
> We don't have this kind of macro at the moment, and this may just mean
> you need to try harder to come up with a definition that only initializes
> each member once if you want to add this.
> 
> How do you currently define it?
> 
>             Arnd

Arnd,

since it is possible to create a macro which will expand N times if N
is a preprocessor numeric constant, i.e.
  EXPAND_N_TIMES(3, macro, args...)
would expand to
  macro(1, args...) macro(2, args...) macro(3, args...)

But the first argument to this EXPAND_N_TIMES macro would have to be a
number when preprocessing, so no expression via division, nor enums.

Example:

  The PHY_INTERFACE_MODE_* constants are defined via enum, and
  the last is PHY_INTERFACE_MODE_MAX.

  We could then implement bitmap initializers for PHY_INTERFACE_MODE
  bitmap in the following way:

  enum {
    ...
    PHY_INTERFACE_MODE_MAX
  };

  /* assume PHY_INTERFACE_MODE_MASK has value 50, so 2 longs on 32-bit
   * and 1 long on 64-bit. These have to be direct constant, no expressions
   * allowed. If more PHY_INTERFACE_MODE_* constants are added to the enum
   * above, the following must be changed accordingly. The static_assert
   * below guards against invalid value.
   */

  #define PHY_INTERFACE_BITMAP_LONGS_64  1
  #define PHY_INTERFACE_BITMAP_LONGS_32  2

  /* check if PHY_INTERFACE_BITMAP_LONGS_* have correct values */
  static_assert(PHY_INTERFACE_BITMAP_LONGS_64 ==
                DIV_ROUND_UP(PHY_INTERFACE_MODE_MASK, 64));
  static_assert(PHY_INTERFACE_BITMAP_LONGS_32 ==
                DIV_ROUND_UP(PHY_INTERFACE_MODE_MASK, 32));

  #define DECLARE_PHY_INTERFACE_MASK(name) \
    DECLARE_BITMAP(name, PHY_INTERFACE_MODE_MAX)

  #define INIT_PHY_INTERFACE_MASK(...) \
    INITIALIZE_BITMAP(PHY_INTERFACE_BITMAP_LONGS, ##__VA_ARGS__)

What do you think?

Marek
diff mbox series

Patch

diff --git a/scripts/Makefile.extrawarn b/scripts/Makefile.extrawarn
index d53825503874..cf7bc1eec5e3 100644
--- a/scripts/Makefile.extrawarn
+++ b/scripts/Makefile.extrawarn
@@ -36,6 +36,7 @@  KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
 KBUILD_CFLAGS += -Wno-missing-field-initializers
 KBUILD_CFLAGS += -Wno-sign-compare
 KBUILD_CFLAGS += -Wno-type-limits
+KBUILD_CFLAGS += $(call cc-disable-warning, override-init)
 
 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1