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