[PATCH] Support "nothrow" function attribute

Roger Sayle roger@eyesopen.com
Fri May 17 07:16:00 GMT 2002


This is a repost of my patch from December 3rd of last year
http://gcc.gnu.org/ml/gcc-patches/2001-12/msg00296.html to
add support for a "nothrow" attribute that has been updated to
match current CVS.

The use of __attribute__((nothrow)) should simplify header files
that may be read by both gcc and g++ that specify that a function
cannot throw a C++ exception.  This allows better code generation
in C++, without breaking the C parser.  It should also be useful
for "exceptions in C" at some point in the future.

The real motivation however is to allow built-ins to easily be
marked as nothrow.  This is a pre-requisite for a dramatic
simplification of parts of the g++ front end that should also
improve C++ compile-time performance.


Retested on i686-pc-linux-gnu with a complete "make bootstrap" and
"make -k check" (all languages except Ada) with no new testsuite
regressions.

Although the patch itself hasn't been reviewed, it has won
"critical acclaim" from both Joseph Myers and Gabriel Dos Reis:
http://gcc.gnu.org/ml/gcc-patches/2002-02/msg00091.html

Ok for mainline?


2002-05-17  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.324
diff -c -3 -p -r1.324 c-common.c
*** c-common.c	16 May 2002 19:09:36 -0000	1.324
--- c-common.c	17 May 2002 02:56:52 -0000
*************** static tree handle_deprecated_attribute
*** 344,349 ****
--- 344,351 ----
  						 bool *));
  static tree handle_vector_size_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));

  /* Table of machine-independent attributes common to all C-like languages.  */
*************** const struct attribute_spec c_common_att
*** 406,411 ****
--- 408,415 ----
  			      handle_vector_size_attribute },
    { "visibility",	      1, 1, true,  false, false,
  			      handle_visibility_attribute },
+   { "nothrow",                0, 0, true,  false, false,
+ 			      handle_nothrow_attribute },
    { NULL,                     0, 0, false, false, false, NULL }
  };

*************** handle_deprecated_attribute (node, name,
*** 5407,5412 ****
--- 5411,5439 ----
        else
  	warning ("`%s' attribute ignored",
  		      IDENTIFIER_POINTER (name));
+     }
+
+   return NULL_TREE;
+ }
+
+ /* 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;
Index: doc/extend.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/extend.texi,v
retrieving revision 1.70
diff -c -3 -p -r1.70 extend.texi
*** doc/extend.texi	11 May 2002 16:25:04 -0000	1.70
--- doc/extend.texi	17 May 2002 02:56:54 -0000
*************** attributes when making a declaration.  T
*** 1881,1887 ****
  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}, and
--- 1881,1887 ----
  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}, and
*************** extern const intfn square;
*** 2005,2010 ****
--- 2005,2018 ----
  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 a C++ 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,37 ----
+ /* Copyright (C) 2001, 2002  Free Software Foundation.
+
+    Test that all GCC function attributes are correctly parsed.
+
+    Written by Roger Sayle, 1st December 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));      */
+
+ 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