This is an archive of the discontinued LLVM Phabricator instance.

[Diagnostics] Fix inconsistent shift-overflow warnings in C++20
ClosedPublic

Authored by xgupta on Jun 10 2022, 11:32 AM.

Details

Reviewers
aaron.ballman
Group Reviewers
Restricted Project
Summary

This fixes https://github.com/llvm/llvm-project/issues/52873.
Don't warn in C++2A mode (and newer), as signed left shifts
always wrap and never overflow. Ref. -
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1236r1.html.

Diff Detail

Event Timeline

xgupta created this revision.Jun 10 2022, 11:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 10 2022, 11:32 AM
xgupta requested review of this revision.Jun 10 2022, 11:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 10 2022, 11:32 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added a reviewer: Restricted Project.Jun 13 2022, 7:31 AM
aaron.ballman added inline comments.
clang/lib/Sema/SemaExpr.cpp
11418

... to early here instead, and it should also check isSignedOverflowDefined() (note: we should probably fix SignedOverflowBehavior in LangOptions.def to be set based on the C++ language mode instead of needing to check for C++20 specifically)

11418–11428

and then we can remove the check for signed overflow defined or C++20 here because we've already bailed out.

11458–11459

I think this should move up...

erichkeane added inline comments.
clang/lib/Sema/SemaExpr.cpp
11418–11428

I think we'd want to remove the CPlusPlus20 check here, right? Signed-overflow should cover this case as well, and the check for C++20 below is no longer necessary?

xgupta updated this revision to Diff 436557.Jun 13 2022, 2:06 PM

Address comments

aaron.ballman added inline comments.Jun 14 2022, 3:56 AM
clang/lib/Sema/SemaExpr.cpp
11419–11421

I think this should also be checking || isSignedOverflowDefined() -- if signed overflow is defined, then all the rest of the diagnostics shouldn't be triggered because the behavior is defined.

(Eventually, we should remove the check for CPlusPlus20 and use ONLY isSignedOverflowDefined() -- that should account for the current language mode instead of requiring us to check additional predicates. But that doesn't have to be fixed up as part of this patch.)

11419–11428

Then the isSignedOverflowDefined() check can be removed here.

This revision is now accepted and ready to land.Jun 14 2022, 6:55 AM

LGTM! Please add a release note about the issue that was fixed when landing.

xgupta closed this revision.Jun 14 2022, 7:52 AM

LGTM! Please add a release note about the issue that was fixed when landing.

Done, Thank you for reviewing.

Committed in https://reviews.llvm.org/rG48e1829874df15fd57e731d4824604f28e177585.