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>
23 look for unnecessary blocks that just catch and rethrow:
26 } catch (exception const &) {
33 class UnnecessaryCatchThrow
:
34 public loplugin::FilteringPlugin
<UnnecessaryCatchThrow
>
37 explicit UnnecessaryCatchThrow(loplugin::InstantiationData
const & data
): FilteringPlugin(data
) {}
39 virtual void run() override
41 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
44 bool VisitCXXTryStmt(CXXTryStmt
const *);
47 bool UnnecessaryCatchThrow::VisitCXXTryStmt(CXXTryStmt
const * tryStmt
)
49 if (ignoreLocation(tryStmt
))
51 if (tryStmt
->getNumHandlers() != 1)
53 auto catchStmt
= tryStmt
->getHandler(0);
54 auto compoundStmt
= dyn_cast
<CompoundStmt
>(catchStmt
->getHandlerBlock());
55 if (!compoundStmt
|| compoundStmt
->size() != 1)
57 auto throwExpr
= dyn_cast
<CXXThrowExpr
>(compoundStmt
->body_front());
60 auto subExpr
= throwExpr
->getSubExpr();
63 if (auto cxxConstructExpr
= dyn_cast
<CXXConstructExpr
>(subExpr
)) {
64 if (!cxxConstructExpr
->getConstructor()->isCopyConstructor())
66 if (!cxxConstructExpr
->getConstructor()->getParent()->hasAttr
<FinalAttr
>())
68 if (cxxConstructExpr
->getNumArgs() != 1)
70 subExpr
= cxxConstructExpr
->getArg(0);
72 auto declRefExpr
= dyn_cast
<DeclRefExpr
>(subExpr
->IgnoreImpCasts());
75 if (declRefExpr
->getDecl() != catchStmt
->getExceptionDecl())
79 report( DiagnosticsEngine::Warning
, "unnecessary catch and throw",
80 compat::getBeginLoc(catchStmt
))
81 << catchStmt
->getSourceRange();
86 loplugin::Plugin::Registration
< UnnecessaryCatchThrow
> unnecessarycatchthrow("unnecessarycatchthrow");
90 #endif // LO_CLANG_SHARED_PLUGINS
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */