From 331faca18ebdd843c06fa2435ee1bf71457e76dc Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Thu, 29 Jan 2015 08:16:13 +0100 Subject: [PATCH] Extract loplugin:redundantcast from loplugin:cstylecast Change-Id: I08f17dd9cc092206083ff41bbbc178e0322e86d0 --- .../source/cpp_uno/shared/vtablefactory.cxx | 6 +- compilerplugins/clang/cstylecast.cxx | 28 ------- compilerplugins/clang/redundantcast.cxx | 84 +++++++++++++++++++ sal/osl/unx/file.cxx | 2 +- 4 files changed, 88 insertions(+), 32 deletions(-) create mode 100644 compilerplugins/clang/redundantcast.cxx diff --git a/bridges/source/cpp_uno/shared/vtablefactory.cxx b/bridges/source/cpp_uno/shared/vtablefactory.cxx index 226a994bf664..474dfcdcfaff 100644 --- a/bridges/source/cpp_uno/shared/vtablefactory.cxx +++ b/bridges/source/cpp_uno/shared/vtablefactory.cxx @@ -91,9 +91,9 @@ extern "C" void * SAL_CALL allocExec( if (p == MAP_FAILED) { p = 0; } - else if (mprotect (static_cast(p), n, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) + else if (mprotect (p, n, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { - munmap (static_cast(p), n); + munmap (p, n); p = 0; } #elif defined SAL_W32 @@ -109,7 +109,7 @@ extern "C" void SAL_CALL freeExec( SAL_UNUSED_PARAMETER rtl_arena_type *, void * address, sal_Size size) { #if defined SAL_UNX - munmap(static_cast< char * >(address), size); + munmap(address, size); #elif defined SAL_W32 (void) size; // unused VirtualFree(address, 0, MEM_RELEASE); diff --git a/compilerplugins/clang/cstylecast.cxx b/compilerplugins/clang/cstylecast.cxx index b6bb453127c0..9775a0875ee5 100644 --- a/compilerplugins/clang/cstylecast.cxx +++ b/compilerplugins/clang/cstylecast.cxx @@ -47,8 +47,6 @@ public: bool VisitCStyleCastExpr(const CStyleCastExpr * expr); - bool VisitImplicitCastExpr(ImplicitCastExpr const * expr); - private: bool externCFunction; }; @@ -135,32 +133,6 @@ bool CStyleCast::VisitCStyleCastExpr(const CStyleCastExpr * expr) { return true; } -bool CStyleCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) { - if (ignoreLocation(expr) || expr->getCastKind() != CK_BitCast) { - return true; - } - QualType t = expr->getType(); - if (!(t->isPointerType() - && t->getAs()->getPointeeType()->isVoidType() - && expr->getSubExpr()->getType()->isPointerType())) - { - return true; - } - Expr const * e = expr->getSubExpr()->IgnoreParenImpCasts(); - while (isa(e)) { - e = dyn_cast(e)->getSubExpr()->IgnoreParenImpCasts(); - } - if (isa(e)) { - report( - DiagnosticsEngine::Warning, - ("redundant reinterpret_cast, result is implicitly cast to void" - " pointer"), - e->getExprLoc()) - << e->getSourceRange(); - } - return true; -} - loplugin::Plugin::Registration< CStyleCast > X("cstylecast"); } diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx new file mode 100644 index 000000000000..e2f61792a55a --- /dev/null +++ b/compilerplugins/clang/redundantcast.cxx @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +// Warn about certain redundant casts: +// +// * A reinterpret_cast(...) whose result is then implicitly cast to a void +// pointer +// +// * A static_cast(e) where e is of void pointer type and whose result is +// then implicitly cast to a void pointer +// +// C-style casts are ignored because it makes this plugin simpler, and they +// should eventually be eliminated via loplugin:cstylecast and/or +// -Wold-style-cast. That implies that this plugin is only relevant for C++ +// code. + +#include "plugin.hxx" + +namespace { + +bool isVoidPointer(QualType type) { + return type->isPointerType() + && type->getAs()->getPointeeType()->isVoidType(); +} + +class RedundantCast: + public RecursiveASTVisitor, public loplugin::Plugin +{ +public: + explicit RedundantCast(InstantiationData const & data): Plugin(data) {} + + virtual void run() override { + if (compiler.getLangOpts().CPlusPlus) { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + bool VisitImplicitCastExpr(ImplicitCastExpr const * expr); +}; + +bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) { + if (ignoreLocation(expr) || expr->getCastKind() != CK_BitCast + || !isVoidPointer(expr->getType()) + || !expr->getSubExpr()->getType()->isPointerType()) + { + return true; + } + Expr const * e = expr->getSubExpr()->IgnoreParenImpCasts(); + while (isa(e)) { + e = dyn_cast(e)->getSubExpr()->IgnoreParenImpCasts(); + } + if (isa(e)) { + report( + DiagnosticsEngine::Warning, + ("redundant reinterpret_cast, result is implicitly cast to void" + " pointer"), + e->getExprLoc()) + << e->getSourceRange(); + } else if (isa(e) + && isVoidPointer( + dyn_cast(e)->getSubExpr() + ->IgnoreParenImpCasts()->getType())) + { + report( + DiagnosticsEngine::Warning, + ("redundant static_cast from void pointer, result is implicitly" + " cast to void pointer"), + e->getExprLoc()) + << e->getSourceRange(); + } + return true; +} + +loplugin::Plugin::Registration X("redundantcast"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/osl/unx/file.cxx b/sal/osl/unx/file.cxx index 659526bcd6ea..564784f1f7f5 100644 --- a/sal/osl/unx/file.cxx +++ b/sal/osl/unx/file.cxx @@ -1198,7 +1198,7 @@ unmapFile (void* pAddr, sal_uInt64 uLength) return osl_File_E_OVERFLOW; size_t const nLength = sal::static_int_cast< size_t >(uLength); - if (-1 == munmap(static_cast(pAddr), nLength)) + if (-1 == munmap(pAddr, nLength)) return oslTranslateFileError(OSL_FET_ERROR, errno); return osl_File_E_None;