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/.
18 Idea from Norbert (shm_get) - look for classes that are
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.
25 - unique classes that exist (A)
26 - unique classes that are instantiated (B)
27 - unique class-subclass relationships (C)
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:
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
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
49 public RecursiveASTVisitor
<MergeClasses
>, public loplugin::Plugin
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
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";
68 myfile
.open( SRCDIR
"/loplugin.mergeclasses.log", ios::app
| ios::out
);
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")
94 // ignore instantiations of pointers and arrays
95 if (s
.endswith("*") || s
.endswith("]")) {
101 // check for implicit construction
102 bool MergeClasses::VisitVarDecl( const VarDecl
* pVarDecl
)
104 if (ignoreLocation(pVarDecl
)) {
107 std::string s
= pVarDecl
->getType().getAsString();
109 instantiatedSet
.insert(s
);
113 // check for implicit construction
114 bool MergeClasses::VisitFieldDecl( const FieldDecl
* pFieldDecl
)
116 if (ignoreLocation(pFieldDecl
)) {
119 std::string s
= pFieldDecl
->getType().getAsString();
121 instantiatedSet
.insert(s
);
125 bool MergeClasses::VisitCXXConstructExpr( const CXXConstructExpr
* pCXXConstructExpr
)
127 if (ignoreLocation(pCXXConstructExpr
)) {
130 // ignore calls when a sub-class is constructing its superclass
131 if (pCXXConstructExpr
->getConstructionKind() != CXXConstructExpr::ConstructionKind::CK_Complete
) {
134 const CXXConstructorDecl
* pCXXConstructorDecl
= pCXXConstructExpr
->getConstructor();
135 const CXXRecordDecl
* pParentCXXRecordDecl
= pCXXConstructorDecl
->getParent();
136 std::string s
= pParentCXXRecordDecl
->getQualifiedNameAsString();
138 instantiatedSet
.insert(s
);
142 bool MergeClasses::VisitCXXRecordDecl(const CXXRecordDecl
* decl
)
144 if (ignoreLocation(decl
)) {
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();
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()) );
167 loplugin::Plugin::Registration
< MergeClasses
> X("mergeclasses", false);
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */