summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/redundantcast.cxx
blob: e2f61792a55aeada1f01aa34ec9fb091eac1dd57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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: */