]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c-common.c: Add support for __attribute__((nothrow)) to specify that a function cannot...
authorRoger Sayle <sayle@gcc.gnu.org>
Mon, 27 May 2002 21:09:38 +0000 (21:09 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Mon, 27 May 2002 21:09:38 +0000 (21:09 +0000)
* 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  <rth@redhat.com>

* g++.dg/ext/attrib6.C: New test case.

From-SVN: r53940

gcc/ChangeLog
gcc/c-common.c
gcc/doc/extend.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/attrib6.C [new file with mode: 0644]

index 6cd3ce63babe20b3f43338f5100cbe16a5ff2a28..e820f949d4b9b38bc3d3a48ad75aa8fa0ef3e6c9 100644 (file)
@@ -1,3 +1,11 @@
+2002-05-27  Roger Sayle  <roger@eyesopen.com>
+
+       * 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.
index 6086b027840c0be7d97054ede5bb2046d5b7e83c..aeb6a2e45f4a94a392de0ac240d59489615a6941 100644 (file)
@@ -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;
+}
 \f
 /* Check for valid arguments being passed to a function.  */
 void
index 9d1fc7e7edec16b07be7499c4955c920bae63498..87701efca98705fb6aa6a163406ebdb75efd0f3c 100644 (file)
@@ -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
index 101b0f07a4f5374484a5ccc9749b191c3bd9f214..2a9cf8b81a3d4f3e0bcb3bd630f70bf5f9167219 100644 (file)
@@ -1,3 +1,7 @@
+2002-05-27  Richard Henderson  <rth@redhat.com>
+
+       * g++.dg/ext/attrib6.C: New test case.
+
 2002-05-27  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 
        * 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 (file)
index 0000000..2bdb180
--- /dev/null
@@ -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() { }
+