Patch to allow disabling individual built-in functions

Joseph S. Myers jsm28@cam.ac.uk
Tue Nov 13 15:03:00 GMT 2001


Sometimes users want the facility to disable individual built-in
functions selectively.  This patch implements this with a
-fno-builtin-FUNCTION option that can be applied to any function name
not starting with __builtin_ (e.g., -fno-builtin-printf).

Bootstrapped with no regressions and passed "make info" and "make dvi"
on i686-pc-linux-gnu.  Applied to mainline.

2001-11-18  Joseph S. Myers  <jsm28@cam.ac.uk>

	* c-common.c (struct disabled_builtin, disabled_builtins,
	disable_builtin_function, builtin_function_disabled_p): New.
	(builtin_function_2): Check for disabled built-in functions.
	* c-common.h (disable_builtin_function): Declare.
	* c-decl.c (c_decode_option): Handle -fno-builtin-FUNCTION.
	* doc/invoke.texi: Document -fno-builtin-FUNCTION.
	* doc/extend.texi: Mention -fno-builtin-FUNCTION.

testsuite:
2001-11-18  Joseph S. Myers  <jsm28@cam.ac.uk>

	* gcc.dg/no-builtin-1.c: New test.

diff -rupN gcc.orig/c-common.c gcc/c-common.c
--- gcc.orig/c-common.c	Wed Oct 31 17:46:46 2001
+++ gcc/c-common.c	Sat Nov 17 21:57:34 2001
@@ -2701,6 +2701,53 @@ build_va_arg (expr, type)
 }
 
 
+/* Linked list of disabled built-in functions.  */
+
+typedef struct disabled_builtin
+{
+  const char *name;
+  struct disabled_builtin *next;
+} disabled_builtin;
+static disabled_builtin *disabled_builtins = NULL;
+
+static bool builtin_function_disabled_p PARAMS ((const char *));
+
+/* Disable a built-in function specified by -fno-builtin-NAME.  If NAME
+   begins with "__builtin_", give an error.  */
+
+void
+disable_builtin_function (name)
+     const char *name;
+{
+  if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
+    error ("cannot disable built-in function `%s'", name);
+  else
+    {
+      disabled_builtin *new = xmalloc (sizeof (disabled_builtin));
+      new->name = name;
+      new->next = disabled_builtins;
+      disabled_builtins = new;
+    }
+}
+
+
+/* Return true if the built-in function NAME has been disabled, false
+   otherwise.  */
+
+static bool
+builtin_function_disabled_p (name)
+     const char *name;
+{
+  disabled_builtin *p;
+  for (p = disabled_builtins; p != NULL; p = p->next)
+    {
+      if (strcmp (name, p->name) == 0)
+	return true;
+    }
+  return false;
+}
+
+
 /* Possibly define a builtin function with one or two names.  BUILTIN_NAME
    is an __builtin_-prefixed name; NAME is the ordinary name; one or both
    of these may be NULL (though both being NULL is useless).
@@ -2741,7 +2788,8 @@ builtin_function_2 (builtin_name, name, 
 	  TREE_SIDE_EFFECTS (bdecl) = 1;
 	}
     }
-  if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin))
+  if (name != 0 && !flag_no_builtin && !builtin_function_disabled_p (name)
+      && !(nonansi_p && flag_no_nonansi_builtin))
     {
       decl = builtin_function (name, type, function_code, class, NULL);
       if (nonansi_p)
diff -rupN gcc.orig/c-common.h gcc/c-common.h
--- gcc.orig/c-common.h	Mon Oct 29 22:43:09 2001
+++ gcc/c-common.h	Sun Nov  4 18:07:00 2001
@@ -538,6 +538,8 @@ extern tree c_build_qualified_type      
    frontends.  */
 extern void c_common_nodes_and_builtins		PARAMS ((void));
 
+extern void disable_builtin_function		PARAMS ((const char *));
+
 extern tree build_va_arg			PARAMS ((tree, tree));
 
 extern void c_common_lang_init			PARAMS ((void));
diff -rupN gcc.orig/c-decl.c gcc/c-decl.c
--- gcc.orig/c-decl.c	Sat Nov  3 00:56:09 2001
+++ gcc/c-decl.c	Sun Nov  4 18:09:17 2001
@@ -596,6 +596,8 @@ c_decode_option (argc, argv)
     flag_no_builtin = 0;
   else if (!strcmp (p, "-fno-builtin"))
     flag_no_builtin = 1;
+  else if (!strncmp (p, "-fno-builtin-", strlen ("-fno-builtin-")))
+    disable_builtin_function (p + strlen ("-fno-builtin-"));
   else if (p[0] == '-' && p[1] == 'f' && dump_switch_p (p + 2))
     ;
   else if (!strcmp (p, "-ansi"))
diff -rupN gcc.orig/doc/extend.texi gcc/doc/extend.texi
--- gcc.orig/doc/extend.texi	Sat Nov  3 13:16:37 2001
+++ gcc/doc/extend.texi	Sun Nov  4 17:52:30 2001
@@ -4374,7 +4374,9 @@ The ISO C89 functions @code{abs}, @code{
 @code{strlen}, @code{strncat}, @code{strncmp}, @code{strncpy},
 @code{strpbrk}, @code{strrchr}, @code{strspn}, and @code{strstr} are all
 recognized as built-in functions unless @option{-fno-builtin} is
-specified.  All of these functions have corresponding versions prefixed
+specified (or @option{-fno-builtin-@var{function}} is specified for an
+individual function).  All of these functions have
+corresponding versions prefixed
 with @code{__builtin_}, except that the version for @code{sqrt} is
 called @code{__builtin_fsqrt}.
 
diff -rupN gcc.orig/doc/invoke.texi gcc/doc/invoke.texi
--- gcc.orig/doc/invoke.texi	Sun Nov  4 02:14:55 2001
+++ gcc/doc/invoke.texi	Sun Nov  4 17:51:01 2001
@@ -158,7 +158,7 @@ in the following sections.
 @xref{C Dialect Options,,Options Controlling C Dialect}.
 @gccoptlist{
 -ansi  -std=@var{standard}  -aux-info @var{filename} @gol
--fno-asm  -fno-builtin @gol
+-fno-asm  -fno-builtin -fno-builtin-@var{function} @gol
 -fhosted  -ffreestanding @gol
 -trigraphs  -traditional  -traditional-cpp @gol
 -fallow-single-precision  -fcond-mismatch @gol
@@ -1020,6 +1020,7 @@ switch only affects the @code{asm} and @
 @code{inline} is a standard keyword in ISO C99.
 
 @item -fno-builtin
+@itemx -fno-builtin-@var{function} @r{(C and Objective-C only)}
 @opindex fno-builtin
 @cindex built-in functions
 Don't recognize built-in functions that do not begin with
@@ -1043,6 +1044,20 @@ optimization benefits of built-in functi
 using the @samp{__builtin_} prefix.  The GNU C++ Standard Library uses
 built-in functions to implement many functions (like
 @code{std::strchr}), so that you automatically get efficient code.
+
+With the @option{-fno-builtin-@var{function}} option, not available
+when compiling C++, only the built-in function @var{function} is
+disabled.  @var{function} must not begin with @samp{__builtin_}.  If a
+function is named this is not built-in in this version of GCC, this
+option is ignored.  There is no corresponding
+@option{-fbuiltin-@var{function}} option; if you wish to enable
+built-in functions selectively when using @option{-fno-builtin} or
+@option{-ffreestanding}, you may define macros such as:
+
+@smallexample
+#define abs(n)          __builtin_abs ((n))
+#define strcpy(d, s)    __builtin_strcpy ((d), (s))
+@end smallexample
 
 @item -fhosted
 @opindex fhosted
diff -rupN gcc.orig/testsuite/gcc.dg/no-builtin-1.c gcc/testsuite/gcc.dg/no-builtin-1.c
--- gcc.orig/testsuite/gcc.dg/no-builtin-1.c	Thu Jan  1 00:00:00 1970
+++ gcc/testsuite/gcc.dg/no-builtin-1.c	Sat Nov 17 23:54:53 2001
@@ -0,0 +1,43 @@
+/* Test for -fno-builtin-FUNCTION.  */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk>.  */
+/* { dg-do run } */
+/* { dg-options "-fno-builtin-abs" } */
+
+/* GCC normally handles abs and labs as built-in functions even without
+   optimization.  So test that with -fno-builtin-abs, labs is so handled
+   but abs isn't.  */
+
+int abs_called = 0;
+
+extern int abs (int);
+extern long labs (long);
+extern void abort (void);
+extern void exit (int);
+
+int
+main (void)
+{
+  if (labs (0) != 0)
+    abort ();
+  if (abs (0) != 0)
+    abort ();
+  if (!abs_called)
+    abort ();
+  exit (0);
+}
+
+/* The labs call above should have been optimized, but the abs call
+   shouldn't have been.  */
+
+static int
+abs (int x)
+{ /* { dg-warning "static" "static decl warning" } */
+  abs_called = 1;
+  return (x < 0 ? -1 : x);
+}
+
+static long
+labs (long x)
+{
+  abort ();
+}

-- 
Joseph S. Myers
jsm28@cam.ac.uk



More information about the Gcc-patches mailing list