Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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)

- Split diagnosis of implicit integer comparison on negation to a new diagnostic group ``-Wimplicit-int-comparison-on-negation``,
so user can turn it off independently.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- Split diagnosis of implicit integer comparison on negation to a new diagnostic group ``-Wimplicit-int-comparison-on-negation``,
so user can turn it off independently.
- Split diagnosis of implicit integer comparison on negation to a new
diagnostic group ``-Wimplicit-int-comparison-on-negation``, grouped under
``-Wimplicit-int-conversion``, so user can turn it off independently.


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

Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ def DeprecatedOFast : DiagGroup<"deprecated-ofast">;
def ObjCSignedCharBoolImplicitIntConversion :
DiagGroup<"objc-signed-char-bool-implicit-int-conversion">;
def Shorten64To32 : DiagGroup<"shorten-64-to-32">;
def ImplicitIntConversionOnNegation : DiagGroup<"implicit-int-conversion-on-negation">;
def ImplicitIntConversion : DiagGroup<"implicit-int-conversion",
[Shorten64To32,
ObjCSignedCharBoolImplicitIntConversion]>;
ObjCSignedCharBoolImplicitIntConversion,
ImplicitIntConversionOnNegation]>;
def ImplicitConstIntFloatConversion : DiagGroup<"implicit-const-int-float-conversion">;
def ImplicitIntFloatConversion : DiagGroup<"implicit-int-float-conversion",
[ImplicitConstIntFloatConversion]>;
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -4217,6 +4217,9 @@ def warn_impcast_integer_sign_conditional : Warning<
def warn_impcast_integer_precision : Warning<
"implicit conversion loses integer precision: %0 to %1">,
InGroup<ImplicitIntConversion>, DefaultIgnore;
def warn_impcast_integer_precision_on_negation : Warning<
"implicit conversion loses integer precision: %0 to %1 on negation">,
InGroup<ImplicitIntConversionOnNegation>, DefaultIgnore;
def warn_impcast_high_order_zero_bits : Warning<
"higher order bits are zeroes after implicit conversion">,
InGroup<ImplicitIntConversion>, DefaultIgnore;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12091,6 +12091,12 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
if (SourceMgr.isInSystemMacro(CC))
return;

if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
if (UO->getOpcode() == UO_Minus)
return DiagnoseImpCast(
*this, E, T, CC, diag::warn_impcast_integer_precision_on_negation);
}

if (TargetRange.Width == 32 && Context.getIntWidth(E->getType()) == 64)
return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_integer_64_32,
/* pruneControlFlow */ true);
Expand Down
64 changes: 64 additions & 0 deletions clang/test/Sema/implicit-int-conversion-on-int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// RUN: %clang_cc1 %s -verify -Wimplicit-int-conversion
// RUN: %clang_cc1 %s -verify -Wimplicit-int-conversion -Wno-implicit-int-conversion-on-negation -DNO_DIAG
Copy link
Collaborator

Choose a reason for hiding this comment

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

A better approach than using -D is to specify a prefix to -verify: https://clang.llvm.org/docs/InternalsManual.html#verifying-diagnostics

Then you can have one comment for the -no-diagnostics RUN line and skip all the preprocessor code for the other RUN line.


char test_char(char x) {
return -x;
#ifndef NO_DIAG
// expected-warning@-2 {{implicit conversion loses integer precision}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this actually checking for the new diagnostic ("on negation")? If so, you should update the expected warning to include the whole diagnostic text.

#else
// expected-no-diagnostics
#endif
}

unsigned char test_unsigned_char(unsigned char x) {
return -x;
#ifndef NO_DIAG
// expected-warning@-2 {{implicit conversion loses integer precision}}
#else
// expected-no-diagnostics
#endif
}

short test_short(short x) {
return -x;
#ifndef NO_DIAG
// expected-warning@-2 {{implicit conversion loses integer precision}}
#else
// expected-no-diagnostics
#endif
}

unsigned short test_unsigned_short(unsigned short x) {
return -x;
#ifndef NO_DIAG
// expected-warning@-2 {{implicit conversion loses integer precision}}
#else
// expected-no-diagnostics
#endif
}

// --- int-width and wider (should NOT warn) ---

int test_i(int x) {
return -x; // no warning
}

unsigned int test_ui(unsigned int x) {
return -x; // no warning
}

long test_l(long x) {
return -x; // no warning
}

unsigned long test_ul(unsigned long x) {
return -x; // no warning
}

long long test_ll(long long x) {
return -x; // no warning
}

unsigned long long test_ull(unsigned long long x) {
return -x; // no warning
}