Bug 1310929

Summary: -Wunused-const-variable gives too many false positives
Product: [Fedora] Fedora Reporter: wibrown <wibrown>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED RAWHIDE QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 24CC: davejohansen, jakub, jwakely, law, mpolacek
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-03-10 15:11:28 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description wibrown@redhat.com 2016-02-23 01:04:52 UTC
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 09:19:44 UTC
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 15:29:02 UTC
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 15:11:28 UTC
Should be fixed in gcc-6.0.0-0.13.fc24.