[PATCH] Support "nothrow" function attribute

Roger Sayle roger@eyesopen.com
Sun May 26 19:16:00 GMT 2002


> Exceptions are not C++-specific.  Just say "cannot throw an exception".

Many thanks for taking the time to look at this.  I've made this
documentation correction in the revised patch below.

I've updated the patch to apply cleanly to mainline CVS.  I've retested
it on i686-pc-linux-gnu with "make bootstrap" and "make -k check" all
languages except Ada and treelang with no new regressions.  Previous
motivations for this change can be found in both

http://gcc.gnu.org/ml/gcc-patches/2002-05/msg01418.html
http://gcc.gnu.org/ml/gcc-patches/2001-12/msg00296.html


Ok for mainline?



2002-05-26  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.

	* gcc.c-torture/compile/attribs-1.c: New test case.


Index: c-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.331
diff -c -3 -p -r1.331 c-common.c
*** c-common.c	23 May 2002 15:47:55 -0000	1.331
--- c-common.c	26 May 2002 15:17:21 -0000
*************** static tree handle_vector_size_attribute
*** 351,356 ****
--- 351,358 ----
  						  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));
*************** const struct attribute_spec c_common_att
*** 422,427 ****
--- 424,431 ----
  			      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 }
  };

*************** get_nonnull_operand (arg_num_expr, valp)
*** 5788,5793 ****
--- 5792,5820 ----

    *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.  */
Index: doc/extend.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/extend.texi,v
retrieving revision 1.74
diff -c -3 -p -r1.74 extend.texi
*** doc/extend.texi	23 May 2002 21:00:49 -0000	1.74
--- doc/extend.texi	26 May 2002 15:17:22 -0000
*************** attributes when making a declaration.  T
*** 1883,1889 ****
  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{format}, @code{format_arg}, @code{no_instrument_function},
  @code{section}, @code{constructor}, @code{destructor}, @code{used},
  @code{unused}, @code{deprecated}, @code{weak}, @code{malloc},
--- 1883,1889 ----
  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{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},
*************** extern const intfn square;
*** 2007,2012 ****
--- 2007,2020 ----
  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
*** /dev/null	Thu Aug 30 14:30:55 2001
--- gcc.c-torture/compile/attribs-1.c	Thu May 16 20:57:22 2002
***************
*** 0 ****
--- 1,41 ----
+ /* Copyright (C) 2001, 2002  Free Software Foundation.
+
+    Test that all GCC function attributes are correctly parsed.
+
+    Written by Roger Sayle, 12/01/2001.  */
+
+ struct tm;
+ typedef __SIZE_TYPE__ size_t;
+
+ extern void abort (void) __attribute__ ((noreturn));
+ extern int abs (int) __attribute__ ((const));
+ extern size_t strlen (const char *) __attribute__ ((pure));
+ extern void *malloc (size_t) __attribute__ ((malloc));
+ extern double sqrt (double) __attribute__ ((nothrow));
+
+ extern int printf (const char *, ...) __attribute__ ((format (printf, 1, 2)));
+ extern int scanf (const char *, ...) __attribute__ ((format (scanf, 1, 2)));
+ extern size_t strftime (char *, size_t, const char *, const struct tm*)
+                        __attribute__ ((format (strftime, 3, 0)));
+ extern size_t strfmon (char *, size_t, const char *, ...)
+                        __attribute__ ((format (strfmon, 3, 4)));
+
+ extern char *dgettext (const char *, const char *)
+                        __attribute__ ((format_arg (2)));
+
+ extern void foo (void) __attribute__ ((section ("cross")));
+ extern void flarg (void) __attribute__ ((no_instrument_function));
+ extern void grok (void) __attribute__ ((no_check_memory_usage));
+
+ extern void genesis (void) __attribute__ ((constructor));
+ extern void revelation (void) __attribute__ ((destructor));
+
+ /* Not all platforms support "weak" function attributes.  */
+ /* extern void strong (void) __attribute__ ((weak));      */
+
+ /* The "noinline" attribute is for function declarations.  */
+ /* extern void bar (void) __attribute__ ((noinline));      */
+
+ extern void superman (void) __attribute__ ((alias ("clark_kent")));
+
+

Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833



More information about the Gcc-patches mailing list