]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
builtin-types.def (BT_FN_STRING_CONST_STRING): New builtin type.
authorRoger Sayle <roger@eyesopen.com>
Mon, 14 Apr 2003 02:55:31 +0000 (02:55 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Mon, 14 Apr 2003 02:55:31 +0000 (02:55 +0000)
* builtin-types.def (BT_FN_STRING_CONST_STRING): New builtin type.
(BT_FN_PTR_SIZE_SIZE): Likewise.
* builtins.def (BUILT_IN_MALLOC, BUILT_IN_CALLOC, BUILT_IN_STRDUP):
New built-in functions for malloc, calloc and strdup respectively.
* calls.c (special_function_p): No need to handle malloc-like
functions any longer.  ECF_MALLOC is set via built-in attributes.

* c-decl.c (duplicate_decls): Preserve pure and malloc attributes.
* cp/decl.c (duplicate_decls): Preserve pure and malloc attributes.
* f/com.c (duplicate_decls): Preserve pure and malloc attributes.

* doc/extend.texi: Document these new built-in functions.

* gcc.dg/builtins-13.c: New test case.
* gcc.dg/builtins-14.c: New test case.

From-SVN: r65560

13 files changed:
gcc/ChangeLog
gcc/builtin-types.def
gcc/builtins.def
gcc/c-decl.c
gcc/calls.c
gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/doc/extend.texi
gcc/f/ChangeLog
gcc/f/com.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/builtins-12.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/builtins-13.c [new file with mode: 0644]

index 25c09aa5530828b6cd33fee19bac04e63dce4677..6f2dae16d870f447b0465efef2e173574bc0e07c 100644 (file)
@@ -1,3 +1,16 @@
+2003-04-13  Roger Sayle  <roger@eyesopen.com>
+
+       * builtin-types.def (BT_FN_STRING_CONST_STRING): New builtin type.
+       (BT_FN_PTR_SIZE_SIZE): Likewise.
+       * builtins.def (BUILT_IN_MALLOC, BUILT_IN_CALLOC, BUILT_IN_STRDUP):
+       New built-in functions for malloc, calloc and strdup respectively.
+       * calls.c (special_function_p): No need to handle malloc-like
+       functions any longer.  ECF_MALLOC is set via built-in attributes.
+
+       * c-decl.c (duplicate_decls): Preserve pure and malloc attributes.
+
+       * doc/extend.texi: Document these new built-in functions.
+
 2003-04-13  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 
        * builtins.c (expand_builtin_memcpy): Add `endp' argument, use it.
index c095d07b609f8a14355378ad93addaf6ca987aa6..a9db4a32be535ce75ab1f1055f85bd4b1f5b9c71 100644 (file)
@@ -124,6 +124,7 @@ DEF_FUNCTION_TYPE_1 (BT_FN_FLOAT_CONST_STRING, BT_FLOAT, BT_CONST_STRING)
 DEF_FUNCTION_TYPE_1 (BT_FN_DOUBLE_CONST_STRING, BT_DOUBLE, BT_CONST_STRING)
 DEF_FUNCTION_TYPE_1 (BT_FN_LONG_DOUBLE_CONST_STRING,
                     BT_LONG_DOUBLE, BT_CONST_STRING)
+DEF_FUNCTION_TYPE_1 (BT_FN_STRING_CONST_STRING, BT_STRING, BT_CONST_STRING)
 
 DEF_FUNCTION_TYPE_2 (BT_FN_VOID_PTR_INT, BT_VOID, BT_PTR, BT_INT)
 DEF_FUNCTION_TYPE_2 (BT_FN_STRING_STRING_CONST_STRING, 
@@ -158,6 +159,8 @@ DEF_FUNCTION_TYPE_2 (BT_FN_LONG_DOUBLE_LONG_DOUBLE_LONG_DOUBLE,
                     BT_LONG_DOUBLE, BT_LONG_DOUBLE, BT_LONG_DOUBLE)
 DEF_FUNCTION_TYPE_2 (BT_FN_INT_CONST_STRING_VALIST_ARG,
                     BT_INT, BT_CONST_STRING, BT_VALIST_ARG)
+DEF_FUNCTION_TYPE_2 (BT_FN_PTR_SIZE_SIZE,
+                    BT_PTR, BT_SIZE, BT_SIZE)
 
 DEF_FUNCTION_TYPE_3 (BT_FN_STRING_STRING_CONST_STRING_SIZE,
                     BT_STRING, BT_STRING, BT_CONST_STRING, BT_SIZE)
index 1d456e9f2012a1854768870691cf11568fd788dd..f0d118b0dc3e57915877104387cfdc1c70d54ffe 100644 (file)
@@ -936,3 +936,19 @@ DEF_C99_BUILTIN(BUILT_IN__EXIT2,
                BT_FN_VOID_INT,
                ATTR_NORETURN_NOTHROW_LIST)
 
+/* Declare malloc, calloc and strdup.  */
+DEF_LIB_BUILTIN(BUILT_IN_MALLOC,
+               "__builtin_malloc",
+               BT_FN_PTR_SIZE,
+               ATTR_MALLOC_NOTHROW_LIST)
+
+DEF_LIB_BUILTIN(BUILT_IN_CALLOC,
+               "__builtin_calloc",
+               BT_FN_PTR_SIZE_SIZE,
+               ATTR_MALLOC_NOTHROW_LIST)
+
+DEF_EXT_LIB_BUILTIN(BUILT_IN_STRDUP,
+                   "__builtin_strdup",
+                   BT_FN_STRING_CONST_STRING,
+                   ATTR_MALLOC_NOTHROW_LIST)
+
index 4b2e6e13c8aaba07c430dfea3932c5da796139e0..3fc1808fe67d8a18a610cfe767d1b4b761cd47f7 100644 (file)
@@ -1391,6 +1391,8 @@ duplicate_decls (newdecl, olddecl, different_binding_level)
          DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
          DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
            |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
+         DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
+         DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
        }
     }
   /* If cannot merge, then use the new type and qualifiers,
index ffb8a21f021cf7796d0da8371695395838a1cb0c..1dc5fc5cc75c0b335e9350c2b6618dbc1d91f628 100644 (file)
@@ -665,9 +665,6 @@ emit_call_1 (funexp, fndecl, funtype, stack_size, rounded_stack_size,
 
    Similarly set LONGJMP for if the function is in the longjmp family.
 
-   Set MALLOC for any of the standard memory allocation functions which
-   allocate from the heap.
-
    Set MAY_BE_ALLOCA for any memory allocation function that might allocate
    space from the stack such as alloca.  */
 
@@ -747,19 +744,6 @@ special_function_p (fndecl, flags)
                       || ((tname[5] == 'p' || tname[5] == 'e')
                           && tname[6] == '\0'))))
        flags |= ECF_FORK_OR_EXEC;
-
-      /* Do not add any more malloc-like functions to this list,
-         instead mark them as malloc functions using the malloc attribute.
-         Note, realloc is not suitable for attribute malloc since
-         it may return the same address across multiple calls.
-         C++ operator new is not suitable because it is not required
-         to return a unique pointer; indeed, the standard placement new
-        just returns its argument.  */
-      else if (TYPE_MODE (TREE_TYPE (TREE_TYPE (fndecl))) == Pmode
-              && (! strcmp (tname, "malloc")
-                  || ! strcmp (tname, "calloc")
-                  || ! strcmp (tname, "strdup")))
-       flags |= ECF_MALLOC;
     }
   return flags;
 }
index b520d2c1bb292826c32b84b5f88f46b5728d5c81..c6cec1fd78065051d87a2d6ad864ca6ec615b8d4 100644 (file)
@@ -1,3 +1,7 @@
+2003-04-13  Roger Sayle  <roger@eyesopen.com>
+
+       * decl.c (duplicate_decls): Preserve pure and malloc attributes.
+
 2003-04-12  Mark Mitchell  <mark@codesourcery.com>
 
        PR c++/10300
index f5ee1a36c29801ea639798ff0234df410f44973d..33881d94f5fa7c253df83e34680b2addb2a3813f 100644 (file)
@@ -3359,8 +3359,9 @@ duplicate_decls (tree newdecl, tree olddecl)
        {
          DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
            |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
-         DECL_NO_LIMIT_STACK (newdecl)
-           |= DECL_NO_LIMIT_STACK (olddecl);
+         DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
+         DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
+         DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
          /* Keep the old RTL.  */
          COPY_DECL_RTL (olddecl, newdecl);
        }
index b61a718b28787f33f45de7556171ed3f81779d47..f5a2c110fbcf2153b6d1b1ac739898709459b9a3 100644 (file)
@@ -4554,6 +4554,7 @@ v4si f (v4si a, v4si b, v4si c)
 @findex atan2l
 @findex bcmp
 @findex bzero
+@findex calloc
 @findex ceil
 @findex ceilf
 @findex ceill
@@ -4596,6 +4597,7 @@ v4si f (v4si a, v4si b, v4si c)
 @findex log
 @findex logf
 @findex logl
+@findex malloc
 @findex memcmp
 @findex memcpy
 @findex mempcpy
@@ -4630,6 +4632,7 @@ v4si f (v4si a, v4si b, v4si c)
 @findex strcmp
 @findex strcpy
 @findex strcspn
+@findex strdup
 @findex strlen
 @findex strncat
 @findex strncmp
@@ -4670,7 +4673,8 @@ Outside strict ISO C mode (@option{-ansi}, @option{-std=c89} or
 @option{-std=c99}), the functions @code{alloca}, @code{bcmp},
 @code{bzero}, @code{_exit}, @code{ffs}, @code{fprintf_unlocked},
 @code{fputs_unlocked}, @code{index}, @code{mempcpy}, @code{printf_unlocked},
-@code{rindex}, and @code{stpcpy} may be handled as built-in functions.
+@code{rindex}, @code{stpcpy} and @code{strdup} 
+may be handled as built-in functions.
 All these functions have corresponding versions
 prefixed with @code{__builtin_}, which may be used even in strict C89
 mode.
@@ -4695,10 +4699,10 @@ that are recognized in any mode since ISO C90 reserves these names for
 the purpose to which ISO C99 puts them.  All these functions have
 corresponding versions prefixed with @code{__builtin_}.
 
-The ISO C90 functions @code{abort}, @code{abs}, @code{atan2}, @code{ceil},
-@code{cos}, @code{exit},
+The ISO C90 functions @code{abort}, @code{abs}, @code{atan2},
+@code{calloc}, @code{ceil}, @code{cos}, @code{exit},
 @code{exp}, @code{fabs}, @code{floor}, @code{fmod},
-@code{fprintf}, @code{fputs}, @code{labs}, @code{log},
+@code{fprintf}, @code{fputs}, @code{labs}, @code{log}, @code{malloc},
 @code{memcmp}, @code{memcpy}, @code{memset}, @code{pow}, @code{printf},
 @code{putchar}, @code{puts}, @code{scanf}, @code{sin}, @code{snprintf},
 @code{sprintf}, @code{sqrt}, @code{sscanf},
index fc712ff07013b078eef37a934d550ab178499952..47240a4c654f884690ada5cb3d79656e70681795 100644 (file)
@@ -1,3 +1,7 @@
+2003-04-13  Roger Sayle  <roger@eyesopen.com>
+
+       * com.c (duplicate_decls): Preserve pure and malloc attributes.
+
 2003-04-12  Zack Weinberg  <zack@codesourcery.com>
 
        * com.c (ffecom_build_complex_constant_, ffecom_expr_)
index 1ccaeb20dcee1caefca1ccb6023e257ff4a9bf33..6c606444ac50b7d0d7225dfef145bd56fa7e3628 100644 (file)
@@ -13335,6 +13335,8 @@ duplicate_decls (tree newdecl, tree olddecl)
        {
          DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
          DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
+         DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
+         DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
        }
     }
   /* If cannot merge, then use the new type and qualifiers,
index 3b8b5c51251e73163fd066817c738e25616c22f3..8bded5bed570cb47a043a225e917bc26b868655d 100644 (file)
@@ -1,3 +1,8 @@
+2003-04-13  Roger Sayle  <roger@eyesopen.com>
+
+       * gcc.dg/builtins-12.c: New test case.
+       * gcc.dg/builtins-13.c: New test case.
+
 2003-04-13  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 
        * gcc.c-torture/execute/string-opt-18.c: New test.
diff --git a/gcc/testsuite/gcc.dg/builtins-12.c b/gcc/testsuite/gcc.dg/builtins-12.c
new file mode 100644 (file)
index 0000000..22ba8ce
--- /dev/null
@@ -0,0 +1,27 @@
+/* Copyright (C) 2003  Free Software Foundation.
+
+   Verify that all the malloc-like __builtin_ allocation functions are
+   recognized by the compiler.
+
+   Written by Roger Sayle, 12th April 2003.  */
+
+/* { dg-do compile } */
+/* { dg-final { scan-assembler-not "__builtin_" } } */
+
+typedef __SIZE_TYPE__ size_t;
+
+void *test1(size_t n)
+{
+  return __builtin_malloc(n);
+}
+
+void *test2(size_t n, size_t s)
+{
+  return __builtin_calloc(n,s);
+}
+
+char *test3(const char *ptr)
+{
+  return __builtin_strdup(ptr);
+}
+
diff --git a/gcc/testsuite/gcc.dg/builtins-13.c b/gcc/testsuite/gcc.dg/builtins-13.c
new file mode 100644 (file)
index 0000000..befcd34
--- /dev/null
@@ -0,0 +1,61 @@
+/* Copyright (C) 2003  Free Software Foundation.
+
+   Verify that the malloc-like __builtin_ allocation functions are
+   correctly aliased by the compiler.
+
+   Written by Roger Sayle, 12th April 2003.  */
+
+/* { dg-do link } */
+
+typedef __SIZE_TYPE__ size_t;
+
+extern void abort (void);
+extern void *malloc (size_t);
+extern void *calloc (size_t,size_t);
+
+extern void link_error (void);
+
+static int x;
+
+void test1(void)
+{
+  int *ptr1, *ptr2;
+
+  ptr1 = &x;
+  ptr2 = (int*) malloc (sizeof (int));
+
+  *ptr1 = 12;
+  *ptr2 = 8;
+
+  if (*ptr1 != 12)
+    link_error();
+}
+
+void test2(void)
+{
+  int *ptr1, *ptr2;
+
+  ptr1 = &x;
+  ptr2 = (int*) calloc (1, sizeof (int));
+
+  *ptr1 = 12;
+  *ptr2 = 8;
+
+  if (*ptr1 != 12)
+    link_error ();
+}
+
+int main()
+{
+  test1 ();
+  test2 ();
+  return 0;
+}
+
+#ifndef __OPTIMIZE__
+void link_error (void)
+{
+  abort ();
+}
+#endif
+