This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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-26  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 | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/gcc/c-family/c-cppbuiltin.c b/gcc/c-family/c-cppbuiltin.c
index c571d1b..b1b96fb 100644
--- a/gcc/c-family/c-cppbuiltin.c
+++ b/gcc/c-family/c-cppbuiltin.c
@@ -1366,14 +1366,28 @@ 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, char_type_node)
+	      || wi::fits_to_tree_p (value, short_integer_type_node)
+	      || wi::fits_to_tree_p (value, integer_type_node)
+	      || wi::fits_to_tree_p (value, long_integer_type_node)
+	      || 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, char_type_node)
+	     || wi::fits_to_tree_p (value, short_integer_type_node)
+	     || 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]