new loplugin:staticdynamic

look for places we are dynamic_cast'ing after static_cast'ing,
which means the dynamic_cast is a waste of time.

Change-Id: Ife11bb675020738040646230bbd038278d84f7f2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111631
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel 2021-02-26 15:20:17 +02:00 committed by Noel Grandin
parent 46d3370d6c
commit 11083d1fcf
6 changed files with 156 additions and 4 deletions

View File

@ -535,8 +535,9 @@ void SchAttribTabDlg::PageCreated(const OString& rId, SfxTabPage &rPage)
else if (rId == "axislabel") else if (rId == "axislabel")
{ {
bool bShowStaggeringControls = m_pParameter->CanAxisLabelsBeStaggered(); bool bShowStaggeringControls = m_pParameter->CanAxisLabelsBeStaggered();
static_cast<SchAxisLabelTabPage&>(rPage).ShowStaggeringControls( bShowStaggeringControls ); auto & rLabelPage = static_cast<SchAxisLabelTabPage&>(rPage);
dynamic_cast< SchAxisLabelTabPage& >( rPage ).SetComplexCategories( m_pParameter->IsComplexCategoriesAxis() ); rLabelPage.ShowStaggeringControls( bShowStaggeringControls );
rLabelPage.SetComplexCategories( m_pParameter->IsComplexCategoriesAxis() );
} }
else if (rId == "axispos") else if (rId == "axispos")
{ {

View File

@ -43,6 +43,7 @@ public:
bool TraverseWhileStmt( WhileStmt* ) { return complain(); } bool TraverseWhileStmt( WhileStmt* ) { return complain(); }
bool TraverseDoStmt( DoStmt* ) { return complain(); } bool TraverseDoStmt( DoStmt* ) { return complain(); }
bool TraverseForStmt( ForStmt* ) { return complain(); } bool TraverseForStmt( ForStmt* ) { return complain(); }
bool TraverseCompoundStmt( CompoundStmt* ) { return complain(); }
bool TraverseCXXForRangeStmt( CXXForRangeStmt* ) { return complain(); } bool TraverseCXXForRangeStmt( CXXForRangeStmt* ) { return complain(); }
bool TraverseConditionalOperator( ConditionalOperator* ) { return complain(); } bool TraverseConditionalOperator( ConditionalOperator* ) { return complain(); }
bool TraverseCXXCatchStmt( CXXCatchStmt* ) { return complain(); } bool TraverseCXXCatchStmt( CXXCatchStmt* ) { return complain(); }

View File

@ -0,0 +1,120 @@
/* -*- 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/.
*/
#ifndef LO_CLANG_SHARED_PLUGINS
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include "compat.hxx"
#include "check.hxx"
#include "plugin.hxx"
namespace
{
class StaticDynamic : public loplugin::FilteringPlugin<StaticDynamic>
{
public:
explicit StaticDynamic(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
void postRun() override {}
virtual void run() override
{
if (preRun())
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitCXXDynamicCastExpr(CXXDynamicCastExpr const*);
bool VisitCXXStaticCastExpr(CXXStaticCastExpr const*);
bool PreTraverseCompoundStmt(CompoundStmt*);
bool PostTraverseCompoundStmt(CompoundStmt*, bool);
bool TraverseCompoundStmt(CompoundStmt*);
private:
// the key is the pair of VarDecl and the type being cast to.
typedef std::map<std::pair<VarDecl const*, clang::Type const*>, SourceLocation> MapType;
MapType staticCastVars;
// only maintain state inside a single basic block, we're not trying to analyse
// cross-block interactions.
std::vector<MapType> blockStack;
};
bool StaticDynamic::PreTraverseCompoundStmt(CompoundStmt*)
{
blockStack.push_back(std::move(staticCastVars));
return true;
}
bool StaticDynamic::PostTraverseCompoundStmt(CompoundStmt*, bool)
{
staticCastVars = std::move(blockStack.back());
blockStack.pop_back();
return true;
}
bool StaticDynamic::TraverseCompoundStmt(CompoundStmt* compoundStmt)
{
bool ret = true;
if (PreTraverseCompoundStmt(compoundStmt))
{
ret = FilteringPlugin::TraverseCompoundStmt(compoundStmt);
PostTraverseCompoundStmt(compoundStmt, ret);
}
return ret;
}
bool StaticDynamic::VisitCXXStaticCastExpr(CXXStaticCastExpr const* staticCastExpr)
{
if (ignoreLocation(staticCastExpr))
return true;
auto subExprDecl = dyn_cast<DeclRefExpr>(staticCastExpr->getSubExpr()->IgnoreParenImpCasts());
if (!subExprDecl)
return true;
auto varDecl = dyn_cast_or_null<VarDecl>(subExprDecl->getDecl());
if (!varDecl)
return true;
staticCastVars.insert({ { varDecl, staticCastExpr->getTypeAsWritten().getTypePtr() },
compat::getBeginLoc(staticCastExpr) });
return true;
}
bool StaticDynamic::VisitCXXDynamicCastExpr(CXXDynamicCastExpr const* dynamicCastExpr)
{
if (ignoreLocation(dynamicCastExpr))
return true;
auto subExprDecl = dyn_cast<DeclRefExpr>(dynamicCastExpr->getSubExpr()->IgnoreParenImpCasts());
if (!subExprDecl)
return true;
auto varDecl = dyn_cast_or_null<VarDecl>(subExprDecl->getDecl());
if (!varDecl)
return true;
auto it = staticCastVars.find({ varDecl, dynamicCastExpr->getTypeAsWritten().getTypePtr() });
if (it == staticCastVars.end())
return true;
report(DiagnosticsEngine::Warning, "dynamic_cast after static_cast",
compat::getBeginLoc(dynamicCastExpr))
<< dynamicCastExpr->getSourceRange();
report(DiagnosticsEngine::Note, "static_cast here", it->second);
return true;
}
loplugin::Plugin::Registration<StaticDynamic> staticdynamic("staticdynamic");
}
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -0,0 +1,28 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* 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/.
*/
struct ClassA
{
virtual ~ClassA() {}
};
struct ClassB : public ClassA
{
void foo() {}
};
void f1(ClassA* p1)
{
// expected-note@+1 {{static_cast here [loplugin:staticdynamic]}}
static_cast<ClassB*>(p1)->foo();
// expected-error@+1 {{dynamic_cast after static_cast [loplugin:staticdynamic]}}
dynamic_cast<ClassB*>(p1)->foo();
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */

View File

@ -412,8 +412,9 @@ SdrPage* SdDrawDocument::RemovePage(sal_uInt16 nPgNum)
bool bLast = ((nPgNum+1)/2 == (GetPageCount()+1)/2); bool bLast = ((nPgNum+1)/2 == (GetPageCount()+1)/2);
static_cast<SdPage*>(pPage)->DisconnectLink(); auto pSdPage = static_cast<SdPage*>(pPage);
ReplacePageInCustomShows( dynamic_cast< SdPage* >( pPage ), nullptr ); pSdPage->DisconnectLink();
ReplacePageInCustomShows( pSdPage, nullptr );
UpdatePageObjectsInNotes(nPgNum); UpdatePageObjectsInNotes(nPgNum);
if (!bLast) if (!bLast)

View File

@ -80,6 +80,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
compilerplugins/clang/test/simplifypointertobool \ compilerplugins/clang/test/simplifypointertobool \
compilerplugins/clang/test/singlevalfields \ compilerplugins/clang/test/singlevalfields \
compilerplugins/clang/test/staticconstfield \ compilerplugins/clang/test/staticconstfield \
compilerplugins/clang/test/staticdynamic \
compilerplugins/clang/test/staticvar \ compilerplugins/clang/test/staticvar \
compilerplugins/clang/test/stdfunction \ compilerplugins/clang/test/stdfunction \
compilerplugins/clang/test/stringadd \ compilerplugins/clang/test/stringadd \