The original length 18 is not enough for HOST_WIDE_INT printing, need
use 20 instead of.

Also need additional bytes for printing related prefix and suffix, and
give a related check.

It passes testsuite under fedora 20 x86_64-unknown-linux-gnu.

2014-11-27  Chen Gang <gang.chen.5...@gmail.com>

        * c-family/c-cppbuiltin.c (builtin_define_with_int_value): Let
        buffer enough to print host wide integer value.
---
 gcc/c-family/c-cppbuiltin.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/gcc/c-family/c-cppbuiltin.c b/gcc/c-family/c-cppbuiltin.c
index c571d1b..92bc06d 100644
--- a/gcc/c-family/c-cppbuiltin.c
+++ b/gcc/c-family/c-cppbuiltin.c
@@ -1366,14 +1366,22 @@ static void
 builtin_define_with_int_value (const char *macro, HOST_WIDE_INT value)
 {
   char *buf;
-  size_t mlen = strlen (macro);
-  size_t vlen = 18;
-  size_t extra = 2; /* space for = and NUL.  */
-
-  buf = (char *) alloca (mlen + vlen + extra);
-  memcpy (buf, macro, mlen);
-  buf[mlen] = '=';
-  sprintf (buf + mlen + 1, HOST_WIDE_INT_PRINT_DEC, value);
+  size_t vlen = 20; /* maximize value length: -9223372036854775807 */
+  size_t extra = 6; /* space for =, NUL, (, ), and L L. */
+
+  gcc_assert (wi::fits_to_tree_p (value, long_long_integer_type_node));
+
+  buf = (char *) alloca (strlen (macro) + vlen + extra);
+
+  sprintf (buf, "%s=%s" HOST_WIDE_INT_PRINT_DEC "%s%s",
+          macro,
+          value < 0 ? "(" : "",
+          value,
+          wi::fits_to_tree_p (value, integer_type_node)
+          ? ""
+          : wi::fits_to_tree_p (value, long_integer_type_node)
+            ? "L" : "LL",
+          value < 0 ? ")" : "");
 
   cpp_define (parse_in, buf);
 }
-- 
1.9.3

Reply via email to