Kaydet (Commit) 50e64425 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Take array-to-pointer decay into account in loplugin:redundantcast

Change-Id: I3c5bace180605d1a72a74feb1bf9f9b184a16d83
Reviewed-on: https://gerrit.libreoffice.org/63545
Tested-by: Jenkins
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst adc8dfe0
......@@ -358,6 +358,28 @@ bool RedundantCast::VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr) {
<< expr->getSourceRange();
return true;
}
if (auto const impl = dyn_cast<ImplicitCastExpr>(expr->getSubExpr())) {
if (impl->getCastKind() == CK_ArrayToPointerDecay && impl->getType() == t2)
//TODO: instead of exact QualType match, allow some variation?
{
auto const fn = handler.getMainFileName();
if (!(loplugin::isSamePathname(
fn, SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx")
|| loplugin::isSamePathname(
fn, SRCDIR "/sal/qa/rtl/strings/test_ostring_stringliterals.cxx")
|| loplugin::isSamePathname(
fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx")
|| loplugin::isSamePathname(
fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_stringliterals.cxx")))
{
report(
DiagnosticsEngine::Warning, "redundant static_cast from %0 to %1",
expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType() << t2 << expr->getSourceRange();
}
return true;
}
}
auto const t3 = expr->getType();
auto const c1 = t1.getCanonicalType();
auto const c3 = t3.getCanonicalType();
......@@ -471,6 +493,30 @@ bool RedundantCast::VisitCXXReinterpretCastExpr(
if (ignoreLocation(expr)) {
return true;
}
if (auto const sub = dyn_cast<ImplicitCastExpr>(expr->getSubExpr())) {
if (sub->getCastKind() == CK_ArrayToPointerDecay && sub->getType() == expr->getType())
//TODO: instead of exact QualType match, allow some variation?
{
if (loplugin::TypeCheck(sub->getType()).Pointer().Const().Char()) {
if (auto const lit = dyn_cast<clang::StringLiteral>(expr->getSubExprAsWritten())) {
if (lit->getKind() == clang::StringLiteral::UTF8) {
// Don't warn about
//
// redundant_cast<char const *>(u8"...")
//
// in pre-C++2a code:
return true;
}
}
}
report(
DiagnosticsEngine::Warning, "redundant reinterpret_cast from %0 to %1",
expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType() << expr->getTypeAsWritten()
<< expr->getSourceRange();
return true;
}
}
if (expr->getSubExpr()->getType()->isVoidPointerType()) {
auto t = expr->getType()->getAs<clang::PointerType>();
if (t == nullptr || !t->getPointeeType()->isObjectType()) {
......
......@@ -386,6 +386,12 @@ void testIntermediaryStaticCast() {
d = int(d) + 1.0; // expected-error {{suspicious functional cast from 'double' to 'int', result is implicitly cast to 'double' [loplugin:redundantcast]}}
};
void testArrayDecay() {
(void) static_cast<char const *>(""); // expected-error {{redundant static_cast from 'const char [1]' to 'const char *' [loplugin:redundantcast]}}
(void) reinterpret_cast<char const *>(""); // expected-error {{redundant reinterpret_cast from 'const char [1]' to 'const char *' [loplugin:redundantcast]}}
(void) reinterpret_cast<char const *>(u8"");
}
int main() {
testConstCast();
testStaticCast();
......@@ -395,6 +401,7 @@ int main() {
testReinterpretConstCast();
testDynamicCast();
testIntermediaryStaticCast();
testArrayDecay();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
......@@ -523,8 +523,8 @@ public:
const char *mpString;
} const aRuns[] = {
#define SET(font,string) { font, reinterpret_cast<const char *>(string) }
SET("sans", "a"), // logical font - no 'sans' font.
SET("opensymbol", "#$%"), // font fallback - $ is missing.
{"sans", "a"}, // logical font - no 'sans' font.
{"opensymbol", "#$%"}, // font fallback - $ is missing.
SET("sans", pInvalid), // unicode invalid character
// tdf#96266 - stacking diacritics
SET("carlito", pDiacritic1),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment