[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
Sat Sep 15 11:10:54 PDT 2018


xbolva00 updated this revision to Diff 165651.

https://reviews.llvm.org/D52137

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaExpr.cpp
  test/OpenMP/teams_thread_limit_messages.cpp
  test/Sema/unary-minus-unsigned.c


Index: test/Sema/unary-minus-unsigned.c
===================================================================
--- test/Sema/unary-minus-unsigned.c
+++ test/Sema/unary-minus-unsigned.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
+
+int test1(void) {
+    unsigned int a = 1;
+    unsigned long b = 2;
+    unsigned long long c = 1;
+ 
+    unsigned int a2 = -a; // expected-error {{unary minus operator applied to type 'unsigned int', result value is still unsigned}}
+    unsigned long b2 = -b; // expected-error {{unary minus operator applied to type 'unsigned long', result value is still unsigned}}
+    unsigned long long c2 = -c; // expected-error {{unary minus operator applied to type 'unsigned long long', result value is still unsigned}}
+
+    int a3 = -a; // expected-error {{unary minus operator applied to type 'unsigned int', result value is still unsigned}}
+    long b3 = -b; // expected-error {{unary minus operator applied to type 'unsigned long', result value is still unsigned}}
+    long long c3 = -c; // expected-error {{unary minus operator applied to type 'unsigned long long', result value is still unsigned}}
+ }
Index: test/OpenMP/teams_thread_limit_messages.cpp
===================================================================
--- test/OpenMP/teams_thread_limit_messages.cpp
+++ test/OpenMP/teams_thread_limit_messages.cpp
@@ -51,7 +51,7 @@
 #pragma omp teams thread_limit(-2) // expected-error {{argument to 'thread_limit' clause must be a strictly positive integer value}}
   foo();
 #pragma omp target
-#pragma omp teams thread_limit(-10u)
+#pragma omp teams thread_limit(-10u) // expected-error {{unary minus operator applied to type 'unsigned int', result value is still unsigned}}
   foo();
 #pragma omp target
 #pragma omp teams thread_limit(3.14) // expected-error 2 {{expression must have integral or unscoped enumeration type, not 'double'}}
@@ -102,7 +102,7 @@
   foo();
 
 #pragma omp target
-#pragma omp teams thread_limit (-10u)
+#pragma omp teams thread_limit (-10u) // expected-error {{unary minus operator applied to type 'unsigned int', result value is still unsigned}}
   foo();
 
 #pragma omp target
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -12646,8 +12646,12 @@
     resultType = Input.get()->getType();
     if (resultType->isDependentType())
       break;
-    if (resultType->isArithmeticType()) // C99 6.5.3.3p1
+    if (resultType->isArithmeticType()) { // C99 6.5.3.3p1
+      if (Opc == UO_Minus && resultType->isUnsignedIntegerType())
+        return ExprError(Diag(OpLoc, diag::err_unsignedtypecheck_unary_minus)
+                         << resultType << Input.get()->getSourceRange());
       break;
+    }
     else if (resultType->isVectorType() &&
              // The z vector extensions don't allow + or - with bool vectors.
              (!Context.getLangOpts().ZVector ||
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -5820,6 +5820,8 @@
   "taking the address of a destructor">;
 def err_typecheck_unary_expr : Error<
   "invalid argument type %0 to unary expression">;
+def err_unsignedtypecheck_unary_minus : Error<
+  "unary minus operator applied to type %0, result value is still unsigned">;
 def err_typecheck_indirection_requires_pointer : Error<
   "indirection requires pointer operand (%0 invalid)">;
 def ext_typecheck_indirection_through_void_pointer : ExtWarn<


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52137.165651.patch
Type: text/x-patch
Size: 3650 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180915/fc89424e/attachment.bin>


More information about the cfe-commits mailing list