1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
10
11 #include <cassert>
12 #include <string>
13 #include <iostream>
14 #include <fstream>
15 #include <set>
16
17 #include <clang/AST/CXXInheritance.h>
18
19 #include "check.hxx"
20 #include "compat.hxx"
21 #include "plugin.hxx"
22
23 /**
24 * look for places we can use std::make_shared
25 */
26
27 namespace
28 {
29 class MakeShared : public loplugin::FilteringPlugin<MakeShared>
30 {
31 public:
MakeShared(loplugin::InstantiationData const & data)32 explicit MakeShared(loplugin::InstantiationData const& data)
33 : FilteringPlugin(data)
34 {
35 }
36
preRun()37 virtual bool preRun() override
38 {
39 StringRef fn(handler.getMainFileName());
40 // uses boost::shared_ptr and we trigger because we're not looking specifically for std::shared_ptr
41 if (loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/cmis/cmis_repo_content.cxx"))
42 return false;
43 if (loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/cmis/cmis_content.cxx"))
44 return false;
45 // TODO something weird with protected base classes going on here
46 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xeextlst.cxx"))
47 return false;
48 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xecontent.cxx"))
49 return false;
50 // no idea what is going on here
51 if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/sidebar/nbdtmg.cxx"))
52 return false;
53
54 // legitimate use of moving std::unique_ptr to std::shared_ptr
55 if (loplugin::isSamePathname(fn, SRCDIR "/comphelper/source/container/enumerablemap.cxx"))
56 return false;
57 if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/items/style.cxx"))
58 return false;
59 if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/app/weldutils.cxx"))
60 return false;
61 if (loplugin::isSamePathname(fn, SRCDIR "/sfx2/source/appl/appopen.cxx"))
62 return false;
63 if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/table/tablertfimporter.cxx"))
64 return false;
65 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/docshell/externalrefmgr.cxx"))
66 return false;
67 if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/attr/swatrset.cxx"))
68 return false;
69 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/condformat/condformatdlg.cxx"))
70 return false;
71 if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/layout/frmtool.cxx"))
72 return false;
73 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xihelper.cxx"))
74 return false;
75 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xeformula.cxx"))
76 return false;
77 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xichart.cxx"))
78 return false;
79 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/html/htmlpars.cxx"))
80 return false;
81 if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/view/cellsh1.cxx"))
82 return false;
83 if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/html/htmltab.cxx"))
84 return false;
85 if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/ww8/docxattributeoutput.cxx"))
86 return false;
87 return true;
88 }
89
run()90 virtual void run() override
91 {
92 if (preRun())
93 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
94 }
95
shouldVisitTemplateInstantiations() const96 bool shouldVisitTemplateInstantiations() const { return true; }
97
98 bool VisitCXXConstructExpr(CXXConstructExpr const*);
99 bool VisitCXXMemberCallExpr(CXXMemberCallExpr const*);
100 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const*);
101 bool VisitVarDecl(VarDecl const*);
102 };
103
VisitCXXConstructExpr(CXXConstructExpr const * constructExpr)104 bool MakeShared::VisitCXXConstructExpr(CXXConstructExpr const* constructExpr)
105 {
106 if (ignoreLocation(constructExpr))
107 return true;
108 if (!loplugin::TypeCheck(constructExpr->getType()).ClassOrStruct("shared_ptr").StdNamespace())
109 return true;
110 if (!(constructExpr->getNumArgs() == 1
111 || (constructExpr->getNumArgs() > 1 && isa<CXXDefaultArgExpr>(constructExpr->getArg(1)))))
112 return true;
113 auto arg0 = constructExpr->getArg(0)->IgnoreParenImpCasts();
114 auto cxxNewExpr = dyn_cast<CXXNewExpr>(arg0);
115 if (cxxNewExpr)
116 {
117 auto construct2 = cxxNewExpr->getConstructExpr();
118 if (construct2)
119 {
120 if (construct2->getConstructor()->getAccess() != AS_public)
121 return true;
122 if (construct2->getNumArgs() == 1
123 && isa<CXXStdInitializerListExpr>(construct2->getArg(0)))
124 return true;
125 }
126 }
127 else if (loplugin::TypeCheck(arg0->getType()).ClassOrStruct("shared_ptr").StdNamespace())
128 return true;
129 else if (loplugin::TypeCheck(arg0->getType()).ClassOrStruct("weak_ptr").StdNamespace())
130 return true;
131 else if (arg0->getType()->isDependentType())
132 return true;
133 else if (isa<CXXNullPtrLiteralExpr>(arg0))
134 return true;
135 else if (auto const call = dyn_cast<CallExpr>(arg0))
136 {
137 if (auto const decl = call->getDirectCallee())
138 {
139 // Don't warn about cases where e.g. the Bitmap* result of calling Windows'
140 // Bitmap::FromBITMAPINFO is wrapped in a shared_ptr:
141 if (decl->getReturnType()->isPointerType()
142 && compiler.getSourceManager().isInSystemHeader(decl->getLocation()))
143 {
144 return true;
145 }
146 }
147 }
148
149 StringRef fn = getFilenameOfLocation(
150 compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(constructExpr)));
151 if (loplugin::isSamePathname(fn, SRCDIR "/include/o3tl/make_shared.hxx"))
152 return true;
153 if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/items/stylepool.cxx"))
154 return true;
155
156 report(DiagnosticsEngine::Warning, "rather use make_shared than constructing from %0",
157 compat::getBeginLoc(constructExpr))
158 << arg0->getType() << constructExpr->getSourceRange();
159 return true;
160 }
161
VisitCXXMemberCallExpr(CXXMemberCallExpr const * cxxMemberCallExpr)162 bool MakeShared::VisitCXXMemberCallExpr(CXXMemberCallExpr const* cxxMemberCallExpr)
163 {
164 if (ignoreLocation(cxxMemberCallExpr))
165 return true;
166
167 if (cxxMemberCallExpr->getNumArgs() != 1)
168 return true;
169
170 // cannot find a way to use the loplugin::DeclCheck stuff here
171 auto templateDecl
172 = dyn_cast<ClassTemplateSpecializationDecl>(cxxMemberCallExpr->getRecordDecl());
173 if (!templateDecl)
174 return true;
175 auto cxxRecordDecl = templateDecl->getSpecializedTemplate()->getTemplatedDecl();
176 if (!cxxRecordDecl->getName().contains("shared_ptr"))
177 return true;
178
179 if (auto const id = cxxMemberCallExpr->getMethodDecl()->getIdentifier())
180 {
181 if (id->getName() != "reset")
182 return true;
183 }
184 auto cxxNewExpr = dyn_cast<CXXNewExpr>(cxxMemberCallExpr->getArg(0)->IgnoreParenImpCasts());
185 if (!cxxNewExpr)
186 return true;
187 if (cxxNewExpr->getConstructExpr()
188 && cxxNewExpr->getConstructExpr()->getConstructor()->getAccess() != AS_public)
189 return true;
190
191 StringRef fn = getFilenameOfLocation(
192 compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(cxxMemberCallExpr)));
193 if (loplugin::isSamePathname(fn, SRCDIR "/include/o3tl/make_shared.hxx"))
194 return true;
195
196 report(DiagnosticsEngine::Warning, "rather use make_shared", compat::getBeginLoc(cxxNewExpr))
197 << cxxNewExpr->getSourceRange();
198
199 return true;
200 }
201
VisitCXXOperatorCallExpr(CXXOperatorCallExpr const * operCallExpr)202 bool MakeShared::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* operCallExpr)
203 {
204 if (ignoreLocation(operCallExpr))
205 return true;
206 if (!operCallExpr->isAssignmentOp())
207 return true;
208
209 if (!loplugin::TypeCheck(operCallExpr->getType()).ClassOrStruct("shared_ptr").StdNamespace())
210 return true;
211
212 if (loplugin::TypeCheck(operCallExpr->getArg(1)->getType())
213 .ClassOrStruct("shared_ptr")
214 .StdNamespace())
215 return true;
216
217 report(DiagnosticsEngine::Warning, "rather use make_shared than constructing from %0",
218 compat::getBeginLoc(operCallExpr))
219 << operCallExpr->getArg(1)->getType() << operCallExpr->getSourceRange();
220
221 return true;
222 }
223
VisitVarDecl(VarDecl const * varDecl)224 bool MakeShared::VisitVarDecl(VarDecl const* varDecl)
225 {
226 if (ignoreLocation(varDecl))
227 return true;
228 if (!varDecl->hasInit())
229 return true;
230
231 if (!loplugin::TypeCheck(varDecl->getType()).ClassOrStruct("shared_ptr").StdNamespace())
232 return true;
233
234 if (varDecl->getInit()->getType().isNull())
235 return true;
236 if (varDecl->getInit()->getType()->isDependentType())
237 return true;
238 if (loplugin::TypeCheck(varDecl->getInit()->IgnoreParenImpCasts()->getType())
239 .ClassOrStruct("shared_ptr")
240 .StdNamespace())
241 return true;
242
243 report(DiagnosticsEngine::Warning, "rather use make_shared than constructing from %0",
244 compat::getBeginLoc(varDecl))
245 << varDecl->getInit()->getType() << varDecl->getSourceRange();
246
247 return true;
248 }
249
250 loplugin::Plugin::Registration<MakeShared> makeshared("makeshared");
251
252 } // namespace
253
254 #endif // LO_CLANG_SHARED_PLUGINS
255
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
257