]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Implement C++17 [[nodiscard]] attribute.
authorJason Merrill <jason@redhat.com>
Thu, 28 Apr 2016 19:01:19 +0000 (15:01 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 28 Apr 2016 19:01:19 +0000 (15:01 -0400)
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.

From-SVN: r235597

13 files changed:
gcc/c-family/ChangeLog
gcc/c-family/c-lex.c
gcc/cp/ChangeLog
gcc/cp/cp-tree.h
gcc/cp/cvt.c
gcc/cp/tree.c
gcc/testsuite/c-c++-common/attr-warn-unused-result.c [moved from gcc/testsuite/gcc.dg/attr-warn-unused-result.c with 98% similarity]
gcc/testsuite/g++.dg/cpp1z/feat-cxx1z.C
gcc/testsuite/g++.dg/cpp1z/nodiscard1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/nodiscard2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/nodiscard3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wunused-result-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/unused-result1.C

index 8b1e76457b739a416ac955de484ed41ee2d1406b..9e1695e5601e09a3b2e1f07fcd76f181c8095b28 100644 (file)
@@ -1,3 +1,7 @@
+2016-04-28  Jason Merrill  <jason@redhat.com>
+
+       * c-lex.c (c_common_has_attribute): Handle nodiscard.
+
 2016-04-28  Eduard Sanou  <dhole@openmailbox.org>
            Matthias Klose  <doko@debian.org>
 
index ff7eb25b9f7c81e35a597ad624a8cc2d350f2768..38a428d533381dfa87e95172771c04e37d6dc54b 100644 (file)
@@ -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;
index ff6d30a00f7377f66a2ebea19053fd7d33889a19..d8f35af2e305983895704a4014316a61aadca9cc 100644 (file)
@@ -1,5 +1,15 @@
 2016-04-28  Jason Merrill  <jason@redhat.com>
 
+       Implement C++17 [[nodiscard]] attribute.
+       PR c++/38172
+       PR c++/54379
+       * 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.
+
        * cvt.c (cp_get_callee): New.
        * constexpr.c (get_function_named_in_call): Use it.
        * cxx-pretty-print.c (postfix_expression): Use it.
index f6ea0b734c2273e68a99f8695a91099c7224cf1b..8a06609dc0497b2101af5618219afe40b0f2f1a2 100644 (file)
@@ -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,
index 8c9d78b641c69147f653fa3eca7223f26cf9be58..2e2bac74307e98ffa17b48d1748688b4f9ead4e0 100644 (file)
@@ -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:;
index 97601aa1f186e055bbcd35f9f06d19f406db3599..d7e9c7b804861a4a026f7ac719fdb81ebdeeaf10 100644 (file)
@@ -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 }
 };
 
similarity index 98%
rename from gcc/testsuite/gcc.dg/attr-warn-unused-result.c
rename to gcc/testsuite/c-c++-common/attr-warn-unused-result.c
index 0404ceceb609151d728a56f5410e68b78c66c883..adf850c19036c3f919e00abead77fadfe65e2754 100644 (file)
@@ -1,6 +1,6 @@
 /* warn_unused_result attribute tests.  */
 /* { dg-do compile } */
-/* { dg-options "-O" } */
+/* { dg-options "-O -ftrack-macro-expansion=0" } */
 
 #define WUR __attribute__((warn_unused_result))
 #define WURAI __attribute__((warn_unused_result, always_inline)) inline
@@ -12,7 +12,7 @@ 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 foo WUR;     /* { dg-message "only applies" } */
 int bar (void);
 extern WURAI int check3 (void) { return bar (); }
 WUR A check4 (void);
index fe7a4c23290ab89e0df6d996bd12788b6dca1953..f8a87a8ddc3721a6feb0119864d2312b7795b49c 100644 (file)
 #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 (file)
index 0000000..5f2345a
--- /dev/null
@@ -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 (file)
index 0000000..56022c3
--- /dev/null
@@ -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 (file)
index 0000000..bc2a032
--- /dev/null
@@ -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 (file)
index 0000000..56ed17a
--- /dev/null
@@ -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" }
+}
index 466c99e7d97b92620e5b7e3eae729e806d26c24b..1b9ef8af786bfe8f695335ad0cce7d95f48a389f 100644 (file)
@@ -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" }
 };