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/.
19 Idea from Norbert (shm_get) - look for classes that are
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.
26 - unique classes that exist (A)
27 - unique classes that are instantiated (B)
28 - unique class-subclass relationships (C)
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:
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
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
50 public loplugin::FilteringPlugin
<MergeClasses
>
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
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";
70 myfile
.open( WORKDIR
"/loplugin.mergeclasses.log", std::ios::app
| std::ios::out
);
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")
96 // ignore instantiations of pointers and arrays
97 if (s
.endswith("*") || s
.endswith("]")) {
103 // check for implicit construction
104 bool MergeClasses::VisitVarDecl( const VarDecl
* pVarDecl
)
106 if (ignoreLocation(pVarDecl
)) {
109 std::string s
= pVarDecl
->getType().getAsString();
111 instantiatedSet
.insert(s
);
115 // check for implicit construction
116 bool MergeClasses::VisitFieldDecl( const FieldDecl
* pFieldDecl
)
118 if (ignoreLocation(pFieldDecl
)) {
121 std::string s
= pFieldDecl
->getType().getAsString();
123 instantiatedSet
.insert(s
);
127 bool MergeClasses::VisitCXXConstructExpr( const CXXConstructExpr
* pCXXConstructExpr
)
129 if (ignoreLocation(pCXXConstructExpr
)) {
132 // ignore calls when a sub-class is constructing its superclass
133 if (pCXXConstructExpr
->getConstructionKind() != CXXConstructExpr::ConstructionKind::CK_Complete
) {
136 const CXXConstructorDecl
* pCXXConstructorDecl
= pCXXConstructExpr
->getConstructor();
137 const CXXRecordDecl
* pParentCXXRecordDecl
= pCXXConstructorDecl
->getParent();
138 std::string s
= pParentCXXRecordDecl
->getQualifiedNameAsString();
140 instantiatedSet
.insert(s
);
144 bool MergeClasses::VisitCXXRecordDecl(const CXXRecordDecl
* decl
)
146 if (ignoreLocation(decl
)) {
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();
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()) );
169 loplugin::Plugin::Registration
< MergeClasses
> X("mergeclasses", false);
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */