update credits
[LibreOffice.git] / compilerplugins / clang / mergeclasses.cxx
blob5081517f402a7a3dc1218b29284cc9089bb0a7a0
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 <set>
12 #include <string>
13 #include <iostream>
14 #include "config_clang.h"
15 #include "compat.hxx"
16 #include "plugin.hxx"
17 #include <fstream>
19 /**
21 Idea from Norbert (shm_get) - look for classes that are
22 (a) not instantiated
23 (b) have zero or one subclasses
24 and warn about them - would allow us to remove a bunch of abstract classes
25 that can be merged into one class and simplified.
27 Dump a list of
28 - unique classes that exist (A)
29 - unique classes that are instantiated (B)
30 - unique class-subclass relationships (C)
31 Then
32 let D = A minus B
33 for each class in D, look in C and count the entries, then dump it if no-entries == 1
35 The process goes something like this:
36 $ make check
37 $ make FORCE_COMPILE=all COMPILER_PLUGIN_TOOL='mergeclasses' check
38 $ ./compilerplugins/clang/mergeclasses.py
40 FIXME exclude 'static-only' classes, which some people may use/have used instead of a namespace to tie together a bunch of functions
44 namespace {
46 // try to limit the voluminous output a little
47 static std::set<std::string> instantiatedSet;
48 static std::set<std::pair<std::string,std::string> > childToParentClassSet; // childClassName -> parentClassName
49 static std::map<std::string,std::string> definitionMap; // className -> filename
51 class MergeClasses:
52 public loplugin::FilteringPlugin<MergeClasses>
54 public:
55 explicit MergeClasses(loplugin::InstantiationData const & data):
56 FilteringPlugin(data) {}
58 virtual void run() override
60 handler.enableTreeWideAnalysisMode();
62 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
64 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
65 // writing to the same logfile
66 std::string output;
67 for (const std::string & s : instantiatedSet)
68 output += "instantiated:\t" + s + "\n";
69 for (const std::pair<std::string,std::string> & s : childToParentClassSet)
70 output += "has-subclass:\t" + s.first + "\t" + s.second + "\n";
71 for (const auto & s : definitionMap)
72 output += "definition:\t" + s.first + "\t" + s.second + "\n";
73 std::ofstream myfile;
74 myfile.open( WORKDIR "/loplugin.mergeclasses.log", std::ios::app | std::ios::out);
75 myfile << output;
76 myfile.close();
79 bool shouldVisitTemplateInstantiations () const { return true; }
81 bool VisitVarDecl(const VarDecl *);
82 bool VisitFieldDecl(const FieldDecl *);
83 bool VisitCXXConstructExpr( const CXXConstructExpr* var );
84 bool VisitCXXRecordDecl( const CXXRecordDecl* decl);
87 bool ignoreClass(StringRef s)
89 // ignore stuff in the standard library, and UNO stuff we can't touch.
90 if (compat::starts_with(s, "rtl::") || compat::starts_with(s, "sal::")
91 || compat::starts_with(s, "com::sun::") || compat::starts_with(s, "std::")
92 || compat::starts_with(s, "boost::")
93 || s == "OString" || s == "OUString" || s == "bad_alloc")
95 return true;
97 // ignore instantiations of pointers and arrays
98 if (compat::ends_with(s, "*") || compat::ends_with(s, "]")) {
99 return true;
101 return false;
104 // check for implicit construction
105 bool MergeClasses::VisitVarDecl( const VarDecl* pVarDecl )
107 if (ignoreLocation(pVarDecl)) {
108 return true;
110 if (pVarDecl->getType()->isReferenceType())
111 return true;
112 std::string s = pVarDecl->getType().getUnqualifiedType().getAsString();
113 if (!ignoreClass(s))
114 instantiatedSet.insert(s);
115 return true;
118 // check for implicit construction
119 bool MergeClasses::VisitFieldDecl( const FieldDecl* pFieldDecl )
121 if (ignoreLocation(pFieldDecl)) {
122 return true;
124 if (pFieldDecl->getType()->isReferenceType())
125 return true;
126 std::string s = pFieldDecl->getType().getUnqualifiedType().getAsString();
127 if (!ignoreClass(s))
128 instantiatedSet.insert(s);
129 return true;
132 bool MergeClasses::VisitCXXConstructExpr( const CXXConstructExpr* pCXXConstructExpr )
134 if (ignoreLocation(pCXXConstructExpr)) {
135 return true;
137 // ignore calls when a sub-class is constructing its superclass
138 if (pCXXConstructExpr->getConstructionKind() != compat::CXXConstructionKind::Complete) {
139 return true;
141 const CXXConstructorDecl* pCXXConstructorDecl = pCXXConstructExpr->getConstructor();
142 const CXXRecordDecl* pParentCXXRecordDecl = pCXXConstructorDecl->getParent();
143 std::string s = pParentCXXRecordDecl->getQualifiedNameAsString();
144 if (!ignoreClass(s))
145 instantiatedSet.insert(s);
146 return true;
149 bool MergeClasses::VisitCXXRecordDecl(const CXXRecordDecl* decl)
151 if (ignoreLocation(decl)) {
152 return true;
154 if (decl->isThisDeclarationADefinition())
156 SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(decl->getBeginLoc());
157 auto filename = getFilenameOfLocation(spellingLocation);
158 filename = filename.substr(strlen(SRCDIR));
159 std::string s = decl->getQualifiedNameAsString();
160 if (ignoreClass(s))
161 return true;
162 definitionMap.insert( std::pair<std::string,std::string>(s, filename.str()) );
163 for (auto it = decl->bases_begin(); it != decl->bases_end(); ++it)
165 const CXXBaseSpecifier spec = *it;
166 // need to look through typedefs, hence the getUnqualifiedDesugaredType
167 QualType baseType = spec.getType().getDesugaredType(compiler.getASTContext());
168 childToParentClassSet.insert( std::pair<std::string,std::string>(s, baseType.getAsString()) );
171 return true;
174 loplugin::Plugin::Registration< MergeClasses > X("mergeclasses", false);
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */