/* -*- 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
/** * look for places we can use std::make_shared
*/
namespace
{ class MakeShared : public loplugin::FilteringPlugin<MakeShared>
{ public: explicit MakeShared(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
virtualbool preRun() override
{
StringRef fn(handler.getMainFileName()); // TODO something weird with protected base classes going on here if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xeextlst.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xecontent.cxx")) returnfalse; // no idea what is going on here if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/sidebar/nbdtmg.cxx")) returnfalse;
// legitimate use of moving std::unique_ptr to std::shared_ptr if (loplugin::isSamePathname(fn, SRCDIR "/comphelper/source/container/enumerablemap.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/items/style.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/app/weldutils.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/appl/appopen.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/table/tablertfimporter.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/docshell/externalrefmgr.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/attr/swatrset.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/condformat/condformatdlg.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/layout/frmtool.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xihelper.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xeformula.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xichart.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/html/htmlpars.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/view/cellsh1.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/html/htmltab.cxx")) returnfalse; if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/ww8/docxattributeoutput.cxx")) returnfalse; returntrue;
}
virtualvoid run() override
{ if (preRun())
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool MakeShared::VisitCXXConstructExpr(CXXConstructExpr const* constructExpr)
{ if (ignoreLocation(constructExpr)) returntrue; if (!loplugin::TypeCheck(constructExpr->getType()).ClassOrStruct("shared_ptr").StdNamespace()) returntrue; if (!(constructExpr->getNumArgs() == 1
|| (constructExpr->getNumArgs() > 1 && isa<CXXDefaultArgExpr>(constructExpr->getArg(1))))) returntrue; auto arg0 = constructExpr->getArg(0)->IgnoreParenImpCasts(); auto cxxNewExpr = dyn_cast<CXXNewExpr>(arg0); if (cxxNewExpr)
{ auto construct2 = cxxNewExpr->getConstructExpr(); if (construct2)
{ if (construct2->getConstructor()->getAccess() != AS_public) returntrue; if (construct2->getNumArgs() == 1
&& isa<CXXStdInitializerListExpr>(construct2->getArg(0))) returntrue;
}
} elseif (loplugin::TypeCheck(arg0->getType()).ClassOrStruct("shared_ptr").StdNamespace()) returntrue; elseif (loplugin::TypeCheck(arg0->getType()).ClassOrStruct("weak_ptr").StdNamespace()) returntrue; elseif (arg0->getType()->isDependentType()) returntrue; elseif (isa<CXXNullPtrLiteralExpr>(arg0)) returntrue; elseif (autoconst call = dyn_cast<CallExpr>(arg0))
{ if (autoconst decl = call->getDirectCallee())
{ // Don't warn about cases where e.g. the Bitmap* result of calling Windows' // Bitmap::FromBITMAPINFO is wrapped in a shared_ptr: if (decl->getReturnType()->isPointerType()
&& compiler.getSourceManager().isInSystemHeader(decl->getLocation()))
{ returntrue;
}
}
}
StringRef fn = getFilenameOfLocation(
compiler.getSourceManager().getSpellingLoc(constructExpr->getBeginLoc())); if (loplugin::isSamePathname(fn, SRCDIR "/include/o3tl/make_shared.hxx")) returntrue; if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/items/stylepool.cxx")) returntrue;
report(DiagnosticsEngine::Warning, "rather use make_shared than constructing from %0",
constructExpr->getBeginLoc())
<< arg0->getType() << constructExpr->getSourceRange(); returntrue;
}
bool MakeShared::VisitCXXMemberCallExpr(CXXMemberCallExpr const* cxxMemberCallExpr)
{ if (ignoreLocation(cxxMemberCallExpr)) returntrue;
if (cxxMemberCallExpr->getNumArgs() != 1) returntrue;
// cannot find a way to use the loplugin::DeclCheck stuff here auto templateDecl
= dyn_cast<ClassTemplateSpecializationDecl>(cxxMemberCallExpr->getRecordDecl()); if (!templateDecl) returntrue; auto cxxRecordDecl = templateDecl->getSpecializedTemplate()->getTemplatedDecl(); if (!cxxRecordDecl->getName().contains("shared_ptr")) returntrue;
if (autoconst id = cxxMemberCallExpr->getMethodDecl()->getIdentifier())
{ if (id->getName() != "reset") returntrue;
} auto cxxNewExpr = dyn_cast<CXXNewExpr>(cxxMemberCallExpr->getArg(0)->IgnoreParenImpCasts()); if (!cxxNewExpr) returntrue; if (cxxNewExpr->getConstructExpr()
&& cxxNewExpr->getConstructExpr()->getConstructor()->getAccess() != AS_public) returntrue;
StringRef fn = getFilenameOfLocation(
compiler.getSourceManager().getSpellingLoc(cxxMemberCallExpr->getBeginLoc())); if (loplugin::isSamePathname(fn, SRCDIR "/include/o3tl/make_shared.hxx")) returntrue;
report(DiagnosticsEngine::Warning, "rather use make_shared", cxxNewExpr->getBeginLoc())
<< cxxNewExpr->getSourceRange();
returntrue;
}
bool MakeShared::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* operCallExpr)
{ if (ignoreLocation(operCallExpr)) returntrue; if (!operCallExpr->isAssignmentOp()) returntrue;
if (!loplugin::TypeCheck(operCallExpr->getType()).ClassOrStruct("shared_ptr").StdNamespace()) returntrue;
if (loplugin::TypeCheck(operCallExpr->getArg(1)->getType())
.ClassOrStruct("shared_ptr")
.StdNamespace()) returntrue;
report(DiagnosticsEngine::Warning, "rather use make_shared than constructing from %0",
operCallExpr->getBeginLoc())
<< operCallExpr->getArg(1)->getType() << operCallExpr->getSourceRange();
returntrue;
}
bool MakeShared::VisitVarDecl(VarDecl const* varDecl)
{ if (ignoreLocation(varDecl)) returntrue; if (!varDecl->hasInit()) returntrue;
if (!loplugin::TypeCheck(varDecl->getType()).ClassOrStruct("shared_ptr").StdNamespace()) returntrue;
if (varDecl->getInit()->getType().isNull()) returntrue; if (varDecl->getInit()->getType()->isDependentType()) returntrue; if (loplugin::TypeCheck(varDecl->getInit()->IgnoreParenImpCasts()->getType())
.ClassOrStruct("shared_ptr")
.StdNamespace()) returntrue;
report(DiagnosticsEngine::Warning, "rather use make_shared than constructing from %0",
varDecl->getBeginLoc())
<< varDecl->getInit()->getType() << varDecl->getSourceRange();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.