This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH for __has_cpp_attribute and fallthrough
- From: Marek Polacek <polacek at redhat dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Cc: Jason Merrill <jason at redhat dot com>, Jakub Jelinek <jakub at redhat dot com>
- Date: Mon, 26 Sep 2016 14:23:46 +0200
- Subject: C++ PATCH for __has_cpp_attribute and fallthrough
- Authentication-results: sourceware.org; auth=none
This patch updates __has_cpp_attribute for fallthrough, which is now supported.
The system.h hunk should aid people building trunk GCC with older GCC 7 not
supporting this attribute yet.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2016-09-26 Marek Polacek <polacek@redhat.com>
* c-lex.c (c_common_has_attribute): Handle attribute fallthrough.
* system.h: Use __has_attribute to check whether the fallthrough
attribute is supported.
* g++.dg/cpp1z/feat-cxx1z.C: Test attribute fallthrough.
diff --git gcc/c-family/c-lex.c gcc/c-family/c-lex.c
index 829c18b..5c6496e 100644
--- gcc/c-family/c-lex.c
+++ gcc/c-family/c-lex.c
@@ -350,7 +350,8 @@ c_common_has_attribute (cpp_reader *pfile)
else if (is_attribute_p ("deprecated", attr_name))
result = 201309;
else if (is_attribute_p ("maybe_unused", attr_name)
- || is_attribute_p ("nodiscard", attr_name))
+ || is_attribute_p ("nodiscard", attr_name)
+ || is_attribute_p ("fallthrough", attr_name))
result = 201603;
if (result)
attr_name = NULL_TREE;
diff --git gcc/system.h gcc/system.h
index 8ca71cf..0952e4f 100644
--- gcc/system.h
+++ gcc/system.h
@@ -746,8 +746,12 @@ extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
#define gcc_unreachable() (fancy_abort (__FILE__, __LINE__, __FUNCTION__))
#endif
-#if GCC_VERSION >= 7000
-# define gcc_fallthrough() __attribute__((fallthrough))
+#if GCC_VERSION >= 7000 && defined(__has_attribute)
+# if __has_attribute(fallthrough)
+# define gcc_fallthrough() __attribute__((fallthrough))
+# else
+# define gcc_fallthrough()
+# endif
#else
# define gcc_fallthrough()
#endif
diff --git gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C
index 982572e..71c8c7d 100644
--- gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C
+++ gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C
@@ -370,6 +370,12 @@
# error "__has_cpp_attribute(nodiscard) != 201603"
# endif
+# if ! __has_cpp_attribute(fallthrough)
+# error "__has_cpp_attribute(fallthrough)"
+# elif __has_cpp_attribute(fallthrough) != 201603
+# error "__has_cpp_attribute(fallthrough) != 201603"
+# endif
+
#else
# error "__has_cpp_attribute"
#endif
Marek