[tor-commits] [tor/release-0.3.5] Add a fallthrough macro.

nickm at torproject.org nickm at torproject.org
Wed May 6 20:50:31 UTC 2020


commit 6c3c94357c196681ceda773425e18161da8cfdef
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed May 6 10:24:21 2020 -0400

    Add a fallthrough macro.
    
    This macro defers to __attribute__((fallthrough)) on GCC (and
    clang).  Previously we had been using GCC's magic /* fallthrough */
    comments, but clang very sensibly doesn't accept those.
    
    Since not all compiler recognize it, we only define it when our
    configure script detects that it works.
    
    Part of a fix for 34078.
---
 configure.ac                 | 16 ++++++++++++++++
 src/lib/cc/compat_compiler.h |  6 ++++++
 2 files changed, 22 insertions(+)

diff --git a/configure.ac b/configure.ac
index bc434ed83..a441162f4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -356,6 +356,22 @@ if test "$tor_cv_c_c99_designated_init" != "yes"; then
   AC_MSG_ERROR([Your compiler doesn't support c99 designated initializers. This is required as of Tor 0.2.6.x])
 fi
 
+saved_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -Werror"
+AC_CACHE_CHECK([for __attribute__((fallthrough))],
+      tor_cv_c_attr_fallthrough,
+      [AC_COMPILE_IFELSE(
+         [AC_LANG_PROGRAM([extern int x; void fn(void) ;],
+	       [[ switch (x) { case 1: fn(); __attribute__((fallthrough));
+                               case 2: fn(); break; } ]])],
+	 [tor_cv_c_attr_fallthrough=yes],
+	 [tor_cv_c_attr_fallthrough=no] )])
+CFLAGS="$saved_CFLAGS"
+
+if test "$tor_cv_c_attr_fallthrough" == "yes"; then
+  AC_DEFINE(HAVE_ATTR_FALLTHROUGH, [1], [defined if we have the fallthrough attribute.])
+fi
+
 TORUSER=_tor
 AC_ARG_WITH(tor-user,
         AS_HELP_STRING(--with-tor-user=NAME, [specify username for tor daemon]),
diff --git a/src/lib/cc/compat_compiler.h b/src/lib/cc/compat_compiler.h
index 3a0f30718..fbe6a38f1 100644
--- a/src/lib/cc/compat_compiler.h
+++ b/src/lib/cc/compat_compiler.h
@@ -50,6 +50,12 @@
 #define CHECK_SCANF(formatIdx, firstArg)
 #endif /* defined(__GNUC__) */
 
+#if defined(HAVE_ATTR_FALLTHROUGH)
+#define FALLTHROUGH __attribute__((fallthrough))
+#else
+#define FALLTHROUGH
+#endif
+
 /* What GCC do we have? */
 #ifdef __GNUC__
 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)





More information about the tor-commits mailing list