473,785 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suppressing "Parameter not used" Warning

Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uin t8_t);

void error_state(uin t8_t status)
{
NOT_USED(status );

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?

Thanks,
-=Dave

-=Dave
--
Change is inevitable, progress is not.
Nov 15 '05
40 7900
On Tue, 4 Oct 2005 10:37:03 +0400, "Alexei A. Frounze"
<al*****@chat.r u> wrote:
"Thad Smith" <Th*******@acm. org> wrote in message
news:43******* *************** *@auth.newsread er.octanews.com ...
...
I have used a void cast for some compilers. For another, I was
successful with

#define UNUSED(x) if(x);


Nice, though some clever compiler could warn here whether or not you're sure
it's what you intend to do, whether or not this code has any effect.


Interesting idea, I'd never thought of that. Though the lint I use
would surely complain about a "questionab le use" of the semicolon
(even if it was removed from the macro itself) unless you changed it
to something like

#define UNUSED(x) if(x){}

Regards,

-=Dave
--
Change is inevitable, progress is not.
Nov 15 '05 #21
Michael N. Moran wrote:
David Brown wrote:
if (test()) {
doThis();
doThat();
} else {
doSomethingElse ();
doThat();
}

gcc will combine the two "doThat()" calls (which is good), and then
warn that one of the "will never be executed", which is bad.

If you know any good ideas to get working "will never be executed"
warnings, even if it is "wait for gcc 4.1.x", I'd love to hear them.

Hmmm. I tried this with a couple of GCC versions at default
and -O[23] optimization levels (cross and native) and saw no
such warning. What options/versions are you using?


As is typical when you write something off the top of your head, my test
case didn't work. Additionally, you need the "-Wunreachable-code"
warning flag on to get the warning. A bit more testing showed that the
following code gives an unwanted warning with msp430-gcc (3.2.3), but
not for avr-gcc (3.4.1). Both give a correct warning on test2(). So it
looks like I've answered my own question - use gcc 3.4 or newer. (gcc
4.x for msp430 should not be too far off, I hope.)
unsigned char a, b, c, d;

void test(void) {
if (a == 1) {
b = 100; // Should be no warning here
} else if (a == 2) {
b = 200;
} else {
b = 100;
}
}

void test2(void) {
if (c) {
d = 10;
} else if (!c) {
d = 20;
} else {
d = 30; // Should give warning here
}
}
Nov 15 '05 #22
On 2005-10-03, Mark Borgerson <> wrote:
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
ol*****@inspire .net.nz says...
Dave Hansen wrote:
> In another group, a poster asked about defining a macro NOT_USED as
> shown above to quiet the compiler warning. His suggested
> implementation was
>
> #define NOT_USED(p) ((void)(p))
>
> In this particular case, he noted the compiler he was using would
> generate the warning even in the presence of this macro. I suggested
> he use
>
> #define NOT_USED(p) ((p)=(p))
>
>
> So I'm curious. Which form (if either) is more common? Are there
> any implementations that will generate executable code for the latter?


Yes, the compiler might warn that 'p' is assigned a value which
is never used. And it might also warn about reading an uninitialized
variable.

One compiler I use has this definition:

#define NOT_USED(junk) { (volatile typeof(junk))ju nk = junk; }

Metrowerks Codewarrior has the

#pragma unused (varname)

construct to solve this problem. I'm surprised that other compilers
don't have the same facility.


They do (or something similar).

--
Grant Edwards grante Yow! My mind is making
at ashtrays in Dayton...
visi.com
Nov 15 '05 #23
In article <43********@new s.wineasy.se>,
da***@westcontr ol.removethisbi t.com says...
Mark Borgerson wrote:
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
ol*****@inspire .net.nz says...
Dave Hansen wrote:

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementati on was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))
So I'm curious. Which form (if either) is more common? Are there
any implementations that will generate executable code for the latter?

Yes, the compiler might warn that 'p' is assigned a value which
is never used. And it might also warn about reading an uninitialized
variable.

One compiler I use has this definition:

#define NOT_USED(junk) { (volatile typeof(junk))ju nk = junk; }
Metrowerks Codewarrior has the

#pragma unused (varname)

construct to solve this problem. I'm surprised that other compilers
don't have the same facility.

Mark Borgerson


The trouble with such pragmas is that they are completely non-portable.
As Grant Edwards said, gcc has an equivalent (using an attribute,
which is gcc prefers over pragmas). Often non-portability is not a
problem, since there are so many other non-portable aspects to typical
embedded systems (and in the case of gcc, it is at least portable to a
few dozen other gcc ports).


I agree that such pragmas are generally non-portable. The
#define UNUSED(x) if(x){}


solution seems to be the best seen so far. I tested it out with
CodeWarrior PalmOS (which I use for some M68K embedded work) and
it issued no warnings and generated no code. Can't ask for much
more than that!

In any case, the fact that you get a warning from the compiler is
a good thing. I have a lot more problems with porting structures
where I have to worry about packing and endian problems---and where
the compiler hasn't a clue what the code on the other end of the
communications line is doing!

Mark Borgerson

Nov 15 '05 #24
pete <pf*****@mindsp ring.com> writes:
Dave Hansen wrote:

Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uin t8_t);

void error_state(uin t8_t status)
{
NOT_USED(status );

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code.


Non executable code tends to generate warnings.
I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?


In distributions file for a sort timing program
my distribution function arguments are of this form:
(e_type *array, size_t n, long unsigned *seed)

but only some of them use the seed for a PRNG.

Others are like this:

void sorted(e_type *a, size_t n, long unsigned *seed)
{
a += n;
while (n-- != 0) {
(*--a).data = n;
}
seed;
}

So, I just make an expression statement out of seed
and that seems to stop the warnings.


But some compilers will issue a warning about the
"expression " result not being used...
Nov 15 '05 #25
"Everett M. Greene" <mo*****@mojave g.iwvisp.com> wrote in message
news:20******** ***********@moj aveg.iwvisp.com ...
pete <pf*****@mindsp ring.com> writes:

....
void sorted(e_type *a, size_t n, long unsigned *seed)
{
a += n;
while (n-- != 0) {
(*--a).data = n;
}
seed;
}

So, I just make an expression statement out of seed
and that seems to stop the warnings.


But some compilers will issue a warning about the
"expression " result not being used...


OK, what if the seed is used in an expression containing a comma, to the
left side of the comma?

Or what if it's used in an expression as
(seed*0)
or
(seed&0)
or
(seed&&0)?
:)
Should the compiler grumble in these 3 latter cases?

Alex
Nov 15 '05 #26
id**@hotmail.co m (Dave Hansen) writes:
Please note crosspost.
Noted, thank you. Responders please note that I am not
a regular reader of comp.arch.embed ded.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uin t8_t);

void error_state(uin t8_t status)
{
NOT_USED(status );

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?


Just a suggestion:

#define NOT_USED(v) ((void)(&(v)?1: 0))
Nov 15 '05 #27
On Tue, 4 Oct 2005 21:31:58 +0400, "Alexei A. Frounze"
<al*****@chat.r u> wrote:
"Everett M. Greene" <mo*****@mojave g.iwvisp.com> wrote in message
news:20******* ************@mo javeg.iwvisp.co m...
pete <pf*****@mindsp ring.com> writes: [...]
> seed;
> }
>
> So, I just make an expression statement out of seed
> and that seems to stop the warnings.
But some compilers will issue a warning about the
"expression " result not being used...


OK, what if the seed is used in an expression containing a comma, to the
left side of the comma?


Depends. What's on the right side on the comma? Is the comma
expression cast to void?

Or what if it's used in an expression as
(seed*0)
or
(seed&0)
or
(seed&&0)?
:)
Should the compiler grumble in these 3 latter cases?


Many compilers won't, but some will, and lint certainly will, unless
the expression is cast to void.

Regards,

-=Dave
--
Change is inevitable, progress is not.
Nov 15 '05 #28
"Dave Hansen" <id**@hotmail.c om> wrote in message
news:1128453424 .692e65550d080b 6df6b829b39cef8 b14@teranews...
On Tue, 4 Oct 2005 21:31:58 +0400, "Alexei A. Frounze"
<al*****@chat.r u> wrote:
"Everett M. Greene" <mo*****@mojave g.iwvisp.com> wrote in message
news:20******* ************@mo javeg.iwvisp.co m...
pete <pf*****@mindsp ring.com> writes: [...] > seed;
> }
>
> So, I just make an expression statement out of seed
> and that seems to stop the warnings.

But some compilers will issue a warning about the
"expression " result not being used...


OK, what if the seed is used in an expression containing a comma, to the
left side of the comma?


Depends. What's on the right side on the comma? Is the comma
expression cast to void?


I don't know. It was just an idea...
Or what if it's used in an expression as
(seed*0)
or
(seed&0)
or
(seed&&0)?
:)
Should the compiler grumble in these 3 latter cases?


Many compilers won't, but some will, and lint certainly will, unless
the expression is cast to void.


No, my point was that to make it a part of the expression whose value *is*
used but happens to be unaffected by seed being multiplied by 0 or anded
with 0.

Alex
Nov 15 '05 #29
On Tue, 4 Oct 2005 23:23:39 +0400, "Alexei A. Frounze"
<al*****@chat.r u> wrote:

[...]

No, my point was that to make it a part of the expression whose value *is*
used but happens to be unaffected by seed being multiplied by 0 or anded
with 0.


Oh, OK, I understand now. I expect there's a good chance it will
work. But it's kind of hard to hide behind a macro.

Regards,

-=Dave
--
Change is inevitable, progress is not.
Nov 15 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5743
by: harry | last post by:
Hi, During compilation, a C# project in my solution triggers the following warning: "warning CS0168: The variable 'ex' is declared but never used" To trigger this warning, it appears the C# compiler WarningLevel needs to be 3 or above.
11
23242
by: Charles Sullivan | last post by:
I have a number of functions, e.g.: int funct1( int arg1, int arg2, int arg3 ); int funct2( int arg1, int arg2, int arg3 ); int funct3( int arg1, int arg2, int arg3 ); that are called via pointers in a table, with the same parameters regardless of the particular function. In some of the functions, one or more of the
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.