This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [Consult] g++: About "-Wunused-variable" for constant variable initialized by function
- From: Chen Gang <gang dot chen dot 5i5j at gmail dot com>
- To: Peter Maydell <peter dot maydell at linaro dot org>
- Cc: Jeff Law <law at redhat dot com>, jason at redhat dot com, nathan at codesourcery dot com, gcc at gcc dot gnu dot org
- Date: Mon, 13 Oct 2014 21:10:31 +0800
- Subject: Re: [Consult] g++: About "-Wunused-variable" for constant variable initialized by function
- Authentication-results: sourceware.org; auth=none
- References: <543A910F dot 7060301 at gmail dot com> <CAFEAcA8__ewvHy83-VX_iASm_=mBRA_jCdy8bMRPHt_H-EeXhw at mail dot gmail dot com>
On 10/13/14 18:58, Peter Maydell wrote:
> On 12 October 2014 16:32, Chen Gang <gang.chen.5i5j@gmail.com> wrote:
>> Hello All:
>>
>> I found an issue about g++, it is OK for "-Wall -O0", but will report
>> -Wunused-variable for "-Wall -O1|2|3|s". The original version (e.g.
>> gcc 4.8.3 redhat version) does not report warning for "-Wall -O?".
>>
>> The related operation:
>>
>> [root@localhost qemu_cc]# cat test.cc
>> const char n() { return 1; }
>> const char c = n();
>> [root@localhost qemu_cc]# /usr/local/bin/g++ -Wall -O0 -c -o test.o test.cc
>> [root@localhost qemu_cc]# /usr/local/bin/g++ -Wall -O2 -c -o test.o test.cc
>> test.cc:2:12: warning: 'c' defined but not used [-Wunused-variable]
>> const char c = n();
>> ^
>
> This cut down test case seems to have discarded two interesting
> things about the original issue
> (context: http://lists.gnu.org/archive/html/qemu-devel/2014-10/msg01253.html)
> (1) the definitions of the const variables are in a header
> (2) gcc doesn't warn about const declarations in the same
> header which use 'int' rather than 'float' or 'double'
>
> That said, how does g++ know that the variable isn't defined
> for the benefit of another translation unit? (Conversely, how
> should a library define constants in a header for the benefit
> of users of the library in a way that doesn't make g++ complain
> if the library using code happens not to use the constant?)
>
Oh, yes. Originally I got this warning by compiling Qemu. And sorry for
my sample (test.cc) may be not quite precise.
For me, I guess:
- If the constant number is defined in the header file, and never be
used, our g++ need not report warning about [-Wunused-variable].
- But if the constant number is defined in the source file, and never
be used, our g++ need report warning about [-Wunused-variable] (just
like what Jason Merrill said).
Is my guess correct?
Thanks.
--
Chen Gang
Open, share, and attitude like air, water, and life which God blessed