[PATCH] D52137: Added warning for unary minus used with unsigned type

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 1 11:19:33 PDT 2018


xbolva00 updated this revision to Diff 167784.
xbolva00 added a comment.

Suggestions implemented


https://reviews.llvm.org/D52137

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaChecking.cpp
  test/Sema/unary-minus-integer-impcast.c


Index: test/Sema/unary-minus-integer-impcast.c
===================================================================
--- test/Sema/unary-minus-integer-impcast.c
+++ test/Sema/unary-minus-integer-impcast.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 %s -verify -Wconversion -fsyntax-only
+
+void test(void) {
+  unsigned int a = 1;
+
+  unsigned long long b = -a; // expected-warning {{higher order bits are zeroes after implicit conversion}}
+  long long c = -a;          // expected-warning {{the resulting value is always non-negative after implicit conversion}}
+}
+
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -10896,6 +10896,18 @@
     return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
   }
 
+  if (TargetRange.Width > SourceRange.Width)
+    if (auto *UO = dyn_cast<UnaryOperator>(E))
+      if (UO->getOpcode() == UO_Minus)
+        if (Source->isUnsignedIntegerType()) {
+          if (Target->isUnsignedIntegerType())
+            return DiagnoseImpCast(S, E, T, CC,
+                                   diag::warn_impcast_high_order_zero_bits);
+          if (Target->isSignedIntegerType())
+            return DiagnoseImpCast(S, E, T, CC,
+                                   diag::warn_impcast_nonnegative_result);
+        }
+
   if (TargetRange.Width == SourceRange.Width && !TargetRange.NonNegative &&
       SourceRange.NonNegative && Source->isSignedIntegerType()) {
     // Warn when doing a signed to signed conversion, warn if the positive
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -3194,6 +3194,12 @@
 def warn_impcast_integer_precision : Warning<
   "implicit conversion loses integer precision: %0 to %1">,
   InGroup<Conversion>, DefaultIgnore;
+def warn_impcast_high_order_zero_bits : Warning<
+  "higher order bits are zeroes after implicit conversion">,
+  InGroup<Conversion>, DefaultIgnore;
+def warn_impcast_nonnegative_result : Warning<
+  "the resulting value is always non-negative after implicit conversion">,
+  InGroup<SignConversion>, DefaultIgnore;
 def warn_impcast_integer_64_32 : Warning<
   "implicit conversion loses integer precision: %0 to %1">,
   InGroup<Shorten64To32>, DefaultIgnore;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52137.167784.patch
Type: text/x-patch
Size: 2440 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181001/377c0468/attachment-0001.bin>


More information about the cfe-commits mailing list