diff options
author | Joseph Myers <joseph@codesourcery.com> | 2022-09-29 22:59:22 +0000 |
---|---|---|
committer | Joseph Myers <joseph@codesourcery.com> | 2022-09-29 22:59:22 +0000 |
commit | 3a3516bc4a0a0307cd48dce044a497e95816c8f5 (patch) | |
tree | dc36354d8478aea7275978397c3f4634444c8358 /gcc/c/c-decl.cc | |
parent | 7ea258a13a115e9e73d60f59369d16892ed07435 (diff) | |
download | gcc-3a3516bc4a0a0307cd48dce044a497e95816c8f5.zip gcc-3a3516bc4a0a0307cd48dce044a497e95816c8f5.tar.gz gcc-3a3516bc4a0a0307cd48dce044a497e95816c8f5.tar.bz2 |
c: C2x noreturn attribute
C2x adds a standard [[noreturn]] attribute (which can also be spelt
[[_Noreturn]] for use with <stdnoreturn.h>), so allowing non-returning
functions to be declared in a manner compatible with C++; the
_Noreturn function specifier remains available but is marked
obsolescent.
Implement this attribute. It's more restricted than GNU
__attribute__ ((noreturn)) - that allows function pointers but using
the standard attribute on a function pointer is a constraint
violation. Thus, the attribute gets its own handler that checks for a
FUNCTION_DECL before calling the handler for the GNU attribute. Tests
for the attribute are based on those for C11 _Noreturn and for other
C2x attributes.
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
gcc/c-family/
* c-lex.cc (c_common_has_attribute): Handle noreturn attribute for
C.
gcc/c/
* c-decl.cc (handle_std_noreturn_attribute): New function.
(std_attribute_table): Add _Noreturn and noreturn.
gcc/testsuite/
* gcc.dg/c2x-attr-noreturn-1.c, gcc.dg/c2x-attr-noreturn-2.c,
gcc.dg/c2x-attr-noreturn-3.c: New tests.
* gcc.dg/c2x-has-c-attribute-2.c: Also test __has_c_attribute for
noreturn attribute.
Diffstat (limited to 'gcc/c/c-decl.cc')
-rw-r--r-- | gcc/c/c-decl.cc | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 740982e..bac8e6c 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -4480,11 +4480,34 @@ handle_nodiscard_attribute (tree *node, tree name, tree /*args*/, } return NULL_TREE; } + +/* Handle the standard [[noreturn]] attribute. */ + +static tree +handle_std_noreturn_attribute (tree *node, tree name, tree args, + int flags, bool *no_add_attrs) +{ + /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]] + only applies to functions, not function pointers. */ + if (TREE_CODE (*node) == FUNCTION_DECL) + return handle_noreturn_attribute (node, name, args, flags, no_add_attrs); + else + { + pedwarn (input_location, OPT_Wattributes, + "standard %qE attribute can only be applied to functions", + name); + *no_add_attrs = true; + return NULL_TREE; + } +} + /* Table of supported standard (C2x) attributes. */ const struct attribute_spec std_attribute_table[] = { /* { name, min_len, max_len, decl_req, type_req, fn_type_req, affects_type_identity, handler, exclude } */ + { "_Noreturn", 0, 0, false, false, false, false, + handle_std_noreturn_attribute, NULL }, { "deprecated", 0, 1, false, false, false, false, handle_deprecated_attribute, NULL }, { "fallthrough", 0, 0, false, false, false, false, @@ -4493,6 +4516,8 @@ const struct attribute_spec std_attribute_table[] = handle_unused_attribute, NULL }, { "nodiscard", 0, 1, false, false, false, false, handle_nodiscard_attribute, NULL }, + { "noreturn", 0, 0, false, false, false, false, + handle_std_noreturn_attribute, NULL }, { NULL, 0, 0, false, false, false, false, NULL, NULL } }; |