[PATCH] Add fallthrough attributes

Joshua Watt jpewhacker@gmail.com
Thu Feb 8 22:57:00 GMT 2018


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 <JPEWhacker@gmail.com>
---
 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 not handled");
diff --git a/libdw/cfi.c b/libdw/cfi.c
index daa845f3..b8f51266 100644
--- a/libdw/cfi.c
+++ b/libdw/cfi.c
@@ -138,6 +138,9 @@ execute_cfi (Dwarf_CFI *cache,
 
 	case DW_CFA_advance_loc1:
 	  operand = *program++;
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* Fallthrough */
 	case DW_CFA_advance_loc + 0 ... DW_CFA_advance_loc + CFI_PRIMARY_MAX:
 	advance_loc:
@@ -301,6 +304,9 @@ execute_cfi (Dwarf_CFI *cache,
 
 	case DW_CFA_restore_extended:
 	  get_uleb128 (operand, program, end);
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* Fallthrough */
 	case DW_CFA_restore + 0 ... DW_CFA_restore + CFI_PRIMARY_MAX:
 
diff --git a/libdw/dwarf_frame_register.c b/libdw/dwarf_frame_register.c
index 37e8e917..5e4d689a 100644
--- a/libdw/dwarf_frame_register.c
+++ b/libdw/dwarf_frame_register.c
@@ -62,6 +62,9 @@ dwarf_frame_register (Dwarf_Frame *fs, int regno, Dwarf_Op *ops_mem,
       /* Use the default rule for registers not yet mentioned in CFI.  */
       if (fs->cache->default_same_value)
 	goto same_value;
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /*FALLTHROUGH*/
     case reg_undefined:
       /* The value is known to be unavailable.  */
diff --git a/libdwfl/dwfl_report_elf.c b/libdwfl/dwfl_report_elf.c
index 6950a37b..b7149bc3 100644
--- a/libdwfl/dwfl_report_elf.c
+++ b/libdwfl/dwfl_report_elf.c
@@ -174,6 +174,9 @@ __libdwfl_elf_address_range (Elf *elf, GElf_Addr base, bool add_p_vaddr,
       /* An assigned base address is meaningless for these.  */
       base = 0;
       add_p_vaddr = true;
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /* Fallthrough. */
     case ET_DYN:
     default:;
diff --git a/libdwfl/frame_unwind.c b/libdwfl/frame_unwind.c
index 4dc9c432..2a67de0d 100644
--- a/libdwfl/frame_unwind.c
+++ b/libdwfl/frame_unwind.c
@@ -442,6 +442,9 @@ expr_eval (Dwfl_Frame *state, Dwarf_Frame *frame, const Dwarf_Op *ops,
 	    }
 	  if (val1 == 0)
 	    break;
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* FALLTHRU */
 	case DW_OP_skip:;
 	  Dwarf_Word offset = op->offset + 1 + 2 + (int16_t) op->number;
diff --git a/libebl/eblobjnote.c b/libebl/eblobjnote.c
index f80a1a57..75525802 100644
--- a/libebl/eblobjnote.c
+++ b/libebl/eblobjnote.c
@@ -223,6 +223,9 @@ ebl_object_note (Ebl *ebl, const char *name, uint32_t type,
 		free (buf);
 	      break;
 	    }
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* FALLTHROUGH */
 
 	default:
diff --git a/libelf/elf32_updatenull.c b/libelf/elf32_updatenull.c
index d83c0b3f..c0265b53 100644
--- a/libelf/elf32_updatenull.c
+++ b/libelf/elf32_updatenull.c
@@ -232,6 +232,9 @@ __elfw2(LIBELFBITS,updatenull_wrlock) (Elf *elf, int *change_bop, size_t shnum)
 		      __libelf_seterrno (ELF_E_GROUP_NOT_REL);
 		      return -1;
 		    }
+#ifdef HAVE_FALLTHROUGH
+		  __attribute__ ((fallthrough));
+#endif
 		  /* FALLTHROUGH */
 		case SHT_SYMTAB_SHNDX:
 		  sh_entsize = elf_typesize (32, ELF_T_WORD, 1);
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index fb3a5b57..3f7be100 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -611,6 +611,9 @@ read_unmmaped_file (int fildes, off_t offset, size_t maxsize, Elf_Cmd cmd,
 			     ? sizeof (Elf32_Ehdr) : sizeof (Elf64_Ehdr)))
 	return file_read_elf (fildes, NULL, mem.header, offset, maxsize, cmd,
 			      parent);
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /* FALLTHROUGH */
 
     default:
@@ -1126,6 +1129,9 @@ elf_begin (int fildes, Elf_Cmd cmd, Elf *ref)
 	  retval = NULL;
 	  break;
 	}
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /* FALLTHROUGH */
 
     case ELF_C_READ:
diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c
index ab13ffb6..ce3f5fc8 100644
--- a/libelf/elf_cntl.c
+++ b/libelf/elf_cntl.c
@@ -62,6 +62,9 @@ elf_cntl (Elf *elf, Elf_Cmd cmd)
 	  result = -1;
 	  break;
 	}
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /* FALLTHROUGH */
 
     case ELF_C_FDDONE:
diff --git a/src/addr2line.c b/src/addr2line.c
index ba414a74..2a10344a 100644
--- a/src/addr2line.c
+++ b/src/addr2line.c
@@ -618,6 +618,9 @@ handle_address (const char *string, Dwfl *dwfl)
 	case 1:
 	  addr = 0;
 	  j = i;
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* Fallthrough */
 	case 2:
 	  if (string[j] != '\0')
diff --git a/src/elfcompress.c b/src/elfcompress.c
index 8e0d5c55..8b159362 100644
--- a/src/elfcompress.c
+++ b/src/elfcompress.c
@@ -149,6 +149,9 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
 		    N_("Only one input file allowed together with '-o'"));
       /* We only use this for checking the number of arguments, we don't
 	 actually want to consume them.  */
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /* Fallthrough */
     default:
       return ARGP_ERR_UNKNOWN;
diff --git a/src/elflint.c b/src/elflint.c
index 51e53c23..1273f9aa 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -1764,6 +1764,9 @@ section [%2d] '%s': entry %zu: pointer does not match address of section [%2d] '
 	  if (dyn->d_tag < DT_ADDRRNGLO || dyn->d_tag > DT_ADDRRNGHI)
 	    /* Value is no pointer.  */
 	    break;
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* FALLTHROUGH */
 
 	case DT_AUXILIARY:
@@ -3993,6 +3996,9 @@ section [%2zu] '%s': merge flag set but entry size is zero\n"),
 	    case SHT_NOBITS:
 	      if (is_debuginfo)
 		break;
+#ifdef HAVE_FALLTHROUGH
+	      __attribute__ ((fallthrough));
+#endif
 	      /* Fallthrough */
 	    default:
 	      ERROR (gettext ("\
@@ -4137,6 +4143,9 @@ section [%2zu] '%s': ELF header says this is the section header string table but
 	    ERROR (gettext ("\
 section [%2zu] '%s': relocatable files cannot have dynamic symbol tables\n"),
 		   cnt, section_name (ebl, cnt));
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* FALLTHROUGH */
 	case SHT_SYMTAB:
 	  check_symtab (ebl, ehdr, shdr, cnt);
@@ -4336,6 +4345,9 @@ section [%2d] '%s': unknown core file note type %" PRIu32
 	    if (nhdr.n_namesz == sizeof "Linux"
 		&& !memcmp (data->d_buf + name_offset, "Linux", sizeof "Linux"))
 	      break;
+#ifdef HAVE_FALLTHROUGH
+	    __attribute__ ((fallthrough));
+#endif
 	    /* Fallthrough */
 	  default:
 	    if (shndx == 0)
diff --git a/src/objdump.c b/src/objdump.c
index 860cfac6..bd651cf2 100644
--- a/src/objdump.c
+++ b/src/objdump.c
@@ -223,6 +223,9 @@ parse_opt (int key, char *arg,
 	}
       /* We only use this for checking the number of arguments, we don't
 	 actually want to consume them.  */
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /* Fallthrough */
     default:
       return ARGP_ERR_UNKNOWN;
diff --git a/src/readelf.c b/src/readelf.c
index 6c49d305..ead6f628 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -6081,6 +6081,9 @@ attr_callback (Dwarf_Attribute *attrp, void *arg)
 			dwarf_form_name (form), (uintmax_t) num);
 	      return DWARF_CB_OK;
 	    }
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* else fallthrough */
 
 	/* These cases always take a loclistptr and no constant. */
diff --git a/src/strings.c b/src/strings.c
index d214356c..ff6e8824 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -246,6 +246,9 @@ parse_opt (int key, char *arg,
 	case 'b':
 	case 'B':
 	  big_endian = true;
+#ifdef HAVE_FALLTHROUGH
+	  __attribute__ ((fallthrough));
+#endif
 	  /* FALLTHROUGH */
 
 	case 'l':
diff --git a/tests/backtrace.c b/tests/backtrace.c
index 21abe8af..f19fd02d 100644
--- a/tests/backtrace.c
+++ b/tests/backtrace.c
@@ -127,6 +127,9 @@ callback_verify (pid_t tid, unsigned frameno, Dwarf_Addr pc,
 	  assert (symname2 == NULL || strcmp (symname2, "jmp") != 0);
 	  break;
 	}
+#ifdef HAVE_FALLTHROUGH
+      __attribute__ ((fallthrough));
+#endif
       /* FALLTHRU */
     case 4:
       /* Some simple frame unwinders get this wrong and think sigusr2
diff --git a/tests/elfstrmerge.c b/tests/elfstrmerge.c
index 6924d0e9..4d471ddd 100644
--- a/tests/elfstrmerge.c
+++ b/tests/elfstrmerge.c
@@ -578,6 +578,9 @@ main (int argc, char **argv)
 	      break;
 
 	    case SHT_DYNAMIC:
+#ifdef HAVE_FALLTHROUGH
+	      __attribute__ ((fallthrough));
+#endif
 	      /* Fallthrough.  There are string indexes in here, but
 		 they (should) point to a allocated string table,
 		 which we don't alter.  */
-- 
2.14.3



More information about the Elfutils-devel mailing list