Skip to content

Commit e9e4634

Browse files
committed
Separate implicit int conversion on negation sign to new diagnostic group
1 parent 995fd47 commit e9e4634

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ def DeprecatedOFast : DiagGroup<"deprecated-ofast">;
110110
def ObjCSignedCharBoolImplicitIntConversion :
111111
DiagGroup<"objc-signed-char-bool-implicit-int-conversion">;
112112
def Shorten64To32 : DiagGroup<"shorten-64-to-32">;
113+
def ImplicitIntConversionOnNegation : DiagGroup<"implicit-int-conversion-on-negation">;
113114
def ImplicitIntConversion : DiagGroup<"implicit-int-conversion",
114115
[Shorten64To32,
115-
ObjCSignedCharBoolImplicitIntConversion]>;
116+
ObjCSignedCharBoolImplicitIntConversion,
117+
ImplicitIntConversionOnNegation]>;
116118
def ImplicitConstIntFloatConversion : DiagGroup<"implicit-const-int-float-conversion">;
117119
def ImplicitIntFloatConversion : DiagGroup<"implicit-int-float-conversion",
118120
[ImplicitConstIntFloatConversion]>;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,6 +4217,9 @@ def warn_impcast_integer_sign_conditional : Warning<
42174217
def warn_impcast_integer_precision : Warning<
42184218
"implicit conversion loses integer precision: %0 to %1">,
42194219
InGroup<ImplicitIntConversion>, DefaultIgnore;
4220+
def warn_impcast_integer_precision_on_negation : Warning<
4221+
"implicit conversion loses integer precision: %0 to %1 on negation">,
4222+
InGroup<ImplicitIntConversionOnNegation>, DefaultIgnore;
42204223
def warn_impcast_high_order_zero_bits : Warning<
42214224
"higher order bits are zeroes after implicit conversion">,
42224225
InGroup<ImplicitIntConversion>, DefaultIgnore;

clang/lib/Sema/SemaChecking.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12091,6 +12091,11 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
1209112091
if (SourceMgr.isInSystemMacro(CC))
1209212092
return;
1209312093

12094+
if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
12095+
return DiagnoseImpCast(*this, E, T, CC,
12096+
diag::warn_impcast_integer_precision_on_negation);
12097+
}
12098+
1209412099
if (TargetRange.Width == 32 && Context.getIntWidth(E->getType()) == 64)
1209512100
return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_integer_64_32,
1209612101
/* pruneControlFlow */ true);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 %s -verify -Wimplicit-int-conversion
2+
// RUN: %clang_cc1 %s -verify -Wimplicit-int-conversion -Wno-implicit-int-conversion-on-negation -DNO_DIAG
3+
4+
#ifdef NO_DIAG
5+
unsigned char test_no_diag(unsigned char x) {
6+
return -x; // expected-no-diagnostics
7+
}
8+
#else
9+
unsigned char test_diag(unsigned char x) {
10+
return -x; // expected-warning {{implicit conversion loses integer precision: 'int' to 'unsigned char' on negation}}
11+
}
12+
#endif

0 commit comments

Comments
 (0)