summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/unnecessarycatchthrow.cxx
blob: 0a8265b170d6205eca4a53f0d72745e0528ec738 (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
/* -*- 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/.
 */

#ifndef LO_CLANG_SHARED_PLUGINS

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

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

/**
look for unnecessary blocks that just catch and rethrow:
    try {
       stuff
    } catch (exception const &) {
      throw;
    }
*/

namespace {

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

    virtual void run() override
    {
        TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
    }

    bool VisitCXXTryStmt(CXXTryStmt const *);
};

bool UnnecessaryCatchThrow::VisitCXXTryStmt(CXXTryStmt const * tryStmt)
{
    if (ignoreLocation(tryStmt))
        return true;
    if (tryStmt->getNumHandlers() != 1)
        return true;
    auto catchStmt = tryStmt->getHandler(0);
    auto compoundStmt = dyn_cast<CompoundStmt>(catchStmt->getHandlerBlock());
    if (!compoundStmt || compoundStmt->size() != 1)
        return true;
    auto throwExpr = dyn_cast<CXXThrowExpr>(compoundStmt->body_front());
    if (!throwExpr)
        return true;
    auto subExpr = throwExpr->getSubExpr();
    if (subExpr)
    {
        if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(subExpr)) {
            if (!cxxConstructExpr->getConstructor()->isCopyConstructor())
                return true;
            if (!cxxConstructExpr->getConstructor()->getParent()->hasAttr<FinalAttr>())
                return true;
            if (cxxConstructExpr->getNumArgs() != 1)
                return true;
            subExpr = cxxConstructExpr->getArg(0);
        }
        auto declRefExpr = dyn_cast<DeclRefExpr>(subExpr->IgnoreImpCasts());
        if (!declRefExpr)
            return true;
        if (declRefExpr->getDecl() != catchStmt->getExceptionDecl())
            return true;
    }

    report( DiagnosticsEngine::Warning, "unnecessary catch and throw",
            catchStmt->getBeginLoc())
            << catchStmt->getSourceRange();
    return true;
}


loplugin::Plugin::Registration< UnnecessaryCatchThrow > unnecessarycatchthrow("unnecessarycatchthrow");

} // namespace

#endif // LO_CLANG_SHARED_PLUGINS

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