2015-03-25 15:37:53 +02: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/.
|
|
|
|
*/
|
|
|
|
|
2016-06-28 16:25:55 +02:00
|
|
|
#include "clang/AST/Attr.h"
|
|
|
|
|
2016-06-28 18:54:31 +02:00
|
|
|
#include "check.hxx"
|
2016-06-28 17:48:22 +02:00
|
|
|
#include "plugin.hxx"
|
2015-03-25 15:37:53 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Look for member functions that can be static
|
|
|
|
*/
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class StaticMethods:
|
2018-08-13 17:24:26 +02:00
|
|
|
public loplugin::FilteringPlugin<StaticMethods>
|
2015-03-25 15:37:53 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool bVisitedThis;
|
|
|
|
public:
|
2018-08-13 17:24:26 +02:00
|
|
|
explicit StaticMethods(loplugin::InstantiationData const & data): FilteringPlugin(data), bVisitedThis(false) {}
|
2015-03-25 15:37:53 +02:00
|
|
|
|
|
|
|
void run() override
|
|
|
|
{ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
|
|
|
|
|
2019-07-15 18:18:57 +02:00
|
|
|
bool TraverseCXXMethodDecl(const CXXMethodDecl * decl);
|
2015-03-25 15:37:53 +02:00
|
|
|
|
|
|
|
bool VisitCXXThisExpr(const CXXThisExpr *) { bVisitedThis = true; return true; }
|
|
|
|
// these two indicate that we hit something that makes our analysis unreliable
|
|
|
|
bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *) { bVisitedThis = true; return true; }
|
|
|
|
bool VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *) { bVisitedThis = true; return true; }
|
|
|
|
private:
|
2017-06-23 15:18:21 +02:00
|
|
|
StringRef getFilename(SourceLocation loc);
|
2015-03-25 15:37:53 +02:00
|
|
|
};
|
|
|
|
|
2017-12-15 14:20:38 +01:00
|
|
|
bool BaseCheckNotTestFixtureSubclass(const CXXRecordDecl *BaseDefinition) {
|
2017-01-26 15:36:47 +02:00
|
|
|
if (loplugin::TypeCheck(BaseDefinition).Class("TestFixture").Namespace("CppUnit").GlobalNamespace()) {
|
2015-03-25 15:37:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDerivedFromTestFixture(const CXXRecordDecl *decl) {
|
|
|
|
if (!decl->hasDefinition())
|
|
|
|
return false;
|
|
|
|
if (// not sure what hasAnyDependentBases() does,
|
|
|
|
// but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
|
|
|
|
!decl->hasAnyDependentBases() &&
|
2020-03-01 20:09:44 +01:00
|
|
|
!decl->forallBases(BaseCheckNotTestFixtureSubclass)) {
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-23 15:18:21 +02:00
|
|
|
StringRef StaticMethods::getFilename(SourceLocation loc)
|
2015-03-25 15:37:53 +02:00
|
|
|
{
|
|
|
|
SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc);
|
2019-10-07 15:29:30 +02:00
|
|
|
return getFilenameOfLocation(spellingLocation);
|
2015-03-25 15:37:53 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 01:00:27 +01:00
|
|
|
bool startsWith(const std::string& rStr, const char* pSubStr) {
|
2015-04-01 16:23:59 +02:00
|
|
|
return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
|
|
|
|
}
|
2015-03-25 15:37:53 +02:00
|
|
|
|
2019-07-15 18:18:57 +02:00
|
|
|
bool StaticMethods::TraverseCXXMethodDecl(const CXXMethodDecl * pCXXMethodDecl) {
|
2015-03-25 15:37:53 +02:00
|
|
|
if (ignoreLocation(pCXXMethodDecl)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-28 18:14:13 +02:00
|
|
|
if (!pCXXMethodDecl->isInstance() || pCXXMethodDecl->isVirtual() || !pCXXMethodDecl->doesThisDeclarationHaveABody() || pCXXMethodDecl->isLateTemplateParsed()) {
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (pCXXMethodDecl->getOverloadedOperator() != OverloadedOperatorKind::OO_None || pCXXMethodDecl->hasAttr<OverrideAttr>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isa<CXXConstructorDecl>(pCXXMethodDecl) || isa<CXXDestructorDecl>(pCXXMethodDecl) || isa<CXXConversionDecl>(pCXXMethodDecl)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-18 08:15:21 +02:00
|
|
|
if (isInUnoIncludeFile(pCXXMethodDecl)) {
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-15 12:23:40 +02:00
|
|
|
if (pCXXMethodDecl->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// the CppUnit stuff uses macros and methods that can't be changed
|
|
|
|
if (isDerivedFromTestFixture(pCXXMethodDecl->getParent())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// don't mess with the backwards compatibility stuff
|
2018-08-10 12:35:21 +02:00
|
|
|
if (loplugin::isSamePathname(getFilename(compat::getBeginLoc(pCXXMethodDecl)), SRCDIR "/cppuhelper/source/compat.cxx")) {
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// the DDE has a dummy implementation on Linux and a real one on Windows
|
2018-08-10 12:35:21 +02:00
|
|
|
auto aFilename = getFilename(compat::getBeginLoc(pCXXMethodDecl->getCanonicalDecl()));
|
2017-05-18 09:56:01 +02:00
|
|
|
if (loplugin::isSamePathname(aFilename, SRCDIR "/include/svl/svdde.hxx")) {
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-29 07:31:26 +02:00
|
|
|
auto cdc = loplugin::DeclCheck(pCXXMethodDecl->getParent());
|
2015-03-25 15:37:53 +02:00
|
|
|
// special case having something to do with static initialisation
|
|
|
|
// sal/osl/all/utility.cxx
|
2016-06-29 07:31:26 +02:00
|
|
|
if (cdc.Class("OGlobalTimer").Namespace("osl").GlobalNamespace()) {
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-01 16:23:59 +02:00
|
|
|
// leave the TopLeft() method alone for consistency with the other "corner" methods
|
2016-06-29 07:31:26 +02:00
|
|
|
if (cdc.Class("BitmapInfoAccess").GlobalNamespace()) {
|
2015-04-01 16:23:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// the unotools and svl config code stuff is doing weird stuff with a reference-counted statically allocated pImpl class
|
2018-03-26 13:37:06 +02:00
|
|
|
if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/unotools/")) {
|
2015-04-01 16:23:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-26 13:37:06 +02:00
|
|
|
if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/svl/")) {
|
2015-04-10 11:07:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-26 13:37:06 +02:00
|
|
|
if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/framework/") || loplugin::hasPathnamePrefix(aFilename, SRCDIR "/framework/")) {
|
2015-04-10 11:07:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// there is some odd stuff happening here I don't fully understand, leave it for now
|
2018-03-26 13:37:06 +02:00
|
|
|
if (loplugin::hasPathnamePrefix(aFilename, SRCDIR "/include/canvas/") || loplugin::hasPathnamePrefix(aFilename, SRCDIR "/canvas/")) {
|
2015-04-01 16:23:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-01-04 12:44:22 +01:00
|
|
|
// classes that have static data and some kind of weird reference-counting trick in its constructor
|
2016-06-29 07:31:26 +02:00
|
|
|
if (cdc.Class("LinguOptions").GlobalNamespace()
|
|
|
|
|| (cdc.Class("EditableExtendedColorConfig").Namespace("svtools")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| (cdc.Class("ExtendedColorConfig").Namespace("svtools")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| cdc.Class("SvtMiscOptions").GlobalNamespace()
|
|
|
|
|| cdc.Class("SvtAccessibilityOptions").GlobalNamespace()
|
|
|
|
|| cdc.Class("ColorConfig").Namespace("svtools").GlobalNamespace()
|
|
|
|
|| cdc.Class("SvtOptionsDrawinglayer").GlobalNamespace()
|
|
|
|
|| cdc.Class("SvtMenuOptions").GlobalNamespace()
|
|
|
|
|| cdc.Class("SvtToolPanelOptions").GlobalNamespace()
|
|
|
|
|| cdc.Class("SvtSlideSorterBarOptions").GlobalNamespace()
|
|
|
|
|| (cdc.Class("SharedResources").Namespace("connectivity")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| (cdc.Class("OParseContextClient").Namespace("svxform")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| cdc.Class("OLimitedFormats").Namespace("frm").GlobalNamespace())
|
2015-04-07 16:18:28 +02:00
|
|
|
{
|
2015-04-07 13:16:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-29 07:31:26 +02:00
|
|
|
auto fdc = loplugin::DeclCheck(pCXXMethodDecl);
|
2015-04-09 08:45:51 +02:00
|
|
|
// only empty on Linux, not on windows
|
2016-06-29 07:31:26 +02:00
|
|
|
if ((fdc.Function("GetVisualRepresentationInNativeFormat_Impl")
|
|
|
|
.Class("OleEmbeddedObject").GlobalNamespace())
|
|
|
|
|| (fdc.Function("GetRidOfComponent").Class("OleEmbeddedObject")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| cdc.Class("SbxDecimal").GlobalNamespace()
|
|
|
|
|| fdc.Function("Call").Class("SbiDllMgr").GlobalNamespace()
|
|
|
|
|| fdc.Function("FreeDll").Class("SbiDllMgr").GlobalNamespace()
|
|
|
|
|| (fdc.Function("InitializeDde").Class("SfxApplication")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| (fdc.Function("RemoveDdeTopic").Class("SfxApplication")
|
|
|
|
.GlobalNamespace())
|
2020-05-22 20:45:46 +02:00
|
|
|
|| (fdc.Function("UpdateSkiaStatus").Class("OfaViewTabPage")
|
|
|
|
.GlobalNamespace())
|
2016-06-29 07:31:26 +02:00
|
|
|
|| (fdc.Function("ReleaseData").Class("ScannerManager")
|
|
|
|
.GlobalNamespace()))
|
|
|
|
{
|
2015-04-10 11:07:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-22 15:17:23 +02:00
|
|
|
// debugging stuff
|
2016-06-29 07:31:26 +02:00
|
|
|
if (fdc.Function("dump").Class("InternalData").Namespace("chart")
|
|
|
|
.GlobalNamespace())
|
|
|
|
{
|
2015-04-22 15:17:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-10 11:07:36 +02:00
|
|
|
// used in a function-pointer-table
|
2016-06-29 07:31:26 +02:00
|
|
|
if ((cdc.Class("SbiRuntime").GlobalNamespace()
|
|
|
|
&& startsWith(pCXXMethodDecl->getNameAsString(), "Step"))
|
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
|
|
|
|| (cdc.Class("OoxFormulaParserImpl").AnonymousNamespace().Namespace("xls").Namespace("oox")
|
2016-06-29 07:31:26 +02:00
|
|
|
.GlobalNamespace())
|
|
|
|
|| cdc.Class("SwTableFormula").GlobalNamespace()
|
|
|
|
|| (cdc.Class("BiffFormulaParserImpl").Namespace("xls").Namespace("oox")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| (fdc.Function("Read_F_Shape").Class("SwWW8ImplReader")
|
|
|
|
.GlobalNamespace())
|
|
|
|
|| (fdc.Function("Read_Majority").Class("SwWW8ImplReader")
|
2016-10-07 11:58:44 +02:00
|
|
|
.GlobalNamespace())
|
|
|
|
|| fdc.Function("Ignore").Class("SwWrtShell").GlobalNamespace())
|
2016-06-29 07:31:26 +02:00
|
|
|
{
|
2015-04-09 08:45:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-30 10:47:56 +02:00
|
|
|
// have no idea why this can't be static, but 'make check' fails with it so...
|
2016-06-29 07:31:26 +02:00
|
|
|
if (fdc.Function("resolveRelationshipsOfTypeFromOfficeDoc").Class("Shape")
|
|
|
|
.Namespace("drawingml").Namespace("oox").GlobalNamespace())
|
|
|
|
{
|
2015-04-30 10:47:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-05-04 10:29:18 +02:00
|
|
|
// template magic
|
2016-06-29 07:31:26 +02:00
|
|
|
if (fdc.Function("getValue").Class("ColumnBatch").GlobalNamespace()
|
|
|
|
|| cdc.Class("TitleImpl").GlobalNamespace()
|
|
|
|
|| (fdc.Function("getDefaultPropertyName").Class("DefaultReturnHelper")
|
|
|
|
.Namespace("vba").Namespace("ooo").GlobalNamespace()))
|
|
|
|
{
|
2015-05-04 10:29:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-05-05 10:33:52 +02:00
|
|
|
// depends on config options
|
2016-06-29 07:31:26 +02:00
|
|
|
if ((fdc.Function("autoInstallFontLangSupport").Class("PrintFontManager")
|
|
|
|
.Namespace("psp").GlobalNamespace())
|
|
|
|
|| fdc.Function("AllocateFrame").Class("GtkSalFrame").GlobalNamespace()
|
2016-06-29 08:54:33 +02:00
|
|
|
|| (fdc.Function("TriggerPaintEvent").Class("GtkSalFrame")
|
2016-06-29 07:31:26 +02:00
|
|
|
.GlobalNamespace()))
|
|
|
|
{
|
2015-05-05 10:33:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-25 15:37:53 +02:00
|
|
|
|
|
|
|
bVisitedThis = false;
|
|
|
|
TraverseStmt(pCXXMethodDecl->getBody());
|
|
|
|
if (bVisitedThis) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-05 09:58:34 +02:00
|
|
|
if (containsPreprocessingConditionalInclusion((pCXXMethodDecl->getSourceRange()))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:37:53 +02:00
|
|
|
report(
|
|
|
|
DiagnosticsEngine::Warning,
|
2016-06-29 07:31:26 +02:00
|
|
|
"this member function can be declared static",
|
2015-05-08 18:55:30 +02:00
|
|
|
pCXXMethodDecl->getCanonicalDecl()->getLocation())
|
2015-03-25 15:37:53 +02:00
|
|
|
<< pCXXMethodDecl->getCanonicalDecl()->getSourceRange();
|
2015-05-08 18:55:30 +02:00
|
|
|
FunctionDecl const * def;
|
|
|
|
if (pCXXMethodDecl->isDefined(def)
|
|
|
|
&& def != pCXXMethodDecl->getCanonicalDecl())
|
|
|
|
{
|
|
|
|
report(DiagnosticsEngine::Note, "defined here:", def->getLocation())
|
|
|
|
<< def->getSourceRange();
|
|
|
|
}
|
2015-03-25 15:37:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:18:57 +02:00
|
|
|
loplugin::Plugin::Registration<StaticMethods> X("staticmethods");
|
2019-07-15 12:14:34 +02:00
|
|
|
|
2019-07-15 18:18:57 +02:00
|
|
|
}
|
2015-03-25 15:37:53 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|