diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 73e5f209182a3080a41987a587cac4af67b67eb1..8bfc5ce1b2e6b550bc920ffa5ad9000fee7d32e7 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2012-09-05  Jakub Jelinek  <jakub@redhat.com>
+
+	PR middle-end/54486
+	* builtins.c (fold_builtin_strspn, fold_builtin_strcspn): Use
+	build_int_cst with size_type_node instead of size_int.
+
 2012-09-05  Uros Bizjak  <ubizjak@gmail.com>
 
 	* config/i386/sse.md (<sse4_1>_blendv<ssemodesuffix><avxsizesuffix>):
diff --git a/gcc/builtins.c b/gcc/builtins.c
index 4b177c48e65e9c09261ce152a8ed7e02294e4d37..26f5d4812035e362f7812e23df49552da0077af7 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -11890,7 +11890,7 @@ fold_builtin_strspn (location_t loc, tree s1, tree s2)
       if (p1 && p2)
 	{
 	  const size_t r = strspn (p1, p2);
-	  return size_int (r);
+	  return build_int_cst (size_type_node, r);
 	}
 
       /* If either argument is "", return NULL_TREE.  */
@@ -11935,7 +11935,7 @@ fold_builtin_strcspn (location_t loc, tree s1, tree s2)
       if (p1 && p2)
 	{
 	  const size_t r = strcspn (p1, p2);
-	  return size_int (r);
+	  return build_int_cst (size_type_node, r);
 	}
 
       /* If the first argument is "", return NULL_TREE.  */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4598e6695134a97a06a5e1d955cb89332ca3052f..c33fddce09707d9257f0dd59feaed5143a45d75e 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2012-09-05  Jakub Jelinek  <jakub@redhat.com>
+
+	PR middle-end/54486
+	* c-c++-common/pr54486.c: New test.
+
 2012-09-05  Dominique Dhumieres  <dominiq@lps.ens.fr>
 
 	PR fortran/54474
diff --git a/gcc/testsuite/c-c++-common/pr54486.c b/gcc/testsuite/c-c++-common/pr54486.c
new file mode 100644
index 0000000000000000000000000000000000000000..e8125fcf398eac2720981685ec009629d9b2a0ef
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr54486.c
@@ -0,0 +1,32 @@
+/* PR middle-end/54486 */
+/* { dg-do compile } */
+/* { dg-options "-Wformat" } */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+typedef __SIZE_TYPE__ size_t;
+extern int printf (const char *, ...);
+extern size_t strspn (const char *, const char *);
+extern size_t strcspn (const char *, const char *);
+extern size_t strlen (const char *);
+#ifdef __cplusplus
+}
+#endif
+
+void
+foo (void)
+{
+  printf ("%zu\n", strspn ("abc", "abcdefg"));
+  printf ("%zu\n", (size_t) strspn ("abc", "abcdefg"));
+  printf ("%zu\n", __builtin_strspn ("abc", "abcdefg"));
+  printf ("%zu\n", (size_t) __builtin_strspn ("abc", "abcdefg"));
+  printf ("%zu\n", strcspn ("abc", "abcdefg"));
+  printf ("%zu\n", (size_t) strcspn ("abc", "abcdefg"));
+  printf ("%zu\n", __builtin_strcspn ("abc", "abcdefg"));
+  printf ("%zu\n", (size_t) __builtin_strcspn ("abc", "abcdefg"));
+  printf ("%zu\n", strlen ("abc"));
+  printf ("%zu\n", (size_t) strlen ("abc"));
+  printf ("%zu\n", __builtin_strlen ("abc"));
+  printf ("%zu\n", (size_t) __builtin_strlen ("abc"));
+}