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]

counting characters in string constants



ISO C doesn't count the trailing zero byte in a string
literal, as it is not a character.

I'll commit after the test on sparc-sun-solaris2.5.1 completes.

-- 
- Geoffrey Keating <geoffk@geoffk.org>

===File ~/patches/cygnus/nortel-stringlen.patch=============
2000-12-01  Geoffrey Keating  <geoffk@redhat.com>

	* c-common.c (combine_strings): When the ISO C standard specifies
 	the maximum length of a string, it doesn't include the trailing
 	zero byte.

2000-12-01  Geoffrey Keating  <geoffk@redhat.com>

	* gcc.dg/20001201-1.c: New testcase.

Index: c-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.195
diff -u -p -r1.195 c-common.c
--- c-common.c	2000/11/27 05:00:05	1.195
+++ c-common.c	2000/12/02 00:22:44
@@ -427,9 +427,9 @@ combine_strings (strings)
   /* Compute the number of elements, for the array type.  */
   nchars = wide_flag ? length / wchar_bytes : length;
 
-  if (pedantic && nchars > nchars_max && c_language == clk_c)
+  if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
     pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support",
-	     nchars, nchars_max, flag_isoc99 ? 99 : 89);
+	     nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
 
   /* Create the array type for the string constant.
      -Wwrite-strings says make the string constant an array of const char
Index: testsuite/gcc.dg/20001201-1.c
===================================================================
RCS file: 20001201-1.c
diff -N 20001201-1.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ 20001201-1.c	Fri Dec  1 16:22:44 2000
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-pedantic" } */
+char *s =
+"0123456789101214161820222426283032343638404244464850525456586062646668707274767880828486889092949698100103106109112115118121124127130133136139142145148151154157160163166169172175178181184187190193196199202205208211214217220223226229232235238241244247250253256259262265268271274277280283286289292295298301304307310313316319322325328331334337340343346349352355358361364367370373376379382385388391394397400403406409412415418421424427430433436439442445448451454457460463466469472475478481484487490493496499502"
+"505x";  /* { dg-bogus "greater than the minimum length" } */
+
+
============================================================

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