static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
+static bool init_target_chars (void);
+
+static unsigned HOST_WIDE_INT target_newline;
+static unsigned HOST_WIDE_INT target_percent;
+static unsigned HOST_WIDE_INT target_c;
+static unsigned HOST_WIDE_INT target_s;
+static char target_percent_c[3];
+static char target_percent_s[3];
+static char target_percent_s_newline[4];
/* Return true if NODE should be considered for inline expansion regardless
of the optimization level. This means whenever a function is invoked with
if (fmt_str == NULL)
return 0;
+ if (!init_target_chars())
+ return 0;
+
/* If the format specifier was "%s\n", call __builtin_puts(arg). */
- if (strcmp (fmt_str, "%s\n") == 0)
+ if (strcmp (fmt_str, target_percent_s_newline) == 0)
{
if (! arglist
|| ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
fn = fn_puts;
}
/* If the format specifier was "%c", call __builtin_putchar(arg). */
- else if (strcmp (fmt_str, "%c") == 0)
+ else if (strcmp (fmt_str, target_percent_c) == 0)
{
if (! arglist
|| TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
else
{
/* We can't handle anything else with % args or %% ... yet. */
- if (strchr (fmt_str, '%'))
+ if (strchr (fmt_str, target_percent))
return 0;
if (arglist)
{
/* If the format specifier was "string\n", call puts("string"). */
size_t len = strlen (fmt_str);
- if (fmt_str[len - 1] == '\n')
+ if ((unsigned char)fmt_str[len - 1] == target_newline)
{
/* Create a NUL-terminated string that's one char shorter
than the original, stripping off the trailing '\n'. */
if (fmt_str == NULL)
return 0;
+ if (!init_target_chars())
+ return 0;
+
/* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
- if (strcmp (fmt_str, "%s") == 0)
+ if (strcmp (fmt_str, target_percent_s) == 0)
{
if (! arglist
|| ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
fn = fn_fputs;
}
/* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
- else if (strcmp (fmt_str, "%c") == 0)
+ else if (strcmp (fmt_str, target_percent_c) == 0)
{
if (! arglist
|| TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
else
{
/* We can't handle anything else with % args or %% ... yet. */
- if (strchr (fmt_str, '%'))
+ if (strchr (fmt_str, target_percent))
return 0;
if (arglist)
if (fmt_str == NULL)
return 0;
+ if (!init_target_chars())
+ return 0;
+
/* If the format doesn't contain % args or %%, use strcpy. */
- if (strchr (fmt_str, '%') == 0)
+ if (strchr (fmt_str, target_percent) == 0)
{
tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
tree exp;
return expand_expr (exp, target, mode, EXPAND_NORMAL);
}
/* If the format is "%s", use strcpy if the result isn't used. */
- else if (strcmp (fmt_str, "%s") == 0)
+ else if (strcmp (fmt_str, target_percent_s) == 0)
{
tree fn, arg, len;
fn = implicit_built_in_decls[BUILT_IN_STRCPY];
call = NULL_TREE;
retval = NULL_TREE;
+ if (!init_target_chars())
+ return 0;
+
/* If the format doesn't contain % args or %%, use strcpy. */
- if (strchr (fmt_str, '%') == NULL)
+ if (strchr (fmt_str, target_percent) == NULL)
{
tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
}
/* If the format is "%s", use strcpy if the result isn't used. */
- else if (fmt_str && strcmp (fmt_str, "%s") == 0)
+ else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
{
tree fn, orig;
fn = implicit_built_in_decls[BUILT_IN_STRCPY];
if (fmt_str == NULL)
return;
+ if (!init_target_chars())
+ return;
+
/* If the format doesn't contain % args or %%, we know its size. */
- if (strchr (fmt_str, '%') == 0)
+ if (strchr (fmt_str, target_percent) == 0)
len = build_int_cstu (size_type_node, strlen (fmt_str));
/* If the format is "%s" and first ... argument is a string literal,
we know it too. */
- else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, "%s") == 0)
+ else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
{
tree arg;
len = NULL_TREE;
+ if (!init_target_chars())
+ return 0;
+
/* Check whether the format is a literal string constant. */
fmt_str = c_getstr (fmt);
if (fmt_str != NULL)
{
/* If the format doesn't contain % args or %%, we know the size. */
- if (strchr (fmt_str, '%') == 0)
+ if (strchr (fmt_str, target_percent) == 0)
{
if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
len = build_int_cstu (size_type_node, strlen (fmt_str));
}
/* If the format is "%s" and first ... argument is a string literal,
we know the size too. */
- else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, "%s") == 0)
+ else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
{
tree arg;
{
if (fmt_str == NULL)
return 0;
- if (strchr (fmt_str, '%') != NULL && strcmp (fmt_str, "%s"))
+ if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
return 0;
}
return 0;
}
+ if (!init_target_chars())
+ return 0;
+
/* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
or if format doesn't contain % chars or is "%s". */
if (! integer_zerop (flag))
fmt_str = c_getstr (fmt);
if (fmt_str == NULL)
return 0;
- if (strchr (fmt_str, '%') != NULL && strcmp (fmt_str, "%s"))
+ if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
return 0;
}
fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
}
- if (strcmp (fmt_str, "%s") == 0 || strchr (fmt_str, '%') == NULL)
+ if (!init_target_chars())
+ return 0;
+
+ if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
{
const char *str;
- if (strcmp (fmt_str, "%s") == 0)
+ if (strcmp (fmt_str, target_percent_s) == 0)
{
if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
return 0;
{
/* If the string was "string\n", call puts("string"). */
size_t len = strlen (str);
- if (str[len - 1] == '\n')
+ if ((unsigned char)str[len - 1] == target_newline)
{
/* Create a NUL-terminated string that's one char shorter
than the original, stripping off the trailing '\n'. */
return 0;
/* If the format specifier was "%s\n", call __builtin_puts(arg). */
- else if (strcmp (fmt_str, "%s\n") == 0)
+ else if (strcmp (fmt_str, target_percent_s_newline) == 0)
{
if (! arglist
|| ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
}
/* If the format specifier was "%c", call __builtin_putchar(arg). */
- else if (strcmp (fmt_str, "%c") == 0)
+ else if (strcmp (fmt_str, target_percent_c) == 0)
{
if (! arglist
|| TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
}
+ if (!init_target_chars())
+ return 0;
+
/* If the format doesn't contain % args or %%, use strcpy. */
- if (strchr (fmt_str, '%') == NULL)
+ if (strchr (fmt_str, target_percent) == NULL)
{
if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
&& arglist)
return 0;
/* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
- else if (strcmp (fmt_str, "%s") == 0)
+ else if (strcmp (fmt_str, target_percent_s) == 0)
{
if (! arglist
|| ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
}
/* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
- else if (strcmp (fmt_str, "%c") == 0)
+ else if (strcmp (fmt_str, target_percent_c) == 0)
{
if (! arglist
|| TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
call = build_function_call_expr (fn, arglist);
return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
}
+
+/* Initialize format string characters in the target charset. */
+
+static bool
+init_target_chars (void)
+{
+ static bool init;
+ if (!init)
+ {
+ target_newline = lang_hooks.to_target_charset ('\n');
+ target_percent = lang_hooks.to_target_charset ('%');
+ target_c = lang_hooks.to_target_charset ('c');
+ target_s = lang_hooks.to_target_charset ('s');
+ if (target_newline == 0 || target_percent == 0 || target_c == 0
+ || target_s == 0)
+ return false;
+
+ target_percent_c[0] = target_percent;
+ target_percent_c[1] = target_c;
+ target_percent_c[2] = '\0';
+
+ target_percent_s[0] = target_percent;
+ target_percent_s[1] = target_s;
+ target_percent_s[2] = '\0';
+
+ target_percent_s_newline[0] = target_percent;
+ target_percent_s_newline[1] = target_s;
+ target_percent_s_newline[2] = target_newline;
+ target_percent_s_newline[3] = '\0';
+
+ init = true;
+ }
+ return true;
+}