This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Warn when shifting a negative value
ClosedPublic

Authored by davide on Jul 3 2015, 7:28 PM.

Details

Summary

While I think there's no need to not mirror gcc warning, probably the message can be reworded in a slightly different way.
I tried, but suggestions are more than welcome. Fixes PR24026

% ./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.

Diff Detail

Event Timeline

davide updated this revision to Diff 29040.Jul 3 2015, 7:28 PM
davide retitled this revision from to [Sema] Warn when shifting a negative value.
davide updated this object.
davide added a reviewer: rsmith.
davide set the repository for this revision to rL LLVM.
davide added a subscriber: cfe-commits.
rsmith accepted this revision.Jul 5 2015, 9:47 PM
rsmith edited edge metadata.

LGTM

lib/Sema/SemaExpr.cpp
7940–7941

While you're here, please reverse the order of these two lines. It's much cheaper to check for an unsigned type than to evaluate the expression.

test/Sema/shift.c
42

Please also add a test that we even warn for shifting a negative value by zero.

This revision is now accepted and ready to land.Jul 5 2015, 9:47 PM
davide closed this revision.Jul 6 2015, 11:18 AM

Now in tree, r241478. Thank you for the review, Richard.