cURL: follow redirects
[LibreOffice.git] / compilerplugins / clang / mergeclasses.cxx
blobfa4eb3c99a3b616897a6d78486d50be2764c9e75
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 <string>
12 #include <iostream>
13 #include "plugin.hxx"
14 #include <fstream>
16 /**
18 Idea from Norbert (shm_get) - look for classes that are
19 (a) not instantiated
20 (b) have zero or one subclasses
21 and warn about them - would allow us to remove a bunch of abstract classes
22 that can be merged into one class and simplified.
24 Dump a list of
25 - unique classes that exist (A)
26 - unique classes that are instantiated (B)
27 - unique class-subclass relationships (C)
28 Then
29 let D = A minus B
30 for each class in D, look in C and count the entries, then dump it if no-entries == 1
32 The process goes something like this:
33 $ make check
34 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='mergeclasses' check
35 $ ./compilerplugins/clang/mergeclasses.py
37 FIXME exclude 'static-only' classes, which some people may use/have used instead of a namespace to tie together a bunch of functions
41 namespace {
43 // try to limit the voluminous output a little
44 static std::set<std::string> instantiatedSet;
45 static std::set<std::pair<std::string,std::string> > childToParentClassSet; // childClassName -> parentClassName
46 static std::map<std::string,std::string> definitionMap; // className -> filename
48 class MergeClasses:
49 public RecursiveASTVisitor<MergeClasses>, public loplugin::Plugin
51 public:
52 explicit MergeClasses(InstantiationData const & data): Plugin(data) {}
54 virtual void run() override
56 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
58 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
59 // writing to the same logfile
60 std::string output;
61 for (const std::string & s : instantiatedSet)
62 output += "instantiated:\t" + s + "\n";
63 for (const std::pair<std::string,std::string> & s : childToParentClassSet)
64 output += "has-subclass:\t" + s.first + "\t" + s.second + "\n";
65 for (const std::pair<std::string,std::string> & s : definitionMap)
66 output += "definition:\t" + s.first + "\t" + s.second + "\n";
67 ofstream myfile;
68 myfile.open( SRCDIR "/loplugin.mergeclasses.log", ios::app | ios::out);
69 myfile << output;
70 myfile.close();
73 bool shouldVisitTemplateInstantiations () const { return true; }
75 bool VisitVarDecl(const VarDecl *);
76 bool VisitFieldDecl(const FieldDecl *);
77 bool VisitCXXConstructExpr( const CXXConstructExpr* var );
78 bool VisitCXXRecordDecl( const CXXRecordDecl* decl);
81 bool startsWith(const std::string& rStr, const char* pSubStr) {
82 return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
85 bool ignoreClass(StringRef s)
87 // ignore stuff in the standard library, and UNO stuff we can't touch.
88 if (startsWith(s, "rtl::") || startsWith(s, "sal::") || startsWith(s, "com::sun::")
89 || startsWith(s, "std::") || startsWith(s, "boost::")
90 || s == "OString" || s == "OUString" || s == "bad_alloc")
92 return true;
94 // ignore instantiations of pointers and arrays
95 if (s.endswith("*") || s.endswith("]")) {
96 return true;
98 return false;
101 // check for implicit construction
102 bool MergeClasses::VisitVarDecl( const VarDecl* pVarDecl )
104 if (ignoreLocation(pVarDecl)) {
105 return true;
107 std::string s = pVarDecl->getType().getAsString();
108 if (!ignoreClass(s))
109 instantiatedSet.insert(s);
110 return true;
113 // check for implicit construction
114 bool MergeClasses::VisitFieldDecl( const FieldDecl* pFieldDecl )
116 if (ignoreLocation(pFieldDecl)) {
117 return true;
119 std::string s = pFieldDecl->getType().getAsString();
120 if (!ignoreClass(s))
121 instantiatedSet.insert(s);
122 return true;
125 bool MergeClasses::VisitCXXConstructExpr( const CXXConstructExpr* pCXXConstructExpr )
127 if (ignoreLocation(pCXXConstructExpr)) {
128 return true;
130 // ignore calls when a sub-class is constructing its superclass
131 if (pCXXConstructExpr->getConstructionKind() != CXXConstructExpr::ConstructionKind::CK_Complete) {
132 return true;
134 const CXXConstructorDecl* pCXXConstructorDecl = pCXXConstructExpr->getConstructor();
135 const CXXRecordDecl* pParentCXXRecordDecl = pCXXConstructorDecl->getParent();
136 std::string s = pParentCXXRecordDecl->getQualifiedNameAsString();
137 if (!ignoreClass(s))
138 instantiatedSet.insert(s);
139 return true;
142 bool MergeClasses::VisitCXXRecordDecl(const CXXRecordDecl* decl)
144 if (ignoreLocation(decl)) {
145 return true;
147 if (decl->isThisDeclarationADefinition())
149 SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(decl->getCanonicalDecl()->getLocStart());
150 std::string filename = compiler.getSourceManager().getFilename(spellingLocation);
151 filename = filename.substr(strlen(SRCDIR));
152 std::string s = decl->getQualifiedNameAsString();
153 if (ignoreClass(s))
154 return true;
155 definitionMap.insert( std::pair<std::string,std::string>(s, filename) );
156 for (auto it = decl->bases_begin(); it != decl->bases_end(); ++it)
158 const CXXBaseSpecifier spec = *it;
159 // need to look through typedefs, hence the getUnqualifiedDesugaredType
160 QualType baseType = spec.getType().getDesugaredType(compiler.getASTContext());
161 childToParentClassSet.insert( std::pair<std::string,std::string>(s, baseType.getAsString()) );
164 return true;
167 loplugin::Plugin::Registration< MergeClasses > X("mergeclasses", false);
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */