When I first started with the Arduino environment I noticed that it had File | Preferences | Compiler Warnings
set to None
, so I set it to All
- and there were many, many warnings in the supplied libraries! So I quickly set it to None
again - until later.
Well, "later" is now, and I started going through the warnings to fix the code to quiet the compiler.
Note that I don't want to disable any warnings - I want to placate the compiler at each spot so that it says to itself "No problem here!"
- If the compiler complains of an unused variable, I comment out the variable;
- If it complains of a signed/unsigned comparison, I change the type;
- If it complains of an unused parameter in a function, I...
...hmmm. If this was C++, I'd simply comment out the parameter name, leaving the type naked:
void Fn(int /*param*/) {
} // Fn(param)
Arduino reports:
error: parameter name omitted
But that isn't legal in C. With some compilers you can use the __unused
attribute:
void Fn(int __unused param) {
} // Fn(param)
Arduino reports:
error: expected ';', ',' or ')' before 'param'
It's interpreting__unused
as the actual parameter name
or if they're fussy:
void Fn(int __attribute__((unused)) param) {
} // Fn(param)
Arduino reports:
warning: unused parameter 'param' [-Wunused-parameter]
That's not the correct__attribute__
, obviously
So none of the above work in the Arduino environment. Suggestions?
I've been programming in C and C++ for over twenty years, and I know that the #1 thing you should always do is ask the compiler to tell you about everything it notices. You need a warning-free compile at all times! Otherwise, you spend hours or days trying to find a bug - only to discover that the compiler has been warning you about it all along...
2 Answers 2
Well, I typed all that question up for posting, then tried one last thing. It worked!
void Fn(__attribute__((unused)) int param) {
} // Fn(param)
So, rather than let a good question go to waste: here's the answer!
Now all we need to do is get the maintainers of all the libraries to put these in... and fix the other warnings...
-
Another idiom often used in C is
void f(int param) { (void) param; ... }
.Edgar Bonet– Edgar Bonet2016年06月17日 08:12:35 +00:00Commented Jun 17, 2016 at 8:12
Within the body of a function you can do this to suppress a warning:
void do_something(int value) {
(void) (value);
. . .
}
You can use a macro to do the same thing, for better clarity:
#define UNUSED(v) (void) (v)
void do_something(int value) {
UNUSED(value);
. . .
}
This can be helpful in situations where you have conditional compilation:
void do_something(int value) {
#ifndef USES_PARAM
UNUSED(value);
#else
other_stuff(value);
#endif
. . .
}
-
I know most compilers will do the "right thing" with that, but I've seen some that emit the "load" - and then do nothing with it. The nice thing about "decorating" a variable declaration is that it does two things: it signals the compiler, not the code generation part; and it merely flags a possibility, not a guarantee. If you actually use a variable that is flagged as
unused
, then the compler doesn't complain. So in your final example, you decoratevalue
and don't need both sides of the#if[n]def
John Burger– John Burger2016年07月17日 09:50:49 +00:00Commented Jul 17, 2016 at 9:50
void f(int) {...}
idiom should work in the C++ files. I agree it's a shame the core generates so many warnings.