2015-10-30 16:05:42 +01:00
/* -*- 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/.
*/
2019-03-07 14:31:17 +01:00
# ifndef LO_CLANG_SHARED_PLUGINS
2016-10-08 23:55:08 +02:00
# include <cassert>
2016-06-28 18:54:31 +02:00
# include "check.hxx"
2015-10-30 16:05:42 +01:00
# include "plugin.hxx"
namespace {
class BadStatics
2018-08-13 17:24:26 +02:00
: public loplugin : : FilteringPlugin < BadStatics >
2015-10-30 16:05:42 +01:00
{
public :
2017-11-07 11:50:47 +01:00
explicit BadStatics ( loplugin : : InstantiationData const & rData ) :
2018-08-13 17:24:26 +02:00
FilteringPlugin ( rData ) { }
2015-10-30 16:05:42 +01:00
2019-03-07 14:31:17 +01:00
bool preRun ( ) override {
return compiler . getLangOpts ( ) . CPlusPlus ; // no non-trivial dtors in C
}
2015-10-30 16:05:42 +01:00
void run ( ) override {
2019-03-07 14:31:17 +01:00
if ( preRun ( ) ) {
2015-10-30 16:05:42 +01:00
TraverseDecl ( compiler . getASTContext ( ) . getTranslationUnitDecl ( ) ) ;
}
}
2015-11-17 15:05:34 +01:00
static std : : pair < bool , std : : vector < FieldDecl const * > > isBadStaticType (
QualType const & rpType , std : : vector < FieldDecl const * > & chain ,
2015-11-12 22:33:36 +01:00
std : : vector < QualType > const & rParents )
2015-11-05 13:16:50 +01:00
{
2016-10-08 23:55:08 +02:00
QualType pt ;
if ( rpType - > isAnyPointerType ( ) ) {
pt = rpType - > getPointeeType ( ) ;
} else if ( auto at = rpType - > getAsArrayTypeUnsafe ( ) ) {
pt = at - > getElementType ( ) ;
} else if ( auto rt = rpType - > getAs < ReferenceType > ( ) ) {
pt = rt - > getPointeeType ( ) ;
}
if ( ! pt . isNull ( ) ) {
QualType const pPointee ( pt . getUnqualifiedType ( ) . getCanonicalType ( ) ) ;
2015-11-12 22:33:36 +01:00
auto const iter ( std : : find ( rParents . begin ( ) , rParents . end ( ) , pPointee ) ) ;
if ( iter = = rParents . end ( ) )
{
std : : vector < QualType > copy ( rParents ) ;
2016-10-08 23:55:08 +02:00
copy . push_back ( rpType . getUnqualifiedType ( ) . getCanonicalType ( ) ) ;
return isBadStaticType ( pt , chain , copy ) ;
2015-11-12 22:33:36 +01:00
} else {
2015-11-17 15:05:34 +01:00
return std : : make_pair ( false , std : : vector < FieldDecl const * > ( ) ) ;
2015-11-12 22:33:36 +01:00
}
}
2016-10-08 23:55:08 +02:00
RecordType const * const pRecordType ( rpType - > getAs < RecordType > ( ) ) ;
2015-11-05 13:16:50 +01:00
if ( ! pRecordType ) {
2015-11-17 15:05:34 +01:00
return std : : make_pair ( false , std : : vector < FieldDecl const * > ( ) ) ;
2015-11-05 13:16:50 +01:00
}
2015-12-08 22:56:02 +01:00
auto const type = loplugin : : TypeCheck ( rpType ) ;
if ( type . Class ( " Image " ) . GlobalNamespace ( )
| | type . Class ( " Bitmap " ) . GlobalNamespace ( )
| | type . Class ( " BitmapEx " ) . GlobalNamespace ( )
2016-04-14 15:07:57 +02:00
| | type . Class ( " VclPtr " ) . GlobalNamespace ( )
2015-11-05 13:16:50 +01:00
)
{
2015-11-17 15:05:34 +01:00
return std : : make_pair ( true , chain ) ;
2015-11-05 13:16:50 +01:00
}
2016-10-08 23:55:08 +02:00
if ( type . Class ( " array " ) . StdNamespace ( )
| | type . Class ( " deque " ) . StdNamespace ( )
| | type . Class ( " forward_list " ) . StdNamespace ( )
| | type . Class ( " initializer_list " ) . StdNamespace ( )
| | type . Class ( " list " ) . StdNamespace ( )
| | type . Class ( " multiset " ) . StdNamespace ( )
| | type . Class ( " set " ) . StdNamespace ( )
| | type . Class ( " unordered_multiset " ) . StdNamespace ( )
| | type . Class ( " unordered_set " ) . StdNamespace ( )
| | type . Class ( " vector " ) . StdNamespace ( ) )
{
std : : vector < QualType > copy ( rParents ) ;
copy . push_back ( rpType . getUnqualifiedType ( ) . getCanonicalType ( ) ) ;
auto ctsd = dyn_cast < ClassTemplateSpecializationDecl > (
pRecordType - > getDecl ( ) ) ;
assert ( ctsd ! = nullptr ) ;
auto const & args = ctsd - > getTemplateArgs ( ) ;
assert ( args . size ( ) > = 1 ) ;
return isBadStaticType ( args . get ( 0 ) . getAsType ( ) , chain , copy ) ;
}
if ( type . Class ( " map " ) . StdNamespace ( )
| | type . Class ( " multimap " ) . StdNamespace ( )
| | type . Class ( " unordered_map " ) . StdNamespace ( )
| | type . Class ( " unordered_multimap " ) . StdNamespace ( ) )
{
std : : vector < QualType > copy ( rParents ) ;
copy . push_back ( rpType . getUnqualifiedType ( ) . getCanonicalType ( ) ) ;
auto ctsd = dyn_cast < ClassTemplateSpecializationDecl > (
pRecordType - > getDecl ( ) ) ;
assert ( ctsd ! = nullptr ) ;
auto const & args = ctsd - > getTemplateArgs ( ) ;
assert ( args . size ( ) > = 2 ) ;
auto ret = isBadStaticType ( args . get ( 0 ) . getAsType ( ) , chain , copy ) ;
if ( ret . first ) {
return ret ;
}
return isBadStaticType ( args . get ( 1 ) . getAsType ( ) , chain , copy ) ;
}
2015-11-05 13:16:50 +01:00
RecordDecl const * const pDefinition ( pRecordType - > getDecl ( ) - > getDefinition ( ) ) ;
2015-11-12 22:33:36 +01:00
if ( ! pDefinition ) { // maybe no definition if it's a pointer/reference
2015-11-17 15:05:34 +01:00
return std : : make_pair ( false , std : : vector < FieldDecl const * > ( ) ) ;
2015-11-12 22:33:36 +01:00
}
2015-12-08 22:56:02 +01:00
if ( type . Class ( " DeleteOnDeinit " ) . Namespace ( " vcl " ) . GlobalNamespace ( )
| | type . Class ( " weak_ptr " ) . StdNamespace ( ) // not owning
| | type . Class ( " ImplWallpaper " ) . GlobalNamespace ( ) // very odd static instance here
| | type . Class ( " Application " ) . GlobalNamespace ( ) // numerous odd subclasses in vclmain::createApplication()
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
| | type . Class ( " DemoMtfApp " ) . AnonymousNamespace ( ) . GlobalNamespace ( ) // one of these Application with own VclPtr
2015-11-12 22:33:36 +01:00
)
{
2015-11-17 15:05:34 +01:00
return std : : make_pair ( false , std : : vector < FieldDecl const * > ( ) ) ;
2015-11-12 22:33:36 +01:00
}
std : : vector < QualType > copy ( rParents ) ;
2016-10-08 23:55:08 +02:00
copy . push_back ( rpType . getUnqualifiedType ( ) . getCanonicalType ( ) ) ;
2015-11-05 13:16:50 +01:00
CXXRecordDecl const * const pDecl ( dyn_cast < CXXRecordDecl > ( pDefinition ) ) ;
assert ( pDecl ) ;
for ( auto it = pDecl - > field_begin ( ) ; it ! = pDecl - > field_end ( ) ; + + it ) {
2015-11-17 15:05:34 +01:00
chain . push_back ( * it ) ;
auto const ret ( isBadStaticType ( ( * it ) - > getType ( ) , chain , copy ) ) ;
chain . pop_back ( ) ;
2015-11-05 13:16:50 +01:00
if ( ret . first ) {
return ret ;
}
}
for ( auto it = pDecl - > bases_begin ( ) ; it ! = pDecl - > bases_end ( ) ; + + it ) {
2015-11-17 15:05:34 +01:00
auto const ret ( isBadStaticType ( ( * it ) . getType ( ) , chain , copy ) ) ;
2015-11-05 13:16:50 +01:00
if ( ret . first ) {
return ret ;
}
}
for ( auto it = pDecl - > vbases_begin ( ) ; it ! = pDecl - > vbases_end ( ) ; + + it ) {
2015-11-17 15:05:34 +01:00
auto const ret ( isBadStaticType ( ( * it ) . getType ( ) , chain , copy ) ) ;
2015-11-05 13:16:50 +01:00
if ( ret . first ) {
return ret ;
}
}
2015-11-17 15:05:34 +01:00
return std : : make_pair ( false , std : : vector < FieldDecl const * > ( ) ) ;
2015-11-05 13:16:50 +01:00
}
2015-10-30 16:05:42 +01:00
bool VisitVarDecl ( VarDecl const * const pVarDecl )
{
if ( ignoreLocation ( pVarDecl ) ) {
return true ;
}
2015-11-05 13:16:50 +01:00
if ( pVarDecl - > hasGlobalStorage ( )
& & pVarDecl - > isThisDeclarationADefinition ( ) )
{
2015-11-12 22:33:36 +01:00
auto const name ( pVarDecl - > getName ( ) ) ;
2019-08-12 10:58:40 +02:00
if ( name = = " s_pPreviousView " // not an owning pointer
2015-11-12 22:33:36 +01:00
| | name = = " s_pDefCollapsed " // SvImpLBox::~SvImpLBox()
| | name = = " s_pDefExpanded " // SvImpLBox::~SvImpLBox()
| | name = = " g_pDDSource " // SvTreeListBox::dispose()
| | name = = " g_pDDTarget " // SvTreeListBox::dispose()
| | name = = " g_pSfxApplication " // SfxApplication::~SfxApplication()
| | name = = " s_SidebarResourceManagerInstance " // ResourceManager::disposeDecks()
| | name = = " s_pGallery " // this is not entirely clear but apparently the GalleryThemeCacheEntry are deleted by GalleryBrowser2::SelectTheme() or GalleryBrowser2::dispose()
| | name = = " s_ExtMgr " // TheExtensionManager::disposing()
| | name = = " s_pDocLockedInsertingLinks " // not owning
2016-04-22 10:08:07 +02:00
| | name = = " s_pVout " // FrameFinit()
2015-11-12 22:33:36 +01:00
| | name = = " s_pPaintQueue " // SwPaintQueue::Remove()
| | name = = " gProp " // only owned (VclPtr) member cleared again
| | name = = " g_OszCtrl " // SwCrsrOszControl::Exit()
| | name = = " g_pSpellIter " // SwEditShell::SpellEnd()
| | name = = " g_pConvIter " // SwEditShell::SpellEnd()
| | name = = " g_pHyphIter " // SwEditShell::HyphEnd()
2020-08-11 17:26:26 +02:00
| | name = = " xFieldEditEngine " // ScGlobal::Clear()
2015-11-12 22:33:36 +01:00
| | name = = " xDrawClipDocShellRef " // ScGlobal::Clear()
2016-12-18 14:31:14 +01:00
| | name = = " s_ImageTree "
// ImageTree::get(), ImageTree::shutDown()
2015-11-17 16:41:40 +01:00
| | name = = " s_pMouseFrame "
// vcl/osx/salframeview.mm, mouseEntered/Exited, not owning
| | name = = " pCurrentMenuBar "
// vcl/osx/salmenu.cxx, AquaSalMenu::set/unsetMainMenu, not
// owning
| | name = = " s_pCaptureFrame " // vcl/osx/salframe.cxx, not owning
2015-11-17 19:38:41 +01:00
| | name = = " pBlink "
// sw/source/core/text/blink.cxx, _TextFinit()
2015-11-17 23:43:26 +01:00
| | name = = " s_pIconCache "
2015-11-17 21:31:46 +01:00
// sd/source/ui/tools/IconCache.cxx, leaked
2016-06-29 21:51:28 +02:00
| | name = = " maInstanceMap "
// sd/source/ui/framework/tools/FrameworkHelper.cxx, would
// leak ViewShellBase* keys if that map is not empty at exit
| | name = = " theAddInAsyncTbl "
// sc/source/core/tool/adiasync.cxx, would leak
// ScAddInAsync* keys if that set is not empty at exit
2016-06-30 12:53:40 +02:00
| | name = = " g_aWindowList "
2020-04-07 12:21:47 +01:00
//vcl/unx/gtk3/a11y/gtk3atkutil.cxx, asserted empty at exit
| | name = = " gFontPreviewVirDevs "
//svtools/source/control/ctrlbox.cxx, empty at exit
2016-12-12 12:13:27 +02:00
| | name = = " aLogger " // FormulaLogger& FormulaLogger::get() in sc/source/core/tool/formulalogger.cxx
2018-12-26 13:59:42 +01:00
| | name = = " m_aUncommittedRegistrations " // sw/source/uibase/dbui/dbmgr.cxx
2016-10-08 23:49:29 +02:00
| | ( loplugin : : DeclCheck ( pVarDecl ) . Var ( " aAllListeners " )
. Class ( " ScAddInListener " ) . GlobalNamespace ( ) ) // not owning
2018-11-07 16:01:30 +01:00
| | ( loplugin : : DeclCheck ( pVarDecl ) . Var ( " maThreadSpecific " )
. Class ( " ScDocument " ) . GlobalNamespace ( ) ) // not owning
2019-08-01 15:15:28 +02:00
| | name = = " s_aLOKWindowsMap " // LOK only, guarded by assert, and LOK never tries to perform a VCL cleanup
2020-03-05 12:18:38 +01:00
| | name = = " s_aLOKWeldBuildersMap " // LOK only, similar case as above
2020-06-18 12:15:13 +02:00
| | name = = " m_pNotebookBarWeldedWrapper " // LOK only, warning about map's key, no VCL cleanup performed
2018-04-04 17:51:26 +09:00
| | name = = " gStaticManager " // vcl/source/graphic/Manager.cxx - stores non-owning pointers
2019-02-25 15:45:43 +01:00
| | name = = " aThreadedInterpreterPool " // ScInterpreterContext(Pool), not owning
| | name = = " aNonThreadedInterpreterPool " // ScInterpreterContext(Pool), not owning
2019-05-07 15:53:16 +02:00
| | name = = " lcl_parserContext " // getParserContext(), the chain from this to a VclPtr is not owning
| | name = = " aReaderWriter " // /home/noel/libo/sw/source/filter/basflt/fltini.cxx, non-owning
2019-10-01 10:35:29 +02:00
| | name = = " aTwain "
// Windows-only extensions/source/scanner/scanwin.cxx, problematic
// Twain::mpThread -> ShimListenerThread::mxTopWindow released via Twain::Reset
// clearing mpThread
2015-11-12 22:33:36 +01:00
) // these variables appear unproblematic
{
return true ;
}
2016-04-18 11:21:44 +02:00
// these two are fairly harmless because they're both empty objects
if ( name = = " s_xEmptyController " // svx/source/fmcomp/gridcell.cxx
2016-04-14 15:07:57 +02:00
| | name = = " xCell " // svx/source/table/svdotable.cxx
)
{
return true ;
}
// ignore pointers, nothing happens to them on shutdown
QualType const pCanonical ( pVarDecl - > getType ( ) . getUnqualifiedType ( ) . getCanonicalType ( ) ) ;
if ( pCanonical - > isPointerType ( ) ) {
return true ;
}
2015-11-17 15:05:34 +01:00
std : : vector < FieldDecl const * > pad ;
auto const ret ( isBadStaticType ( pVarDecl - > getType ( ) , pad ,
2015-11-12 22:33:36 +01:00
std : : vector < QualType > ( ) ) ) ;
2015-11-05 13:16:50 +01:00
if ( ret . first ) {
2015-10-30 16:05:42 +01:00
report ( DiagnosticsEngine : : Warning ,
" bad static variable causes crash on shutdown " ,
pVarDecl - > getLocation ( ) )
< < pVarDecl - > getSourceRange ( ) ;
2017-09-21 16:37:40 +02:00
if ( ! isUnitTestMode ( ) )
{
for ( auto i : ret . second ) {
report ( DiagnosticsEngine : : Note ,
" ... due to this member of %0 " ,
i - > getLocation ( ) )
< < i - > getParent ( ) < < i - > getSourceRange ( ) ;
}
2015-11-05 13:16:50 +01:00
}
2015-10-30 16:05:42 +01:00
}
}
return true ;
}
} ;
2019-03-07 14:31:17 +01:00
loplugin : : Plugin : : Registration < BadStatics > badstatics ( " badstatics " ) ;
2015-10-30 16:05:42 +01:00
} // namespace
2019-03-07 14:31:17 +01:00
# endif // LO_CLANG_SHARED_PLUGINS
2015-10-30 16:05:42 +01:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */