diff mbox

[v4] gcc/c-family/c-cppbuiltin.c: Let buffer enough to print host wide integer value

Message ID 54763E30.5010709@gmail.com
State New
Headers show

Commit Message

Chen Gang Nov. 26, 2014, 8:55 p.m. UTC
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.5i5j@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(-)

Comments

Joseph Myers Nov. 26, 2014, 9:10 p.m. UTC | #1
On Thu, 27 Nov 2014, Chen Gang wrote:

> 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.5i5j@gmail.com>
> 
>         * c-family/c-cppbuiltin.c (builtin_define_with_int_value): Let
>         buffer enough to print host wide integer value.

OK.  Properly implementing the (-9223372036854775807LL-1) and similar 
cases (when the value is the least int, long or long long on the target) 
can be a followup fix.
diff mbox

Patch

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);
 }