diff options
author | Jakub Jelinek <jakub@redhat.com> | 2020-11-10 15:56:20 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2020-11-10 15:56:20 +0100 |
commit | 0000ea4fb4eaacbd2c954d78d7f8e9f03c7be739 (patch) | |
tree | ea86e5aadf8bd224dbbdbaf8d0aea40a784e69b3 /gcc | |
parent | 1693746302e4306b43cb66a0afe589137069bd8e (diff) | |
download | gcc-0000ea4fb4eaacbd2c954d78d7f8e9f03c7be739.zip gcc-0000ea4fb4eaacbd2c954d78d7f8e9f03c7be739.tar.gz gcc-0000ea4fb4eaacbd2c954d78d7f8e9f03c7be739.tar.bz2 |
c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748]
The -Wunused-value warning in both C and C++ FEs (implemented
significantly differently between the two) sees the COMPLEX_EXPRs created
e.g. for complex pre/post increment and many other expressions as useless
and warns about it.
For the C warning implementation, on e.g.
COMPLEX_EXPR < ++REALPART_EXPR <x>, IMAGPART_EXPR <x>>;
would warn even on the IMAGPART_EXPR <x> there alone etc., so what works
is check if we'd warn about both operands of COMPLEX_EXPR and if yes,
warn on the whole COMPLEX_EXPR, otherwise don't warn.
The C++ warning implementation is significantly different and for that one
the only warn if both would be warned about doesn't really work,
we then miss warnings e.g. about
COMPLEX_EXPR <REALPART_EXPR <SAVE_EXPR <x>> + 1.0e+0, IMAGPART_EXPR <SAVE_EXPR <x>>> >>>>>
The patch replaces the warning_at call with call to the c-family
warn_if_unused_value function.
On the testcase which after the initial new tests contains pretty much
everything from gcc.dg/Wunused-value-1.c both approaches seem to work
nicely.
2020-11-10 Jakub Jelinek <jakub@redhat.com>
PR c/97748
gcc/c-family/
* c-common.h (warn_if_unused_value): Add quiet argument defaulted
to false.
* c-warn.c (warn_if_unused_value): Likewise. Pass it down
recursively and just return true instead of warning if it is true.
Handle COMPLEX_EXPR.
gcc/cp/
* cvt.c (convert_to_void): Check (complain & tf_warning) in the outer
if rather than twice times in the inner one. Use warn_if_unused_value.
Formatting fix.
gcc/testsuite/
* c-c++-common/Wunused-value-1.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/c-family/c-common.h | 2 | ||||
-rw-r--r-- | gcc/c-family/c-warn.c | 13 | ||||
-rw-r--r-- | gcc/cp/cvt.c | 38 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/Wunused-value-1.c | 33 |
4 files changed, 59 insertions, 27 deletions
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index b80db23..94f4868 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1365,7 +1365,7 @@ extern void warn_tautological_cmp (const op_location_t &, enum tree_code, tree, tree); extern void warn_logical_not_parentheses (location_t, enum tree_code, tree, tree); -extern bool warn_if_unused_value (const_tree, location_t); +extern bool warn_if_unused_value (const_tree, location_t, bool = false); extern bool strict_aliasing_warning (location_t, tree, tree); extern void sizeof_pointer_memaccess_warning (location_t *, tree, vec<tree, va_gc> *, tree *, diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index 68b093e..6d1f9a7 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -585,7 +585,7 @@ warn_logical_not_parentheses (location_t location, enum tree_code code, (potential) location of the expression. */ bool -warn_if_unused_value (const_tree exp, location_t locus) +warn_if_unused_value (const_tree exp, location_t locus, bool quiet) { restart: if (TREE_USED (exp) || TREE_NO_WARNING (exp)) @@ -633,7 +633,7 @@ warn_if_unused_value (const_tree exp, location_t locus) goto restart; case COMPOUND_EXPR: - if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) + if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet)) return true; /* Let people do `(foo (), 0)' without a warning. */ if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) @@ -648,6 +648,13 @@ warn_if_unused_value (const_tree exp, location_t locus) return false; goto warn; + case COMPLEX_EXPR: + /* Warn only if both operands are unused. */ + if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, true) + && warn_if_unused_value (TREE_OPERAND (exp, 1), locus, true)) + goto warn; + return false; + case INDIRECT_REF: /* Don't warn about automatic dereferencing of references, since the user cannot control it. */ @@ -671,6 +678,8 @@ warn_if_unused_value (const_tree exp, location_t locus) return false; warn: + if (quiet) + return true; return warning_at (locus, OPT_Wunused_value, "value computed is not used"); } } diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index c9e7b1f..bcd7c5a 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -1568,12 +1568,13 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain) && warn_unused_value && !TREE_NO_WARNING (expr) && !processing_template_decl - && !cp_unevaluated_operand) + && !cp_unevaluated_operand + && (complain & tf_warning)) { /* The middle end does not warn about expressions that have been explicitly cast to void, so we must do so here. */ - if (!TREE_SIDE_EFFECTS (expr)) { - if (complain & tf_warning) + if (!TREE_SIDE_EFFECTS (expr)) + { switch (implicit) { case ICV_SECOND_OF_COND: @@ -1605,14 +1606,10 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain) default: gcc_unreachable (); } - } + } else { - tree e; - enum tree_code code; - enum tree_code_class tclass; - - e = expr; + tree e = expr; /* We might like to warn about (say) "(int) f()", as the cast has no effect, but the compiler itself will generate implicit conversions under some @@ -1626,21 +1623,14 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain) while (TREE_CODE (e) == NOP_EXPR) e = TREE_OPERAND (e, 0); - code = TREE_CODE (e); - tclass = TREE_CODE_CLASS (code); - if ((tclass == tcc_comparison - || tclass == tcc_unary - || (tclass == tcc_binary - && !(code == MODIFY_EXPR - || code == INIT_EXPR - || code == PREDECREMENT_EXPR - || code == PREINCREMENT_EXPR - || code == POSTDECREMENT_EXPR - || code == POSTINCREMENT_EXPR)) - || code == VEC_PERM_EXPR - || code == VEC_COND_EXPR) - && (complain & tf_warning)) - warning_at (loc, OPT_Wunused_value, "value computed is not used"); + enum tree_code code = TREE_CODE (e); + enum tree_code_class tclass = TREE_CODE_CLASS (code); + if (tclass == tcc_comparison + || tclass == tcc_unary + || tclass == tcc_binary + || code == VEC_PERM_EXPR + || code == VEC_COND_EXPR) + warn_if_unused_value (e, loc); } } expr = build1 (CONVERT_EXPR, void_type_node, expr); diff --git a/gcc/testsuite/c-c++-common/Wunused-value-1.c b/gcc/testsuite/c-c++-common/Wunused-value-1.c new file mode 100644 index 0000000..90c9d93 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wunused-value-1.c @@ -0,0 +1,33 @@ +/* PR c/97748 */ +/* { dg-do compile } */ +/* { dg-options "-Wunused-value" } */ + +double _Complex f (); +double _Complex *p; + +double _Complex +foo (double _Complex x) +{ + ++x; /* { dg-bogus "value computed is not used" } */ + --x; /* { dg-bogus "value computed is not used" } */ + x += 1; /* { dg-bogus "value computed is not used" } */ + x += 1.0iF; /* { dg-bogus "value computed is not used" } */ + x++; /* { dg-bogus "value computed is not used" } */ + x--; /* { dg-bogus "value computed is not used" } */ + x + 1; /* { dg-warning "value computed is not used" } */ + (void) (x + 1); /* { dg-bogus "value computed is not used" } */ + 1 + f (); /* { dg-warning "value computed is not used" } */ + f () + f (); /* { dg-warning "value computed is not used" } */ + f () + f (), f (); /* { dg-warning "value computed is not used" } */ + f (); + (void) f (); + *p++; /* { dg-warning "value computed is not used" } */ + ++*p; /* { dg-bogus "value computed is not used" } */ + (*p ? f () : 0); + ({ f (); }); + ({ f () + 1; }); + ({ f (); 0; }); + ({ f () + 1; 0; }); /* { dg-warning "value computed is not used" } */ + 1 + ({ f (); }); /* { dg-warning "value computed is not used" } */ + return x; +} |