diff mbox

C++ PATCH to implement C++17 [[nodiscard]] attribute

Message ID 57225DB0.5080404@redhat.com
State New
Headers show

Commit Message

Jason Merrill April 28, 2016, 7 p.m. UTC
C++17 adds a [[nodiscard]] attribute, which is similar to the GNU 
warn_unused_result attribute, except that it applies to function 
declarations and class/enum types rather than to function types and is 
suppressed by an explicit conversion to void. I considered just treating 
it as warn_unused_result, but fn_type_req is hard to get around, so I 
decided to handle it separately.

While I was at it, I fixed 38172, an outstanding bug with 
warn_unused_result false negatives in the C++ front end, and moved its 
testcase into c-c++-common.

This patch does not address bug 66425, which argues that (void) ought to 
suppress the warn_unused_result warning, as it does for [[nodiscard]]. 
I agree with this argument, but will leave that change for a future patch.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit 4c253f9fdbd6acc274f9cf141887ae0e6f1c31ec
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Apr 28 10:17:09 2016 -0400

    	Implement C++17 [[nodiscard]] attribute.
    
    	PR c++/38172
    	PR c++/54379
    gcc/c-family/
    	* c-lex.c (c_common_has_attribute): Handle nodiscard.
    gcc/cp/
    	* parser.c (cp_parser_std_attribute): Handle [[nodiscard]].
    	* tree.c (handle_nodiscard_attribute): New.
    	(cxx_attribute_table): Add [[nodiscard]].
    	* cvt.c (cp_get_fndecl_from_callee, cp_get_callee_fndecl): New.
    	(maybe_warn_nodiscard): New.
    	(convert_to_void): Call it.

diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index ff7eb25..38a428d 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -347,7 +347,8 @@  c_common_has_attribute (cpp_reader *pfile)
 		result = 200809;
 	      else if (is_attribute_p ("deprecated", attr_name))
 		result = 201309;
-	      else if (is_attribute_p ("maybe_unused", attr_name))
+	      else if (is_attribute_p ("maybe_unused", attr_name)
+		       || is_attribute_p ("nodiscard", attr_name))
 		result = 201603;
 	      if (result)
 		attr_name = NULL_TREE;
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index f6ea0b7..8a06609 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -5695,6 +5695,8 @@  extern tree cp_convert				(tree, tree, tsubst_flags_t);
 extern tree cp_convert_and_check                (tree, tree, tsubst_flags_t);
 extern tree cp_fold_convert			(tree, tree);
 extern tree cp_get_callee			(tree);
+extern tree cp_get_callee_fndecl		(tree);
+extern tree cp_get_fndecl_from_callee		(tree);
 extern tree convert_to_void			(tree, impl_conv_void,
                                  		 tsubst_flags_t);
 extern tree convert_force			(tree, tree, int,
diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 8c9d78b..2e2bac7 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -918,6 +918,104 @@  cp_get_callee (tree call)
   return NULL_TREE;
 }
 
+/* FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL
+   if we can.  */
+
+tree
+cp_get_fndecl_from_callee (tree fn)
+{
+  if (fn == NULL_TREE)
+    return fn;
+  if (TREE_CODE (fn) == FUNCTION_DECL)
+    return fn;
+  tree type = TREE_TYPE (fn);
+  if (type == unknown_type_node)
+    return NULL_TREE;
+  gcc_assert (POINTER_TYPE_P (type));
+  fn = maybe_constant_init (fn);
+  STRIP_NOPS (fn);
+  if (TREE_CODE (fn) == ADDR_EXPR)
+    {
+      fn = TREE_OPERAND (fn, 0);
+      if (TREE_CODE (fn) == FUNCTION_DECL)
+	return fn;
+    }
+  return NULL_TREE;
+}
+
+/* Like get_callee_fndecl, but handles AGGR_INIT_EXPR as well and uses the
+   constexpr machinery.  */
+
+tree
+cp_get_callee_fndecl (tree call)
+{
+  return cp_get_fndecl_from_callee (cp_get_callee (call));
+}
+
+/* Subroutine of convert_to_void.  Warn if we're discarding something with
+   attribute [[nodiscard]].  */
+
+static void
+maybe_warn_nodiscard (tree expr, impl_conv_void implicit)
+{
+  tree call = expr;
+  if (TREE_CODE (expr) == TARGET_EXPR)
+    call = TARGET_EXPR_INITIAL (expr);
+  location_t loc = EXPR_LOC_OR_LOC (call, input_location);
+  tree callee = cp_get_callee (call);
+  if (!callee)
+    return;
+
+  tree type = TREE_TYPE (callee);
+  if (TYPE_PTRMEMFUNC_P (type))
+    type = TYPE_PTRMEMFUNC_FN_TYPE (type);
+  if (POINTER_TYPE_P (type))
+    type = TREE_TYPE (type);
+
+  tree rettype = TREE_TYPE (type);
+  tree fn = cp_get_fndecl_from_callee (callee);
+  if (implicit != ICV_CAST && fn
+      && lookup_attribute ("nodiscard", DECL_ATTRIBUTES (fn)))
+    {
+      if (warning_at (loc, OPT_Wunused_result,
+		      "ignoring return value of %qD, "
+		      "declared with attribute nodiscard", fn))
+	inform (DECL_SOURCE_LOCATION (fn), "declared here");
+    }
+  else if (implicit != ICV_CAST
+	   && lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype)))
+    {
+      if (warning_at (loc, OPT_Wunused_result,
+		      "ignoring returned value of type %qT, "
+		      "declared with attribute nodiscard", rettype))
+	{
+	  if (fn)
+	    inform (DECL_SOURCE_LOCATION (fn),
+		    "in call to %qD, declared here", fn);
+	  inform (DECL_SOURCE_LOCATION (TYPE_NAME (rettype)),
+		  "%qT declared here", rettype);
+	}
+    }
+  else if (TREE_CODE (expr) == TARGET_EXPR
+	   && lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (type)))
+    {
+      /* The TARGET_EXPR confuses do_warn_unused_result into thinking that the
+	 result is used, so handle that case here.  */
+      if (fn)
+	{
+	  if (warning_at (loc, OPT_Wunused_result,
+			  "ignoring return value of %qD, "
+			  "declared with attribute warn_unused_result",
+			  fn))
+	    inform (DECL_SOURCE_LOCATION (fn), "declared here");
+	}
+      else
+	warning_at (loc, OPT_Wunused_result,
+		    "ignoring return value of function "
+		    "declared with attribute warn_unused_result");
+    }
+}
+
 /* When an expression is used in a void context, its value is discarded and
    no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
    stmt.expr/1, expr.comma/1].  This permits dereferencing an incomplete type
@@ -1032,6 +1130,7 @@  convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
       break;
 
     case CALL_EXPR:   /* We have a special meaning for volatile void fn().  */
+      maybe_warn_nodiscard (expr, implicit);
       break;
 
     case INDIRECT_REF:
@@ -1257,12 +1356,14 @@  convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
 	    {
 	      tree fn = AGGR_INIT_EXPR_FN (init);
 	      expr = build_call_array_loc (input_location,
-					   TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
+					   TREE_TYPE (TREE_TYPE
+						      (TREE_TYPE (fn))),
 					   fn,
 					   aggr_init_expr_nargs (init),
 					   AGGR_INIT_EXPR_ARGP (init));
 	    }
 	}
+      maybe_warn_nodiscard (expr, implicit);
       break;
 
     default:;
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index 97601aa..d7e9c7b 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -3570,6 +3570,30 @@  zero_init_p (const_tree t)
   return 1;
 }
 
+/* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
+   warn_unused_result attribute.  */
+
+static tree
+handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
+			    int /*flags*/, bool *no_add_attrs)
+{
+  if (TREE_CODE (*node) == FUNCTION_DECL)
+    {
+      if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
+	warning (OPT_Wattributes, "%qE attribute applied to %qD with void "
+		 "return type", name, *node);
+    }
+  else if (OVERLOAD_TYPE_P (*node))
+    /* OK */;
+  else
+    {
+      warning (OPT_Wattributes, "%qE attribute can only be applied to "
+	       "functions or to class or enumeration types", name);
+      *no_add_attrs = true;
+    }
+  return NULL_TREE;
+}
+
 /* Table of valid C++ attributes.  */
 const struct attribute_spec cxx_attribute_table[] =
 {
@@ -3591,6 +3615,8 @@  const struct attribute_spec std_attribute_table[] =
        affects_type_identity } */
   { "maybe_unused", 0, 0, false, false, false,
     handle_unused_attribute, false },
+  { "nodiscard", 0, 0, false, false, false,
+    handle_nodiscard_attribute, false },
   { NULL,	      0, 0, false, false, false, NULL, false }
 };
 
diff --git a/gcc/testsuite/c-c++-common/attr-warn-unused-result.c b/gcc/testsuite/c-c++-common/attr-warn-unused-result.c
new file mode 100644
index 0000000..adf850c
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/attr-warn-unused-result.c
@@ -0,0 +1,188 @@ 
+/* warn_unused_result attribute tests.  */
+/* { dg-do compile } */
+/* { dg-options "-O -ftrack-macro-expansion=0" } */
+
+#define WUR __attribute__((warn_unused_result))
+#define WURAI __attribute__((warn_unused_result, always_inline)) inline
+typedef WUR int (*fnt) (void);
+
+typedef struct { long i; } A;
+typedef struct { long i; long j; } B;
+typedef struct { char big[1024]; fnt fn; } C;
+
+WUR int check1 (void);
+WUR void check2 (void); /* { dg-warning "attribute ignored" } */
+int foo WUR;	  /* { dg-message "only applies" } */
+int bar (void);
+extern WURAI int check3 (void) { return bar (); }
+WUR A check4 (void);
+WUR B check5 (void);
+WUR C check6 (void);
+A bar7 (void);
+B bar8 (void);
+C bar9 (void);
+extern WURAI A check7 (void) { return bar7 (); }
+extern WURAI B check8 (void) { return bar8 (); }
+extern WURAI C check9 (void) { return bar9 (); }
+/* This is useful for checking whether return value of statement
+   expressions (returning int in this case) is used.  */
+extern WURAI int check_int_result (int res) { return res; }
+#define GU(v) ({ int e = 0; (v) = bar (); if ((v) < 23) e = 14; e; })
+fnt fnptr;
+WUR int check10 (void);
+int baz (void);
+extern WURAI int check11 (void) { return baz (); }
+int k;
+
+void
+test (void)
+{
+  int i = 0, j;
+  const fnt pcheck1 = check1;
+  const fnt pcheck3 = check3;
+  A a;
+  B b;
+  C c;
+  if (check1 ())
+    return;
+  i += check1 ();
+  i += ({ check1 (); });
+  check1 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check1 ();	/* { dg-warning "ignoring return value of" } */
+  check1 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  check2 ();
+  (void) check2 ();
+  check2 (), bar ();
+  if (check3 ())
+    return;
+  i += check3 ();
+  i += ({ check3 (); });
+  check3 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check3 ();	/* { dg-warning "ignoring return value of" } */
+  check3 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  a = check4 ();
+  if (a.i)
+    return;
+  if (check4 ().i)
+    return;
+  if (({ check4 (); }).i)
+    return;
+  check4 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check4 ();	/* { dg-warning "ignoring return value of" } */
+  check4 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  b = check5 ();
+  if (b.i + b.j)
+    return;
+  if (check5 ().j)
+    return;
+  if (({ check5 (); }).j)
+    return;
+  check5 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check5 ();	/* { dg-warning "ignoring return value of" } */
+  check5 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  c = check6 ();
+  if (c.big[12] + c.big[29])
+    return;
+  if (check6 ().big[27])
+    return;
+  if (({ check6 (); }).big[0])
+    return;
+  check6 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check6 ();	/* { dg-warning "ignoring return value of" } */
+  check6 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  a = check7 ();
+  if (a.i)
+    return;
+  if (check7 ().i)
+    return;
+  if (({ check7 (); }).i)
+    return;
+  check7 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check7 ();	/* { dg-warning "ignoring return value of" } */
+  check7 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  b = check8 ();
+  if (b.i + b.j)
+    return;
+  if (check8 ().j)
+    return;
+  if (({ check8 (); }).j)
+    return;
+  check8 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check8 ();	/* { dg-warning "ignoring return value of" } */
+  check8 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  c = check9 ();
+  if (c.big[12] + c.big[29])
+    return;
+  if (check9 ().big[27])
+    return;
+  if (({ check9 (); }).big[0])
+    return;
+  check9 ();		/* { dg-warning "ignoring return value of" } */
+  (void) check9 ();	/* { dg-warning "ignoring return value of" } */
+  check9 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  if (check_int_result (GU (j)))
+    return;
+  i += check_int_result (GU (j));
+  i += ({ check_int_result (GU (j)); });
+  check_int_result (GU (j)); /* { dg-warning "ignoring return value of" } */
+  (void) check_int_result (GU (j)); /* { dg-warning "ignoring return value of" } */
+  check_int_result (GU (j)), bar (); /* { dg-warning "ignoring return value of" } */
+  if (fnptr ())
+    return;
+  i += fnptr ();
+  i += ({ fnptr (); });
+  fnptr ();		/* { dg-warning "ignoring return value of" } */
+  (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
+  fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
+  fnptr = check1;
+  if (fnptr ())
+    return;
+  i += fnptr ();
+  i += ({ fnptr (); });
+  fnptr ();		/* { dg-warning "ignoring return value of" } */
+  (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
+  fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
+  fnptr = check3;
+  if (fnptr ())
+    return;
+  i += fnptr ();
+  i += ({ fnptr (); });
+  fnptr ();		/* { dg-warning "ignoring return value of" } */
+  (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
+  fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
+  if (bar9 ().fn ())
+    return;
+  i += bar9 ().fn ();
+  i += ({ bar9 ().fn (); });
+  bar9 ().fn ();	/* { dg-warning "ignoring return value of" } */
+  (void) bar9 ().fn ();	/* { dg-warning "ignoring return value of" } */
+  bar9 ().fn (), bar (); /* { dg-warning "ignoring return value of" } */
+  if ((k ? check1 : check10) ())
+    return;
+  i += (k ? check1 : check10) ();
+  i += ({ (k ? check1 : check10) (); });
+  (k ? check1 : check10) (); /* { dg-warning "ignoring return value of" } */
+  (void) (k ? check1 : check10) (); /* { dg-warning "ignoring return value of" } */
+  (k ? check1 : check10) (), bar (); /* { dg-warning "ignoring return value of" } */
+  if ((k ? check3 : check11) ())
+    return;
+  i += (k ? check3 : check11) ();
+  i += ({ (k ? check3 : check11) (); });
+  (k ? check3 : check11) (); /* { dg-warning "ignoring return value of" } */
+  (void) (k ? check3 : check11) (); /* { dg-warning "ignoring return value of" } */
+  (k ? check3 : check11) (), bar (); /* { dg-warning "ignoring return value of" } */
+  if (pcheck1 ())
+    return;
+  i += pcheck1 ();
+  i += ({ pcheck1 (); });
+  pcheck1 ();		/* { dg-warning "ignoring return value of" } */
+  (void) pcheck1 ();	/* { dg-warning "ignoring return value of" } */
+  pcheck1 (), bar ();	/* { dg-warning "ignoring return value of" } */
+  if (pcheck3 ())
+    return;
+  i += pcheck3 ();
+  i += ({ pcheck3 (); });
+  pcheck3 ();		/* { dg-warning "ignoring return value of" } */
+  (void) pcheck3 ();	/* { dg-warning "ignoring return value of" } */
+  pcheck3 (), bar ();	/* { dg-warning "ignoring return value of" } */
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C b/gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C
index fe7a4c2..f8a87a8 100644
--- a/gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C
+++ b/gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C
@@ -345,11 +345,19 @@ 
 #endif
 
 #ifdef __has_cpp_attribute
+
 #  if ! __has_cpp_attribute(maybe_unused)
 #    error "__has_cpp_attribute(maybe_unused)"
 #  elif __has_cpp_attribute(maybe_unused) != 201603
 #    error "__has_cpp_attribute(maybe_unused) != 201603"
 #  endif
+
+#  if ! __has_cpp_attribute(nodiscard)
+#    error "__has_cpp_attribute(nodiscard)"
+#  elif __has_cpp_attribute(nodiscard) != 201603
+#    error "__has_cpp_attribute(nodiscard) != 201603"
+#  endif
+
 #else
 #  error "__has_cpp_attribute"
 #endif
diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard1.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard1.C
new file mode 100644
index 0000000..5f2345a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard1.C
@@ -0,0 +1,8 @@ 
+// { dg-do compile { target c++11 } }
+
+[[nodiscard]] int f();
+
+int main()
+{
+  f();				// { dg-warning "" }
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard2.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard2.C
new file mode 100644
index 0000000..56022c3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard2.C
@@ -0,0 +1,10 @@ 
+// { dg-do compile { target c++11 } }
+
+struct [[nodiscard]] A { };	// { dg-message "" }
+
+A f();				// { dg-message "" }
+
+int main()
+{
+  f();				// { dg-warning "Wunused-result" }
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard3.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard3.C
new file mode 100644
index 0000000..bc2a032
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard3.C
@@ -0,0 +1,203 @@ 
+/* nodiscard attribute tests, adapted from gcc.dg/attr-warn-unused-result.c.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c++1z -O -ftrack-macro-expansion=0" } */
+
+#define WUR [[nodiscard]]
+#define WURAI [[nodiscard, gnu::always_inline]] inline
+enum [[nodiscard]] E { e };
+typedef E (*fnt) (void);
+
+typedef struct { long i; } A;
+typedef struct { long i; long j; } B;
+typedef struct { char big[1024]; fnt fn; } C;
+struct [[nodiscard]] D { int i; D(); ~D(); };
+
+WUR E check1 (void);
+WUR void check2 (void); /* { dg-warning "nodiscard" } */
+WUR int foo;		/* { dg-warning "nodiscard" } */
+int bar (void);
+WURAI E check3 (void) { return (E)bar (); }
+WUR A check4 (void);
+WUR B check5 (void);
+WUR C check6 (void);
+A bar7 (void);
+B bar8 (void);
+C bar9 (void);
+WURAI A check7 (void) { return bar7 (); }
+WURAI B check8 (void) { return bar8 (); }
+WURAI C check9 (void) { return bar9 (); }
+/* This is useful for checking whether return value of statement
+   expressions (returning int in this case) is used.  */
+WURAI int check_int_result (int res) { return res; }
+#define GU(v) ({ int e = 0; (v) = bar (); if ((v) < 23) e = 14; e; })
+fnt fnptr;
+WUR E check10 (void);
+int baz (void);
+WURAI E check11 (void) { return (E)baz (); }
+int k;
+
+D check12();
+
+void
+test (void)
+{
+  int i = 0, j;
+  const fnt pcheck1 = check1;
+  const fnt pcheck3 = check3;
+  A a;
+  B b;
+  C c;
+  D d;
+  if (check1 ())
+    return;
+  i += check1 ();
+  i += ({ check1 (); });
+  check1 ();		/* { dg-warning "nodiscard" } */
+  (void) check1 ();
+  check1 (), bar ();	/* { dg-warning "nodiscard" } */
+  check2 ();
+  (void) check2 ();
+  check2 (), bar ();
+  if (check3 ())
+    return;
+  i += check3 ();
+  i += ({ check3 (); });
+  check3 ();		/* { dg-warning "nodiscard" } */
+  (void) check3 ();
+  check3 (), bar ();	/* { dg-warning "nodiscard" } */
+  a = check4 ();
+  if (a.i)
+    return;
+  if (check4 ().i)
+    return;
+  if (({ check4 (); }).i)
+    return;
+  check4 ();		/* { dg-warning "nodiscard" } */
+  (void) check4 ();
+  check4 (), bar ();	/* { dg-warning "nodiscard" } */
+  b = check5 ();
+  if (b.i + b.j)
+    return;
+  if (check5 ().j)
+    return;
+  if (({ check5 (); }).j)
+    return;
+  check5 ();		/* { dg-warning "nodiscard" } */
+  (void) check5 ();
+  check5 (), bar ();	/* { dg-warning "nodiscard" } */
+  c = check6 ();
+  if (c.big[12] + c.big[29])
+    return;
+  if (check6 ().big[27])
+    return;
+  if (({ check6 (); }).big[0])
+    return;
+  check6 ();		/* { dg-warning "nodiscard" } */
+  (void) check6 ();
+  check6 (), bar ();	/* { dg-warning "nodiscard" } */
+  a = check7 ();
+  if (a.i)
+    return;
+  if (check7 ().i)
+    return;
+  if (({ check7 (); }).i)
+    return;
+  check7 ();		/* { dg-warning "nodiscard" } */
+  (void) check7 ();
+  check7 (), bar ();	/* { dg-warning "nodiscard" } */
+  b = check8 ();
+  if (b.i + b.j)
+    return;
+  if (check8 ().j)
+    return;
+  if (({ check8 (); }).j)
+    return;
+  check8 ();		/* { dg-warning "nodiscard" } */
+  (void) check8 ();
+  check8 (), bar ();	/* { dg-warning "nodiscard" } */
+  c = check9 ();
+  if (c.big[12] + c.big[29])
+    return;
+  if (check9 ().big[27])
+    return;
+  if (({ check9 (); }).big[0])
+    return;
+  check9 ();		/* { dg-warning "nodiscard" } */
+  (void) check9 ();
+  check9 (), bar ();	/* { dg-warning "nodiscard" } */
+  if (check_int_result (GU (j)))
+    return;
+  i += check_int_result (GU (j));
+  i += ({ check_int_result (GU (j)); });
+  check_int_result (GU (j)); /* { dg-warning "nodiscard" } */
+  (void) check_int_result (GU (j));
+  check_int_result (GU (j)), bar (); /* { dg-warning "nodiscard" } */
+  if (fnptr ())
+    return;
+  i += fnptr ();
+  i += ({ fnptr (); });
+  fnptr ();		/* { dg-warning "nodiscard" } */
+  (void) fnptr ();
+  fnptr (), bar ();	/* { dg-warning "nodiscard" } */
+  fnptr = check1;
+  if (fnptr ())
+    return;
+  i += fnptr ();
+  i += ({ fnptr (); });
+  fnptr ();		/* { dg-warning "nodiscard" } */
+  (void) fnptr ();
+  fnptr (), bar ();	/* { dg-warning "nodiscard" } */
+  fnptr = check3;
+  if (fnptr ())
+    return;
+  i += fnptr ();
+  i += ({ fnptr (); });
+  fnptr ();		/* { dg-warning "nodiscard" } */
+  (void) fnptr ();
+  fnptr (), bar ();	/* { dg-warning "nodiscard" } */
+  if (bar9 ().fn ())
+    return;
+  i += bar9 ().fn ();
+  i += ({ bar9 ().fn (); });
+  bar9 ().fn ();	/* { dg-warning "nodiscard" } */
+  (void) bar9 ().fn ();
+  bar9 ().fn (), bar (); /* { dg-warning "nodiscard" } */
+  if ((k ? check1 : check10) ())
+    return;
+  i += (k ? check1 : check10) ();
+  i += ({ (k ? check1 : check10) (); });
+  (k ? check1 : check10) (); /* { dg-warning "nodiscard" } */
+  (void) (k ? check1 : check10) ();
+  (k ? check1 : check10) (), bar (); /* { dg-warning "nodiscard" } */
+  if ((k ? check3 : check11) ())
+    return;
+  i += (k ? check3 : check11) ();
+  i += ({ (k ? check3 : check11) (); });
+  (k ? check3 : check11) (); /* { dg-warning "nodiscard" } */
+  (void) (k ? check3 : check11) ();
+  (k ? check3 : check11) (), bar (); /* { dg-warning "nodiscard" } */
+  if (pcheck1 ())
+    return;
+  i += pcheck1 ();
+  i += ({ pcheck1 (); });
+  pcheck1 ();		/* { dg-warning "nodiscard" } */
+  (void) pcheck1 ();
+  pcheck1 (), bar ();	/* { dg-warning "nodiscard" } */
+  if (pcheck3 ())
+    return;
+  i += pcheck3 ();
+  i += ({ pcheck3 (); });
+  pcheck3 ();		/* { dg-warning "nodiscard" } */
+  (void) pcheck3 ();
+  pcheck3 (), bar ();	/* { dg-warning "nodiscard" } */
+  d = check12 ();
+  if (d.i)
+    return;
+  if (check12 ().i)
+    return;
+  if (({ check12 (); }).i)
+    return;
+  check12 ();		/* { dg-warning "nodiscard" } */
+  (void) check12 ();
+  check12 (), bar ();	/* { dg-warning "nodiscard" } */
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-result-2.C b/gcc/testsuite/g++.dg/warn/Wunused-result-2.C
new file mode 100644
index 0000000..56ed17a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-result-2.C
@@ -0,0 +1,21 @@ 
+// PR c++/66177
+
+struct QSize
+{
+    QSize(int w, int h) : wd(w), ht(h) {}
+
+    QSize expandedTo() const __attribute__ ((__warn_unused_result__))
+    {
+        return QSize(2, 3);
+    }
+
+private:
+    int wd;
+    int ht;
+};
+
+void foo()
+{
+    QSize sz(2, 2);
+    sz.expandedTo();		// { dg-warning "warn_unused_result" }
+}
diff --git a/gcc/testsuite/g++.dg/warn/unused-result1.C b/gcc/testsuite/g++.dg/warn/unused-result1.C
index 466c99e..1b9ef8a 100644
--- a/gcc/testsuite/g++.dg/warn/unused-result1.C
+++ b/gcc/testsuite/g++.dg/warn/unused-result1.C
@@ -6,5 +6,5 @@  public:
 };
 class QString {
   QByteArray toLocal8Bit() const __attribute__ ((warn_unused_result));
-  void fooWarnHere() const { toLocal8Bit(); } // { dg-warning "ignoring" "" { xfail *-*-* } }
+  void fooWarnHere() const { toLocal8Bit(); } // { dg-warning "ignoring" }
 };
diff --git a/gcc/testsuite/gcc.dg/attr-warn-unused-result.c b/gcc/testsuite/gcc.dg/attr-warn-unused-result.c
deleted file mode 100644
index 0404cec..0000000
--- a/gcc/testsuite/gcc.dg/attr-warn-unused-result.c
+++ /dev/null
@@ -1,188 +0,0 @@ 
-/* warn_unused_result attribute tests.  */
-/* { dg-do compile } */
-/* { dg-options "-O" } */
-
-#define WUR __attribute__((warn_unused_result))
-#define WURAI __attribute__((warn_unused_result, always_inline)) inline
-typedef WUR int (*fnt) (void);
-
-typedef struct { long i; } A;
-typedef struct { long i; long j; } B;
-typedef struct { char big[1024]; fnt fn; } C;
-
-WUR int check1 (void);
-WUR void check2 (void); /* { dg-warning "attribute ignored" } */
-int foo WUR;	  /* { dg-warning "only applies" } */
-int bar (void);
-extern WURAI int check3 (void) { return bar (); }
-WUR A check4 (void);
-WUR B check5 (void);
-WUR C check6 (void);
-A bar7 (void);
-B bar8 (void);
-C bar9 (void);
-extern WURAI A check7 (void) { return bar7 (); }
-extern WURAI B check8 (void) { return bar8 (); }
-extern WURAI C check9 (void) { return bar9 (); }
-/* This is useful for checking whether return value of statement
-   expressions (returning int in this case) is used.  */
-extern WURAI int check_int_result (int res) { return res; }
-#define GU(v) ({ int e = 0; (v) = bar (); if ((v) < 23) e = 14; e; })
-fnt fnptr;
-WUR int check10 (void);
-int baz (void);
-extern WURAI int check11 (void) { return baz (); }
-int k;
-
-void
-test (void)
-{
-  int i = 0, j;
-  const fnt pcheck1 = check1;
-  const fnt pcheck3 = check3;
-  A a;
-  B b;
-  C c;
-  if (check1 ())
-    return;
-  i += check1 ();
-  i += ({ check1 (); });
-  check1 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check1 ();	/* { dg-warning "ignoring return value of" } */
-  check1 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  check2 ();
-  (void) check2 ();
-  check2 (), bar ();
-  if (check3 ())
-    return;
-  i += check3 ();
-  i += ({ check3 (); });
-  check3 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check3 ();	/* { dg-warning "ignoring return value of" } */
-  check3 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  a = check4 ();
-  if (a.i)
-    return;
-  if (check4 ().i)
-    return;
-  if (({ check4 (); }).i)
-    return;
-  check4 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check4 ();	/* { dg-warning "ignoring return value of" } */
-  check4 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  b = check5 ();
-  if (b.i + b.j)
-    return;
-  if (check5 ().j)
-    return;
-  if (({ check5 (); }).j)
-    return;
-  check5 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check5 ();	/* { dg-warning "ignoring return value of" } */
-  check5 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  c = check6 ();
-  if (c.big[12] + c.big[29])
-    return;
-  if (check6 ().big[27])
-    return;
-  if (({ check6 (); }).big[0])
-    return;
-  check6 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check6 ();	/* { dg-warning "ignoring return value of" } */
-  check6 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  a = check7 ();
-  if (a.i)
-    return;
-  if (check7 ().i)
-    return;
-  if (({ check7 (); }).i)
-    return;
-  check7 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check7 ();	/* { dg-warning "ignoring return value of" } */
-  check7 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  b = check8 ();
-  if (b.i + b.j)
-    return;
-  if (check8 ().j)
-    return;
-  if (({ check8 (); }).j)
-    return;
-  check8 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check8 ();	/* { dg-warning "ignoring return value of" } */
-  check8 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  c = check9 ();
-  if (c.big[12] + c.big[29])
-    return;
-  if (check9 ().big[27])
-    return;
-  if (({ check9 (); }).big[0])
-    return;
-  check9 ();		/* { dg-warning "ignoring return value of" } */
-  (void) check9 ();	/* { dg-warning "ignoring return value of" } */
-  check9 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  if (check_int_result (GU (j)))
-    return;
-  i += check_int_result (GU (j));
-  i += ({ check_int_result (GU (j)); });
-  check_int_result (GU (j)); /* { dg-warning "ignoring return value of" } */
-  (void) check_int_result (GU (j)); /* { dg-warning "ignoring return value of" } */
-  check_int_result (GU (j)), bar (); /* { dg-warning "ignoring return value of" } */
-  if (fnptr ())
-    return;
-  i += fnptr ();
-  i += ({ fnptr (); });
-  fnptr ();		/* { dg-warning "ignoring return value of" } */
-  (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
-  fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
-  fnptr = check1;
-  if (fnptr ())
-    return;
-  i += fnptr ();
-  i += ({ fnptr (); });
-  fnptr ();		/* { dg-warning "ignoring return value of" } */
-  (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
-  fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
-  fnptr = check3;
-  if (fnptr ())
-    return;
-  i += fnptr ();
-  i += ({ fnptr (); });
-  fnptr ();		/* { dg-warning "ignoring return value of" } */
-  (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
-  fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
-  if (bar9 ().fn ())
-    return;
-  i += bar9 ().fn ();
-  i += ({ bar9 ().fn (); });
-  bar9 ().fn ();	/* { dg-warning "ignoring return value of" } */
-  (void) bar9 ().fn ();	/* { dg-warning "ignoring return value of" } */
-  bar9 ().fn (), bar (); /* { dg-warning "ignoring return value of" } */
-  if ((k ? check1 : check10) ())
-    return;
-  i += (k ? check1 : check10) ();
-  i += ({ (k ? check1 : check10) (); });
-  (k ? check1 : check10) (); /* { dg-warning "ignoring return value of" } */
-  (void) (k ? check1 : check10) (); /* { dg-warning "ignoring return value of" } */
-  (k ? check1 : check10) (), bar (); /* { dg-warning "ignoring return value of" } */
-  if ((k ? check3 : check11) ())
-    return;
-  i += (k ? check3 : check11) ();
-  i += ({ (k ? check3 : check11) (); });
-  (k ? check3 : check11) (); /* { dg-warning "ignoring return value of" } */
-  (void) (k ? check3 : check11) (); /* { dg-warning "ignoring return value of" } */
-  (k ? check3 : check11) (), bar (); /* { dg-warning "ignoring return value of" } */
-  if (pcheck1 ())
-    return;
-  i += pcheck1 ();
-  i += ({ pcheck1 (); });
-  pcheck1 ();		/* { dg-warning "ignoring return value of" } */
-  (void) pcheck1 ();	/* { dg-warning "ignoring return value of" } */
-  pcheck1 (), bar ();	/* { dg-warning "ignoring return value of" } */
-  if (pcheck3 ())
-    return;
-  i += pcheck3 ();
-  i += ({ pcheck3 (); });
-  pcheck3 ();		/* { dg-warning "ignoring return value of" } */
-  (void) pcheck3 ();	/* { dg-warning "ignoring return value of" } */
-  pcheck3 (), bar ();	/* { dg-warning "ignoring return value of" } */
-}

commit 1cf189fd1dd64f663407d8a71b009bfef7bab042
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Apr 28 10:14:07 2016 -0400

    	* cvt.c (cp_get_callee): New.
    
    	* constexpr.c (get_function_named_in_call): Use it.
    	* cxx-pretty-print.c (postfix_expression): Use it.
    	* except.c (check_noexcept_r): Use it.
    	* method.c (check_nontriv): Use it.
    	* tree.c (build_aggr_init_expr): Use it.
    	* cp-tree.h: Declare it.

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index f0307a3..6054d1a 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -1044,21 +1044,7 @@  save_fundef_copy (tree fun, tree copy)
 static tree
 get_function_named_in_call (tree t)
 {
-  tree fun = NULL;
-  switch (TREE_CODE (t))
-    {
-    case CALL_EXPR:
-      fun = CALL_EXPR_FN (t);
-      break;
-
-    case AGGR_INIT_EXPR:
-      fun = AGGR_INIT_EXPR_FN (t);
-      break;
-
-    default:
-      gcc_unreachable();
-      break;
-    }
+  tree fun = cp_get_callee (t);
   if (fun && TREE_CODE (fun) == ADDR_EXPR
       && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
     fun = TREE_OPERAND (fun, 0);
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 4c548c9..f6ea0b7 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -5694,6 +5694,7 @@  extern tree ocp_convert				(tree, tree, int, int,
 extern tree cp_convert				(tree, tree, tsubst_flags_t);
 extern tree cp_convert_and_check                (tree, tree, tsubst_flags_t);
 extern tree cp_fold_convert			(tree, tree);
+extern tree cp_get_callee			(tree);
 extern tree convert_to_void			(tree, impl_conv_void,
                                  		 tsubst_flags_t);
 extern tree convert_force			(tree, tree, int,
diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 0d1048c..8c9d78b 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -904,6 +904,20 @@  ocp_convert (tree type, tree expr, int convtype, int flags,
   return error_mark_node;
 }
 
+/* If CALL is a call, return the callee; otherwise null.  */
+
+tree
+cp_get_callee (tree call)
+{
+  if (call == NULL_TREE)
+    return call;
+  else if (TREE_CODE (call) == CALL_EXPR)
+    return CALL_EXPR_FN (call);
+  else if (TREE_CODE (call) == AGGR_INIT_EXPR)
+    return AGGR_INIT_EXPR_FN (call);
+  return NULL_TREE;
+}
+
 /* When an expression is used in a void context, its value is discarded and
    no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
    stmt.expr/1, expr.comma/1].  This permits dereferencing an incomplete type
diff --git a/gcc/cp/cxx-pretty-print.c b/gcc/cp/cxx-pretty-print.c
index cc28045..3b52a35 100644
--- a/gcc/cp/cxx-pretty-print.c
+++ b/gcc/cp/cxx-pretty-print.c
@@ -490,8 +490,7 @@  cxx_pretty_printer::postfix_expression (tree t)
     case AGGR_INIT_EXPR:
     case CALL_EXPR:
       {
-	tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
-					   : CALL_EXPR_FN (t));
+	tree fun = cp_get_callee (t);
 	tree saved_scope = enclosing_scope;
 	bool skipfirst = false;
 	tree arg;
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index 5336710..014df50 100644
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
@@ -1158,8 +1158,7 @@  check_noexcept_r (tree *tp, int * /*walk_subtrees*/, void * /*data*/)
 	 translation unit, creating ODR problems.
 
          We could use TREE_NOTHROW (t) for !TREE_PUBLIC fns, though... */
-      tree fn = (code == AGGR_INIT_EXPR
-		 ? AGGR_INIT_EXPR_FN (t) : CALL_EXPR_FN (t));
+      tree fn = cp_get_callee (t);
       tree type = TREE_TYPE (fn);
       gcc_assert (POINTER_TYPE_P (type));
       type = TREE_TYPE (type);
diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index 862451f..0e501d9 100644
--- a/gcc/cp/method.c
+++ b/gcc/cp/method.c
@@ -1002,12 +1002,8 @@  get_inherited_ctor (tree ctor)
 static tree
 check_nontriv (tree *tp, int *, void *)
 {
-  tree fn;
-  if (TREE_CODE (*tp) == CALL_EXPR)
-    fn = CALL_EXPR_FN (*tp);
-  else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
-    fn = AGGR_INIT_EXPR_FN (*tp);
-  else
+  tree fn = cp_get_callee (*tp);
+  if (fn == NULL_TREE)
     return NULL_TREE;
 
   if (TREE_CODE (fn) == ADDR_EXPR)
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index 5b5d5ba..97601aa 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -470,11 +470,8 @@  build_aggr_init_expr (tree type, tree init)
   if (processing_template_decl)
     return init;
 
-  if (TREE_CODE (init) == CALL_EXPR)
-    fn = CALL_EXPR_FN (init);
-  else if (TREE_CODE (init) == AGGR_INIT_EXPR)
-    fn = AGGR_INIT_EXPR_FN (init);
-  else
+  fn = cp_get_callee (init);
+  if (fn == NULL_TREE)
     return convert (type, init);
 
   is_ctor = (TREE_CODE (fn) == ADDR_EXPR