summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/simplifydynamiccast.cxx
blob: ffb81658d8c8aa619c8e91c2e948b9223cbf1bec (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*- 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/.
 */

#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>

#include <clang/AST/CXXInheritance.h>
#include "plugin.hxx"

namespace
{
class SimplifyDynamicCast : public loplugin::FilteringPlugin<SimplifyDynamicCast>
{
public:
    explicit SimplifyDynamicCast(loplugin::InstantiationData const& data)
        : FilteringPlugin(data)
    {
    }

    virtual void run() override
    {
        //        StringRef fn(handler.getMainFileName());
        //        if (loplugin::isSamePathname(fn, WORKDIR "/YaccTarget/unoidl/source/sourceprovider-parser.cxx"))
        //             return;

        TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
    }

    bool TraverseIfStmt(IfStmt*);
    bool VisitCXXStaticCastExpr(CXXStaticCastExpr const*);

private:
    std::vector<QualType> dynamicCastVec;
    std::vector<Decl const*> dynamicCastSubExprVec;
    std::vector<IfStmt const*> ifVec;
};

bool SimplifyDynamicCast::TraverseIfStmt(IfStmt* ifStmt)
{
    auto condExpr = ifStmt->getCond()->IgnoreParenImpCasts();
    auto dynamicCastExpr = dyn_cast<CXXDynamicCastExpr>(condExpr);
    if (!dynamicCastExpr)
    {
        if (auto binaryOp = dyn_cast<BinaryOperator>(condExpr))
        {
            if (binaryOp->getOpcode() == BO_NE)
                dynamicCastExpr
                    = dyn_cast<CXXDynamicCastExpr>(binaryOp->getLHS()->IgnoreParenImpCasts());
        }
    }
    Decl const* subExprDecl = nullptr;
    if (dynamicCastExpr)
    {
        auto subExprDeclRefExpr
            = dyn_cast<DeclRefExpr>(dynamicCastExpr->getSubExpr()->IgnoreParenImpCasts());
        if (!subExprDeclRefExpr)
            dynamicCastExpr = nullptr;
        else
            subExprDecl = subExprDeclRefExpr->getDecl();
    }
    if (dynamicCastExpr)
    {
        auto qt = dynamicCastExpr->getTypeAsWritten();
        dynamicCastVec.push_back(qt);
        dynamicCastSubExprVec.push_back(subExprDecl);
        ifVec.push_back(ifStmt);
    }
    bool ret = RecursiveASTVisitor::TraverseIfStmt(ifStmt);
    if (dynamicCastExpr)
    {
        dynamicCastVec.pop_back();
        dynamicCastSubExprVec.pop_back();
        ifVec.pop_back();
    }
    return ret;
}

bool SimplifyDynamicCast::VisitCXXStaticCastExpr(CXXStaticCastExpr const* staticCastExpr)
{
    if (ignoreLocation(staticCastExpr))
        return true;
    if (dynamicCastVec.empty())
        return true;

    auto qt = staticCastExpr->getTypeAsWritten();
    auto it = std::find(dynamicCastVec.begin(), dynamicCastVec.end(), qt);
    if (it == dynamicCastVec.end())
        return true;
    int idx = it - dynamicCastVec.begin();
    auto subExprDecl = dyn_cast<DeclRefExpr>(staticCastExpr->getSubExpr()->IgnoreParenImpCasts());
    if (!subExprDecl)
        return true;
    if (dynamicCastSubExprVec[idx] != subExprDecl->getDecl())
        return true;
    report(DiagnosticsEngine::Warning, "simplify, use var in if", staticCastExpr->getBeginLoc())
        << staticCastExpr->getSourceRange();
    auto ifStmt = ifVec[idx];
    report(DiagnosticsEngine::Note, "if here", ifStmt->getBeginLoc()) << ifStmt->getSourceRange();
    return true;
}

loplugin::Plugin::Registration<SimplifyDynamicCast> X("simplifydynamiccast", true);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */