]> granicus.if.org Git - clang/commitdiff
Added warning for unary minus used with unsigned type
authorDavid Bolvansky <david.bolvansky@gmail.com>
Tue, 2 Oct 2018 06:02:30 +0000 (06:02 +0000)
committerDavid Bolvansky <david.bolvansky@gmail.com>
Tue, 2 Oct 2018 06:02:30 +0000 (06:02 +0000)
Summary:
Inspired by MSVC, which found some occurrences of this expression on our code base.

Fixes PR38950

Reviewers: rsmith, craig.topper, spatel, RKSimon, aaron.ballman, thakis

Reviewed By: rsmith

Subscribers: joerg, Quuxplusone, lebedev.ri, craig.topper, RKSimon, cfe-commits

Differential Revision: https://reviews.llvm.org/D52137

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@343560 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Sema/unary-minus-integer-impcast.c [new file with mode: 0644]

index 7a95b53a51ac72fb6dc70f93a7553ecf6c7fc08c..85450d7f397843895bffc37f0d1bafa4b79bf81b 100644 (file)
@@ -3197,6 +3197,12 @@ def warn_impcast_integer_sign_conditional : Warning<
 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;
index a258d349c6835ac36353ec858624935545357e0c..d6183dde95f13bb4901c371936ba69e4d46f0cef 100644 (file)
@@ -10896,6 +10896,19 @@ CheckImplicitConversion(Sema &S, Expr *E, QualType T, SourceLocation CC,
     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
diff --git a/test/Sema/unary-minus-integer-impcast.c b/test/Sema/unary-minus-integer-impcast.c
new file mode 100644 (file)
index 0000000..0eb4401
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 %s -verify -Wconversion -fsyntax-only -triple x86_64-pc-linux-gnu
+// RUN: %clang_cc1 %s -verify -Wconversion -fsyntax-only -triple i386-pc-linux-gnu
+
+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}}
+
+  unsigned long b2 = -a;
+#ifdef __x86_64__
+// expected-warning@-2 {{higher order bits are zeroes after implicit conversion}}
+#endif
+  long c2 = -a;
+#ifdef __x86_64__
+// expected-warning@-2 {{the resulting value is always non-negative after implicit conversion}}
+#else
+// expected-warning@-4 {{implicit conversion changes signedness: 'unsigned int' to 'long'}}
+#endif
+}