2017-09-22 07:52:24 -05:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
2019-07-16 01:13:59 -05:00
|
|
|
#ifndef LO_CLANG_SHARED_PLUGINS
|
|
|
|
|
2017-09-22 07:52:24 -05:00
|
|
|
#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:
|
2018-08-23 07:35:15 -05:00
|
|
|
public loplugin::FilteringPlugin<UnnecessaryCatchThrow>
|
2017-09-22 07:52:24 -05:00
|
|
|
{
|
|
|
|
public:
|
2018-08-23 07:35:15 -05:00
|
|
|
explicit UnnecessaryCatchThrow(loplugin::InstantiationData const & data): FilteringPlugin(data) {}
|
2017-09-22 07:52:24 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-09-26 02:53:13 -05:00
|
|
|
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);
|
|
|
|
}
|
2017-09-22 07:52:24 -05:00
|
|
|
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",
|
2022-02-15 08:03:24 -06:00
|
|
|
catchStmt->getBeginLoc())
|
2017-09-22 07:52:24 -05:00
|
|
|
<< catchStmt->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-16 01:13:59 -05:00
|
|
|
loplugin::Plugin::Registration< UnnecessaryCatchThrow > unnecessarycatchthrow("unnecessarycatchthrow");
|
2019-07-15 05:14:34 -05:00
|
|
|
|
2019-07-16 01:13:59 -05:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif // LO_CLANG_SHARED_PLUGINS
|
2017-09-22 07:52:24 -05:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|