[PATCH] Add fallthrough attributes
Adds the __attribute__ ((fallthrough)) annotation to all the places where switch case fallthrough was occurring. This allows the -Wimplicit-fallthrough warning to be used even after the source has been pre-processed. Signed-off-by: Joshua Watt --- backends/i386_regs.c | 3 +++ backends/i386_retval.c | 3 +++ backends/m68k_retval.c | 3 +++ backends/ppc_regs.c | 3 +++ backends/x86_64_regs.c | 3 +++ configure.ac | 22 ++ libcpu/i386_disasm.c | 3 +++ libdw/cfi.c | 6 ++ libdw/dwarf_frame_register.c | 3 +++ libdwfl/dwfl_report_elf.c| 3 +++ libdwfl/frame_unwind.c | 3 +++ libebl/eblobjnote.c | 3 +++ libelf/elf32_updatenull.c| 3 +++ libelf/elf_begin.c | 6 ++ libelf/elf_cntl.c| 3 +++ src/addr2line.c | 3 +++ src/elfcompress.c| 3 +++ src/elflint.c| 12 src/objdump.c| 3 +++ src/readelf.c| 3 +++ src/strings.c| 3 +++ tests/backtrace.c| 3 +++ tests/elfstrmerge.c | 3 +++ 23 files changed, 103 insertions(+) diff --git a/backends/i386_regs.c b/backends/i386_regs.c index fd963a62..1488c1e7 100644 --- a/backends/i386_regs.c +++ b/backends/i386_regs.c @@ -92,6 +92,9 @@ i386_register_info (Ebl *ebl __attribute__ ((unused)), case 5: case 8: *type = DW_ATE_address; +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fallthrough */ case 0 ... 3: case 6 ... 7: diff --git a/backends/i386_retval.c b/backends/i386_retval.c index 4aa646fe..56493a74 100644 --- a/backends/i386_retval.c +++ b/backends/i386_retval.c @@ -123,6 +123,9 @@ i386_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) if (size <= 8) return nloc_intregpair; } +#ifdef HAVE_FALLTHROUGH +__attribute__ ((fallthrough)); +#endif /* Fallthrough */ case DW_TAG_structure_type: diff --git a/backends/m68k_retval.c b/backends/m68k_retval.c index c68ed022..d9ff5cf6 100644 --- a/backends/m68k_retval.c +++ b/backends/m68k_retval.c @@ -135,6 +135,9 @@ m68k_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) if (size <= 8) return nloc_intregpair; } +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fallthrough */ case DW_TAG_structure_type: case DW_TAG_class_type: diff --git a/backends/ppc_regs.c b/backends/ppc_regs.c index c2d50118..0e0fed20 100644 --- a/backends/ppc_regs.c +++ b/backends/ppc_regs.c @@ -140,6 +140,9 @@ ppc_register_info (Ebl *ebl __attribute__ ((unused)), case 100: if (*bits == 32) return stpcpy (name, "mq") + 1 - name; +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fallthrough */ case 102 ... 107: name[0] = 's'; diff --git a/backends/x86_64_regs.c b/backends/x86_64_regs.c index 84304407..a867b0d3 100644 --- a/backends/x86_64_regs.c +++ b/backends/x86_64_regs.c @@ -87,6 +87,9 @@ x86_64_register_info (Ebl *ebl __attribute__ ((unused)), case 6 ... 7: *type = DW_ATE_address; +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fallthrough */ case 0 ... 5: name[0] = 'r'; diff --git a/configure.ac b/configure.ac index 4ab8816a..3e91b367 100644 --- a/configure.ac +++ b/configure.ac @@ -143,6 +143,28 @@ if test "$ac_cv_visibility" = "yes"; then [Defined if __attribute__((visibility())) is supported]) fi +AC_CACHE_CHECK([whether gcc supports __attribute__((fallthrough))], + ac_cv_fallthrough, [dnl +save_CFLAGS="$CFLAGS" +CFLAGS="$save_CFLAGS -Werror" +AC_COMPILE_IFELSE([AC_LANG_SOURCE([dnl +void +foo (int a) +{ + switch(a) +{ +case 1: +__attribute__((fallthrough)); +default: +break; +} +}])], ac_cv_fallthrough=yes, ac_cv_fallthrough=no) +CFLAGS="$save_CFLAGS"]) +if test "$ac_cv_fallthrough" = "yes"; then + AC_DEFINE([HAVE_FALLTHROUGH], [1], + [Defined if __attribute__((fallthrough)) is supported]) +fi + AC_CACHE_CHECK([whether gcc supports __attribute__((gcc_struct))], ac_cv_gcc_struct, [dnl save_CFLAGS="$CFLAGS" diff --git a/libcpu/i386_disasm.c b/libcpu/i386_disasm.c index 831afbe2..740a637a 100644 --- a/libcpu/i386_disasm.c +++ b/libcpu/i386_disasm.c @@ -819,6 +819,9 @@ i386_disasm (Ebl *ebl __attribute__((unused)), ++param_start; break; } +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fallthrough */ default: assert (! "INVALID
[PATCH v2] Add fallthrough attributes
Adds the __attribute__ ((fallthrough)) annotation to all the places where switch case fallthrough was occurring. This allows the -Wimplicit-fallthrough warning to be used even after the source has been pre-processed. If the fallthrough attribute is supported, the -Wimplicit-fallthrough warning is strengthened so that it *only* allows the attribute; comments alone are insufficient. Signed-off-by: Joshua Watt --- backends/aarch64_retval.c| 3 +++ backends/alpha_retval.c | 6 ++ backends/arm_regs.c | 3 +++ backends/arm_retval.c| 3 +++ backends/i386_regs.c | 3 +++ backends/i386_retval.c | 6 ++ backends/ia64_retval.c | 3 +++ backends/linux-core-note.c | 3 +++ backends/m68k_retval.c | 6 ++ backends/ppc64_retval.c | 9 + backends/ppc_regs.c | 3 +++ backends/ppc_retval.c| 6 ++ backends/s390_retval.c | 6 ++ backends/sh_retval.c | 3 +++ backends/sparc_retval.c | 3 +++ backends/tilegx_retval.c | 6 ++ backends/x86_64_regs.c | 3 +++ backends/x86_64_retval.c | 3 +++ config/eu.am | 4 configure.ac | 24 libcpu/i386_disasm.c | 3 +++ libdw/cfi.c | 6 ++ libdw/dwarf_frame_register.c | 3 +++ libdwfl/dwfl_report_elf.c| 3 +++ libdwfl/frame_unwind.c | 3 +++ libebl/eblobjnote.c | 3 +++ libelf/elf32_updatenull.c| 3 +++ libelf/elf_begin.c | 6 ++ libelf/elf_cntl.c| 3 +++ src/addr2line.c | 3 +++ src/elfcompress.c| 3 +++ src/elflint.c| 12 src/objdump.c| 3 +++ src/readelf.c| 12 src/strings.c| 3 +++ tests/backtrace.c| 3 +++ tests/elfstrmerge.c | 3 +++ 37 files changed, 181 insertions(+) diff --git a/backends/aarch64_retval.c b/backends/aarch64_retval.c index 68de307e..4b4d7c54 100644 --- a/backends/aarch64_retval.c +++ b/backends/aarch64_retval.c @@ -292,6 +292,9 @@ aarch64_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) assert (count > 0); if (count <= 4) return pass_hfa (locp, base_size, count); +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fall through. */ case 1: diff --git a/backends/alpha_retval.c b/backends/alpha_retval.c index 53dbfa45..f19c5e4a 100644 --- a/backends/alpha_retval.c +++ b/backends/alpha_retval.c @@ -85,6 +85,9 @@ alpha_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) typedie = dwarf_formref_die (attr, &die_mem); tag = DWARF_TAG_OR_RETURN (typedie); } +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fall through. */ case DW_TAG_base_type: @@ -131,6 +134,9 @@ alpha_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) } } +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Else fall through. */ case DW_TAG_structure_type: diff --git a/backends/arm_regs.c b/backends/arm_regs.c index 21c5ad3a..1c1ee8fd 100644 --- a/backends/arm_regs.c +++ b/backends/arm_regs.c @@ -77,6 +77,9 @@ arm_register_info (Ebl *ebl __attribute__ ((unused)), case 16 + 0 ... 16 + 7: regno += 96 - 16; +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fall through. */ case 96 + 0 ... 96 + 7: *setname = "FPA"; diff --git a/backends/arm_retval.c b/backends/arm_retval.c index 7aced742..7fe6e6a9 100644 --- a/backends/arm_retval.c +++ b/backends/arm_retval.c @@ -82,6 +82,9 @@ arm_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) typedie = dwarf_formref_die (attr, &die_mem); tag = DWARF_TAG_OR_RETURN (typedie); } +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fall through. */ case DW_TAG_base_type: diff --git a/backends/i386_regs.c b/backends/i386_regs.c index fd963a62..1488c1e7 100644 --- a/backends/i386_regs.c +++ b/backends/i386_regs.c @@ -92,6 +92,9 @@ i386_register_info (Ebl *ebl __attribute__ ((unused)), case 5: case 8: *type = DW_ATE_address; +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* Fallthrough */ case 0 ... 3: case 6 ... 7: diff --git a/backends/i386_retval.c b/backends/i386_retval.c index 4aa646fe..e36e6c4a 100644 --- a/backends/i386_retval.c +++ b/backends/i386_retval.c @@ -85,6 +85,9 @@ i386_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) typedie = dwarf_formref_die (attr, &die_mem); tag = DWARF_TAG_OR_RETURN (typedie); } +#ifdef HAVE_FALLTHROUGH + __attribute__ ((fallthrough)); +#endif /* F
Re: [PATCH v2] Add fallthrough attributes
On Fri, 2018-02-09 at 10:26 +0100, Mark Wielaard wrote: > On Fri, Feb 09, 2018 at 10:08:09AM +0100, Ulf Hermann wrote: > > > [...] > > > +#ifdef HAVE_FALLTHROUGH > > > + __attribute__ ((fallthrough)); > > > +#endif > > > [...] > > > > I would like to see this stanza wrapped in a macro, so that we only > > have one "#ifdef HAVE_FALLTHROUGH" in the code, not another one in > > every place we want to fall through. See the "internal_function" > > macro defined in lib/eu-config.h for a similar case. > > Agreed. Having 4 lines for a fallthrough instead of 1 is really too > much. Also could you explain a bit more why you would like this? > The advantage of the comments really is that they should work > everywhere. I'm attempting to build Yocto using Icecream to do distributed compiling. Icecream pre-processes the file before sending it to the remote compiling node, thus removing the comments and triggering the error. There is a fix in Icecream to allow it to not remove comments when pre- processing, but it will be a while before that change (which hasn't been released yet) trickles down to all the end Icecream users. I figured making the change here wouldn't hurt in the meantime. > > If the comment really doesn't work in your situation maybe we could > do > like gnulib did: > http://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=11fdf80b21f2b4 > 0a10687b9a3d16c852b19d512c > > The idea is that those versions of GCC that support > -Wimplicit-fallthrough also have support for the __attribute__ > ((fallthrough)) statement. So they can always be used together. Yes, that is cleaner and makes more sense. I will change my patch to do that. Thanks, Joshua Watt > > Cheers, > > Mark
[PATCH v3] Use fallthrough attribute
Use __attribute__ ((fallthrough)) to indicate switch case fall through instead of a comment. This ensure that the fallthrough warning is not triggered even if the file is pre-processed (hence stripping the comments) before it is compiled. The actual fallback implementation is hidden behind a FALLBACK macro in case the compiler doesn't support it. Finally, the -Wimplict-fallthrough warning was upgraded to only allow the attribute to satisfy it; a comment alone is no longer sufficient. Signed-off-by: Joshua Watt --- backends/aarch64_retval.c| 2 +- backends/alpha_retval.c | 4 ++-- backends/arm_regs.c | 2 +- backends/arm_retval.c| 2 +- backends/i386_regs.c | 2 +- backends/i386_retval.c | 4 ++-- backends/ia64_retval.c | 2 +- backends/linux-core-note.c | 2 +- backends/m68k_retval.c | 4 ++-- backends/ppc64_retval.c | 6 +++--- backends/ppc_regs.c | 2 +- backends/ppc_retval.c| 4 ++-- backends/s390_retval.c | 4 ++-- backends/sh_retval.c | 2 +- backends/sparc_retval.c | 2 +- backends/tilegx_retval.c | 4 ++-- backends/x86_64_regs.c | 2 +- backends/x86_64_retval.c | 2 +- config/eu.am | 4 +++- configure.ac | 6 ++ lib/eu-config.h | 7 +++ libcpu/i386_disasm.c | 2 +- libdw/cfi.c | 4 ++-- libdw/dwarf_frame_register.c | 2 +- libdwfl/dwfl_report_elf.c| 2 +- libdwfl/frame_unwind.c | 2 +- libebl/eblobjnote.c | 2 +- libelf/elf32_updatenull.c| 2 +- libelf/elf_begin.c | 4 ++-- libelf/elf_cntl.c| 2 +- src/addr2line.c | 2 +- src/elfcompress.c| 2 +- src/elflint.c| 8 src/objdump.c| 2 +- src/readelf.c| 8 src/strings.c| 2 +- tests/backtrace.c| 2 +- tests/elfstrmerge.c | 3 ++- 38 files changed, 68 insertions(+), 52 deletions(-) diff --git a/backends/aarch64_retval.c b/backends/aarch64_retval.c index 68de307e..1308340b 100644 --- a/backends/aarch64_retval.c +++ b/backends/aarch64_retval.c @@ -292,7 +292,7 @@ aarch64_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) assert (count > 0); if (count <= 4) return pass_hfa (locp, base_size, count); - /* Fall through. */ + FALLTHROUGH; case 1: /* Not a HFA. */ diff --git a/backends/alpha_retval.c b/backends/alpha_retval.c index 53dbfa45..d9bae3bc 100644 --- a/backends/alpha_retval.c +++ b/backends/alpha_retval.c @@ -85,7 +85,7 @@ alpha_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) typedie = dwarf_formref_die (attr, &die_mem); tag = DWARF_TAG_OR_RETURN (typedie); } - /* Fall through. */ + FALLTHROUGH; case DW_TAG_base_type: case DW_TAG_enumeration_type: @@ -131,7 +131,7 @@ alpha_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) } } - /* Else fall through. */ + FALLTHROUGH; case DW_TAG_structure_type: case DW_TAG_class_type: diff --git a/backends/arm_regs.c b/backends/arm_regs.c index 21c5ad3a..a46a4c99 100644 --- a/backends/arm_regs.c +++ b/backends/arm_regs.c @@ -77,7 +77,7 @@ arm_register_info (Ebl *ebl __attribute__ ((unused)), case 16 + 0 ... 16 + 7: regno += 96 - 16; - /* Fall through. */ + FALLTHROUGH; case 96 + 0 ... 96 + 7: *setname = "FPA"; *type = DW_ATE_float; diff --git a/backends/arm_retval.c b/backends/arm_retval.c index 7aced742..1c28f016 100644 --- a/backends/arm_retval.c +++ b/backends/arm_retval.c @@ -82,7 +82,7 @@ arm_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) typedie = dwarf_formref_die (attr, &die_mem); tag = DWARF_TAG_OR_RETURN (typedie); } - /* Fall through. */ + FALLTHROUGH; case DW_TAG_base_type: case DW_TAG_enumeration_type: diff --git a/backends/i386_regs.c b/backends/i386_regs.c index fd963a62..7ec93bb9 100644 --- a/backends/i386_regs.c +++ b/backends/i386_regs.c @@ -92,7 +92,7 @@ i386_register_info (Ebl *ebl __attribute__ ((unused)), case 5: case 8: *type = DW_ATE_address; - /* Fallthrough */ + FALLTHROUGH; case 0 ... 3: case 6 ... 7: name[0] = 'e'; diff --git a/backends/i386_retval.c b/backends/i386_retval.c index 4aa646fe..32fec728 100644 --- a/backends/i386_retval.c +++ b/backends/i386_retval.c @@ -85,7 +85,7 @@ i386_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp) typedie = dwarf_formref_die (attr, &die_mem); tag = DWARF_TAG_OR_RETURN (typedie); } - /* Fall through. */ + FALLTHROUGH; case DW_TAG_base_type: case DW_TAG_enumeration_type: @@ -123,7 +123,7 @@ i386_re