]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR c++/84076 (Warning about objects through POD mistakenly claims the...
authorJakub Jelinek <jakub@redhat.com>
Fri, 22 Jun 2018 20:34:33 +0000 (22:34 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 22 Jun 2018 20:34:33 +0000 (22:34 +0200)
Backported from mainline
2018-03-09  Jason Merrill  <jason@redhat.com>
    Jakub Jelinek  <jakub@redhat.com>

PR c++/84076
* call.c (convert_arg_to_ellipsis): Instead of cp_build_addr_expr
build ADDR_EXPR with REFERENCE_TYPE.
(build_over_call): For purposes of check_function_arguments, if
argarray[j] is ADDR_EXPR with REFERENCE_TYPE created above, use
its operand rather than the argument itself.

* g++.dg/warn/Wformat-2.C: New test.

From-SVN: r261917

gcc/cp/ChangeLog
gcc/cp/call.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wformat-2.C [new file with mode: 0644]

index 1b2cc21a981e200b0e6aa0fd1526bf6910699ba0..0ddafc8a1b4df8274ffcc8b9c4e604281ee8c09f 100644 (file)
@@ -1,6 +1,16 @@
 2018-06-22  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2018-03-09  Jason Merrill  <jason@redhat.com>
+                   Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/84076
+       * call.c (convert_arg_to_ellipsis): Instead of cp_build_addr_expr
+       build ADDR_EXPR with REFERENCE_TYPE.
+       (build_over_call): For purposes of check_function_arguments, if
+       argarray[j] is ADDR_EXPR with REFERENCE_TYPE created above, use
+       its operand rather than the argument itself.
+
        2018-03-08  Jason Merrill  <jason@redhat.com>
                    Jakub Jelinek  <jakub@redhat.com>
 
index 16a6a33bca76799dd0f6eb3f74f183a7bdfb1c24..5145e438460d2f221ea9e4ef21dbc9ab13d45643 100644 (file)
@@ -7144,7 +7144,7 @@ convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
                     "passing objects of non-trivially-copyable "
                     "type %q#T through %<...%> is conditionally supported",
                     arg_type);
-         return cp_build_addr_expr (arg, complain);
+         return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
        }
       /* Build up a real lvalue-to-rvalue conversion in case the
         copy constructor is trivial but not callable.  */
@@ -7935,7 +7935,15 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       tree *fargs = (!nargs ? argarray
                            : (tree *) alloca (nargs * sizeof (tree)));
       for (j = 0; j < nargs; j++)
-       fargs[j] = maybe_constant_value (argarray[j]);
+       {
+         /* For -Wformat undo the implicit passing by hidden reference
+            done by convert_arg_to_ellipsis.  */
+         if (TREE_CODE (argarray[j]) == ADDR_EXPR
+             && TREE_CODE (TREE_TYPE (argarray[j])) == REFERENCE_TYPE)
+           fargs[j] = TREE_OPERAND (argarray[j], 0);
+         else
+           fargs[j] = maybe_constant_value (argarray[j]);
+       }
 
       warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
                                           nargs, fargs);
index 10e1880184c36f0e78b14d5cc2da4d59ef21a3d0..e464c19ad356a6f785e11881cc0780814e1e77ea 100644 (file)
@@ -1,6 +1,12 @@
 2018-06-22  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2018-03-09  Jason Merrill  <jason@redhat.com>
+                   Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/84076
+       * g++.dg/warn/Wformat-2.C: New test.
+
        2018-03-09  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/84767
diff --git a/gcc/testsuite/g++.dg/warn/Wformat-2.C b/gcc/testsuite/g++.dg/warn/Wformat-2.C
new file mode 100644 (file)
index 0000000..ff8b54b
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/84076
+// { dg-do compile }
+// { dg-options "-Wformat" }
+
+struct S { ~S (); };
+struct T { T (); T (const T &); };
+
+void
+foo ()
+{
+  S s;
+  T t;
+  __builtin_printf ("%s\n", s);        // { dg-warning "format '%s' expects argument of type 'char\\*', but argument 2 has type 'S'" }
+  __builtin_printf ("%s\n", t);        // { dg-warning "format '%s' expects argument of type 'char\\*', but argument 2 has type 'T'" }
+  __builtin_printf ("%s\n", &s);// { dg-warning "format '%s' expects argument of type 'char\\*', but argument 2 has type 'S\\*'" }
+  __builtin_printf ("%s\n", &t);// { dg-warning "format '%s' expects argument of type 'char\\*', but argument 2 has type 'T\\*'" }
+}