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

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 1 15:09:56 PDT 2018


rsmith accepted this revision.
rsmith added a comment.
This revision is now accepted and ready to land.

Thanks!



================
Comment at: lib/Sema/SemaChecking.cpp:10899
 
+  if (TargetRange.Width > SourceRange.Width)
+    if (auto *UO = dyn_cast<UnaryOperator>(E))
----------------
Add braces to these outer `if`s.


================
Comment at: test/Sema/unary-minus-integer-impcast.c:1-8
+// 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}}
----------------
This test is not portable (it will fail on a target where `sizeof(int) == sizeof(long long)`); please add a `-triple` flag.

Please also add a test that we don't warn for the same-size cases (eg, pick a target where `sizeof(int) == sizeof(long)` and test that `unsigned long b2 = -a;` and `long c2 = -a;` do not warn).


https://reviews.llvm.org/D52137





More information about the cfe-commits mailing list