Pages: 1 2 3 [4]   Go Down
Author Topic: if(a=b) is not if(a==b) could IDE check this b4 upload?  (Read 4649 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now for the really bad news. Nobody and and I mean nobody has seen a warning
from the compiler using the IDE. The IDE explicitly disables ALL warnings from the compiler!!
(again you'll have to ask the arduino team why they decided to do this)

I agree with Bill on this one. In fact I thought I had the solution, after some Googling I found this:

Code:
#pragma GCC diagnostic warning "-Wall"

That is, with a pragma, enable all warnings. Even saw that code inside some Arduino library.

But no dice. The problem "if" statement does not produce warnings.

Now I can understand the Arduino developers thinking "We won't confuse all those newbies with warning messages they don't understand".  But what they have substituted is program behaviour, even crashes, that they don't understand. So instead of spending 10 minutes eliminating warnings, you spend days puzzling over why your code isn't working as you expect.

And, look! The compiler, as distributed, does support it (the parenthesis warnings). Look at this test case:

Code:
$ avr-gcc -Wall test.cpp
test.cpp: In function 'int main()':
test.cpp:4: warning: suggest parentheses around assignment used as truth value
$

Interestingly though, the avr-gcc compiler does not honour the pragma. Did they deliberately compile it to accept the pragma but do nothing? Seems like a lot of work to go through to stop people, who really want them, from getting warnings.
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2347
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick,
I don't think it is as nefarious as you think.
I think you have stumbled on a legitimate compiler bug or at least something that
needs clarification in the gcc documentation.
I was unaware of the pragmas to alter the diagnostic messages (I seldom use pragmas as they are so unportable)
However, if this worked it would be exactly what the OP was looking for.

According to the gcc documentation:
http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
The pragmas are supposed to override any command line options. (from the current documentation):
Quote
Note that these pragmas override any command-line options.

In this case, the offending commandline option is "-w" (set by the Arduino IDE) which disables all warnings.
See documentation: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html


What wasn't clear in the gcc documentation is that "-w" cannot be overridden by the pragmas.
While I believe that the overriding of -w with pragmas could be argued either way,
(personally I think pragmas should be allowed to override -w)
the way it currently works (you can't override it with a #pragma),
definitely could use a comment about this in the section about the diagnostic pragmas
indicating that no warnings can be enabled if the -w command line option was used
to disable all warnings.

Note: The pragma does work if the -w command line option is not used.
(at least in avr-gcc version 4.3.4 which is what I'm using on linux)
Windows may be running an older version and if so, the pragmas may not be supported.


But unfortunately, currently the only way to get rid of the -w command line option
being passed to gcc from the IDE is to patch the java code and rebuild the IDE.

What would be nice is if the Arduino guys would update the IDE to allow overriding the commandline options.
As an alternative they could remove the -w option from the compiler commandline
and use a pragma to disable warnings in WProgram.h

That way users that want/need to see warnings could turn them back on by simply inserting
a pragma in their code.

It would be the best of all worlds. Naive users never get warnings and more advanced users
could easily turn them back on.


--- bill

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What would be nice is if the Arduino guys would update the IDE to allow overriding the commandline options.
As an alternative they could remove the -w option from the compiler commandline
and use a pragma to disable warnings in WProgram.h

Bill,

I agree with you 100%.

Suppressing warnings is just shifting the problem. For example, if your car was running out of petrol, you might be annoyed by flashing "low fuel" message, but you will be even more annoyed when you run out of gas on a lonely country road.

However, to the test case. I have a program here:

Code:
#pragma GCC diagnostic warning "-Wall"

int main ()
  {
  int a;
  if (a = 1)
    {};  // do something
  return 0;
  }

If I compile that without any -w flag, it still does not raise a warning.

Code:
$ avr-gcc test.cpp
$

However with -Wall it does.

Code:
$ avr-gcc test.cpp -Wall
test.cpp: In function 'int main()':
test.cpp:6: warning: suggest parentheses around assignment used as truth value
$

So the -w flag is not suppressing warnings. Something else is. Or the pragma just doesn't work.
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2347
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting. I was testing using -Wparentheses not -Wall
(what version of the Compiler are you using)

What is very interesting for me on is that on avr-gcc version 4.3.4
#pragma GCC diagnostic warning "-Wall"
will not output the warning while:
#pragma GCC diagnostic warning "-Wparentheses"

will turn on the warning. (but only if -w is not used on the commandline).

Either one on the commandline will turn on the warning.

It seems like a bug to me that -Wall on the command line behaves differently
than
#pragma GCC diagnostic warning "-Wall"

--- bill

Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
#pragma GCC diagnostic warning "-Wall"
Does #pragma GCC diagnostic warning "-Wparen" work?  It wouldn't surprise me if Wall is a special case.

One reason that -Wall isn't turned on by default is that it tends to result in a lot of relatively confusing warnings for mostly-non-issues (for example, comparison between a signed and unsigned int.)

Another reason is that there is currently a G++ bug that causes a bogus warning for initialized PROGMEM variables.

A third reason is that the Arduino "core" and official libraries are not "warning free."
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2347
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
#pragma GCC diagnostic warning "-Wall"
Does #pragma GCC diagnostic warning "-Wparen" work?  It wouldn't surprise me if Wall is a special case.

One reason that -Wall isn't turned on by default is that it tends to result in a lot of relatively confusing warnings for mostly-non-issues (for example, comparison between a signed and unsigned int.)

Another reason is that there is currently a G++ bug that causes a bogus warning for initialized PROGMEM variables.

A third reason is that the Arduino "core" and official libraries are not "warning free."


Code:
#pragma GCC diagnostic warning "-Wparen"
does not work. However, as I mentioned above:
Code:
#pragma GCC diagnostic warning "-Wparentheses"
does work if you don't use -w on the command line.
-w seems to disable all warnings even if pragmas attempt to turn them back on.

Yep I saw the wierd errors for progmem constants. I hadn't tracked that down yet.

Still seems to like it would be logical that 
Code:
#pragma GCC diagnostic warning "-Wall"
should behave the same as -Wall on the commandline though.

(I started a thread over on the AVRfreaks gcc forum about it)

--- bill
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yep I saw the wierd errors for progmem constants. I hadn't tracked that down yet.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34734
Not getting fixed very quickly...
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting. I was testing using -Wparentheses not -Wall
(what version of the Compiler are you using)

What is very interesting for me on is that on avr-gcc version 4.3.4
#pragma GCC diagnostic warning "-Wall"
will not output the warning while:
#pragma GCC diagnostic warning "-Wparentheses"

will turn on the warning. (but only if -w is not used on the commandline).

Code:
$ avr-gcc --version
avr-gcc (GCC) 4.3.3
Copyright (C) 2008 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.

Ah yes, confirmed. The specific pragma works, but not if you use -w.

However this (the -Wall part) is contradicted by "man gcc":

Code:
-Wparentheses
           Warn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a truth value is expected, ...

           This warning is enabled by -Wall.

Quote
Yep I saw the wierd errors for progmem constants. I hadn't tracked that down yet.

I still would like to enable -Wparentheses. Cancelling all warnings is like throwing out the baby with the bathwater. Or something.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is not cancelling all warnings akin to clapping your hands over your ears, screwing your eyes tightly shut, and loudly chanting "la la la la la...."?

:-)
Logged

Offline Offline
Full Member
***
Karma: 8
Posts: 147
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is not cancelling all warnings akin to clapping your hands over your ears, screwing your eyes tightly shut, and loudly chanting "la la la la la...."?

:-)


If I had to guess I'd say the problem is that Arduino recompiles all the libraries on each compilation. My bet is the libraries produce loads of warnings. Not a very elegant solution though.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16548
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I had to guess I'd say the problem is that Arduino recompiles all the libraries on each compilation. My bet is the libraries produce loads of warnings. Not a very elegant solution though.

Several (4-5?) versions ago of the IDE, it use to look to see if there were any precompiled .o files and would use them, if not it would recompile from source. Then they changed it to always force a complete compile. Not sure of the reason for that change, however there were always problems with people forgetting to delete a .o file, after making changes to a library source file(s) and not seeing any change in behaviour.  smiley-wink

Not competent enough to pass judgment on suppression of warning messages, I have enough challenge just trying to get all the fatal compilation errors to go away. Plus I already have a wife, the ultimate warning message machine.  smiley-wink

Lefty
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wife aggro, huh?

As for the libraries, I don't see why you can't compile libraries suppressing warnings, but keep them for the "user" code. Or alternatively, huh, fix the libraries. I think there is a pragma that suppresses warnings, so I would rather go through and "knowingly" suppress a warning I know to be benign, than have them all suppressed.
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2347
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See this thread for a very nice solution to this issue.
It allows advanced users to set their own compiler and linker options.

It would be really nice if this would make it into the next Arduino release.
(seems like a no brainer to me)

http://arduino.cc/forum/index.php/topic,54456.0.html

--- bill
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: