[PATCH] D139114: [Clang][Sema] Enabled Wshorten-64-to-32 for CompoundAssignment operator.
Fahad Nayyar via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 12 06:33:52 PST 2022
fahadnayyar updated this revision to Diff 482102.
fahadnayyar added a comment.
Added forward declare for CheckImplicitConversion to make the diff look clean.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D139114/new/
https://reviews.llvm.org/D139114
Files:
clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/conversion-64-32.c
Index: clang/test/Sema/conversion-64-32.c
===================================================================
--- clang/test/Sema/conversion-64-32.c
+++ clang/test/Sema/conversion-64-32.c
@@ -17,3 +17,25 @@
int test2(long v) {
return v / 2; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
}
+
+// rdar://10466193
+void test3(int i, long long ll) {
+ i += ll; // expected-warning {{implicit conversion loses integer precision}}
+ i -= ll; // expected-warning {{implicit conversion loses integer precision}}
+ i *= ll; // expected-warning {{implicit conversion loses integer precision}}
+ i /= ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+
+void test4(int i, long long ll) {
+ i += i-ll; // expected-warning {{implicit conversion loses integer precision}}
+ i += i+ll; // expected-warning {{implicit conversion loses integer precision}}
+ i -= i-ll; // expected-warning {{implicit conversion loses integer precision}}
+ i -= i+ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+
+void test5(int i, int j, long long ll) {
+ i += (i-j)*ll; // expected-warning {{implicit conversion loses integer precision}}
+ i += (i+j)*ll; // expected-warning {{implicit conversion loses integer precision}}
+ i -= ll/(i-j); // expected-warning {{implicit conversion loses integer precision}}
+ i -= ll/(i-j); // expected-warning {{implicit conversion loses integer precision}}
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -13374,6 +13374,11 @@
}
}
+static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
+ SourceLocation CC,
+ bool *ICContext = nullptr,
+ bool IsListInit = false);
+
/// Analyze the given compound assignment for the possible losing of
/// floating-point precision.
static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) {
@@ -13392,8 +13397,16 @@
->getComputationResultType()
->getAs<BuiltinType>();
+ // Check for implicit conversion loss of precision form 64-to-32 for compound
+ // statements.
+ if (E->getLHS()->getType()->isIntegerType() &&
+ E->getRHS()->getType()->isIntegerType() && !E->isShiftAssignOp())
+ CheckImplicitConversion(S, E->getRHS(), E->getType(),
+ E->getRHS()->getExprLoc());
+
// The below checks assume source is floating point.
- if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return;
+ if (!ResultBT || !RBT || !RBT->isFloatingPoint())
+ return;
// If source is floating point but target is an integer.
if (ResultBT->isInteger())
@@ -13682,9 +13695,8 @@
}
static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
- SourceLocation CC,
- bool *ICContext = nullptr,
- bool IsListInit = false) {
+ SourceLocation CC, bool *ICContext,
+ bool IsListInit) {
if (E->isTypeDependent() || E->isValueDependent()) return;
const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139114.482102.patch
Type: text/x-patch
Size: 3405 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221212/d0e89a92/attachment.bin>
More information about the cfe-commits
mailing list