Commit b632761d by Jason Merrill Committed by Jason Merrill

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.

From-SVN: r235597
parent babaa9df
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>
......
......@@ -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;
......
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.
......
......@@ -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,
......
......@@ -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:;
......
......@@ -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 }
};
......
/* 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);
......
......@@ -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
// { dg-do compile { target c++11 } }
[[nodiscard]] int f();
int main()
{
f(); // { dg-warning "" }
}
// { dg-do compile { target c++11 } }
struct [[nodiscard]] A { }; // { dg-message "" }
A f(); // { dg-message "" }
int main()
{
f(); // { dg-warning "Wunused-result" }
}
/* 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" } */
}
// 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" }
}
......@@ -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" }
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment