aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHauke Mehrtens <hauke@hauke-m.de>2020-06-20 18:48:32 +0200
committerHauke Mehrtens <hauke@hauke-m.de>2020-06-22 20:55:02 +0200
commitcc203387cc17783a8b6715dcaf43c324cc415212 (patch)
tree329b9232273b8fe635f145b9542b09d5d3795cda
parentd715a931e24a943aa4694b023cfe99244ef004f3 (diff)
downloadbackports-cc203387cc17783a8b6715dcaf43c324cc415212.tar.gz
backports: Add fallthrough attribute
This adds the fallthrough attribute from Linux commit 294f69e662d1 ("compiler_attributes.h: Add 'fallthrough' pseudo keyword for switch/case use") This also adds the __has_attribute() define for older compilers like GCC < 5. linux/compiler_attributes.h needs to be included, this file was only added in more recent kernel versions. Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
-rw-r--r--backport/backport-include/linux/compiler.h2
-rw-r--r--backport/backport-include/linux/compiler_attributes.h34
2 files changed, 36 insertions, 0 deletions
diff --git a/backport/backport-include/linux/compiler.h b/backport/backport-include/linux/compiler.h
index 53c069df..51a6ec2c 100644
--- a/backport/backport-include/linux/compiler.h
+++ b/backport/backport-include/linux/compiler.h
@@ -2,6 +2,8 @@
#define __BACKPORT_LINUX_COMPILER_H
#include_next <linux/compiler.h>
+#include <linux/compiler_attributes.h>
+
#ifndef __rcu
#define __rcu
#endif
diff --git a/backport/backport-include/linux/compiler_attributes.h b/backport/backport-include/linux/compiler_attributes.h
new file mode 100644
index 00000000..31ddc163
--- /dev/null
+++ b/backport/backport-include/linux/compiler_attributes.h
@@ -0,0 +1,34 @@
+#ifndef _BACKPORTS_LINUX_COMPILER_ATTRIBUTES_H
+#define _BACKPORTS_LINUX_COMPILER_ATTRIBUTES_H 1
+
+#if LINUX_VERSION_IS_GEQ(4,20,0)
+#include_next <linux/compiler_attributes.h>
+#endif
+
+#ifndef __has_attribute
+# define __has_attribute(x) __GCC4_has_attribute_##x
+#endif
+
+#ifndef __GCC4_has_attribute___fallthrough__
+# define __GCC4_has_attribute___fallthrough__ 0
+#endif /* __GCC4_has_attribute___fallthrough__ */
+
+#ifndef fallthrough
+/*
+ * Add the pseudo keyword 'fallthrough' so case statement blocks
+ * must end with any of these keywords:
+ * break;
+ * fallthrough;
+ * goto <label>;
+ * return [expression];
+ *
+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#Statement-Attributes
+ */
+#if __has_attribute(__fallthrough__)
+# define fallthrough __attribute__((__fallthrough__))
+#else
+# define fallthrough do {} while (0) /* fallthrough */
+#endif
+#endif /* fallthrough */
+
+#endif /* _BACKPORTS_LINUX_COMPILER_ATTRIBUTES_H */