<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Sep 20, 2012, at 12:44 PM, jahanian wrote:</div><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Sep 18, 2012, at 6:13 PM, Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">On Tue, Sep 18, 2012 at 10:37 AM, Fariborz Jahanian <span dir="ltr"><<a href="mailto:fjahanian@apple.com" target="_blank">fjahanian@apple.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: fjahanian<br>
Date: Tue Sep 18 12:37:21 2012<br>
New Revision: 164143<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=164143&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=164143&view=rev</a><br>
Log:<br>
c: warn when an integer value comparison with an<br>
integral expression have the obvious result.<br>
Patch reviewed by John McCall off line.<br>
// <a href="rdar://12202422">rdar://12202422</a><br>
<br>
Added:<br>
cfe/trunk/test/Sema/outof-range-constant-compare.c<br>
Modified:<br>
cfe/trunk/include/clang/Basic/DiagnosticGroups.td<br>
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
cfe/trunk/lib/Sema/SemaChecking.cpp<br>
cfe/trunk/test/Analysis/additive-folding.cpp<br>
cfe/trunk/test/Sema/compare.c<br>
cfe/trunk/test/SemaCXX/compare.cpp<br>
cfe/trunk/test/SemaCXX/for-range-examples.cpp<br>
cfe/trunk/test/SemaCXX/warn-enum-compare.cpp<br>
<br>
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=164143&r1=164142&r2=164143&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=164143&r1=164142&r2=164143&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)<br>
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Sep 18 12:37:21 2012<br>
@@ -196,6 +196,7 @@<br>
def StringPlusInt : DiagGroup<"string-plus-int">;<br>
def StrncatSize : DiagGroup<"strncat-size">;<br>
def TautologicalCompare : DiagGroup<"tautological-compare">;<br>
+def TautologicalOutofRangeCompare : DiagGroup<"tautological-constant-out-of-range-compare">;<br></blockquote><div><br></div><div>Have you considered making this be a subgroup of -Wtautological-compare?</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
def HeaderHygiene : DiagGroup<"header-hygiene">;<br>
def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;<br>
<br>
<br>
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=164143&r1=164142&r2=164143&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=164143&r1=164142&r2=164143&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Sep 18 12:37:21 2012<br>
@@ -4068,6 +4068,9 @@<br>
def warn_lunsigned_always_true_comparison : Warning<<br>
"comparison of unsigned%select{| enum}2 expression %0 is always %1">,<br>
InGroup<TautologicalCompare>;<br>
+def warn_outof_range_compare : Warning<<br>
+ "comparison of literal %0 with expression of type %1 is always "<br>
+ "%select{false|true}2">, InGroup<TautologicalOutofRangeCompare>;<br></blockquote><div><br></div><div>Should this be warn_out_of_range_compare, and TautologicalOutOfRangeCompare?</div><div><br></div>
<div>Also, the constant operand isn't always a literal:</div><div><br></div><div><div><stdin>:1:50: warning: comparison of literal 10000 with expression of type 'unsigned char' is always false [-Wtautological-constant-out-of-range-compare]</div>
<div>const int n = 10000; unsigned char c; bool b = n == c;</div><div> ~ ^ ~</div></div><div><br></div><div>Perhaps 'comparison of constant 10000 with expression of type ...'.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
def warn_runsigned_always_true_comparison : Warning<<br>
"comparison of %0 unsigned%select{| enum}2 expression is always %1">,<br>
InGroup<TautologicalCompare>;<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=164143&r1=164142&r2=164143&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=164143&r1=164142&r2=164143&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Sep 18 12:37:21 2012<br>
@@ -4301,6 +4301,45 @@<br>
}<br>
}<br>
<br>
+static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,<br>
+ Expr *lit, Expr *other,<br>
+ llvm::APSInt Value,<br>
+ bool rhsLiteral) {<br></blockquote><div><br></div><div>Please capitalize all of these.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ BinaryOperatorKind op = E->getOpcode();<br>
+ QualType OtherT = other->getType();<br>
+ const Type *OtherPtrT = S.Context.getCanonicalType(OtherT).getTypePtr();<br>
+ const Type *LitPtrT = S.Context.getCanonicalType(lit->getType()).getTypePtr();<br>
+ if (OtherPtrT == LitPtrT)<br>
+ return;<br></blockquote><div><br></div><div>This is S.getContext().hasSameUnqualifiedType(OtherT, Lit->getType()).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ assert((OtherT->isIntegerType() && LitPtrT->isIntegerType())<br>
+ && "comparison with non-integer type");<br>
+ IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT);<br></blockquote><div><br></div><div><div>Have you considered using GetExprRange on the non-constant expression, in order to catch more cases of tautological comparisons (bitwise operations on one of the operands, and the like).</div></div></div></blockquote><div><br></div>Doing this would cause false positive in ((signed char)a - 5) < (-0x81LL). We have taken a more conservative approach overall,</div><div>to catch simple cases for now.</div></div></blockquote><div><br></div></div>Right. The problem is that GetExprRange is designed around the needs of -Wconversion, where being conservative means returning a tighter range than you might otherwise: for example, -Wconversion pretends that arithmetic is generally closed on the input types, because otherwise we'd find ourselves complaining about (short = short + 1). That's not the right heuristic here, because here being conservative means returning a *broader* range. It's not insurmountable, but it wasn't worth getting right in the first iteration.<div><br></div><div>John.</div></body></html>