<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Nov 28, 2012, at 16:50 , Ted Kremenek <<a href="mailto:kremenek@apple.com">kremenek@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Author: kremenek<br>Date: Wed Nov 28 18:50:20 2012<br>New Revision: 168843<br><br>URL: <a href="http://llvm.org/viewvc/llvm-project?rev=168843&view=rev">http://llvm.org/viewvc/llvm-project?rev=168843&view=rev</a><br>Log:<br>Correctly handle IntegralToBool casts in C++ in the static analyzer. Fixes <<a href="rdar://problem/12759044">rdar://problem/12759044</a>>.<br><br>Modified:<br> cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp<br> cfe/trunk/test/Analysis/misc-ps-region-store.cpp<br><br>Modified: cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp?rev=168843&r1=168842&r2=168843&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp?rev=168843&r1=168842&r2=168843&view=diff</a><br>==============================================================================<br>--- cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp (original)<br>+++ cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp Wed Nov 28 18:50:20 2012<br>@@ -101,6 +101,12 @@<br> if (!isa<nonloc::ConcreteInt>(val))<br> return UnknownVal();<br><br>+ // Handle casts to a boolean type.<br>+ if (castTy->isBooleanType()) {<br>+ bool b = cast<nonloc::ConcreteInt>(val).getValue().getBoolValue();<br>+ return makeTruthVal(b, castTy);<br>+ }<br></blockquote><div><br></div><div>We can do better about nonloc::LocAsInteger and evalCastFromLoc as well. We <i>would</i> be able to do better about symbols if we had a state around for casts, but we don't.</div><div><br></div><br><blockquote type="cite"> // Only handle casts from integers to integers - if val is an integer constant<br> // being cast to a non integer type, produce unknown.<br> if (!isLocType && !castTy->isIntegerType())<br>@@ -735,7 +741,7 @@<br> NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);<br> if (!LeftIndex)<br> return UnknownVal();<br>- LeftIndexVal = evalCastFromNonLoc(*LeftIndex, resultTy);<br>+ LeftIndexVal = evalCastFromNonLoc(*LeftIndex, ArrayIndexTy);<br> LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);<br> if (!LeftIndex)<br> return UnknownVal();<br>@@ -745,7 +751,7 @@<br> NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);<br> if (!RightIndex)<br> return UnknownVal();<br>- RightIndexVal = evalCastFromNonLoc(*RightIndex, resultTy);<br>+ RightIndexVal = evalCastFromNonLoc(*RightIndex, ArrayIndexTy);<br> RightIndex = dyn_cast<NonLoc>(&RightIndexVal);<br> if (!RightIndex)<br> return UnknownVal();<br><br>Modified: cfe/trunk/test/Analysis/misc-ps-region-store.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps-region-store.cpp?rev=168843&r1=168842&r2=168843&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps-region-store.cpp?rev=168843&r1=168842&r2=168843&view=diff</a><br>==============================================================================<br>--- cfe/trunk/test/Analysis/misc-ps-region-store.cpp (original)<br>+++ cfe/trunk/test/Analysis/misc-ps-region-store.cpp Wed Nov 28 18:50:20 2012<br>@@ -694,3 +694,14 @@<br> static const Rdar12755044_foo Rdar12755044_foo_list[] = { { { } } };<br> return Rdar12755044_foo_list; // no-warning<br> }<br>+<br>+// Test the correct handling of integer to bool conversions. Previously<br>+// this resulted in a false positive because integers were being truncated<br>+// and not tested for non-zero.<br>+void rdar12759044() {<br>+ int flag = 512;<br>+ if (!(flag & 512)) {<br>+ int *p = 0;<br>+ *p = 0xDEADBEEF; // no-warning<br>+ }<br>+}<br><br><br>_______________________________________________<br>cfe-commits mailing list<br><a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits<br></blockquote></div><br></body></html>