Add -Wvarargs option
authorDodji Seketeli <dodji@redhat.com>
Mon, 30 Apr 2012 11:43:57 +0000 (11:43 +0000)
committerDodji Seketeli <dodji@gcc.gnu.org>
Mon, 30 Apr 2012 11:43:57 +0000 (13:43 +0200)
Several warnings related to questionable usage cases of variadic
function related macros (like va_start) could not be controlled by any
warning-related macro.  Fixed thus, by introducing the -Wvarargs
option.

Tested on x86_64-unknown-linux-gnu against trunk.

gcc/c-family/

* c.opt (Wvarargs):  Define new option.

gcc/
* builtins.c (fold_builtin_next_arg):  Use OPT_Wvarargs as an
argument for the various warning_at calls.

gcc/doc/

* invoke.texi: Update the documentation.

gcc/testsuite/

* c-c++-common/Wvarargs.c: New test case.
* c-c++-common/Wvarargs-2.c: Likewise.

From-SVN: r186978

gcc/ChangeLog
gcc/builtins.c
gcc/c-family/ChangeLog
gcc/c-family/c.opt
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/Wvarargs-2.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/Wvarargs.c [new file with mode: 0644]

index 43d3f5daa04264e49ce065e88896ca082954d4c2..264c997e4a196b51b23d9cdf33e8464016f1a533 100644 (file)
@@ -1,3 +1,10 @@
+2012-04-30  Dodji Seketeli  <dodji@redhat.com>
+
+       Add -Wvarargs option
+       * builtins.c (fold_builtin_next_arg):  Use OPT_Wvarargs as an
+       argument for the various warning_at calls.
+       * docs/invoke.texi: Update the documentation.
+
 2012-04-30  Dodji Seketeli  <dodji@redhat.com>
 
        Switch -ftrack-macro-expansion=2 on by default.
index 5ddc47bd7ea651a7481a11888fc1553056aebaa9..41a052b1d8fa55781fa36a902f1f71cb1c5ba491 100644 (file)
@@ -12127,8 +12127,8 @@ fold_builtin_next_arg (tree exp, bool va_start_p)
          /* Evidently an out of date version of <stdarg.h>; can't validate
             va_start's second argument, but can still work as intended.  */
          warning_at (current_location,
-                     0,
-                     "%<__builtin_next_arg%> called without an argument");
+                     OPT_Wvarargs,
+                  "%<__builtin_next_arg%> called without an argument");
          return true;
        }
       else if (nargs > 1)
@@ -12164,7 +12164,7 @@ fold_builtin_next_arg (tree exp, bool va_start_p)
             argument so that we will get wrong-code because of
             it.  */
          warning_at (current_location,
-                     0,
+                     OPT_Wvarargs,
                      "second parameter of %<va_start%> not last named argument");
        }
 
@@ -12177,7 +12177,7 @@ fold_builtin_next_arg (tree exp, bool va_start_p)
       else if (DECL_REGISTER (arg))
        {
          warning_at (current_location,
-                     0,
+                     OPT_Wvarargs,
                      "undefined behaviour when second parameter of "
                      "%<va_start%> is declared with %<register%> storage");
        }
index b108b509971ac3544e7fbb6c7d90f78c14f93fd8..9f62db21b6d333a8e04358a852c6fb28622579d1 100644 (file)
@@ -1,3 +1,8 @@
+2012-04-30  Dodji Seketeli  <dodji@redhat.com>
+
+       Add -Wvarargs option
+       * c.opt (Wvarargs):  Define new option.
+
 2012-04-30  Manuel López-Ibáñez  <manu@gcc.gnu.org>
 
        * c-common.c (check_function_arguments): Replace
index ff8ce94f96cf4341b2fc6746068c7e262c5c7661..d2cf6ee6caae6c521dc309018581a087fac8a93b 100644 (file)
@@ -689,6 +689,10 @@ Wvariadic-macros
 C ObjC C++ ObjC++ Warning
 Do not warn about using variadic macros when -pedantic
 
+Wvarargs
+C ObjC C++ ObjC++ Warning Var(warn_varargs) Init(1)
+Warn about questionable usage of the macros used to retrieve variable arguments
+
 Wvla
 C ObjC C++ ObjC++ Var(warn_vla) Init(-1) Warning
 Warn if a variable length array is used
index c6ee28fb4c213a51cdafe9b343959f730e46aa54..a7efaa7e021be6686be01c150aaf68524e8bfaa0 100644 (file)
@@ -4663,6 +4663,13 @@ Warn if variadic macros are used in pedantic ISO C90 mode, or the GNU
 alternate syntax when in pedantic ISO C99 mode.  This is default.
 To inhibit the warning messages, use @option{-Wno-variadic-macros}.
 
+@item -Wvarargs
+@opindex Wvarargs
+@opindex Wno-varargs
+Warn upon questionable usage of the macros used to handle variable
+arguments like @samp{va_start}.  This is default.  To inhibit the
+warning messages, use @option{-Wno-varargs}.
+
 @item -Wvector-operation-performance
 @opindex Wvector-operation-performance
 @opindex Wno-vector-operation-performance
index 000d0a865f477248f0d409add2e99beadfd9d81f..f5cfe3710abd1a0ed609ace212c6d7144ae88cff 100644 (file)
@@ -1,3 +1,9 @@
+2012-04-30  Dodji Seketeli  <dodji@redhat.com>
+
+       Add -Wvarargs option
+       * c-c++-common/Wvarargs.c: New test case.
+       * c-c++-common/Wvarargs-2.c: Likewise.
+
 2012-04-30  Dodji Seketeli  <dodji@redhat.com>
 
        Adjust relevant test cases wrt -ftrack-macro-expansion=[0|2]
diff --git a/gcc/testsuite/c-c++-common/Wvarargs-2.c b/gcc/testsuite/c-c++-common/Wvarargs-2.c
new file mode 100644 (file)
index 0000000..a2e031f
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+   { dg-options "-Wno-varargs" }
+   { dg-do compile }
+ */
+
+#include <stdarg.h>
+
+void
+err (int a)
+{
+  va_list vp;
+  va_start (vp, a); // { dg-error "used in function with fixed args" }
+}
+
+void
+foo0 (int a, int b, ...)
+{
+    va_list vp;
+    /* 'a' is not the last argument of the enclosing function, but
+       don't warn because we are ignoring -Wvarargs.  */
+    va_start (vp, a);
+    va_end (vp);
+}
+
+void
+foo1 (int a, register int b, ...)
+{
+    va_list vp;
+    /* 'b' is declared with register storage, but don't warn
+       because we are ignoring -Wvarargs.  */
+    va_start (vp, b);
+    va_end (vp);
+}
diff --git a/gcc/testsuite/c-c++-common/Wvarargs.c b/gcc/testsuite/c-c++-common/Wvarargs.c
new file mode 100644 (file)
index 0000000..408f427
--- /dev/null
@@ -0,0 +1,54 @@
+/* { dg-do compile } */
+
+#include <stdarg.h>
+
+void
+err (int a)
+{
+  va_list vp;
+  va_start (vp, a); // { dg-error "used in function with fixed args" }
+}
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wvarargs"
+
+void
+foo0 (int a, int b, ...)
+{
+    va_list vp;
+    /* 'a' is not the last argument of the enclosing function, but
+       don't warn because we are ignoring -Wvarargs.  */
+    va_start (vp, a);
+    va_end (vp);
+}
+
+void
+foo1 (int a, register int b, ...)
+{
+    va_list vp;
+    /* 'b' is declared with register storage, but don't warn
+       because we are ignoring -Wvarargs.  */
+    va_start (vp, b);
+    va_end (vp);
+}
+
+#pragma GCC diagnostic pop
+
+void
+foo2 (int a, int b, ...)
+{
+    va_list vp;
+    /* 'a' is not the last argument of the enclosing function, so
+       warn.  */
+    va_start (vp, a); /* { dg-warning "second parameter" } */
+    va_end (vp);
+}
+
+void
+foo3 (int a, register int b, ...)
+{
+    va_list vp;
+    /* 'b' is declared with register storage, so warn.  */
+    va_start (vp, b); /* { dg-warning "undefined behaviour" } */
+    va_end (vp);
+}