bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / unnecessarycatchthrow.cxx
blob73cb01456a40ea2a6e9dfe0b68b9b397901ed060
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 #include <cassert>
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <set>
16 #include <clang/AST/CXXInheritance.h>
17 #include "compat.hxx"
18 #include "plugin.hxx"
20 /**
21 look for unnecessary blocks that just catch and rethrow:
22 try {
23 stuff
24 } catch (exception const &) {
25 throw;
29 namespace {
31 class UnnecessaryCatchThrow:
32 public loplugin::FilteringPlugin<UnnecessaryCatchThrow>
34 public:
35 explicit UnnecessaryCatchThrow(loplugin::InstantiationData const & data): FilteringPlugin(data) {}
37 virtual void run() override
39 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
42 bool VisitCXXTryStmt(CXXTryStmt const *);
45 bool UnnecessaryCatchThrow::VisitCXXTryStmt(CXXTryStmt const * tryStmt)
47 if (ignoreLocation(tryStmt))
48 return true;
49 if (tryStmt->getNumHandlers() != 1)
50 return true;
51 auto catchStmt = tryStmt->getHandler(0);
52 auto compoundStmt = dyn_cast<CompoundStmt>(catchStmt->getHandlerBlock());
53 if (!compoundStmt || compoundStmt->size() != 1)
54 return true;
55 auto throwExpr = dyn_cast<CXXThrowExpr>(compoundStmt->body_front());
56 if (!throwExpr)
57 return true;
58 auto subExpr = throwExpr->getSubExpr();
59 if (subExpr)
61 if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(subExpr)) {
62 if (!cxxConstructExpr->getConstructor()->isCopyConstructor())
63 return true;
64 if (!cxxConstructExpr->getConstructor()->getParent()->hasAttr<FinalAttr>())
65 return true;
66 if (cxxConstructExpr->getNumArgs() != 1)
67 return true;
68 subExpr = cxxConstructExpr->getArg(0);
70 auto declRefExpr = dyn_cast<DeclRefExpr>(subExpr->IgnoreImpCasts());
71 if (!declRefExpr)
72 return true;
73 if (declRefExpr->getDecl() != catchStmt->getExceptionDecl())
74 return true;
77 report( DiagnosticsEngine::Warning, "unnecessary catch and throw",
78 compat::getBeginLoc(catchStmt))
79 << catchStmt->getSourceRange();
80 return true;
84 loplugin::Plugin::Registration< UnnecessaryCatchThrow > X("unnecessarycatchthrow");
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */