From 6efffbbfce9c27439f54970f7a569b069ce46eba Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Thu, 9 Jan 2020 19:38:32 +0100 Subject: Improve loplugin:redundantcast for sal_Int... vs. ::sal_Int... Change-Id: I1548a76fdc03afee68f1e5c01bc665e616f2edf2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86501 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- compilerplugins/clang/check.cxx | 15 ++++++++++++++- compilerplugins/clang/test/redundantcast.cxx | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/check.cxx b/compilerplugins/clang/check.cxx index be1b1f764eb1..472e296907b0 100644 --- a/compilerplugins/clang/check.cxx +++ b/compilerplugins/clang/check.cxx @@ -273,6 +273,19 @@ bool isExtraWarnUnusedType(clang::QualType type) { namespace { +// Make sure Foo and ::Foo are considered equal: +bool areSameSugaredType(clang::QualType type1, clang::QualType type2) { + auto t1 = type1.getLocalUnqualifiedType(); + if (auto const et = llvm::dyn_cast(t1)) { + t1 = et->getNamedType(); + } + auto t2 = type2.getLocalUnqualifiedType(); + if (auto const et = llvm::dyn_cast(t2)) { + t2 = et->getNamedType(); + } + return t1 == t2; +} + bool isArithmeticOp(clang::Expr const * expr) { expr = expr->IgnoreParenImpCasts(); if (auto const e = llvm::dyn_cast(expr)) { @@ -311,7 +324,7 @@ bool isOkToRemoveArithmeticCast( // suffix like L it could still be either long or long long): if ((t1->isIntegralType(context) || t1->isRealFloatingType()) - && ((t1.getLocalUnqualifiedType() != t2.getLocalUnqualifiedType() + && ((!areSameSugaredType(t1, t2) && (loplugin::TypeCheck(t1).Typedef() || loplugin::TypeCheck(t2).Typedef() || llvm::isa(t1) || llvm::isa(t2))) diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx index 2ffd8f93c96f..03ce47796d65 100644 --- a/compilerplugins/clang/test/redundantcast.cxx +++ b/compilerplugins/clang/test/redundantcast.cxx @@ -7,8 +7,12 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include + #include +#include + #include "redundantcast.hxx" void f1(char *) {} @@ -418,6 +422,15 @@ auto testNullFunctionPointer(int i, F p) { } } +void testSalIntTypes() { + sal_Int16 const n = 0; + (void) static_cast(n); // expected-error-re {{static_cast from 'const sal_Int16' (aka 'const {{.+}}') lvalue to 'sal_Int16' (aka '{{.+}}') prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}} + (void) static_cast<::sal_Int16>(n); // expected-error-re {{static_cast from 'const sal_Int16' (aka 'const {{.+}}') lvalue to '::sal_Int16' (aka '{{.+}}') prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}} + (void) static_cast(n); // doesn't warn, even if 'sal_Int16' is 'short' + using Other = sal_Int16; + (void) static_cast(n); // doesn't warn either +} + int main() { testConstCast(); testStaticCast(); @@ -428,6 +441,7 @@ int main() { testDynamicCast(); testIntermediaryStaticCast(); testArrayDecay(); + testSalIntTypes(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit