[PATCH] Preserve builtin attributes in C/C++

Roger Sayle roger@eyesopen.com
Wed Nov 14 10:53:00 GMT 2001



This patch preserves the "pure" and "const" attributes on GCC's
builtin functions, if an explicit (matching) function prototype
is encountered for the builtin.  It's common for non-GNU system
headers to define standard library functions without explicit
__attribute__((pure)) and __attribute__((const)).

For example, in the following code, the function "strlen" is called
twice with just the previous patch, as the explicit function prototype
strips the pure attribute.  Without the explicit function prototype the
previous patch works as expected.

typedef __SIZE_TYPE__ size_t;
extern size_t strlen (const char *ptr);

size_t
foo (char *ptr)
{
  return strlen(ptr)+strlen(ptr);
}


This patch updates both the C and C++ front-ends so that the attributes
of builtin functions are maintained by the compiler.  This solves the
above problem, but also safeguards against adding inappropriate function
attributes to other builtin functions, potentially leading to
miscompilation problems.

This patch was tested, both with and without the previous patch, by
"make bootstrap" and "make check-gcc" on i686-pc-linux-gnu with no
new regressions.



2001-11-24  Roger Sayle <roger@eyesopen.com>
	* c-decl.c (duplicate_decls): Preserve the pure and const attributes
	of builtin function declared with explicit prototypes.
	* cp/decl.c (duplicate_decls): The same for the C++ front-end.


*** gcc/gcc/c-decl.c	Fri Nov 23 10:56:16 2001
--- patch3/gcc/c-decl.c	Sat Nov 24 10:01:43 2001
*************** duplicate_decls (newdecl, olddecl, diffe
*** 2007,2012 ****
--- 2007,2014 ----
  		 it stays built in.  */
  	      DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
  	      DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
+               TREE_READONLY (newdecl) = TREE_READONLY (olddecl);
+               DECL_IS_PURE (newdecl) = DECL_IS_PURE (olddecl);
  	    }
  	}
        /* Also preserve various other info from the definition.  */
*** gcc/gcc/cp/decl.c	Fri Nov 23 10:56:45 2001
--- patch3/gcc/cp/decl.c	Sat Nov 24 10:03:05 2001
*************** duplicate_decls (newdecl, olddecl)
*** 3706,3711 ****
--- 3706,3713 ----
  	    {
  	      DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
  	      DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
+               TREE_READONLY (newdecl) = TREE_READONLY (olddecl);
+               DECL_IS_PURE (newdecl) = DECL_IS_PURE (olddecl);
  	      /* If we're keeping the built-in definition, keep the rtl,
  		 regardless of declaration matches.  */
  	      SET_DECL_RTL (newdecl, DECL_RTL (olddecl));


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-438-3470




More information about the Gcc-patches mailing list