Skip to content

[Clang] Suppress a -Wimplicit-int-conversionwarning introduced in #126846 #139429

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -352,6 +352,9 @@ Improvements to Clang's diagnostics
- Now correctly diagnose a tentative definition of an array with static
storage duration in pedantic mode in C. (#GH50661)

- The -Wimplicit-int-conversion warning no longer triggers for direct assignments between integer types narrower than int.
However, -Wsign-compare can now incorrectly produce a warning when comparing a value to another with just one more bit of width.

Improvements to Clang's time-trace
----------------------------------

Expand Down
24 changes: 5 additions & 19 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10626,25 +10626,7 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
case UO_AddrOf: // should be impossible
return IntRange::forValueOfType(C, GetExprType(E));

case UO_Minus: {
if (E->getType()->isUnsignedIntegerType()) {
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
Approximate);
}

std::optional<IntRange> SubRange = TryGetExprRange(
C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);

if (!SubRange)
return std::nullopt;

// If the range was previously non-negative, we need an extra bit for the
// sign bit. If the range was not non-negative, we need an extra bit
// because the negation of the most-negative value is one bit wider than
// that value.
return IntRange(SubRange->Width + 1, false);
}

case UO_Minus:
case UO_Not: {
if (E->getType()->isUnsignedIntegerType()) {
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
Expand All @@ -10659,6 +10641,10 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,

// The width increments by 1 if the sub-expression cannot be negative
// since it now can be.
// This isn't technically correct for UO_Minus since we need an extra bit
// because the negation of the most-negative value is one bit wider than
// the original value. However, the correct version triggers many unwanted
// warnings.
return IntRange(SubRange->Width + (int)SubRange->NonNegative, false);
}

Expand Down
23 changes: 3 additions & 20 deletions clang/test/Sema/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,30 +454,13 @@ int test20(int n) {
}
#endif

int test21(short n) {
return -n == 32768; // no-warning
}

#if TEST == 1
int test22(short n) {
return -n == 65536; // expected-warning {{result of comparison of 17-bit signed value == 65536 is always false}}
}
#endif

int test23(unsigned short n) {
return ~n == 32768; // no-warning
}

int test24(short n) {
return ~n == 32767; // no-warning
}

#if TEST == 1
int test25(unsigned short n) {
return ~n == 65536; // expected-warning {{result of comparison of 17-bit signed value == 65536 is always false}}
}

int test26(short n) {
return ~n == 32768; // expected-warning {{result of comparison of 16-bit signed value == 32768 is always false}}
}
#endif
unsigned char test25(unsigned char n) {
return -n; // no-warning
}