cid#1555694 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / compilerplugins / clang / unnecessarycatchthrow.cxx
blob0a8265b170d6205eca4a53f0d72745e0528ec738
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <cassert>
13 #include <string>
14 #include <iostream>
15 #include <fstream>
16 #include <set>
18 #include <clang/AST/CXXInheritance.h>
19 #include "plugin.hxx"
21 /**
22 look for unnecessary blocks that just catch and rethrow:
23 try {
24 stuff
25 } catch (exception const &) {
26 throw;
30 namespace {
32 class UnnecessaryCatchThrow:
33 public loplugin::FilteringPlugin<UnnecessaryCatchThrow>
35 public:
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))
49 return true;
50 if (tryStmt->getNumHandlers() != 1)
51 return true;
52 auto catchStmt = tryStmt->getHandler(0);
53 auto compoundStmt = dyn_cast<CompoundStmt>(catchStmt->getHandlerBlock());
54 if (!compoundStmt || compoundStmt->size() != 1)
55 return true;
56 auto throwExpr = dyn_cast<CXXThrowExpr>(compoundStmt->body_front());
57 if (!throwExpr)
58 return true;
59 auto subExpr = throwExpr->getSubExpr();
60 if (subExpr)
62 if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(subExpr)) {
63 if (!cxxConstructExpr->getConstructor()->isCopyConstructor())
64 return true;
65 if (!cxxConstructExpr->getConstructor()->getParent()->hasAttr<FinalAttr>())
66 return true;
67 if (cxxConstructExpr->getNumArgs() != 1)
68 return true;
69 subExpr = cxxConstructExpr->getArg(0);
71 auto declRefExpr = dyn_cast<DeclRefExpr>(subExpr->IgnoreImpCasts());
72 if (!declRefExpr)
73 return true;
74 if (declRefExpr->getDecl() != catchStmt->getExceptionDecl())
75 return true;
78 report( DiagnosticsEngine::Warning, "unnecessary catch and throw",
79 catchStmt->getBeginLoc())
80 << catchStmt->getSourceRange();
81 return true;
85 loplugin::Plugin::Registration< UnnecessaryCatchThrow > unnecessarycatchthrow("unnecessarycatchthrow");
87 } // namespace
89 #endif // LO_CLANG_SHARED_PLUGINS
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */