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]

strftime format checking patch


This patch adjusts the warning for strftime %Ey to be the 'some
locales' one, since in general %Ey is an era-offset rather than a
2-digit year (but some locales may ignore %E).  It also adds more
strftime format checking tests.

With this patch applied, the format checking and its testcases should
be feature-complete for C standard and X/Open non-wide-string formats,
except for intmax_t support, but not yet with testcases for GNU
extensions, and with some suboptimal warning messages (e.g., referring
to `hh' length modifier as `H').

Bootstrapped with no regressions on i686-pc-linux-gnu.  With this patch
applied, the tests it includes all pass.  OK to commit?

gcc/ChangeLog:
2000-08-22  Joseph S. Myers  <jsm28@cam.ac.uk>

	* c-common.c (check_format_info): Give the 'some locales' warning
	for strftime %Ey rather than the unconditional 'only last 2 digits
	of year' one.

gcc/testsuite/ChangeLog:
2000-08-22  Joseph S. Myers  <jsm28@cam.ac.uk>

	* gcc.dg/c90-strftime-2.c, gcc.dg/c99-strftime-1.c,
	gcc.dg/c99-strftime-2.c: New tests.

--- c-common.c.orig	Mon Aug 21 19:35:35 2000
+++ c-common.c	Mon Aug 21 22:58:40 2000
@@ -2162,11 +2162,12 @@
 	}
       if (wide && index (fci->flag_chars, 'w') == 0)
 	warning ("width used with `%c' format", format_char);
-      if (index (fci->flag_chars, '2') != 0)
-	warning ("`%%%c' yields only last 2 digits of year", format_char);
-      else if (index (fci->flag_chars, '3') != 0)
+      if (index (fci->flag_chars, '3') != 0
+	  || (format_char == 'y' && index (flag_chars, 'E')))
 	warning ("`%%%c' yields only last 2 digits of year in some locales",
 		 format_char);
+      else if (index (fci->flag_chars, '2') != 0)
+	warning ("`%%%c' yields only last 2 digits of year", format_char);
       if (precise && index (fci->flag_chars, 'p') == 0)
 	warning ("precision used with `%c' format", format_char);
       if (aflag && index (fci->flag_chars, 'a') == 0)
--- /dev/null	Fri Sep 11 11:31:59 1998
+++ c90-strftime-2.c	Mon Aug 21 22:53:41 2000
@@ -0,0 +1,35 @@
+/* Test for strftime formats.  Rejection of formats using C99 features in
+   pedantic C90 mode.  */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic -Wformat" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+struct tm;
+
+extern size_t strftime (char *, size_t, const char *, const struct tm *);
+
+void
+foo (char *s, size_t m, const struct tm *tp)
+{
+  strftime (s, m, "%C", tp); /* { dg-warning "C" "%C not in C90" } */
+  strftime (s, m, "%D", tp); /* { dg-warning "C" "%D not in C90" } */
+  /* { dg-warning "only last 2" "2-digit year" { target *-*-* } 17 } */
+  strftime (s, m, "%e", tp); /* { dg-warning "C" "%e not in C90" } */
+  strftime (s, m, "%F", tp); /* { dg-warning "C" "%F not in C90" } */
+  strftime (s, m, "%g", tp); /* { dg-warning "C" "%g not in C90" } */
+  /* { dg-warning "only last 2" "2-digit year" { target *-*-* } 21 } */
+  strftime (s, m, "%G", tp); /* { dg-warning "C" "%G not in C90" } */
+  strftime (s, m, "%h", tp); /* { dg-warning "C" "%h not in C90" } */
+  strftime (s, m, "%n", tp); /* { dg-warning "C" "%n not in C90" } */
+  strftime (s, m, "%r", tp); /* { dg-warning "C" "%r not in C90" } */
+  strftime (s, m, "%R", tp); /* { dg-warning "C" "%R not in C90" } */
+  strftime (s, m, "%t", tp); /* { dg-warning "C" "%t not in C90" } */
+  strftime (s, m, "%T", tp); /* { dg-warning "C" "%T not in C90" } */
+  strftime (s, m, "%u", tp); /* { dg-warning "C" "%u not in C90" } */
+  strftime (s, m, "%V", tp); /* { dg-warning "C" "%V not in C90" } */
+  strftime (s, m, "%z", tp); /* { dg-warning "C" "%z not in C90" } */
+  strftime (s, m, "%EX", tp); /* { dg-warning "C" "%E not in C90" } */
+  strftime (s, m, "%OW", tp); /* { dg-warning "C" "%O not in C90" } */
+}
--- /dev/null	Fri Sep 11 11:31:59 1998
+++ c99-strftime-1.c	Mon Aug 21 21:50:04 2000
@@ -0,0 +1,99 @@
+/* Test for strftime formats.  Formats using C99 features.  */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic -Wformat" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+struct tm;
+
+extern size_t strftime (char *, size_t, const char *, const struct tm *);
+
+void
+foo (char *s, size_t m, const struct tm *tp)
+{
+  /* See ISO/IEC 9899:1990 (E) subclause 7.12.3.5 (pages 174-175).  */
+  /* Formats which are Y2K-compliant (no 2-digit years).  */
+  strftime (s, m, "%a%A%b%B%C%d%e%F%G%h%H%I%j%m%M%p%R%S%t%T%u%U%V%w%W%X%Y%z%Z%%", tp);
+  strftime (s, m, "%EC%EX%EY%Od%Oe%OH%OI%Om%OM%OS%Ou%OU%OV%Ow%OW", tp);
+  /* Formats with 2-digit years.  */
+  strftime (s, m, "%D", tp); /* { dg-warning "only last 2" "2-digit year" } */
+  strftime (s, m, "%g", tp); /* { dg-warning "only last 2" "2-digit year" } */
+  strftime (s, m, "%y", tp); /* { dg-warning "only last 2" "2-digit year" } */
+  strftime (s, m, "%Oy", tp); /* { dg-warning "only last 2" "2-digit year" } */
+  /* Formats with 2-digit years in some locales.  */
+  strftime (s, m, "%c", tp); /* { dg-warning "some locales" "2-digit year" } */
+  strftime (s, m, "%Ec", tp); /* { dg-warning "some locales" "2-digit year" } */
+  strftime (s, m, "%x", tp); /* { dg-warning "some locales" "2-digit year" } */
+  strftime (s, m, "%Ex", tp); /* { dg-warning "some locales" "2-digit year" } */
+  /* %Ey is explicitly an era offset not a 2-digit year; but in some
+     locales the E modifier may be ignored.
+  */
+  strftime (s, m, "%Ey", tp); /* { dg-warning "some locales" "2-digit year" } */
+  /* Bad uses of %E and %O.  */
+  strftime (s, m, "%EEY", tp); /* { dg-warning "multiple" "multiple %E/%O" } */
+  strftime (s, m, "%EOY", tp); /* { dg-warning "multiple" "multiple %E/%O" } */
+  strftime (s, m, "%OEV", tp); /* { dg-warning "multiple" "multiple %E/%O" } */
+  strftime (s, m, "%OOV", tp); /* { dg-warning "multiple" "multiple %E/%O" } */
+  strftime (s, m, "%Ea", tp); /* { dg-warning "flag" "bad %Ea" } */
+  strftime (s, m, "%EA", tp); /* { dg-warning "flag" "bad %EA" } */
+  strftime (s, m, "%Eb", tp); /* { dg-warning "flag" "bad %Eb" } */
+  strftime (s, m, "%EB", tp); /* { dg-warning "flag" "bad %EB" } */
+  strftime (s, m, "%Ed", tp); /* { dg-warning "flag" "bad %Ed" } */
+  strftime (s, m, "%ED", tp); /* { dg-warning "flag" "bad %ED" } */
+  /* { dg-warning "only last 2" "2-digit year" { target *-*-* } 43 } */
+  strftime (s, m, "%Ee", tp); /* { dg-warning "flag" "bad %Ee" } */
+  strftime (s, m, "%EF", tp); /* { dg-warning "flag" "bad %EF" } */
+  strftime (s, m, "%Eg", tp); /* { dg-warning "flag" "bad %Eg" } */
+  /* { dg-warning "only last 2" "2-digit year" { target *-*-* } 47 } */
+  strftime (s, m, "%EG", tp); /* { dg-warning "flag" "bad %EG" } */
+  strftime (s, m, "%Eh", tp); /* { dg-warning "flag" "bad %Eh" } */
+  strftime (s, m, "%EH", tp); /* { dg-warning "flag" "bad %EH" } */
+  strftime (s, m, "%EI", tp); /* { dg-warning "flag" "bad %EI" } */
+  strftime (s, m, "%Ej", tp); /* { dg-warning "flag" "bad %Ej" } */
+  strftime (s, m, "%Em", tp); /* { dg-warning "flag" "bad %Em" } */
+  strftime (s, m, "%EM", tp); /* { dg-warning "flag" "bad %EM" } */
+  strftime (s, m, "%En", tp); /* { dg-warning "flag" "bad %En" } */
+  strftime (s, m, "%Ep", tp); /* { dg-warning "flag" "bad %Ep" } */
+  strftime (s, m, "%Er", tp); /* { dg-warning "flag" "bad %Er" } */
+  strftime (s, m, "%ER", tp); /* { dg-warning "flag" "bad %ER" } */
+  strftime (s, m, "%ES", tp); /* { dg-warning "flag" "bad %ES" } */
+  strftime (s, m, "%Et", tp); /* { dg-warning "flag" "bad %Et" } */
+  strftime (s, m, "%ET", tp); /* { dg-warning "flag" "bad %ET" } */
+  strftime (s, m, "%Eu", tp); /* { dg-warning "flag" "bad %Eu" } */
+  strftime (s, m, "%EU", tp); /* { dg-warning "flag" "bad %EU" } */
+  strftime (s, m, "%EV", tp); /* { dg-warning "flag" "bad %EV" } */
+  strftime (s, m, "%Ew", tp); /* { dg-warning "flag" "bad %Ew" } */
+  strftime (s, m, "%EW", tp); /* { dg-warning "flag" "bad %EW" } */
+  strftime (s, m, "%Ez", tp); /* { dg-warning "flag" "bad %Ez" } */
+  strftime (s, m, "%EZ", tp); /* { dg-warning "flag" "bad %EZ" } */
+  strftime (s, m, "%E%", tp); /* { dg-warning "flag" "bad %E%" } */
+  strftime (s, m, "%Oa", tp); /* { dg-warning "flag" "bad %Oa" } */
+  strftime (s, m, "%OA", tp); /* { dg-warning "flag" "bad %OA" } */
+  strftime (s, m, "%Ob", tp); /* { dg-warning "flag" "bad %Ob" } */
+  strftime (s, m, "%OB", tp); /* { dg-warning "flag" "bad %OB" } */
+  strftime (s, m, "%Oc", tp); /* { dg-warning "flag" "bad %Oc" } */
+  /* { dg-warning "in some locales" "2-digit year" { target *-*-* } 75 } */
+  strftime (s, m, "%OC", tp); /* { dg-warning "flag|C" "bad %OC" } */
+  strftime (s, m, "%OD", tp); /* { dg-warning "flag" "bad %OD" } */
+  /* { dg-warning "only last 2" "2-digit year" { target *-*-* } 78 } */
+  strftime (s, m, "%OF", tp); /* { dg-warning "flag" "bad %OF" } */
+  strftime (s, m, "%Og", tp); /* { dg-warning "flag|C" "bad %Og" } */
+  /* { dg-warning "only last 2" "2-digit year" { target *-*-* } 81 } */
+  strftime (s, m, "%OG", tp); /* { dg-warning "flag|C" "bad %OG" } */
+  strftime (s, m, "%Oh", tp); /* { dg-warning "flag" "bad %Oh" } */
+  strftime (s, m, "%Oj", tp); /* { dg-warning "flag|C" "bad %Oj" } */
+  strftime (s, m, "%On", tp); /* { dg-warning "flag" "bad %On" } */
+  strftime (s, m, "%Op", tp); /* { dg-warning "flag" "bad %Op" } */
+  strftime (s, m, "%Or", tp); /* { dg-warning "flag" "bad %Or" } */
+  strftime (s, m, "%OR", tp); /* { dg-warning "flag" "bad %OR" } */
+  strftime (s, m, "%Ot", tp); /* { dg-warning "flag" "bad %Ot" } */
+  strftime (s, m, "%OT", tp); /* { dg-warning "flag" "bad %OT" } */
+  strftime (s, m, "%Ox", tp); /* { dg-warning "flag" "bad %Ox" } */
+  /* { dg-warning "in some locales" "2-digit year" { target *-*-* } 92 } */
+  strftime (s, m, "%OX", tp); /* { dg-warning "flag" "bad %OX" } */
+  strftime (s, m, "%OY", tp); /* { dg-warning "flag|C" "bad %OY" } */
+  strftime (s, m, "%Oz", tp); /* { dg-warning "flag|C" "bad %Oz" } */
+  strftime (s, m, "%OZ", tp); /* { dg-warning "flag" "bad %OZ" } */
+  strftime (s, m, "%O%", tp); /* { dg-warning "flag" "bad %O%" } */
+}
--- /dev/null	Fri Sep 11 11:31:59 1998
+++ c99-strftime-2.c	Mon Aug 21 23:20:20 2000
@@ -0,0 +1,27 @@
+/* Test for strftime formats.  Rejection of extensions in pedantic mode.  */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic -Wformat" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+struct tm;
+
+extern size_t strftime (char *, size_t, const char *, const struct tm *);
+
+void
+foo (char *s, size_t m, const struct tm *tp)
+{
+  /* %P is a lowercase version of %p.  */
+  strftime (s, m, "%P", tp); /* { dg-warning "C" "strftime %P" } */
+  /* %k is %H but padded with a space rather than 0 if necessary.  */
+  strftime (s, m, "%k", tp); /* { dg-warning "C" "strftime %k" } */
+  /* %l is %I but padded with a space rather than 0 if necessary.  */
+  strftime (s, m, "%l", tp); /* { dg-warning "C" "strftime %l" } */
+  /* %s is the number of seconds since the Epoch.  */
+  strftime (s, m, "%s", tp); /* { dg-warning "C" "strftime %s" } */
+  /* Extensions using %O already tested in c99-strftime-1.c.  */
+  /* Width and flags are GNU extensions for strftime.  */
+  strftime (s, m, "%20Y", tp); /* { dg-warning "C" "strftime width" } */
+  strftime (s, m, "%^A", tp); /* { dg-warning "C" "strftime flags" } */
+}

-- 
Joseph S. Myers
jsm28@cam.ac.uk


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