Board index » cppbuilder » bcb6 incorrectly warns 'declared but never used'

bcb6 incorrectly warns 'declared but never used'


2004-01-08 05:52:53 PM
cppbuilder39
if you try this code with bcb6 you'll get warning W8080 'nTimes is declared
but never used'
const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}
on bcb4 instead there is no wrong warning with the same code.
is this a bug?
masi
 
 

Re:bcb6 incorrectly warns 'declared but never used'

On Thu, 8 Jan 2004 10:52:53 +0100, masi wrote:
Quote
is this a bug?
Yes. Known and reported on..um..oh yeah, QC, over a year ago.
Don't expect a fix.
--
Andrue Cope
[Bicester UK]
 

Re:bcb6 incorrectly warns 'declared but never used'

"masi" < XXXX@XXXXX.COM >wrote:
Quote
if you try this code with bcb6 you'll get warning W8080 'nTimes is declared
but never used'

const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}

on bcb4 instead there is no wrong warning with the same code.

is this a bug?
Yes. The optimiser is effectively doing two things. Firstly, it
generates an explicit '3' instead of a reference to nTimes in the loop
condition. Secondly, it checks to see if anything is referring to
nTimes. Due to step 1, nothing is.
A typical case of the left hand not knowing what the right hand is
doing.
A workaround could be to use an enum instead. Or disable the warning. Or
use "(void) nTimes;" as a separate statement.
Alan Bellingham
--
Team Mongolian Wok
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

{smallsort}

Re:bcb6 incorrectly warns 'declared but never used'

Yes.
I think it does that if the constant is declared within the same function
block but not if it is outside. For instance:
No warning
------------------
const int nTimes = 3;
int FuncName()
{
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
------------------
Warning
------------------
int FuncName()
{
const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
------------------
If there are to be bugs, let them all be so benign as this one.
. Ed
Quote
masi wrote in message
news: XXXX@XXXXX.COM ...

if you try this code with bcb6 you'll get warning W8080
'nTimes is declared but never used'

const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}

on bcb4 instead there is no wrong warning with the
same code.

is this a bug?
 

Re:bcb6 incorrectly warns 'declared but never used'

I get around this bug by using the static keyword e.g.
static const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}
simon
"masi" < XXXX@XXXXX.COM >wrote in message
Quote

if you try this code with bcb6 you'll get warning W8080 'nTimes is
declared
but never used'

const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}

on bcb4 instead there is no wrong warning with the same code.

is this a bug?

masi


 

Re:bcb6 incorrectly warns 'declared but never used'

"Simon Hooper" < XXXX@XXXXX.COM >writes:
Quote
I get around this bug by using the static keyword e.g.

static const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}

simon
You might look at the generated asm code, to make sure that you're not
penalizing your runtime performance at the expense of quieting a
warning. (Before, it was "inlining" the value, but if it is now
having to do a memory fetch for this value, you'll be doing extra work
each time you look at this value.) It's not very MUCH work, but it's
still needless, and you're going out of your way to cause it.
I'd suggest just adding a statement like this, like Alan suggested:
const int nTimes = 3;
(void) nTimes; /////<<<<<<<<<<< Just add this, it should noop.
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}
--
Chris (TeamB);
 

Re:bcb6 incorrectly warns 'declared but never used'

Quote
I'd suggest just adding a statement like this, like Alan suggested:

const int nTimes = 3;
(void) nTimes; /////<<<<<<<<<<< Just add this, it should noop.
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}

Wouldn't it be more elegant to temporarily disable the warning using
pragma's?
You are trying to circumvent a bug in the compiler and I think disabling
the warning states clearer what you intend then inserting extra statements.
Eelke
 

Re:bcb6 incorrectly warns 'declared but never used'

Eelke Klein < XXXX@XXXXX.COM >wrote:
Quote
Wouldn't it be more elegant to temporarily disable the warning using
pragma's?
Possibly. Less portable, though. The "(void) expr;" is a fairly well
known idiom used for shutting compilers up.
Quote
You are trying to circumvent a bug in the compiler and I think disabling
the warning states clearer what you intend then inserting extra statements.
# define SHUT_UP_BORLAND_COMPILER(x) (void) x;
:-)
Alan Bellingham
--
Team Thai Kingdom
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:bcb6 incorrectly warns 'declared but never used'

Chris Uzdavinis (TeamB) wrote:
Quote
I'd suggest just adding a statement like this, like Alan suggested:

const int nTimes = 3;
(void) nTimes; /////<<<<<<<<<<< Just add this, it should noop.
This doesn't work unfortunately.
 

Re:bcb6 incorrectly warns 'declared but never used'

Philippe Allain < XXXX@XXXXX.COM >wrote:
Quote
This doesn't work unfortunately.
Rats. Well, I didn't have BCB6 to hand, so sorry.
Alan Bellingham
--
Team Mongolian Wok
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:bcb6 incorrectly warns 'declared but never used'

Alan Bellingham < XXXX@XXXXX.COM >writes:
Quote
# define SHUT_UP_BORLAND_COMPILER(x) (void) x;
Shouldn't that be
#define SHUT_UP_ALMOST_ANY_COMPILER(x) ((void) x);
--
Chris (TeamB);
 

Re:bcb6 incorrectly warns 'declared but never used'

Here's a {*word*193} one that works:
*const_cast<int*>(&nTimes) = nTimes;
But I'm not sure if I would do this. This works too, but I'm not sure
how portable it is. You might want to #ifdef the #pragmas for Borland:
#pragma warn -8080
void f()
{
// ...
}
#pragma warn +8080
Tom
Quote


This doesn't work unfortunately.

 

Re:bcb6 incorrectly warns 'declared but never used'

Alan Bellingham wrote:
Quote
Eelke Klein < XXXX@XXXXX.COM >wrote:


>Wouldn't it be more elegant to temporarily disable the warning using
>pragma's?


Possibly. Less portable, though. The "(void) expr;" is a fairly well
known idiom used for shutting compilers up.
My idea was that it didn't have to be portable because it was a compiler
bug. Other compilers would happily ignore the pragma and be able to
compile the normal code instead of some code which only exist to prevent
a warning from a buggy compiler.
Eelke
 

Re:bcb6 incorrectly warns 'declared but never used'

Philippe Allain < XXXX@XXXXX.COM >writes:
Quote
Chris Uzdavinis (TeamB) wrote:
>I'd suggest just adding a statement like this, like Alan suggested:
>const int nTimes = 3;
>(void) nTimes; /////<<<<<<<<<<< Just add this, it should noop.

This doesn't work unfortunately.
Can you define what you mean by "doesn't work". Doesn't compile, or
doesn't suppress the error?
In the ACE library there is a macro called ACE_UNUSED_ARG which you
can use to silence such warnings. For example:
ACE_UNUSED_ARG(nTimes);
You don't want to use ace just for this macro but you can duplicate
what it does. On some platforms the (void) trick works nicely. I
thought borland was one of those platforms but I was writing from
memory. I'll look at ace to see how this macro is implemented for
borland... AH, here it is:
# define ACE_UNUSED_ARG(a) (a)
Hmm, looks pretty simple. I like this macro, BTW, rather than doing
whatever you do inside code to fix it, since this actually documents
WHAT you're trying to do.
--
Chris (TeamB);
 

Re:bcb6 incorrectly warns 'declared but never used'

Tamas Demjen < XXXX@XXXXX.COM >writes:
Quote
Here's a {*word*193} one that works:
*const_cast<int*>(&nTimes) = nTimes;

But I'm not sure if I would do this. This works too, but I'm not sure
how portable it is. You might want to #ifdef the #pragmas for Borland:
The original code looked like this:
const int nTimes = 3;
for (int k=0; k<nTimes; k++){
MessageBox(0,"MESSAGE","TITLE",0);
}
Thus nTimes is REALLY const. It is undefined to cast away constness
on variables that are created being const, and especially bad to write
to those addresses. It can/will crash on many systems. The compiler
can put "truly const" values into a read-only memory page, so if you
attempt to write to it you'll cause a fault from the OS, likely
becomming an access violation or segmentation fault.
And since this is doing a real assignment, it is not a zero-overhead
solution, even if it doesn't crash.
--
Chris (TeamB);