This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Support "nothrow" function attribute




This patch adds support for __attribute__((nothrow)) to indicate that
a function cannot throw a C++ extension.  Following Jason Merrill's
suggestion it is valid for both C and C++ front-ends.  The machinery
for TREE_NOTHROW was already in place making this patch quite small.
This is another step towards simplifying the C++ front end by
replacing nothrow_libfn_p, cfns.gperf and cfns.h with a much cleaner
solution using builtins.

Strangely, function attribute syntax is not fully tested in the
testsuite, so the attached "attribs-1.c" testcase checks that all
of the documented function attributes, not just nothrow, can be
compiled.  I'm not 100% sure where this test belongs,
gcc.c-execute/compile?


Tested by "make bootstrap" and "make -k check" on i686-pc-linux-gnu
with no new regressions.  Ok for mainline?


2001-12-01  Roger Sayle <roger@eyesopen.com>
	* attribs.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.
	* extend.texi: Document the new function attribute.
	* gcc.c-torture/compile/attribs-1.c: New test case.


*** attribs.c.old	Sat Dec  1 20:38:16 2001
--- attribs.c	Sat Dec  1 20:42:48 2001
*************** static tree handle_no_limit_stack_attrib
*** 84,89 ****
--- 84,91 ----
  						     bool *));
  static tree handle_pure_attribute	PARAMS ((tree *, tree, tree, int,
  						 bool *));
+ static tree handle_nothrow_attribute	PARAMS ((tree *, tree, tree, int,
+ 						 bool *));

  /* Table of machine-independent attributes common to all C-like languages.  */
  static const struct attribute_spec c_common_attribute_table[] =
*************** static const struct attribute_spec c_com
*** 139,144 ****
--- 141,148 ----
  			      handle_no_limit_stack_attribute },
    { "pure",                   0, 0, true,  false, false,
  			      handle_pure_attribute },
+   { "nothrow",                0, 0, true,  false, false,
+ 			      handle_nothrow_attribute },
    { NULL,                     0, 0, false, false, false, NULL }
  };

*************** handle_pure_attribute (node, name, args,
*** 1146,1151 ****
--- 1150,1178 ----
  {
    if (TREE_CODE (*node) == FUNCTION_DECL)
      DECL_IS_PURE (*node) = 1;
+   /* ??? TODO: Support types.  */
+   else
+     {
+       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
+       *no_add_attrs = true;
+     }
+
+   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
      {
*** extend.texi.old	Sat Dec  1 20:44:18 2001
--- extend.texi	Sat Dec  1 20:59:40 2001
*************** attributes are currently defined for fun
*** 1932,1940 ****
  @code{noreturn}, @code{noinline}, @code{pure}, @code{const},
  @code{format}, @code{format_arg}, @code{no_instrument_function},
  @code{section}, @code{constructor}, @code{destructor}, @code{used},
! @code{unused}, @code{weak}, @code{malloc}, @code{alias} and
! @code{no_check_memory_usage}.  Several other attributes are defined for
! functions on particular target systems.  Other attributes, including
  @code{section} are supported for variables declarations (@pxref{Variable
  Attributes}) and for types (@pxref{Type Attributes}).

--- 1932,1940 ----
  @code{noreturn}, @code{noinline}, @code{pure}, @code{const},
  @code{format}, @code{format_arg}, @code{no_instrument_function},
  @code{section}, @code{constructor}, @code{destructor}, @code{used},
! @code{unused}, @code{weak}, @code{malloc}, @code{alias}, @code{nothrow}
! and @code{no_check_memory_usage}.  Several other attributes are defined
! for functions on particular target systems.  Other attributes, including
  @code{section} are supported for variables declarations (@pxref{Variable
  Attributes}) and for types (@pxref{Type Attributes}).

*************** declares @samp{f} to be a weak alias for
*** 2226,2231 ****
--- 2226,2240 ----
  mangled name for the target must be used.

  Not all target machines support this attribute.
+
+ @item nothrow
+ @cindex @code{nothrow} function attribute
+ 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.1.

  @item no_check_memory_usage
  @cindex @code{no_check_memory_usage} function attribute
*** /dev/null	Sun Dec  2 21:53:13 2001
--- attribs-1.c	Sun Dec  2 21:52:02 2001
***************
*** 0 ****
--- 1,39 ----
+ /* Copyright (C) 2001  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 bar (void) __attribute__ ((noinline));
+ 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 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-438-3470



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]