diff options
author | jakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-03-14 20:30:24 +0000 |
---|---|---|
committer | jakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-03-14 20:30:24 +0000 |
commit | 750d7b16193e75b13fa0a369305e55cb029f3af5 (patch) | |
tree | 123d933dc08e81d842d0ad90b7fab6a8cced05a9 /gcc/builtins.c | |
parent | 510f22a93fea5dff7077a55c1401b63315e3568c (diff) | |
download | gcc-750d7b16193e75b13fa0a369305e55cb029f3af5.tar.gz |
PR middle-end/47917
* builtins.c (fold_builtin_snprintf): New function.
(fold_builtin_3): Call it for BUILT_IN_SNPRINTF.
(fold_builtin_4): Likewise.
* gcc.c-torture/execute/pr47917.c: New test.
* gcc.dg/pr47917.c: New test.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@170959 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/builtins.c')
-rw-r--r-- | gcc/builtins.c | 130 |
1 files changed, 129 insertions, 1 deletions
diff --git a/gcc/builtins.c b/gcc/builtins.c index 3361264c4c2..a59b23c5040 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -1,6 +1,6 @@ /* Expand builtin functions. Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, - 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 + 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. This file is part of GCC. @@ -191,6 +191,7 @@ static tree fold_builtin_strncat (location_t, tree, tree, tree); static tree fold_builtin_strspn (location_t, tree, tree); static tree fold_builtin_strcspn (location_t, tree, tree); static tree fold_builtin_sprintf (location_t, tree, tree, tree, int); +static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int); static rtx expand_builtin_object_size (tree); static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode, @@ -10596,6 +10597,9 @@ fold_builtin_3 (location_t loc, tree fndecl, case BUILT_IN_SPRINTF: return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore); + case BUILT_IN_SNPRINTF: + return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore); + case BUILT_IN_STRCPY_CHK: case BUILT_IN_STPCPY_CHK: return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE, @@ -10661,6 +10665,9 @@ fold_builtin_4 (location_t loc, tree fndecl, case BUILT_IN_STRNCAT_CHK: return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3); + case BUILT_IN_SNPRINTF: + return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore); + case BUILT_IN_FPRINTF_CHK: case BUILT_IN_VFPRINTF_CHK: if (!validate_arg (arg1, INTEGER_TYPE) @@ -11919,6 +11926,127 @@ fold_builtin_sprintf (location_t loc, tree dest, tree fmt, return call; } +/* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE, + FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't + attempt to simplify calls with more than 4 arguments. + + Return NULL_TREE if no simplification was possible, otherwise return the + simplified form of the call as a tree. If IGNORED is true, it means that + the caller does not use the returned value of the function. */ + +static tree +fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt, + tree orig, int ignored) +{ + tree call, retval; + const char *fmt_str = NULL; + unsigned HOST_WIDE_INT destlen; + + /* Verify the required arguments in the original call. We deal with two + types of snprintf() calls: 'snprintf (str, cst, fmt)' and + 'snprintf (dest, cst, "%s", orig)'. */ + if (!validate_arg (dest, POINTER_TYPE) + || !validate_arg (destsize, INTEGER_TYPE) + || !validate_arg (fmt, POINTER_TYPE)) + return NULL_TREE; + if (orig && !validate_arg (orig, POINTER_TYPE)) + return NULL_TREE; + + if (!host_integerp (destsize, 1)) + return NULL_TREE; + + /* Check whether the format is a literal string constant. */ + fmt_str = c_getstr (fmt); + if (fmt_str == NULL) + return NULL_TREE; + + call = NULL_TREE; + retval = NULL_TREE; + + if (!init_target_chars ()) + return NULL_TREE; + + destlen = tree_low_cst (destsize, 1); + + /* If the format doesn't contain % args or %%, use strcpy. */ + if (strchr (fmt_str, target_percent) == NULL) + { + tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; + size_t len = strlen (fmt_str); + + /* Don't optimize snprintf (buf, 4, "abc", ptr++). */ + if (orig) + return NULL_TREE; + + /* We could expand this as + memcpy (str, fmt, cst - 1); str[cst - 1] = '\0'; + or to + memcpy (str, fmt_with_nul_at_cstm1, cst); + but in the former case that might increase code size + and in the latter case grow .rodata section too much. + So punt for now. */ + if (len >= destlen) + return NULL_TREE; + + if (!fn) + return NULL_TREE; + + /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when + 'format' is known to contain no % formats and + strlen (fmt) < cst. */ + call = build_call_expr_loc (loc, fn, 2, dest, fmt); + + if (!ignored) + retval = build_int_cst (NULL_TREE, strlen (fmt_str)); + } + + /* If the format is "%s", use strcpy if the result isn't used. */ + else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) + { + tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; + unsigned HOST_WIDE_INT origlen; + + /* Don't crash on snprintf (str1, cst, "%s"). */ + if (!orig) + return NULL_TREE; + + retval = c_strlen (orig, 1); + if (!retval || !host_integerp (retval, 1)) + return NULL_TREE; + + origlen = tree_low_cst (retval, 1); + /* We could expand this as + memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0'; + or to + memcpy (str1, str2_with_nul_at_cstm1, cst); + but in the former case that might increase code size + and in the latter case grow .rodata section too much. + So punt for now. */ + if (origlen >= destlen) + return NULL_TREE; + + /* Convert snprintf (str1, cst, "%s", str2) into + strcpy (str1, str2) if strlen (str2) < cst. */ + if (!fn) + return NULL_TREE; + + call = build_call_expr_loc (loc, fn, 2, dest, orig); + + if (ignored) + retval = NULL_TREE; + } + + if (call && retval) + { + retval = fold_convert_loc + (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SNPRINTF])), + retval); + return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval); + } + else + return call; +} + /* Expand a call EXP to __builtin_object_size. */ rtx |