Re: [Fwd: Re: assertion warnings]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Bill,

  > Note, that I don't think it "must" give a warning.  I'm
  > asking if there is a way I can coerce it to do so, eg
  > '-Wassertions_with_side_effects'

To the best of my knowledge, the answer to your question is "No, gcc cannot be made to warn you about that." As mentioned previously, assert() is generally (perhaps always) implemented as a macro in C.

Behind that macro is a function, of course, that is called by the macro when assertions are enabled. From what I can tell, that function accepts an "int" parameter as its conditional test. Therefore, anything that evaluates (or can be converted) to an "int" is acceptable as a condition for the assert macro.

If assert was an actual *language* feature, rather than a library function/macro, it might be possible to do what you ask. But in C, it is not a language feature (ie. there is no "assert" keyword in the C grammar).

As for "if (i=2)" issuing a warning, that is possible because "if" is handled as part of the C language grammar, rather than being handled as an ordinary function call.

Incidentally, "if (i++)" does not generate a warning with GCC (using -W -Wall). The warning is only generated by assignment expressions, presumably because the C language parser can tell the difference between "if (assignment_expression)" and "if (other_expression)". Obviously, that same capability does not extend to ordinary function calls, such as assert.

--
Tony Wetmore
Solipsys Corporation (http://www.solipsys.com)
mailto:tony.wetmore@xxxxxxxxxxxx


bill wrote:
For exactly the same reasons that "if (i=2)" gives
a warning--it is syntactically correct, but it's
usually an error. Assertions should not have side effects, and coding that uses side effects of an
assertion is very bad practice.  Coding "assert(x=2)"
or "assert(x++)" is almost always an unintended
mistake or bad design, and I'm hoping there's a way
to get gcc to catch instances for me.  An instance
of this sort came up yesterday, and I'd really like
to be able to easily modify my makefiles to catch
any other instances.



Arturas Moskvinas wrote:

Why do you think it must give you a warning? x=2 returns some result,
and x++ also returns some result.

Arturas M.





Is there any way to get gcc to generate warnings for the following code?
-Wall gives no complaints at all.
I expect that it's asking too much to get a warning for the first
assertion, but
the other two seem to be pretty obvious candidates for a warning.

#include <assert.h>
int
foo(int *x)
{
   *x = *x+1;
   return *x;
}

int
main()
{
   int x;
   assert(foo(&x));
   assert(x=2);
   assert(x++);
   return 0;
}










[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux