1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
14 #include "config_clang.h"
21 Idea from Norbert (shm_get) - look for classes that are
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.
28 - unique classes that exist (A)
29 - unique classes that are instantiated (B)
30 - unique class-subclass relationships (C)
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:
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
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
52 public loplugin::FilteringPlugin
<MergeClasses
>
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
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";
74 myfile
.open( WORKDIR
"/loplugin.mergeclasses.log", std::ios::app
| std::ios::out
);
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")
97 // ignore instantiations of pointers and arrays
98 if (compat::ends_with(s
, "*") || compat::ends_with(s
, "]")) {
104 // check for implicit construction
105 bool MergeClasses::VisitVarDecl( const VarDecl
* pVarDecl
)
107 if (ignoreLocation(pVarDecl
)) {
110 if (pVarDecl
->getType()->isReferenceType())
112 std::string s
= pVarDecl
->getType().getUnqualifiedType().getAsString();
114 instantiatedSet
.insert(s
);
118 // check for implicit construction
119 bool MergeClasses::VisitFieldDecl( const FieldDecl
* pFieldDecl
)
121 if (ignoreLocation(pFieldDecl
)) {
124 if (pFieldDecl
->getType()->isReferenceType())
126 std::string s
= pFieldDecl
->getType().getUnqualifiedType().getAsString();
128 instantiatedSet
.insert(s
);
132 bool MergeClasses::VisitCXXConstructExpr( const CXXConstructExpr
* pCXXConstructExpr
)
134 if (ignoreLocation(pCXXConstructExpr
)) {
137 // ignore calls when a sub-class is constructing its superclass
138 if (pCXXConstructExpr
->getConstructionKind() != compat::CXXConstructionKind::Complete
) {
141 const CXXConstructorDecl
* pCXXConstructorDecl
= pCXXConstructExpr
->getConstructor();
142 const CXXRecordDecl
* pParentCXXRecordDecl
= pCXXConstructorDecl
->getParent();
143 std::string s
= pParentCXXRecordDecl
->getQualifiedNameAsString();
145 instantiatedSet
.insert(s
);
149 bool MergeClasses::VisitCXXRecordDecl(const CXXRecordDecl
* decl
)
151 if (ignoreLocation(decl
)) {
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();
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()) );
174 loplugin::Plugin::Registration
< MergeClasses
> X("mergeclasses", false);
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */