<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jan 10, 2014 at 5:07 AM, Artyom Skrobov <span dir="ltr"><<a href="mailto:Artyom.Skrobov@arm.com" target="_blank">Artyom.Skrobov@arm.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im HOEnZb">Thank you for your suggestions Richard!<br>
One point though:<br>
<br>
</div><div class="im HOEnZb">> Please use RetValExp->isNullPointerConstant instead.<br>
> Please also add testcases for operator new returning nullptr, and<br>
returning expressions such as 1 - 1, and for operator new marked as<br>
'noexcept'.<br>
<br>
</div><div class="im HOEnZb">I want to note that expressions such as 1-1 are invalid as return values<br>
from operator new, and produce "error: cannot initialize return object of<br>
type 'void *' with an rvalue of type 'int'"<br></div></blockquote><div><br></div><div>Expressions such as 1 - 1 are valid null pointer constants in C++98 but not in C++11.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im HOEnZb">
At the same time, expressions such as (void*)(1-1) are not recognized either<br>
by isNullPointerConstant or by EvaluateAsInt as integer zeroes, namely<br>
because they are not integers.<br></div></blockquote><div><br></div><div>In C++11, (void*)(1 - 1) is a reinterpret_cast of 0 to void*, and isn't (necessarily) a null pointer. In C++98, it's a static_cast, and is a null pointer.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im HOEnZb">
For a reference, GCC doesn't warn on<br>
<br>
void *operator new(size_t n) {<br>
return (void*)(1-1);<br>
}<br>
<br>
which isn't too bad; but neither does it warn on<br>
<br>
void *operator new(size_t n) {<br>
void* blah = 0;<br>
return blah;<br>
}<br></div></blockquote><div><br></div><div>OK, I would not expect a warning here.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im HOEnZb">
nor even on<br>
<br>
void *operator new(size_t n) {<br>
return (void*)0;<br>
}<br></div></blockquote><div><br></div><div>I *would* expect a warning here.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im HOEnZb">
which seems quite valuable to be able to detect.<br>
<br>
isNullPointerConstant doesn't recognize the two latter cases as null pointer<br>
constants, either.</div></blockquote><div><br></div><div>Sorry for sending you in the wrong direction!</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im HOEnZb">
Do you think Clang should be able to detect such null-pointer-expressions?<br>
If so, could you advise how it could be implemented, seeing that<br>
isNullPointerConstant and EvaluateAsInt prove ineffective?</div></blockquote><div><br></div><div>It looks like the best way to achieve this is to use Expr::EvaluateAsBooleanCondition on the (converted) return expression. That matches what we do for __attribute__((nonnull)).<br>
</div></div></div></div>