From 39f2f3c85a1e9999e3f38bfb5b9bbd35b062df67 Mon Sep 17 00:00:00 2001 From: Roger Sayle Date: Mon, 27 May 2002 21:09:38 +0000 Subject: [PATCH] c-common.c: Add support for __attribute__((nothrow)) to specify that a function cannot... * c-common.c: Add support for __attribute__((nothrow)) to specify that a function cannot throw an exception (using TREE_NOTHROW). (handle_nothrow_attribute): New function to process this attribute. * doc/extend.texi: Document the new nothrow function attribute. 2002-05-27 Richard Henderson * g++.dg/ext/attrib6.C: New test case. From-SVN: r53940 --- gcc/ChangeLog | 8 ++++++++ gcc/c-common.c | 27 +++++++++++++++++++++++++++ gcc/doc/extend.texi | 10 +++++++++- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/g++.dg/ext/attrib6.C | 21 +++++++++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/ext/attrib6.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 6cd3ce63bab..e820f949d4b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2002-05-27 Roger Sayle + + * c-common.c: Add support for __attribute__((nothrow)) to specify + that a function cannot throw an exception (using TREE_NOTHROW). + (handle_nothrow_attribute): New function to process this attribute. + + * doc/extend.texi: Document the new nothrow function attribute. + 2002-05-27 H.J. Lu (hjl@gnu.org) * cppexp.c (num_trim): Use 1UL instead of 1 for long int. diff --git a/gcc/c-common.c b/gcc/c-common.c index 6086b027840..aeb6a2e45f4 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -351,6 +351,8 @@ static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int, bool *)); static tree handle_nonnull_attribute PARAMS ((tree *, tree, tree, int, bool *)); +static tree handle_nothrow_attribute PARAMS ((tree *, tree, tree, int, + bool *)); static tree vector_size_helper PARAMS ((tree, tree)); static void check_function_nonnull PARAMS ((tree, tree)); @@ -425,6 +427,8 @@ const struct attribute_spec c_common_attribute_table[] = handle_visibility_attribute }, { "nonnull", 0, -1, false, true, true, handle_nonnull_attribute }, + { "nothrow", 0, 0, true, false, false, + handle_nothrow_attribute }, { NULL, 0, 0, false, false, false, NULL } }; @@ -5795,6 +5799,29 @@ get_nonnull_operand (arg_num_expr, valp) *valp = TREE_INT_CST_LOW (arg_num_expr); return true; } + +/* Handle a "nothrow" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_nothrow_attribute (node, name, args, flags, no_add_attrs) + tree *node; + tree name; + tree args ATTRIBUTE_UNUSED; + int flags ATTRIBUTE_UNUSED; + bool *no_add_attrs; +{ + if (TREE_CODE (*node) == FUNCTION_DECL) + TREE_NOTHROW (*node) = 1; + /* ??? TODO: Support types. */ + else + { + warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); + *no_add_attrs = true; + } + + return NULL_TREE; +} /* Check for valid arguments being passed to a function. */ void diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 9d1fc7e7ede..87701efca98 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1883,7 +1883,7 @@ attributes when making a declaration. This keyword is followed by an attribute specification inside double parentheses. The following attributes are currently defined for functions on all targets: @code{noreturn}, @code{noinline}, @code{always_inline}, -@code{pure}, @code{const}, +@code{pure}, @code{const}, @code{nothrow}, @code{format}, @code{format_arg}, @code{no_instrument_function}, @code{section}, @code{constructor}, @code{destructor}, @code{used}, @code{unused}, @code{deprecated}, @code{weak}, @code{malloc}, @@ -2007,6 +2007,14 @@ extern const intfn square; This approach does not work in GNU C++ from 2.6.0 on, since the language specifies that the @samp{const} must be attached to the return value. +@cindex @code{nothrow} function attribute +@item nothrow +The @code{nothrow} attribute is used to inform the compiler that a +function cannot throw an exception. For example, most functions in +the standard C library can be guaranteed not to throw an exception +with the notable exceptions of @code{qsort} and @code{bsearch} that +take function pointer arguments. The @code{nothrow} attribute is not +implemented in GCC versions earlier than 3.2. @item format (@var{archetype}, @var{string-index}, @var{first-to-check}) @cindex @code{format} function attribute diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 101b0f07a4f..2a9cf8b81a3 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2002-05-27 Richard Henderson + + * g++.dg/ext/attrib6.C: New test case. + 2002-05-27 Kaveh R. Ghazi * gcc.c-torture/execute/pure-1.c: New test. diff --git a/gcc/testsuite/g++.dg/ext/attrib6.C b/gcc/testsuite/g++.dg/ext/attrib6.C new file mode 100644 index 00000000000..2bdb180d1b4 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/attrib6.C @@ -0,0 +1,21 @@ +// Copyright (C) 2002 Free Software Foundation. +// +// Test that the nothrow attribute is working correctly. +// +// Written by Richard Henderson, 26 May 2002. + +// { dg-do link } +extern void foo() __attribute__((nothrow)); +extern void link_error(); + +int main() +{ + try { + foo(); + } catch (...) { + link_error(); + } +} + +void foo() { } + -- 2.30.2