1 2007-10-02 Jakub Jelinek <jakub@redhat.com>
3 * decl.c (duplicate_decls): When redeclaring a builtin function,
4 keep the merged decl builtin whenever types match, even if new
5 decl defines a function.
7 * gcc.dg/builtins-85.c: New test.
8 * g++.dg/ext/builtin30.C: New test.
10 --- gcc/cp/decl.c.jj 2007-10-01 22:11:09.000000000 +0200
11 +++ gcc/cp/decl.c 2007-10-02 11:39:46.000000000 +0200
12 @@ -2137,39 +2137,37 @@ duplicate_decls (tree newdecl, tree oldd
13 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
14 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
16 + /* If redeclaring a builtin function, it stays built in. */
17 + if (types_match && DECL_BUILT_IN (olddecl))
19 + DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
20 + DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
21 + /* If we're keeping the built-in definition, keep the rtl,
22 + regardless of declaration matches. */
23 + COPY_DECL_RTL (olddecl, newdecl);
24 + if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
26 + enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
29 + /* If a compatible prototype of these builtin functions
30 + is seen, assume the runtime implements it with the
31 + expected semantics. */
32 + case BUILT_IN_STPCPY:
33 + if (builtin_decl_explicit_p (fncode))
34 + set_builtin_decl_implicit_p (fncode, true);
41 if (new_defines_function)
42 /* If defining a function declared with other language
43 linkage, use the previously declared language linkage. */
44 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
47 - /* If redeclaring a builtin function, and not a definition,
48 - it stays built in. */
49 - if (DECL_BUILT_IN (olddecl))
51 - DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
52 - DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
53 - /* If we're keeping the built-in definition, keep the rtl,
54 - regardless of declaration matches. */
55 - COPY_DECL_RTL (olddecl, newdecl);
56 - if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
58 - enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
61 - /* If a compatible prototype of these builtin functions
62 - is seen, assume the runtime implements it with the
63 - expected semantics. */
64 - case BUILT_IN_STPCPY:
65 - if (builtin_decl_explicit_p (fncode))
66 - set_builtin_decl_implicit_p (fncode, true);
74 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
75 /* Don't clear out the arguments if we're just redeclaring a
77 --- gcc/testsuite/gcc.dg/builtins-85.c.jj 2007-10-02 11:23:51.000000000 +0200
78 +++ gcc/testsuite/gcc.dg/builtins-85.c 2007-10-02 11:24:12.000000000 +0200
80 +/* { dg-do compile } */
81 +/* { dg-options "-O2" } */
83 +typedef __SIZE_TYPE__ size_t;
84 +extern void __chk_fail (void);
85 +extern int snprintf (char *, size_t, const char *, ...);
86 +extern inline __attribute__((gnu_inline, always_inline)) int snprintf (char *a, size_t b, const char *fmt, ...)
88 + if (__builtin_object_size (a, 0) != -1UL && __builtin_object_size (a, 0) < b)
90 + return __builtin_snprintf (a, b, fmt, __builtin_va_arg_pack ());
92 +extern int snprintf (char *, size_t, const char *, ...) __asm ("mysnprintf");
99 + snprintf (buf, 10, "%d%d\n", 10, 10);
103 +/* { dg-final { scan-assembler "mysnprintf" } } */
104 +/* { dg-final { scan-assembler-not "__chk_fail" } } */
105 --- gcc/testsuite/g++.dg/ext/builtin30.C.jj 2007-10-02 11:19:45.000000000 +0200
106 +++ gcc/testsuite/g++.dg/ext/builtin30.C 2007-10-02 11:23:26.000000000 +0200
108 +// { dg-do compile }
109 +// { dg-options "-O2" }
111 +typedef __SIZE_TYPE__ size_t;
113 +extern void __chk_fail (void);
114 +extern int snprintf (char *, size_t, const char *, ...);
115 +extern inline __attribute__((gnu_inline, always_inline)) int snprintf (char *a, size_t b, const char *fmt, ...)
117 + if (__builtin_object_size (a, 0) != -1UL && __builtin_object_size (a, 0) < b)
119 + return __builtin_snprintf (a, b, fmt, __builtin_va_arg_pack ());
121 +extern int snprintf (char *, size_t, const char *, ...) __asm ("mysnprintf");
129 + snprintf (buf, 10, "%d%d\n", 10, 10);
133 +// { dg-final { scan-assembler "mysnprintf" } }
134 +// { dg-final { scan-assembler-not "__chk_fail" } }