1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
23 class SimplifyConstruct
: public loplugin::FilteringPlugin
<SimplifyConstruct
>
26 explicit SimplifyConstruct(loplugin::InstantiationData
const& data
)
27 : FilteringPlugin(data
)
31 virtual void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
33 bool VisitCXXConstructExpr(CXXConstructExpr
const*);
34 bool VisitVarDecl(VarDecl
const*);
36 // ignore some contexts within which nullptr is fine
37 bool TraverseReturnStmt(ReturnStmt
*) { return true; }
38 bool TraverseInitListExpr(InitListExpr
*) { return true; }
39 bool TraverseCXXBindTemporaryExpr(CXXBindTemporaryExpr
*) { return true; }
40 // ignore them for the shared visitor too
41 bool PreTraverseReturnStmt(ReturnStmt
*) { return false; }
42 bool PreTraverseInitListExpr(InitListExpr
*) { return false; }
43 bool PreTraverseCXXBindTemporaryExpr(CXXBindTemporaryExpr
*) { return false; }
46 bool SimplifyConstruct::VisitCXXConstructExpr(CXXConstructExpr
const* constructExpr
)
48 if (ignoreLocation(constructExpr
))
50 auto tc
= loplugin::TypeCheck(constructExpr
->getType());
51 if (!tc
.Class("unique_ptr").StdNamespace() && !tc
.Class("shared_ptr").StdNamespace()
52 && !tc
.Class("SvRef").Namespace("tools").GlobalNamespace()
53 && !tc
.Class("Reference").Namespace("rtl").GlobalNamespace()
54 && !tc
.Class("Reference")
61 if (constructExpr
->getNumArgs() == 1
62 && isa
<CXXNullPtrLiteralExpr
>(constructExpr
->getArg(0)->IgnoreParenImpCasts()))
64 report(DiagnosticsEngine::Warning
,
65 "no need to explicitly init an instance of %0 with nullptr, just use default "
67 constructExpr
->getSourceRange().getBegin())
68 << constructExpr
->getType() << constructExpr
->getSourceRange();
73 bool SimplifyConstruct::VisitVarDecl(VarDecl
const* varDecl
)
75 if (ignoreLocation(varDecl
))
77 // cannot use OUString s("xxx") style syntax in a parameter
78 if (isa
<ParmVarDecl
>(varDecl
))
80 varDecl
= varDecl
->getCanonicalDecl();
81 if (!varDecl
->getInit())
83 if (varDecl
->getInitStyle() != VarDecl::InitializationStyle::CInit
)
85 if (!varDecl
->getType()->isRecordType())
87 if (isa
<AutoType
>(varDecl
->getType()))
90 auto init
= varDecl
->getInit();
91 auto const e1
= init
->IgnoreImplicit();
92 if (!isa
<CXXFunctionalCastExpr
>(e1
) && !isa
<CXXTemporaryObjectExpr
>(e1
))
95 // e.g. the LANGUAGE_DONTKNOW defines
96 if (compiler
.getSourceManager().isMacroBodyExpansion(init
->getBeginLoc()))
99 report(DiagnosticsEngine::Warning
, "simplify", varDecl
->getLocation())
100 << varDecl
->getSourceRange();
105 loplugin::Plugin::Registration
<SimplifyConstruct
> simplifyconstruct("simplifyconstruct", true);
108 #endif // LO_CLANG_SHARED_PLUGINS
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */