[PATCH] Add a truncation warning for values under a bitwise or

Richard Smith richard at metafoo.co.uk
Wed Mar 6 13:41:47 PST 2013



================
Comment at: lib/Sema/SemaChecking.cpp:4828-4830
@@ +4827,5 @@
+    case BO_OrAssign:
+    case BO_Add:
+    case BO_AddAssign:
+    case BO_Sub:
+    case BO_SubAssign:
----------------
The BO_Add and BO_Sub cases might have false positives, for instance:
'''
unsigned char X = 0x1000 - Y;
'''
is fine if Y is known to be between 0x0f01 and 0x1000.

I think the cases we can warn on here are BO_AddAssign, BO_SubAssign, and unsigned BO_Add. (Though even unsigned add may have false positives if it's intended to wrap.) Maybe just drop the non-Assign Add and Sub checks for now?

================
Comment at: lib/Sema/SemaChecking.cpp:4822-4824
@@ +4821,5 @@
+    case BO_AndAssign:
+      return (EvalAsInt(Ctx, Value, BO->getLHS(), NarrowedExpr) ||
+              EvalAsInt(Ctx, Value, BO->getRHS(), NarrowedExpr)) &&
+             TruncationChangesValue(Value, TargetRange, true);
+
----------------
What happens if both LHS and RHS can be evaluated, and only RHS implicitly narrows?

================
Comment at: lib/Sema/SemaChecking.cpp:5057-5058
@@ -4981,5 +5056,4 @@
 
-    // People want to build with -Wshorten-64-to-32 and not -Wconversion.
-    if (S.SourceMgr.isInSystemMacro(CC))
-      return;
+        if (S.SourceMgr.isInSystemMacro(CC))
+          return;
 
----------------
This same check occurs both within the 'if' and after it; please move it before 'EvaluateAsInt'.


http://llvm-reviews.chandlerc.com/D405



More information about the cfe-commits mailing list