Skip to content

[Sema] Fixed faulty shift count warning #69521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ Bug Fixes in This Version
- Clang no longer permits using the `_BitInt` types as an underlying type for an
enumeration as specified in the C23 Standard.
Fixes (`#69619 <https://github.com/llvm/llvm-project/issues/69619>`_)
- Fixed an issue when a shift count specified by a small constant ``_BitInt()``,
in a left shift operation, could result in a faulty warnings about
``shift count >= width of type``.
- Clang now accepts anonymous members initialized with designated initializers
inside templates.
Fixes (`#65143 <https://github.com/llvm/llvm-project/issues/65143>`_)
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12143,8 +12143,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
auto FXSema = S.Context.getFixedPointSemantics(LHSExprType);
LeftSize = FXSema.getWidth() - (unsigned)FXSema.hasUnsignedPadding();
}
llvm::APInt LeftBits(Right.getBitWidth(), LeftSize);
if (Right.uge(LeftBits)) {
if (Right.uge(LeftSize)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like LeftSizeInBits would be a more appropriate name but the changes looks right.

S.DiagRuntimeBehavior(Loc, RHS.get(),
S.PDiag(diag::warn_shift_gt_typewidth)
<< RHS.get()->getSourceRange());
Expand Down Expand Up @@ -12186,7 +12185,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,

llvm::APInt ResultBits =
static_cast<llvm::APInt &>(Right) + Left.getSignificantBits();
if (LeftBits.uge(ResultBits))
if (ResultBits.ule(LeftSize))
return;
llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue());
Result = Result.shl(Right);
Expand All @@ -12200,7 +12199,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
// bugs -- if the result is cast back to an unsigned type, it will have the
// expected value. Thus we place this behind a different warning that can be
// turned off separately if needed.
if (LeftBits == ResultBits - 1) {
if (ResultBits - 1 == LeftSize) {
S.Diag(Loc, diag::warn_shift_result_sets_sign_bit)
<< HexResult << LHSType
<< LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Sema/c2x-expr-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ void test1(int *a) {
void test2(__uint128_t *a) {
(void)(*a >> ((__uint128_t)__UINT64_MAX__ + 1) <= 0); // expected-warning {{shift count >= width of type}}
}

// Regression test for bug where a faulty warning was given. We don't expect to
// see any warning in this case.
_BitInt(128) test3(_BitInt(128) a) {
return a << 12wb;
}

// Similar to test3 above, but with a too large shift count. We expect to see a
// warning in this case.
_BitInt(128) test4(_BitInt(128) a) {
return a << 129wb; // expected-warning {{shift count >= width of type}}
}