Bug 92666 - [10 Regression] bogus -Wunused-but-set-variable in gcov.c with -Wno-restrict
Summary: [10 Regression] bogus -Wunused-but-set-variable in gcov.c with -Wno-restrict
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: 10.0
Assignee: Jakub Jelinek
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2019-11-25 21:01 UTC by Martin Sebor
Modified: 2024-02-17 20:55 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-11-25 00:00:00


Attachments
gcc10-pr92666.patch (1.01 KB, patch)
2019-12-18 17:46 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2019-11-25 21:01:59 UTC
Bootstrap with -Wno-restrict fails due to -Werror=unused-but-set-variable in gcc/gcov.c:

/src/gcc/svn/gcc/gcov.c: In function ‘const char* format_count(gcov_type)’:
/src//gcc/svn/gcc/gcov.c:2328:9: warning: variable ‘r’ set but not used [-Wunused-but-set-variable]
 2328 |   float r = 1.0f * count / divisor;
      |         ^

A small test case reduced from the file is as follows.  This is not reproducible with the C front-end:

$ cat t.c && gcc -O2 -S -Wall -Wextra -Wno-restrict -xc++ t.c
extern "C" int printf (const char*, ...);

void f (unsigned n)
{
  int i = 0;
  while (n >>= 1)
    ++i;
  float r = 1.0f * i;
  printf ("%f", r);
}
t.c: In function ‘void f(unsigned int)’:
t.c:8:9: warning: variable ‘r’ set but not used [-Wunused-but-set-variable]
    8 |   float r = 1.0f * i;
      |         ^
Comment 1 Martin Sebor 2019-11-25 21:12:12 UTC
I thought the problem was introduced with -Wrestrict but it looks like the false positive is actually a regression caused by r276059:

r276059 | jason | 2019-09-23 13:48:00 -0400 (Mon, 23 Sep 2019) | 10 lines

	PR c++/91809 - bit-field and ellipsis.

decay_conversion converts a bit-field access to its declared type, which
isn't what we want here; it even has a comment that the caller is expected
to have already used default_conversion to perform integral promotion.  This
function handles arithmetic promotion differently, but we still don't want
to call decay_conversion before that happens.

	* call.c (convert_arg_to_ellipsis): Don't call decay_conversion for
	arithmetic arguments.
Comment 2 Martin Sebor 2019-11-25 21:36:47 UTC
The check_function_restrict() function is called to do the -Wrestrict checking.  It's called conditionally, from check_function_arguments(), when warn_restrict is nonzero.  When called, the function calls fold_for_warn() for all arguments of the functions it checks for aliasing violations.  fold_for_warn() ends up calling mark_rvalue_use() which ultimately calls mark_exp_read() which sets DECL_READ_P (exp) = 1 for VAR_DECL and PARM_DECL.  None of this happens when -Wno-restrict is set.
Comment 3 Jakub Jelinek 2019-12-18 17:46:32 UTC
Created attachment 47521 [details]
gcc10-pr92666.patch

Untested fix.  Code inspection revealed a wrong-code bug too.
Comment 4 Jakub Jelinek 2019-12-20 23:17:30 UTC
Author: jakub
Date: Fri Dec 20 23:16:58 2019
New Revision: 279681

URL: https://gcc.gnu.org/viewcvs?rev=279681&root=gcc&view=rev
Log:
	PR c++/92666
	* call.c (convert_arg_to_ellipsis): For floating point or
	decltype(nullptr) arguments call mark_rvalue_use.

	* g++.dg/warn/Wunused-var-36.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Wunused-var-36.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Jakub Jelinek 2019-12-20 23:42:27 UTC
Fixed (although the wrong-code fix needs to be backported).