[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 10:58:07 PDT 2018
xbolva00 created this revision.
xbolva00 added a reviewer: rsmith.
Herald added a subscriber: cfe-commits.
Inspired by MSVC, which found some occurrences of this expression on our code base.
Fixes PR38950
Repository:
rC Clang
https://reviews.llvm.org/D52137
Files:
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.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,11 @@
+// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
+
+void 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 positive}}
+ unsigned long b2 = -b; // expected-error {{unary minus operator applied to type 'unsigned long', result value is still positive}}
+ unsigned long long c2 = -c; // expected-error {{unary minus operator applied to type 'unsigned long long', result value is still positive}}
+ }
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 positive">;
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.165650.patch
Type: text/x-patch
Size: 2251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180915/f2c8886e/attachment-0001.bin>
More information about the cfe-commits
mailing list