Author: xbolva00 Date: Mon Oct 1 23:02:30 2018 New Revision: 343560 URL: http://llvm.org/viewvc/llvm-project?rev=343560&view=rev Log: Added warning for unary minus used with unsigned type
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 Added: cfe/trunk/test/Sema/unary-minus-integer-impcast.c Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td cfe/trunk/lib/Sema/SemaChecking.cpp Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=343560&r1=343559&r2=343560&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Oct 1 23:02:30 2018 @@ -3197,6 +3197,12 @@ def warn_impcast_integer_sign_conditiona 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; Modified: cfe/trunk/lib/Sema/SemaChecking.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=343560&r1=343559&r2=343560&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaChecking.cpp (original) +++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Oct 1 23:02:30 2018 @@ -10896,6 +10896,19 @@ CheckImplicitConversion(Sema &S, Expr *E 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 Added: cfe/trunk/test/Sema/unary-minus-integer-impcast.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/unary-minus-integer-impcast.c?rev=343560&view=auto ============================================================================== --- cfe/trunk/test/Sema/unary-minus-integer-impcast.c (added) +++ cfe/trunk/test/Sema/unary-minus-integer-impcast.c Mon Oct 1 23:02:30 2018 @@ -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 +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits