Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / badstatics.cxx
blobd91f3b430a3084a569c22370f687faf71ec18678
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 */
10 #include <cassert>
12 #include "check.hxx"
13 #include "plugin.hxx"
15 namespace {
17 class BadStatics
18 : public clang::RecursiveASTVisitor<BadStatics>
19 , public loplugin::Plugin
22 public:
23 explicit BadStatics(loplugin::InstantiationData const& rData):
24 Plugin(rData) {}
26 void run() override {
27 if (compiler.getLangOpts().CPlusPlus) { // no non-trivial dtors in C
28 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
32 static std::pair<bool, std::vector<FieldDecl const*>> isBadStaticType(
33 QualType const& rpType, std::vector<FieldDecl const*> & chain,
34 std::vector<QualType> const& rParents)
36 QualType pt;
37 if (rpType->isAnyPointerType()) {
38 pt = rpType->getPointeeType();
39 } else if (auto at = rpType->getAsArrayTypeUnsafe()) {
40 pt = at->getElementType();
41 } else if (auto rt = rpType->getAs<ReferenceType>()) {
42 pt = rt->getPointeeType();
44 if (!pt.isNull()) {
45 QualType const pPointee(pt.getUnqualifiedType().getCanonicalType());
46 auto const iter(std::find(rParents.begin(), rParents.end(), pPointee));
47 if (iter == rParents.end())
49 std::vector<QualType> copy(rParents);
50 copy.push_back(rpType.getUnqualifiedType().getCanonicalType());
51 return isBadStaticType(pt, chain, copy);
52 } else {
53 return std::make_pair(false, std::vector<FieldDecl const*>());
56 RecordType const*const pRecordType(rpType->getAs<RecordType>());
57 if (!pRecordType) {
58 return std::make_pair(false, std::vector<FieldDecl const*>());
60 auto const type = loplugin::TypeCheck(rpType);
61 if ( type.Class("Image").GlobalNamespace()
62 || type.Class("Bitmap").GlobalNamespace()
63 || type.Class("BitmapEx").GlobalNamespace()
64 || type.Class("VclPtr").GlobalNamespace()
67 return std::make_pair(true, chain);
69 if (type.Class("array").StdNamespace()
70 || type.Class("deque").StdNamespace()
71 || type.Class("forward_list").StdNamespace()
72 || type.Class("initializer_list").StdNamespace()
73 || type.Class("list").StdNamespace()
74 || type.Class("multiset").StdNamespace()
75 || type.Class("set").StdNamespace()
76 || type.Class("unordered_multiset").StdNamespace()
77 || type.Class("unordered_set").StdNamespace()
78 || type.Class("vector").StdNamespace())
80 std::vector<QualType> copy(rParents);
81 copy.push_back(rpType.getUnqualifiedType().getCanonicalType());
82 auto ctsd = dyn_cast<ClassTemplateSpecializationDecl>(
83 pRecordType->getDecl());
84 assert(ctsd != nullptr);
85 auto const & args = ctsd->getTemplateArgs();
86 assert(args.size() >= 1);
87 return isBadStaticType(args.get(0).getAsType(), chain, copy);
89 if (type.Class("map").StdNamespace()
90 || type.Class("multimap").StdNamespace()
91 || type.Class("unordered_map").StdNamespace()
92 || type.Class("unordered_multimap").StdNamespace())
94 std::vector<QualType> copy(rParents);
95 copy.push_back(rpType.getUnqualifiedType().getCanonicalType());
96 auto ctsd = dyn_cast<ClassTemplateSpecializationDecl>(
97 pRecordType->getDecl());
98 assert(ctsd != nullptr);
99 auto const & args = ctsd->getTemplateArgs();
100 assert(args.size() >= 2);
101 auto ret = isBadStaticType(args.get(0).getAsType(), chain, copy);
102 if (ret.first) {
103 return ret;
105 return isBadStaticType(args.get(1).getAsType(), chain, copy);
107 RecordDecl const*const pDefinition(pRecordType->getDecl()->getDefinition());
108 if (!pDefinition) { // maybe no definition if it's a pointer/reference
109 return std::make_pair(false, std::vector<FieldDecl const*>());
111 if ( type.Class("DeleteOnDeinit").Namespace("vcl").GlobalNamespace()
112 || type.Class("weak_ptr").StdNamespace() // not owning
113 || type.Class("ImplWallpaper").GlobalNamespace() // very odd static instance here
114 || type.Class("Application").GlobalNamespace() // numerous odd subclasses in vclmain::createApplication()
115 || type.Class("DemoMtfApp").GlobalNamespace() // one of these Application with own VclPtr
118 return std::make_pair(false, std::vector<FieldDecl const*>());
120 std::vector<QualType> copy(rParents);
121 copy.push_back(rpType.getUnqualifiedType().getCanonicalType());
122 CXXRecordDecl const*const pDecl(dyn_cast<CXXRecordDecl>(pDefinition));
123 assert(pDecl);
124 for (auto it = pDecl->field_begin(); it != pDecl->field_end(); ++it) {
125 chain.push_back(*it);
126 auto const ret(isBadStaticType((*it)->getType(), chain, copy));
127 chain.pop_back();
128 if (ret.first) {
129 return ret;
132 for (auto it = pDecl->bases_begin(); it != pDecl->bases_end(); ++it) {
133 auto const ret(isBadStaticType((*it).getType(), chain, copy));
134 if (ret.first) {
135 return ret;
138 for (auto it = pDecl->vbases_begin(); it != pDecl->vbases_end(); ++it) {
139 auto const ret(isBadStaticType((*it).getType(), chain, copy));
140 if (ret.first) {
141 return ret;
144 return std::make_pair(false, std::vector<FieldDecl const*>());
147 bool VisitVarDecl(VarDecl const*const pVarDecl)
149 if (ignoreLocation(pVarDecl)) {
150 return true;
153 if (pVarDecl->hasGlobalStorage()
154 && pVarDecl->isThisDeclarationADefinition())
156 auto const name(pVarDecl->getName());
157 if ( name == "g_pI18NStatusInstance" // I18NStatus::free()
158 || name == "s_pPreviousView" // not a owning pointer
159 || name == "s_pDefCollapsed" // SvImpLBox::~SvImpLBox()
160 || name == "s_pDefExpanded" // SvImpLBox::~SvImpLBox()
161 || name == "g_pDDSource" // SvTreeListBox::dispose()
162 || name == "g_pDDTarget" // SvTreeListBox::dispose()
163 || name == "g_pSfxApplication" // SfxApplication::~SfxApplication()
164 || name == "s_SidebarResourceManagerInstance" // ResourceManager::disposeDecks()
165 || name == "s_pGallery" // this is not entirely clear but apparently the GalleryThemeCacheEntry are deleted by GalleryBrowser2::SelectTheme() or GalleryBrowser2::dispose()
166 || name == "s_ExtMgr" // TheExtensionManager::disposing()
167 || name == "s_pDocLockedInsertingLinks" // not owning
168 || name == "s_pVout" // FrameFinit()
169 || name == "s_pPaintQueue" // SwPaintQueue::Remove()
170 || name == "gProp" // only owned (VclPtr) member cleared again
171 || name == "g_OszCtrl" // SwCrsrOszControl::Exit()
172 || name == "g_pSpellIter" // SwEditShell::SpellEnd()
173 || name == "g_pConvIter" // SwEditShell::SpellEnd()
174 || name == "g_pHyphIter" // SwEditShell::HyphEnd()
175 || name == "pFieldEditEngine" // ScGlobal::Clear()
176 || name == "xDrawClipDocShellRef" // ScGlobal::Clear()
177 || name == "s_ImageTree"
178 // ImageTree::get(), ImageTree::shutDown()
179 || name == "s_pMouseFrame"
180 // vcl/osx/salframeview.mm, mouseEntered/Exited, not owning
181 || name == "pCurrentMenuBar"
182 // vcl/osx/salmenu.cxx, AquaSalMenu::set/unsetMainMenu, not
183 // owning
184 || name == "s_pCaptureFrame" // vcl/osx/salframe.cxx, not owning
185 || name == "pBlink"
186 // sw/source/core/text/blink.cxx, _TextFinit()
187 || name == "s_pIconCache"
188 // sd/source/ui/tools/IconCache.cxx, leaked
189 || name == "maInstanceMap"
190 // sd/source/ui/framework/tools/FrameworkHelper.cxx, would
191 // leak ViewShellBase* keys if that map is not empty at exit
192 || name == "theAddInAsyncTbl"
193 // sc/source/core/tool/adiasync.cxx, would leak
194 // ScAddInAsync* keys if that set is not empty at exit
195 || name == "g_aWindowList"
196 //vcl/unx/gtk/a11y/atkutil.cxx, asserted empty at exit
197 || name == "aLogger" // FormulaLogger& FormulaLogger::get() in sc/source/core/tool/formulalogger.cxx
198 || name == "m_aUncommitedRegistrations" // sw/source/uibase/dbui/dbmgr.cxx
199 || (loplugin::DeclCheck(pVarDecl).Var("aAllListeners")
200 .Class("ScAddInListener").GlobalNamespace()) // not owning
201 || name == "s_pLOKWindowsMap" // LOK only, guarded by assert, and LOK never tries to perform a VCL cleanup
202 || name == "gStaticManager" // vcl/source/graphic/Manager.cxx - stores non-owning pointers
203 ) // these variables appear unproblematic
205 return true;
207 // these two are fairly harmless because they're both empty objects
208 if ( name == "s_xEmptyController" // svx/source/fmcomp/gridcell.cxx
209 || name == "xCell" // svx/source/table/svdotable.cxx
212 return true;
214 // ignore pointers, nothing happens to them on shutdown
215 QualType const pCanonical(pVarDecl->getType().getUnqualifiedType().getCanonicalType());
216 if (pCanonical->isPointerType()) {
217 return true;
219 std::vector<FieldDecl const*> pad;
220 auto const ret(isBadStaticType(pVarDecl->getType(), pad,
221 std::vector<QualType>()));
222 if (ret.first) {
223 report(DiagnosticsEngine::Warning,
224 "bad static variable causes crash on shutdown",
225 pVarDecl->getLocation())
226 << pVarDecl->getSourceRange();
227 if (!isUnitTestMode())
229 for (auto i: ret.second) {
230 report(DiagnosticsEngine::Note,
231 "... due to this member of %0",
232 i->getLocation())
233 << i->getParent() << i->getSourceRange();
239 return true;
244 loplugin::Plugin::Registration<BadStatics> X("badstatics");
246 } // namespace
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */