1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 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 10 #include "plugin.hxx" 11 12 namespace { 13 14 class DeadClass: 15 public loplugin::FilteringPlugin<DeadClass> 16 { 17 public: DeadClass(InstantiationData const & data)18 explicit DeadClass(InstantiationData const & data): FilteringPlugin(data) {} 19 20 void run() override; 21 22 bool VisitCXXRecordDecl(CXXRecordDecl const *); 23 }; 24 run()25void DeadClass::run() { 26 if (compiler.getLangOpts().CPlusPlus) { 27 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); 28 } 29 } 30 VisitCXXRecordDecl(CXXRecordDecl const * decl)31bool DeadClass::VisitCXXRecordDecl(CXXRecordDecl const * decl) { 32 if (ignoreLocation(decl) || !decl->isThisDeclarationADefinition()) 33 return true; 34 if (decl->needsImplicitDefaultConstructor()) 35 return true; 36 if (decl->getDescribedClassTemplate()) 37 return true; 38 if (isa<ClassTemplateSpecializationDecl>(decl)) 39 return true; 40 int otherCnt = 0; 41 int copyMoveCnt = 0; 42 for (auto i = decl->ctor_begin(); i != decl->ctor_end(); ++i) { 43 if (!i->isUserProvided()) 44 continue; 45 if (i->isCopyOrMoveConstructor()) 46 copyMoveCnt++; 47 else 48 otherCnt++; 49 } 50 if (otherCnt == 0 && copyMoveCnt > 0) 51 { 52 report( 53 DiagnosticsEngine::Warning, 54 "class has only copy/move constructors, must be dead", 55 decl->getLocStart()) 56 << decl->getSourceRange(); 57 for (auto i = decl->ctor_begin(); i != decl->ctor_end(); ++i) { 58 if (i->isDeleted()) 59 continue; 60 } 61 } 62 return true; 63 } 64 65 loplugin::Plugin::Registration<DeadClass> X("deadclass"); 66 67 } 68 69 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 70