diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2020-01-09 19:38:32 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2020-01-09 20:21:59 +0100 |
commit | 6efffbbfce9c27439f54970f7a569b069ce46eba (patch) | |
tree | cecff16ae5325875c3e6471170040c38859bff56 /compilerplugins | |
parent | 2ab481b038b62b1ff576ac4d49d03c1798cd7f84 (diff) |
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 <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/check.cxx | 15 | ||||
-rw-r--r-- | compilerplugins/clang/test/redundantcast.cxx | 14 |
2 files changed, 28 insertions, 1 deletions
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<clang::ElaboratedType>(t1)) { + t1 = et->getNamedType(); + } + auto t2 = type2.getLocalUnqualifiedType(); + if (auto const et = llvm::dyn_cast<clang::ElaboratedType>(t2)) { + t2 = et->getNamedType(); + } + return t1 == t2; +} + bool isArithmeticOp(clang::Expr const * expr) { expr = expr->IgnoreParenImpCasts(); if (auto const e = llvm::dyn_cast<clang::BinaryOperator>(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<clang::DecltypeType>(t1) || llvm::isa<clang::DecltypeType>(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 <sal/config.h> + #include <cstddef> +#include <sal/types.h> + #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<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<::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<short>(n); // doesn't warn, even if 'sal_Int16' is 'short' + using Other = sal_Int16; + (void) static_cast<Other>(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: */ |