bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / store / deletedspecial.cxx
blob5d66de352184851bd16ec9ef66445538b34ed4cb
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>
11 #include <iterator>
12 #include <string>
14 #include "plugin.hxx"
16 // Second-guess that certain private special member function declarations for
17 // which no definition can be found are left undefined to prevent them from
18 // being implicitly declared. Such situations are better expressed by marking
19 // the function as = delete (it e.g. helps compilers determine
20 // whether class members are unused if all of a class's member definitions are
21 // seen in a compilation unit). (Default constructors for classes with multiple
22 // constructors are exempted as they would not be implicitly declared.
23 // Destructors are exempted because it is likely that a destructor is defined
24 // private on purpose.)
26 namespace {
28 CXXRecordDecl const * getClass(CXXMethodDecl const * decl) {
29 CXXRecordDecl const * cls = dyn_cast<CXXRecordDecl>(decl->getDeclContext());
30 assert(cls != nullptr);
31 return cls;
34 class DeletedSpecial:
35 public loplugin::FilteringPlugin<DeletedSpecial>
37 public:
38 explicit DeletedSpecial(InstantiationData const & data): FilteringPlugin(data) {}
40 virtual void run() override;
42 bool VisitCXXMethodDecl(CXXMethodDecl const * decl);
44 private:
45 bool whitelist(
46 CXXMethodDecl const * decl, std::string const & name,
47 std::string const & path);
50 void DeletedSpecial::run() {
51 if (compiler.getLangOpts().CPlusPlus
52 && compiler.getPreprocessor().getIdentifierInfo(
53 "LIBO_INTERNAL_ONLY")->hasMacroDefinition())
55 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
59 bool DeletedSpecial::VisitCXXMethodDecl(CXXMethodDecl const * decl) {
60 if (ignoreLocation(decl) || !decl->isFirstDecl() || decl->isDefined()
61 || decl->isDefaulted() || decl->getAccess() != AS_private)
63 return true;
65 std::string desc;
66 if (decl->isCopyAssignmentOperator()) {
67 if (whitelist(decl, "ImpGraphic", "vcl/inc/impgraph.hxx")
68 || whitelist(decl, "SwSubFont", "sw/source/core/inc/swfont.hxx"))
70 return true;
72 desc = "copy assignment operator";
73 } else if (decl->isMoveAssignmentOperator()) {
74 desc = "move assignment operator";
75 } else {
76 CXXConstructorDecl const * ctor = dyn_cast<CXXConstructorDecl>(decl);
77 CXXRecordDecl const * cls = getClass(decl);
78 if (ctor != nullptr && ctor->isCopyConstructor()) {
79 if (whitelist(decl, "ImpGraphic", "vcl/inc/impgraph.hxx")
80 || whitelist(decl, "SbMethod", "include/basic/sbmeth.hxx")
81 || whitelist(decl, "ScDBCollection::NamedDBs", "sc/inc/dbdata.hxx")
82 || whitelist(decl, "ScDrawPage", "sc/inc/drawpage.hxx")
83 || whitelist(decl, "SmEditSource", "starmath/source/accessibility.hxx")
84 || whitelist(decl, "SwChartDataSequence", "sw/inc/unochart.hxx")
85 || whitelist(decl, "SwDPage", "sw/inc/dpage.hxx")
86 || whitelist(decl, "SwRedlineExtraData_Format", "sw/inc/redline.hxx")
87 || whitelist(decl, "SwRedlineExtraData_FormattingChanges", "sw/inc/redline.hxx")
88 || whitelist(decl, "SwTextAPIEditSource", "sw/source/core/inc/textapi.hxx")
89 || whitelist(decl, "XclImpBiff5Decrypter", "sc/source/filter/inc/xistream.hxx")
90 || whitelist(decl, "XclImpBiff8Decrypter", "sc/source/filter/inc/xistream.hxx")
91 || whitelist(decl, "configmgr::LocalizedPropertyNode", "configmgr/source/localizedpropertynode.hxx")
92 || whitelist(decl, "configmgr::LocalizedValueNode", "configmgr/source/localizedvaluenode.hxx")
93 || whitelist(decl, "configmgr::PropertyNode", "configmgr/source/propertynode.hxx")
94 || whitelist(decl, "oox::xls::BiffDecoder_RCF", "sc/source/filter/inc/biffcodec.hxx")
95 || whitelist(decl, "oox::xls::BiffDecoder_XOR", "sc/source/filter/inc/biffcodec.hxx")
96 || whitelist(decl, "rptui::OReportPage", "reportdesign/inc/RptPage.hxx"))
98 return true;
100 desc = "copy constructor";
101 } else if (ctor != nullptr && ctor->isMoveConstructor()) {
102 desc = "move constructor";
103 } else if (ctor != nullptr && ctor->isDefaultConstructor()
104 && std::distance(cls->ctor_begin(), cls->ctor_end()) == 1)
106 if (whitelist(decl, "AquaA11yFocusListener", "vcl/osx/a11yfocuslistener.hxx")
107 || whitelist(decl, "DocTemplLocaleHelper", "sfx2/source/doc/doctemplateslocal.hxx")
108 || whitelist(decl, "ScViewDataTable", "sc/source/filter/excel/../../ui/inc/viewdata.hxx")
109 || whitelist(decl, "ScViewDataTable", "sc/source/ui/inc/viewdata.hxx")
110 || whitelist(decl, "SwLineInfo", "sw/source/core/text/inftxt.hxx")
111 || whitelist(decl, "XRenderPeer", "vcl/unx/generic/gdi/xrender_peer.hxx")
112 || whitelist(decl, "desktop::DispatchWatcher", "desktop/source/app/dispatchwatcher.hxx")
113 || whitelist(decl, "desktop::RequestHandler", "desktop/source/app/officeipcthread.hxx")
114 || whitelist(decl, "desktop::RequestHandler", "desktop/source/lib/../app/officeipcthread.hxx")
115 || whitelist(decl, "sd::DiscoveryService", "sd/source/ui/remotecontrol/DiscoveryService.hxx")
116 || whitelist(decl, "sd::IconCache", "sd/source/ui/inc/tools/IconCache.hxx")
117 || whitelist(decl, "sd::RemoteServer", "sd/source/ui/inc/RemoteServer.hxx")
118 || whitelist(decl, "sd::slidesorter::cache::PageCacheManager", "sd/source/ui/slidesorter/inc/cache/SlsPageCacheManager.hxx")
119 || whitelist(decl, "framework::CommandInfoProvider", "include/framework/commandinfoprovider.hxx")
120 || whitelist(decl, "vcl::SettingsConfigItem", "include/vcl/configsettings.hxx")
121 || whitelist(decl, "writerfilter::ooxml::OOXMLFactory", "writerfilter/source/ooxml/OOXMLFactory.hxx"))
123 return true;
125 desc = "default constructor";
126 } else {
127 return true;
130 report(
131 DiagnosticsEngine::Warning,
132 ("private %0 is not defined at least in this compilation unit, maybe it"
133 " should be marked as deleted?"),
134 decl->getLocation())
135 << desc << decl->getSourceRange();
136 return true;
139 bool DeletedSpecial::whitelist(
140 CXXMethodDecl const * decl, std::string const & name,
141 std::string const & path)
143 return getClass(decl)->getQualifiedNameAsString() == name
144 && (compiler.getSourceManager().getFilename(
145 compiler.getSourceManager().getSpellingLoc(decl->getLocation()))
146 == SRCDIR "/" + path);
149 loplugin::Plugin::Registration<DeletedSpecial> X("deletedspecial", true);
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */