Bug 1310929 - -Wunused-const-variable gives too many false positives
-Wunused-const-variable gives too many false positives
Status: CLOSED RAWHIDE
Product: Fedora
Classification: Fedora
Component: gcc (Show other bugs)
24
Unspecified Unspecified
unspecified Severity unspecified
: ---
: ---
Assigned To: Jakub Jelinek
Fedora Extras Quality Assurance
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2016-02-22 20:04 EST by wibrown@redhat.com
Modified: 2016-03-10 10:11 EST (History)
5 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2016-03-10 10:11:28 EST
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description wibrown@redhat.com 2016-02-22 20:04:52 EST
Description of problem:
It's a common pattern in large software projects to define a single header file shared by many smaller object files.

This leads to gcc displaying hundreds of unused const variable warnings where a const defined in the .h may be used in 2 or 3 source, files, but not all. In any file it is not used, the warning is omitted.

For example:

I0> cat test_b.c 

#include "test.h"


void do_nothing() {
    return;
}

I0> cat test_a.c 

#include "test.h"

int main(int argc, char **argv) {
    do_nothing();
    printf("%s\n", hello_world);
}

I0> cat test.h 
#include <stdio.h>

void do_nothing();

static char* const hello_world     = "Hello World";

I0> cat Makefile 

all: test_a

test_a: test_b
	gcc -Wall -o test test_b.o test_a.c

test_b:
	gcc -Wall -c -fpic -o test_b.o test_b.c


Running this yields:

gcc -Wall -c -fpic -o test_b.o test_b.c
In file included from test_b.c:2:0:
test.h:5:20: warning: ‘hello_world’ defined but not used [-Wunused-const-variable]
 static char* const hello_world     = "Hello World";
                    ^~~~~~~~~~~
gcc -Wall -o test test_b.o test_a.c


Which is incorrect: the const variable *is* used, but just not in this file yet.

I *like* the idea of the unused-const-variable check, but too many false positives will lead to it being disabled.

I am not sure what the correct solution is here: Perhaps const variables in .h files are ignored? Perhaps there needs to be a way to track these over man runs of gcc through a cache / check file?

Until this is fixed, I do not think that it is wise to include unused-const-variable in -Wall, as it will create excessive noise and could diminish the value of the feature.
Comment 1 Marek Polacek 2016-02-23 04:19:44 EST
As discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=28901, -Wunused-const-variable now has two levels: "One level to only check for unused static const variables in the main
compilation file. Which is enabled by -Wunused-variable. And a second
level that also checks for unused static const variables in included
files. Which must be explicitly enabled."

It'll be in the next Fedora GCC build I suspect.
Comment 2 Jan Kurik 2016-02-24 10:29:02 EST
This bug appears to have been reported against 'rawhide' during the Fedora 24 development cycle.
Changing version to '24'.

More information and reason for this action is here:
https://fedoraproject.org/wiki/Fedora_Program_Management/HouseKeeping/Fedora24#Rawhide_Rebase
Comment 3 Marek Polacek 2016-03-10 10:11:28 EST
Should be fixed in gcc-6.0.0-0.13.fc24.

Note You need to log in before you can comment on or make changes to this bug.