Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++-5.1.1 warns about unused variable only when optimization flag is used

In a large project, I've been getting some compiler warnings from g++-5.1.1 only when building the release version (which uses optimization flags) but not while building the debug version (which disables most compiler optimization). I've narrowed down the problem to a minimal example listed below with commands to reproduce the problem. The problem does not occur if I use g++-4.8.4. Is this a bug in g++-5.1.1? Or, is this code doing something that is legitimately wrong and warrants that warning? Why doesn't it produce any warnings for the last three cases listed in the code (see edit at the bottom for some explanation)?

For those who are interested, here is the bug report in GCC's Bugzilla.

/*

This code complains that the variable 'container' is unused only if optimization
flag is used with g++-5.1.1 while g++-4.8.4 does not produce any warnings in
either case.  Here are the commands to try it out:

$ g++ --version
g++ (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -c -std=c++11 -Wall  -g -O0 test_warnings.cpp

$ g++ -c -std=c++11 -Wall  -O3 test_warnings.cpp
test_warnings.cpp:34:27: warning: ‘container’ defined but not used [-Wunused-variable]
 const std::array<Item, 5> container {} ;
                            ^
*/
#include <array>
struct Item 
{
    int itemValue_ {0} ;
    Item() {} ;
} ;

//
// The warning will go away if you do any one of the following:
// 
// - Comment out the constructor for Item.
// - Remove 'const' from the next line (i.e. make container non-const).
// - Remove '{}' from the next line (i.e. remove initializer list).
//
const std::array<Item, 5> container {} ;
//
// These lines do not produce any warnings:
//
const std::array<Item, 5> container_1 ;
std::array<Item, 5> container_2 ;
std::array<Item, 5> container_3 {} ;

Edit: As mentioned by Ryan Haining in the comments, container_2 and container_3 will have extern linkage and the compiler has no way of warning about their usage.

like image 680
crayzeewulf Avatar asked Oct 03 '15 00:10

crayzeewulf


People also ask

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

What does unused variable mean in C?

No nothing is wrong the compiler just warns you that you declared a variable and you are not using it. It is just a warning not an error. While nothing is wrong, You must avoid declaring variables that you do not need because they just occupy memory and add to the overhead when they are not needed in the first place.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

Which flags would you pass to your C++ compiler so it warns you about uninitialized variables?

Warn about uninitialized variables that are initialized with themselves. Note this option can only be used with the -Wuninitialized option. This warning is enabled by -Wall in C++. This option controls warnings when a declaration does not specify a type.


1 Answers

This looks like a bug, if we look at the documentation for -Wunused-variable it says (emphasis mine):

This option implies -Wunused-const-variable for C, but not for C++

and if we look at -Wunused-const-variable it says:

Warn whenever a constant static variable is unused aside from its declaration. This warning is enabled by -Wunused-variable for C, but not for C++. In C++ this is normally not an error since const variables take the place of #defines in C++.

and we can see this warning goes away in the head revision of gcc.

This gcc bug report: -Wunused-variable ignores unused const initialised variables is also relevant. Although it is against C the C++ case is also discussed.

like image 137
Shafik Yaghmour Avatar answered Sep 27 '22 20:09

Shafik Yaghmour