c6d234
commit 2084e7ca4d344c39eb39e53848b51b5d84444414
c6d234
Author: Joseph Myers <joseph@codesourcery.com>
c6d234
Date:   Wed Dec 10 00:41:19 2014 +0000
c6d234
c6d234
    Add macros for diagnostic control, use for scanf %a tests.
c6d234
    
c6d234
    In <https://sourceware.org/ml/libc-alpha/2014-11/msg00326.html>,
c6d234
    Roland requested internal macros for use of "#pragma GCC diagnostic".
c6d234
    
c6d234
    This patch adds such macros and uses them to disable -Wformat warnings
c6d234
    for some code testing GNU scanf %as where GCC expects C99 scanf %a
c6d234
    (several other stdio tests currently use -Wno-format to disable
c6d234
    warnings).  Limitations in GCC's diagnostic pragmas require separate
c6d234
    macros before and after the code generating the warnings, rather than
c6d234
    a single macro taking that code as an argument.
c6d234
    
c6d234
    The macros are named DIAG_*_NEEDS_COMMENT to emphasise to reviewers
c6d234
    the need for a comment accompanying any use of them (such comments may
c6d234
    however just appear once for several uses of the macros for the same
c6d234
    issue in the same file).  I put a GCC version in the arguments to
c6d234
    DIAG_IGNORE_NEEDS_COMMENT, as that seems something useful to grep for
c6d234
    when obsoleting support for an old GCC version and needing to decide
c6d234
    if warning-disabling code is still relevant.
c6d234
    
c6d234
    These macros should be usable for replacing existing -Wno-* use in
c6d234
    makefiles (as also suggested by Roland), though I have no plans to
c6d234
    work on that (only on use of the macros in cases where warnings are
c6d234
    currently present that need disabling to use -Werror).
c6d234
    
c6d234
    Tested for x86_64.
c6d234
    
c6d234
            * include/libc-internal.h (DIAG_PUSH_NEEDS_COMMENT): New macro.
c6d234
            (DIAG_POP_NEEDS_COMMENT): Likewise.
c6d234
            (_DIAG_STR1): Likewise.
c6d234
            (_DIAG_STR): Likewise.
c6d234
            (DIAG_IGNORE_NEEDS_COMMENT): Likewise.
c6d234
            * stdio-common/bug21.c: Include <libc-internal.h>.
c6d234
            (do_test): Disable -Wformat around call to sscanf.
c6d234
            * stdio-common/scanf14.c: Include <libc-internal.h>.
c6d234
            (main): Disable -Wformat around some calls to scanf functions.
c6d234
c6d234
diff --git a/include/libc-internal.h b/include/libc-internal.h
c6d234
index 2ced1c17d3dff93d..bca59a46144b736f 100644
c6d234
--- a/include/libc-internal.h
c6d234
+++ b/include/libc-internal.h
c6d234
@@ -76,4 +76,35 @@ extern void __init_misc (int, char **, char **);
c6d234
 #define ignore_value(x) \
c6d234
   ({ __typeof__ (x) __ignored_value = (x); (void) __ignored_value; })
c6d234
 
c6d234
+/* The macros to control diagnostics are structured like this, rather
c6d234
+   than a single macro that both pushes and pops diagnostic state and
c6d234
+   takes the affected code as an argument, because the GCC pragmas
c6d234
+   work by disabling the diagnostic for a range of source locations
c6d234
+   and do not work when all the pragmas and the affected code are in a
c6d234
+   single macro expansion.  */
c6d234
+
c6d234
+/* Push diagnostic state.  */
c6d234
+#define DIAG_PUSH_NEEDS_COMMENT _Pragma ("GCC diagnostic push")
c6d234
+
c6d234
+/* Pop diagnostic state.  */
c6d234
+#define DIAG_POP_NEEDS_COMMENT _Pragma ("GCC diagnostic pop")
c6d234
+
c6d234
+#define _DIAG_STR1(s) #s
c6d234
+#define _DIAG_STR(s) _DIAG_STR1(s)
c6d234
+
c6d234
+/* Ignore the diagnostic OPTION.  VERSION is the most recent GCC
c6d234
+   version for which the diagnostic has been confirmed to appear in
c6d234
+   the absence of the pragma (in the form MAJOR.MINOR for GCC 4.x,
c6d234
+   just MAJOR for GCC 5 and later).  Uses of this pragma should be
c6d234
+   reviewed when the GCC version given is no longer supported for
c6d234
+   building glibc; the version number should always be on the same
c6d234
+   source line as the macro name, so such uses can be found with grep.
c6d234
+   Uses should come with a comment giving more details of the
c6d234
+   diagnostic, and an architecture on which it is seen if possibly
c6d234
+   optimization-related and not in architecture-specific code.  This
c6d234
+   macro should only be used if the diagnostic seems hard to fix (for
c6d234
+   example, optimization-related false positives).  */
c6d234
+#define DIAG_IGNORE_NEEDS_COMMENT(version, option)	\
c6d234
+  _Pragma (_DIAG_STR (GCC diagnostic ignored option))
c6d234
+
c6d234
 #endif /* _LIBC_INTERNAL  */
c6d234
diff --git a/stdio-common/bug21.c b/stdio-common/bug21.c
c6d234
index d22b9c1a9717a197..ca27272ba191321e 100644
c6d234
--- a/stdio-common/bug21.c
c6d234
+++ b/stdio-common/bug21.c
c6d234
@@ -1,4 +1,5 @@
c6d234
 #include <stdio.h>
c6d234
+#include <libc-internal.h>
c6d234
 
c6d234
 static int
c6d234
 do_test (void)
c6d234
@@ -6,7 +7,15 @@ do_test (void)
c6d234
   static const char buf[] = " ";
c6d234
   char *str;
c6d234
 
c6d234
+  /* GCC in C99 mode treats %a as the C99 format expecting float *,
c6d234
+     but glibc with _GNU_SOURCE treats %as as the GNU allocation
c6d234
+     extension, so resulting in "warning: format '%a' expects argument
c6d234
+     of type 'float *', but argument 3 has type 'char **'".  This
c6d234
+     applies to the other %as, %aS and %a[] formats below as well.  */
c6d234
+  DIAG_PUSH_NEEDS_COMMENT;
c6d234
+  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
c6d234
   int r = sscanf (buf, "%as", &str);
c6d234
+  DIAG_POP_NEEDS_COMMENT;
c6d234
   printf ("%d %p\n", r, str);
c6d234
 
c6d234
   return r != -1 || str != NULL;
c6d234
diff --git a/stdio-common/scanf14.c b/stdio-common/scanf14.c
c6d234
index 6ca5c7c5679632a3..cffccb0b19d2927b 100644
c6d234
--- a/stdio-common/scanf14.c
c6d234
+++ b/stdio-common/scanf14.c
c6d234
@@ -2,6 +2,7 @@
c6d234
 #include <stdlib.h>
c6d234
 #include <string.h>
c6d234
 #include <wchar.h>
c6d234
+#include <libc-internal.h>
c6d234
 
c6d234
 #define FAIL() \
c6d234
   do {							\
c6d234
@@ -23,6 +24,13 @@ main (void)
c6d234
     FAIL ();
c6d234
   else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
c6d234
     FAIL ();
c6d234
+  /* GCC in C99 mode treats %a as the C99 format expecting float *,
c6d234
+     but glibc with _GNU_SOURCE treats %as as the GNU allocation
c6d234
+     extension, so resulting in "warning: format '%a' expects argument
c6d234
+     of type 'float *', but argument 3 has type 'char **'".  This
c6d234
+     applies to the other %as, %aS and %a[] formats below as well.  */
c6d234
+  DIAG_PUSH_NEEDS_COMMENT;
c6d234
+  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
c6d234
   if (sscanf (" 1.25s x", "%as%2c", &sp, c) != 2)
c6d234
     FAIL ();
c6d234
   else
c6d234
@@ -32,10 +40,14 @@ main (void)
c6d234
       memset (sp, 'x', sizeof "1.25s");
c6d234
       free (sp);
c6d234
     }
c6d234
+  DIAG_POP_NEEDS_COMMENT;
c6d234
   if (sscanf (" 2.25s x", "%las%2c", &d, c) != 2)
c6d234
     FAIL ();
c6d234
   else if (d != 2.25 || memcmp (c, " x", 2) != 0)
c6d234
     FAIL ();
c6d234
+  /* See explanation above.  */
c6d234
+  DIAG_PUSH_NEEDS_COMMENT;
c6d234
+  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
c6d234
   if (sscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2)
c6d234
     FAIL ();
c6d234
   else
c6d234
@@ -54,6 +66,7 @@ main (void)
c6d234
       memset (sp, 'x', sizeof "4.25");
c6d234
       free (sp);
c6d234
     }
c6d234
+  DIAG_POP_NEEDS_COMMENT;
c6d234
   if (sscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
c6d234
     FAIL ();
c6d234
   else if (d != 5.25 || memcmp (c, " x", 2) != 0)
c6d234
@@ -82,6 +95,9 @@ main (void)
c6d234
 	FAIL ();
c6d234
       if (fseek (fp, 0, SEEK_SET) != 0)
c6d234
 	FAIL ();
c6d234
+      /* See explanation above.  */
c6d234
+      DIAG_PUSH_NEEDS_COMMENT;
c6d234
+      DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
c6d234
       if (fscanf (fp, "%as%2c", &sp, c) != 2)
c6d234
 	FAIL ();
c6d234
       else
c6d234
@@ -91,11 +107,15 @@ main (void)
c6d234
 	  memset (sp, 'x', sizeof "1.25s");
c6d234
 	  free (sp);
c6d234
 	}
c6d234
+      DIAG_POP_NEEDS_COMMENT;
c6d234
 
c6d234
       if (freopen (fname, "r", stdin) == NULL)
c6d234
 	FAIL ();
c6d234
       else
c6d234
 	{
c6d234
+	  /* See explanation above.  */
c6d234
+	  DIAG_PUSH_NEEDS_COMMENT;
c6d234
+	  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
c6d234
 	  if (scanf ("%as%2c", &sp, c) != 2)
c6d234
 	    FAIL ();
c6d234
 	  else
c6d234
@@ -105,6 +125,7 @@ main (void)
c6d234
 	      memset (sp, 'x', sizeof "1.25s");
c6d234
 	      free (sp);
c6d234
 	    }
c6d234
+	  DIAG_POP_NEEDS_COMMENT;
c6d234
 	}
c6d234
 
c6d234
       fclose (fp);