summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-06-27 09:58:44 (GMT)
committerGitHub <noreply@github.com>2024-06-27 09:58:44 (GMT)
commit12af8ec864225248c3d2916cb142a5e7ee36cbe2 (patch)
treed29a5c87668a1cd1e67ceee0b9074ce7f2c74392 /Include
parent4999e0bda091826fcdf303dd439364e1d303a5ce (diff)
downloadcpython-12af8ec864225248c3d2916cb142a5e7ee36cbe2.zip
cpython-12af8ec864225248c3d2916cb142a5e7ee36cbe2.tar.gz
cpython-12af8ec864225248c3d2916cb142a5e7ee36cbe2.tar.bz2
gh-121040: Use __attribute__((fallthrough)) (#121044)
Fix warnings when using -Wimplicit-fallthrough compiler flag. Annotate explicitly "fall through" switch cases with a new _Py_FALLTHROUGH macro which uses __attribute__((fallthrough)) if available. Replace "fall through" comments with _Py_FALLTHROUGH. Add _Py__has_attribute() macro. No longer define __has_attribute() macro if it's not defined. Move also _Py__has_builtin() at the top of pyport.h. Co-Authored-By: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Include')
-rw-r--r--Include/exports.h5
-rw-r--r--Include/pyport.h45
2 files changed, 36 insertions, 14 deletions
diff --git a/Include/exports.h b/Include/exports.h
index ce60121..0c646d5 100644
--- a/Include/exports.h
+++ b/Include/exports.h
@@ -41,11 +41,8 @@
* we may still need to support gcc >= 4, as some Ubuntu LTS and Centos versions
* have 4 < gcc < 5.
*/
- #ifndef __has_attribute
- #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
- #endif
#if (defined(__GNUC__) && (__GNUC__ >= 4)) ||\
- (defined(__clang__) && __has_attribute(visibility))
+ (defined(__clang__) && _Py__has_attribute(visibility))
#define Py_IMPORTED_SYMBOL __attribute__ ((visibility ("default")))
#define Py_EXPORTED_SYMBOL __attribute__ ((visibility ("default")))
#define Py_LOCAL_SYMBOL __attribute__ ((visibility ("hidden")))
diff --git a/Include/pyport.h b/Include/pyport.h
index 1f7a9b4..2b6bd4c 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -9,6 +9,24 @@
#endif
+// Preprocessor check for a builtin preprocessor function. Always return 0
+// if __has_builtin() macro is not defined.
+//
+// __has_builtin() is available on clang and GCC 10.
+#ifdef __has_builtin
+# define _Py__has_builtin(x) __has_builtin(x)
+#else
+# define _Py__has_builtin(x) 0
+#endif
+
+// Preprocessor check for a compiler __attribute__. Always return 0
+// if __has_attribute() macro is not defined.
+#ifdef __has_attribute
+# define _Py__has_attribute(x) __has_attribute(x)
+#else
+# define _Py__has_attribute(x) 0
+#endif
+
// Macro to use C++ static_cast<> in the Python C API.
#ifdef __cplusplus
# define _Py_STATIC_CAST(type, expr) static_cast<type>(expr)
@@ -532,16 +550,6 @@ extern "C" {
#endif
-// Preprocessor check for a builtin preprocessor function. Always return 0
-// if __has_builtin() macro is not defined.
-//
-// __has_builtin() is available on clang and GCC 10.
-#ifdef __has_builtin
-# define _Py__has_builtin(x) __has_builtin(x)
-#else
-# define _Py__has_builtin(x) 0
-#endif
-
// _Py_TYPEOF(expr) gets the type of an expression.
//
// Example: _Py_TYPEOF(x) x_copy = (x);
@@ -607,4 +615,21 @@ extern "C" {
# define _SGI_MP_SOURCE
#endif
+// Explicit fallthrough in switch case to avoid warnings
+// with compiler flag -Wimplicit-fallthrough.
+//
+// Usage example:
+//
+// switch (value) {
+// case 1: _Py_FALLTHROUGH;
+// case 2: code; break;
+// }
+//
+// __attribute__((fallthrough)) was introduced in GCC 7.
+#if _Py__has_attribute(fallthrough)
+# define _Py_FALLTHROUGH __attribute__((fallthrough))
+#else
+# define _Py_FALLTHROUGH do { } while (0)
+#endif
+
#endif /* Py_PYPORT_H */