From 1b09cf0d1a46c2d937473aceb25762f115fe0ce5 Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Tue, 28 Nov 2017 14:50:32 -0500
Subject: [PATCH] Configure: Add KWSYS_FALLTHROUGH macro for C++ code

Some C++ compilers now warn when a switch case falls through to another
case, but compilers vary in how to suppress the warning with an explicit
fallthrough specifier.  Provide a macro for the specifier.
---
 CMakeLists.txt    |  1 +
 Configure.hxx.in  | 26 ++++++++++++++++++++++++++
 testConfigure.cxx | 30 ++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 testConfigure.cxx

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 51f40887..64b64840 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1033,6 +1033,7 @@ IF(KWSYS_STANDALONE OR CMake_SOURCE_DIR)
         )
     ENDIF()
     SET(KWSYS_CXX_TESTS ${KWSYS_CXX_TESTS}
+      testConfigure
       testSystemTools
       testCommandLineArguments
       testCommandLineArguments1
diff --git a/Configure.hxx.in b/Configure.hxx.in
index 1c07a4ef..1e67874c 100644
--- a/Configure.hxx.in
+++ b/Configure.hxx.in
@@ -12,6 +12,31 @@
 #define @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H                         \
   @KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H@
 
+#if defined(__SUNPRO_CC) && __SUNPRO_CC > 0x5130 && defined(__has_attribute)
+#define @KWSYS_NAMESPACE@__has_cpp_attribute(x) __has_attribute(x)
+#elif defined(__has_cpp_attribute)
+#define @KWSYS_NAMESPACE@__has_cpp_attribute(x) __has_cpp_attribute(x)
+#else
+#define @KWSYS_NAMESPACE@__has_cpp_attribute(x) 0
+#endif
+
+#ifndef @KWSYS_NAMESPACE@_FALLTHROUGH
+#if __cplusplus >= 201703L && @KWSYS_NAMESPACE@__has_cpp_attribute(fallthrough)
+#define @KWSYS_NAMESPACE@_FALLTHROUGH [[fallthrough]]
+#elif __cplusplus >= 201103L &&                                               \
+  @KWSYS_NAMESPACE@__has_cpp_attribute(gnu::fallthrough)
+#define @KWSYS_NAMESPACE@_FALLTHROUGH [[gnu::fallthrough]]
+#elif __cplusplus >= 201103L &&                                               \
+  @KWSYS_NAMESPACE@__has_cpp_attribute(clang::fallthrough)
+#define @KWSYS_NAMESPACE@_FALLTHROUGH [[clang::fallthrough]]
+#endif
+#endif
+#ifndef @KWSYS_NAMESPACE@_FALLTHROUGH
+#define @KWSYS_NAMESPACE@_FALLTHROUGH static_cast<void>(0)
+#endif
+
+#undef @KWSYS_NAMESPACE@__has_cpp_attribute
+
 /* If building a C++ file in kwsys itself, give the source file
    access to the macros without a configured namespace.  */
 #if defined(KWSYS_NAMESPACE)
@@ -22,6 +47,7 @@
 #define KWSYS_STL_HAS_WSTRING @KWSYS_NAMESPACE@_STL_HAS_WSTRING
 #define KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H                                     \
   @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H
+#define KWSYS_FALLTHROUGH @KWSYS_NAMESPACE@_FALLTHROUGH
 #endif
 
 #endif
diff --git a/testConfigure.cxx b/testConfigure.cxx
new file mode 100644
index 00000000..916dcc13
--- /dev/null
+++ b/testConfigure.cxx
@@ -0,0 +1,30 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
+#include "kwsysPrivate.h"
+#include KWSYS_HEADER(Configure.hxx)
+
+// Work-around CMake dependency scanning limitation.  This must
+// duplicate the above list of headers.
+#if 0
+#include "Configure.hxx.in"
+#endif
+
+static bool testFallthrough(int n)
+{
+  int r = 0;
+  switch (n) {
+    case 1:
+      ++r;
+      KWSYS_FALLTHROUGH;
+    default:
+      ++r;
+  }
+  return r == 2;
+}
+
+int testConfigure(int, char* [])
+{
+  bool res = true;
+  res = testFallthrough(1) && res;
+  return res ? 0 : 1;
+}
-- 
GitLab