r241478 - [Sema] Warn when shifting a negative value.
Rafael EspĂndola
rafael.espindola at gmail.com
Mon Jul 6 16:47:19 PDT 2015
Looks like this broke bootstrap:
http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux/builds/18993/steps/bootstrap%20clang/logs/stdio
On 6 July 2015 at 14:02, Davide Italiano <davide at freebsd.org> wrote:
> Author: davide
> Date: Mon Jul 6 13:02:09 2015
> New Revision: 241478
>
> URL: http://llvm.org/viewvc/llvm-project?rev=241478&view=rev
> Log:
> [Sema] Warn when shifting a negative value.
>
> Example:
> % ./clang -Wshift-negative-value emit.c
> emit.c:3:14: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
> int a = -1 << 3;
> ~~ ^
> 1 warning generated.
>
> PR: 24026
> Differential Revision: http://reviews.llvm.org/D10938
> Reviewed by: rsmith
>
> Modified:
> cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> cfe/trunk/lib/Sema/SemaExpr.cpp
> cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp
> cfe/trunk/test/Sema/shift.c
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=241478&r1=241477&r2=241478&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jul 6 13:02:09 2015
> @@ -4740,6 +4740,8 @@ def warn_division_by_zero : Warning<"div
> InGroup<DivZero>;
> def warn_remainder_by_zero : Warning<"remainder by zero is undefined">,
> InGroup<DivZero>;
> +def warn_shift_lhs_negative : Warning<"shifting a negative signed value is undefined">,
> + InGroup<DiagGroup<"shift-negative-value">>;
> def warn_shift_negative : Warning<"shift count is negative">,
> InGroup<DiagGroup<"shift-count-negative">>;
> def warn_shift_gt_typewidth : Warning<"shift count >= width of type">,
>
> Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=241478&r1=241477&r2=241478&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Jul 6 13:02:09 2015
> @@ -7937,9 +7937,19 @@ static void DiagnoseBadShiftValues(Sema&
> // representable in the result type, so never warn for those.
> llvm::APSInt Left;
> if (LHS.get()->isValueDependent() ||
> - !LHS.get()->isIntegerConstantExpr(Left, S.Context) ||
> - LHSType->hasUnsignedIntegerRepresentation())
> + LHSType->hasUnsignedIntegerRepresentation() ||
> + !LHS.get()->EvaluateAsInt(Left, S.Context))
> return;
> +
> + // If LHS does not have a signed type and non-negative value
> + // then, the behavior is undefined. Warn about it.
> + if (Left.isNegative()) {
> + S.DiagRuntimeBehavior(Loc, LHS.get(),
> + S.PDiag(diag::warn_shift_lhs_negative)
> + << LHS.get()->getSourceRange());
> + return;
> + }
> +
> llvm::APInt ResultBits =
> static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits();
> if (LeftBits.uge(ResultBits))
>
> Modified: cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp?rev=241478&r1=241477&r2=241478&view=diff
> ==============================================================================
> --- cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp (original)
> +++ cfe/trunk/test/CXX/expr/expr.const/p2-0x.cpp Mon Jul 6 13:02:09 2015
> @@ -157,7 +157,7 @@ namespace UndefinedBehavior {
> constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
> constexpr int shl_unsigned_into_sign = 1u << 31; // ok
> constexpr int shl_unsigned_overflow = 1024u << 31; // ok
> - constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
> + constexpr int shl_signed_negative = (-3) << 1; // expected-warning {{shifting a negative signed value is undefined}} // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
> constexpr int shl_signed_ok = 1 << 30; // ok
> constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
> constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
>
> Modified: cfe/trunk/test/Sema/shift.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/shift.c?rev=241478&r1=241477&r2=241478&view=diff
> ==============================================================================
> --- cfe/trunk/test/Sema/shift.c (original)
> +++ cfe/trunk/test/Sema/shift.c Mon Jul 6 13:02:09 2015
> @@ -39,7 +39,8 @@ void test() {
> i = 1 << (WORD_BIT - 2);
> i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}
> i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}}
> - i = -1 << (WORD_BIT - 1);
> + i = -1 << (WORD_BIT - 1); // expected-warning {{shifting a negative signed value is undefined}}
> + i = -1 << 0; // expected-warning {{shifting a negative signed value is undefined}}
> i = 0 << (WORD_BIT - 1);
> i = (char)1 << (WORD_BIT - 2);
>
> @@ -48,7 +49,7 @@ void test() {
> u = 5U << (WORD_BIT - 1);
>
> long long int lli;
> - lli = INT_MIN << 2; // expected-warning {{bits to represent, but 'int' only has}}
> + lli = INT_MIN << 2; // expected-warning {{shifting a negative signed value is undefined}}
> lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
> }
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list