summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2015-01-29 08:16:13 +0100
committerStephan Bergmann <sbergman@redhat.com>2015-01-29 22:21:04 +0100
commit331faca18ebdd843c06fa2435ee1bf71457e76dc (patch)
tree58cd9eb7793300349bc9a4548904d6dc1f0c961e /compilerplugins
parentab2d0ff4d7c0a3c76401b0f07b85dc2267d1eb7c (diff)
Extract loplugin:redundantcast from loplugin:cstylecast
Change-Id: I08f17dd9cc092206083ff41bbbc178e0322e86d0
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/cstylecast.cxx28
-rw-r--r--compilerplugins/clang/redundantcast.cxx84
2 files changed, 84 insertions, 28 deletions
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<PointerType>()->getPointeeType()->isVoidType()
- && expr->getSubExpr()->getType()->isPointerType()))
- {
- return true;
- }
- Expr const * e = expr->getSubExpr()->IgnoreParenImpCasts();
- while (isa<CXXConstCastExpr>(e)) {
- e = dyn_cast<CXXConstCastExpr>(e)->getSubExpr()->IgnoreParenImpCasts();
- }
- if (isa<CXXReinterpretCastExpr>(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<T*>(...) whose result is then implicitly cast to a void
+// pointer
+//
+// * A static_cast<T*>(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<PointerType>()->getPointeeType()->isVoidType();
+}
+
+class RedundantCast:
+ public RecursiveASTVisitor<RedundantCast>, 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<CXXConstCastExpr>(e)) {
+ e = dyn_cast<CXXConstCastExpr>(e)->getSubExpr()->IgnoreParenImpCasts();
+ }
+ if (isa<CXXReinterpretCastExpr>(e)) {
+ report(
+ DiagnosticsEngine::Warning,
+ ("redundant reinterpret_cast, result is implicitly cast to void"
+ " pointer"),
+ e->getExprLoc())
+ << e->getSourceRange();
+ } else if (isa<CXXStaticCastExpr>(e)
+ && isVoidPointer(
+ dyn_cast<CXXStaticCastExpr>(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<RedundantCast> X("redundantcast");
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */