[en_US] Added 13 autocorrect words
[LibreOffice.git] / compilerplugins / clang / sfxpoolitem.cxx
blobce52336c8e046883fa72ee483dd0bd299a214522
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 <string>
12 #include <iostream>
14 #include "plugin.hxx"
15 #include "check.hxx"
16 #include "clang/AST/CXXInheritance.h"
19 what might be more interesting is a warning about subclasses that add
20 members but do not override virtual operator==() - that is easily
21 forgotten and hard to notice.
23 namespace {
25 class SfxPoolItem:
26 public loplugin::FilteringPlugin<SfxPoolItem>
28 public:
29 explicit SfxPoolItem(loplugin::InstantiationData const & data): FilteringPlugin(data)
32 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
34 bool VisitCXXRecordDecl( const CXXRecordDecl* );
37 bool BaseCheckNotSfxPoolItemSubclass(const CXXRecordDecl *BaseDefinition) {
38 if (BaseDefinition && loplugin::TypeCheck(BaseDefinition).Class("SfxPoolItem").GlobalNamespace()) {
39 return false;
41 return true;
44 bool isDerivedFromSfxPoolItem(const CXXRecordDecl *decl) {
45 if (!decl)
46 return false;
47 if (loplugin::TypeCheck(decl).Class("SfxPoolItem").GlobalNamespace())
48 return true;
49 if (!decl->hasDefinition()) {
50 return false;
52 if (// not sure what hasAnyDependentBases() does,
53 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
54 !decl->hasAnyDependentBases() &&
55 !decl->forallBases(BaseCheckNotSfxPoolItemSubclass, true)) {
56 return true;
58 return false;
62 bool BaseCheckNotSwMsgPoolItemSubclass(const CXXRecordDecl *BaseDefinition) {
63 if (BaseDefinition && loplugin::TypeCheck(BaseDefinition).Class("SwMsgPoolItem")) {
64 return false;
66 return true;
69 bool isDerivedFromSwMsgPoolItem(const CXXRecordDecl *decl) {
70 if (!decl)
71 return false;
72 if (loplugin::TypeCheck(decl).Class("SwMsgPoolItem").GlobalNamespace())
73 return true;
74 if (!decl->hasDefinition()) {
75 return false;
77 if (// not sure what hasAnyDependentBases() does,
78 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
79 !decl->hasAnyDependentBases() &&
80 !decl->forallBases(BaseCheckNotSwMsgPoolItemSubclass, true)) {
81 return true;
83 return false;
86 bool endsWith(const std::string& a, const std::string& b) {
87 if (b.size() > a.size()) return false;
88 return std::equal(a.begin() + a.size() - b.size(), a.end(), b.begin());
91 bool SfxPoolItem::VisitCXXRecordDecl(const CXXRecordDecl* decl)
93 if (ignoreLocation(decl)) {
94 return true;
96 if (!decl->hasDefinition()) {
97 return true;
99 // check if this class is derived from Window
100 if (!isDerivedFromSfxPoolItem(decl)) {
101 return true;
103 // the SwMsgPoolItem are some sort of hack to transport down-castable objects to SwClient::Modify(), they're not "real" items
104 if (isDerivedFromSwMsgPoolItem(decl)) {
105 return true;
107 if (decl->field_begin() == decl->field_end()) {
108 return true;
110 // the enum types do some weird stuff involving SfxEnumItemInterface
111 auto tc = loplugin::TypeCheck(decl);
112 if (tc.Class("SfxEnumItem").GlobalNamespace() || tc.Class("SfxAllEnumItem").GlobalNamespace())
113 return true;
115 for (auto it = decl->method_begin(); it != decl->method_end(); ++it) {
116 if ( endsWith((*it)->getQualifiedNameAsString(), "::operator==") )
117 return true;
119 report(
120 DiagnosticsEngine::Warning,
121 "SfxPoolItem subclass %0 declares new fields, but does not override operator==",
122 compat::getBeginLoc(decl))
123 << decl->getQualifiedNameAsString() << decl->getSourceRange();
124 return true;
128 loplugin::Plugin::Registration< SfxPoolItem > sfxpoolitem("sfxpoolitem");
130 } // namespace
132 #endif // LO_CLANG_SHARED_PLUGINS
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */