bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / mergeclasses.cxx
blobbac4a36df1d81c8483c02eabc50fb488b42785b1
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 "plugin.hxx"
15 #include <fstream>
17 /**
19 Idea from Norbert (shm_get) - look for classes that are
20 (a) not instantiated
21 (b) have zero or one subclasses
22 and warn about them - would allow us to remove a bunch of abstract classes
23 that can be merged into one class and simplified.
25 Dump a list of
26 - unique classes that exist (A)
27 - unique classes that are instantiated (B)
28 - unique class-subclass relationships (C)
29 Then
30 let D = A minus B
31 for each class in D, look in C and count the entries, then dump it if no-entries == 1
33 The process goes something like this:
34 $ make check
35 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='mergeclasses' check
36 $ ./compilerplugins/clang/mergeclasses.py
38 FIXME exclude 'static-only' classes, which some people may use/have used instead of a namespace to tie together a bunch of functions
42 namespace {
44 // try to limit the voluminous output a little
45 static std::set<std::string> instantiatedSet;
46 static std::set<std::pair<std::string,std::string> > childToParentClassSet; // childClassName -> parentClassName
47 static std::map<std::string,std::string> definitionMap; // className -> filename
49 class MergeClasses:
50 public loplugin::FilteringPlugin<MergeClasses>
52 public:
53 explicit MergeClasses(loplugin::InstantiationData const & data):
54 FilteringPlugin(data) {}
56 virtual void run() override
58 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
60 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
61 // writing to the same logfile
62 std::string output;
63 for (const std::string & s : instantiatedSet)
64 output += "instantiated:\t" + s + "\n";
65 for (const std::pair<std::string,std::string> & s : childToParentClassSet)
66 output += "has-subclass:\t" + s.first + "\t" + s.second + "\n";
67 for (const std::pair<std::string,std::string> & s : definitionMap)
68 output += "definition:\t" + s.first + "\t" + s.second + "\n";
69 std::ofstream myfile;
70 myfile.open( WORKDIR "/loplugin.mergeclasses.log", std::ios::app | std::ios::out);
71 myfile << output;
72 myfile.close();
75 bool shouldVisitTemplateInstantiations () const { return true; }
77 bool VisitVarDecl(const VarDecl *);
78 bool VisitFieldDecl(const FieldDecl *);
79 bool VisitCXXConstructExpr( const CXXConstructExpr* var );
80 bool VisitCXXRecordDecl( const CXXRecordDecl* decl);
83 bool startsWith(const std::string& rStr, const char* pSubStr) {
84 return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
87 bool ignoreClass(StringRef s)
89 // ignore stuff in the standard library, and UNO stuff we can't touch.
90 if (startsWith(s, "rtl::") || startsWith(s, "sal::") || startsWith(s, "com::sun::")
91 || startsWith(s, "std::") || startsWith(s, "boost::")
92 || s == "OString" || s == "OUString" || s == "bad_alloc")
94 return true;
96 // ignore instantiations of pointers and arrays
97 if (s.endswith("*") || s.endswith("]")) {
98 return true;
100 return false;
103 // check for implicit construction
104 bool MergeClasses::VisitVarDecl( const VarDecl* pVarDecl )
106 if (ignoreLocation(pVarDecl)) {
107 return true;
109 std::string s = pVarDecl->getType().getAsString();
110 if (!ignoreClass(s))
111 instantiatedSet.insert(s);
112 return true;
115 // check for implicit construction
116 bool MergeClasses::VisitFieldDecl( const FieldDecl* pFieldDecl )
118 if (ignoreLocation(pFieldDecl)) {
119 return true;
121 std::string s = pFieldDecl->getType().getAsString();
122 if (!ignoreClass(s))
123 instantiatedSet.insert(s);
124 return true;
127 bool MergeClasses::VisitCXXConstructExpr( const CXXConstructExpr* pCXXConstructExpr )
129 if (ignoreLocation(pCXXConstructExpr)) {
130 return true;
132 // ignore calls when a sub-class is constructing its superclass
133 if (pCXXConstructExpr->getConstructionKind() != CXXConstructExpr::ConstructionKind::CK_Complete) {
134 return true;
136 const CXXConstructorDecl* pCXXConstructorDecl = pCXXConstructExpr->getConstructor();
137 const CXXRecordDecl* pParentCXXRecordDecl = pCXXConstructorDecl->getParent();
138 std::string s = pParentCXXRecordDecl->getQualifiedNameAsString();
139 if (!ignoreClass(s))
140 instantiatedSet.insert(s);
141 return true;
144 bool MergeClasses::VisitCXXRecordDecl(const CXXRecordDecl* decl)
146 if (ignoreLocation(decl)) {
147 return true;
149 if (decl->isThisDeclarationADefinition())
151 SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(decl));
152 std::string filename = compiler.getSourceManager().getFilename(spellingLocation);
153 filename = filename.substr(strlen(SRCDIR));
154 std::string s = decl->getQualifiedNameAsString();
155 if (ignoreClass(s))
156 return true;
157 definitionMap.insert( std::pair<std::string,std::string>(s, filename) );
158 for (auto it = decl->bases_begin(); it != decl->bases_end(); ++it)
160 const CXXBaseSpecifier spec = *it;
161 // need to look through typedefs, hence the getUnqualifiedDesugaredType
162 QualType baseType = spec.getType().getDesugaredType(compiler.getASTContext());
163 childToParentClassSet.insert( std::pair<std::string,std::string>(s, baseType.getAsString()) );
166 return true;
169 loplugin::Plugin::Registration< MergeClasses > X("mergeclasses", false);
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */