1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
18 #include <clang/AST/CXXInheritance.h>
22 look for unnecessary blocks that just catch and rethrow:
25 } catch (exception const &) {
32 class UnnecessaryCatchThrow
:
33 public loplugin::FilteringPlugin
<UnnecessaryCatchThrow
>
36 explicit UnnecessaryCatchThrow(loplugin::InstantiationData
const & data
): FilteringPlugin(data
) {}
38 virtual void run() override
40 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
43 bool VisitCXXTryStmt(CXXTryStmt
const *);
46 bool UnnecessaryCatchThrow::VisitCXXTryStmt(CXXTryStmt
const * tryStmt
)
48 if (ignoreLocation(tryStmt
))
50 if (tryStmt
->getNumHandlers() != 1)
52 auto catchStmt
= tryStmt
->getHandler(0);
53 auto compoundStmt
= dyn_cast
<CompoundStmt
>(catchStmt
->getHandlerBlock());
54 if (!compoundStmt
|| compoundStmt
->size() != 1)
56 auto throwExpr
= dyn_cast
<CXXThrowExpr
>(compoundStmt
->body_front());
59 auto subExpr
= throwExpr
->getSubExpr();
62 if (auto cxxConstructExpr
= dyn_cast
<CXXConstructExpr
>(subExpr
)) {
63 if (!cxxConstructExpr
->getConstructor()->isCopyConstructor())
65 if (!cxxConstructExpr
->getConstructor()->getParent()->hasAttr
<FinalAttr
>())
67 if (cxxConstructExpr
->getNumArgs() != 1)
69 subExpr
= cxxConstructExpr
->getArg(0);
71 auto declRefExpr
= dyn_cast
<DeclRefExpr
>(subExpr
->IgnoreImpCasts());
74 if (declRefExpr
->getDecl() != catchStmt
->getExceptionDecl())
78 report( DiagnosticsEngine::Warning
, "unnecessary catch and throw",
79 catchStmt
->getBeginLoc())
80 << catchStmt
->getSourceRange();
85 loplugin::Plugin::Registration
< UnnecessaryCatchThrow
> unnecessarycatchthrow("unnecessarycatchthrow");
89 #endif // LO_CLANG_SHARED_PLUGINS
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */