Bug 1310929 - -Wunused-const-variable gives too many false positives
Summary: -Wunused-const-variable gives too many false positives
Keywords:
Status: CLOSED RAWHIDE
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc
Version: 24
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-02-23 01:04 UTC by wibrown@redhat.com
Modified: 2016-03-10 15:11 UTC (History)
5 users (show)

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2016-03-10 15:11:28 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

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.


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