Improvements to Clang's diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+- Clang now supports implicit conversion warnings (``-Wsign-conversion``,
+ ``Wshorten-64-to-32``, etc) for compound assignment operators (like +=, -=, <<=, >>= etc)
+ with integral operands.
- Clang will now check compile-time determinable string literals as format strings.
Fixes `Issue 55805: <https://github.com/llvm/llvm-project/issues/55805>`_.
- ``-Wformat`` now recognizes ``%b`` for the ``printf``/``scanf`` family of
}
}
+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.
+/// floating-point precision and for implicit conversions for integral operands.
static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) {
assert(isa<CompoundAssignOperator>(E) &&
"Must be compound assignment operation");
->getComputationResultType()
->getAs<BuiltinType>();
+ // Check for implicit conversions for compound assignment statements with
+ // intergral operands.
+ // FIXME: should this handle floating-point as well?
+ 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())
}
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();
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}}
+}
+
+void test6(char c) {
+ c <<= 999999; // expected-warning {{shift count >= width of type}} \
+ // cxx17-warning {{shift count >= width of type}} \
+ // ref-warning {{shift count >= width of type}} \
+ // ref-cxx17-warning {{shift count >= width of type}}
+ c >>= 999999; // expected-warning {{shift count >= width of type}} \
+ // cxx17-warning {{shift count >= width of type}} \
+ // ref-warning {{shift count >= width of type}} \
+ // ref-cxx17-warning {{shift count >= width of type}}
+}