public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* [Fwd: Re: assertion warnings]
@ 2005-11-16  6:49 bill
  2005-11-16 14:50 ` Tony Wetmore
  0 siblings, 1 reply; 2+ messages in thread
From: bill @ 2005-11-16  6:49 UTC (permalink / raw)
  To: gcc-help

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.

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'


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;
>>}
>>    
>>

>



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Fwd: Re: assertion warnings]
  2005-11-16  6:49 [Fwd: Re: assertion warnings] bill
@ 2005-11-16 14:50 ` Tony Wetmore
  0 siblings, 0 replies; 2+ messages in thread
From: Tony Wetmore @ 2005-11-16 14:50 UTC (permalink / raw)
  To: bill; +Cc: gcc-help

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@solipsys.com


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;
>>> }
>>>   
> 
> 
>>
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-11-16 14:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-16  6:49 [Fwd: Re: assertion warnings] bill
2005-11-16 14:50 ` Tony Wetmore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).