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 <unordered_set>
19 Dump a list of virtual methods and a list of methods overriding virtual methods.
20 Then we will post-process the 2 lists and find the set of virtual methods which don't need to be virtual.
22 Also, we look for virtual methods where the bodies of all the overrides are empty i.e. this is leftover code
23 that no longer has a purpose.
25 The process goes something like this:
27 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='unnecessaryvirtual' check
28 $ ./compilerplugins/clang/unnecessaryvirtual.py
29 $ for dir in *; do make FORCE_COMPILE_ALL=1 UPDATE_FILES=$dir COMPILER_PLUGIN_TOOL='removevirtuals' $dir; done
31 Note that the actual process may involve a fair amount of undoing, hand editing, and general messing around
34 TODO some boost bind stuff appears to confuse it, notably in the xmloff module
42 std::string sourceLocation
;
45 bool operator < (const MyFuncInfo
&lhs
, const MyFuncInfo
&rhs
)
47 return lhs
.name
< rhs
.name
;
50 // try to limit the voluminous output a little
51 static std::set
<MyFuncInfo
> definitionSet
;
52 static std::unordered_set
<std::string
> overridingSet
;
53 static std::unordered_set
<std::string
> nonEmptySet
;
55 class UnnecessaryVirtual
:
56 public RecursiveASTVisitor
<UnnecessaryVirtual
>, public loplugin::Plugin
59 explicit UnnecessaryVirtual(loplugin::InstantiationData
const & data
):
62 virtual void run() override
64 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
66 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
67 // writing to the same logfile
69 for (const MyFuncInfo
& s
: definitionSet
)
70 output
+= "definition:\t" + s
.name
+ "\t" + s
.sourceLocation
+ "\n";
71 for (const std::string
& s
: overridingSet
)
72 output
+= "overriding:\t" + s
+ "\n";
73 for (const std::string
& s
: nonEmptySet
)
74 output
+= "nonempty:\t" + s
+ "\n";
76 myfile
.open( WORKDIR
"/loplugin.unnecessaryvirtual.log", std::ios::app
| std::ios::out
);
80 bool shouldVisitTemplateInstantiations () const { return true; }
81 bool shouldVisitImplicitCode() const { return true; }
83 bool VisitCXXMethodDecl( const CXXMethodDecl
* decl
);
85 void MarkRootOverridesNonEmpty( const CXXMethodDecl
* methodDecl
);
86 std::string
toString(SourceLocation loc
);
89 std::string
niceName(const CXXMethodDecl
* cxxMethodDecl
)
91 while (cxxMethodDecl
->getTemplateInstantiationPattern())
92 cxxMethodDecl
= dyn_cast
<CXXMethodDecl
>(cxxMethodDecl
->getTemplateInstantiationPattern());
93 while (cxxMethodDecl
->getInstantiatedFromMemberFunction())
94 cxxMethodDecl
= dyn_cast
<CXXMethodDecl
>(cxxMethodDecl
->getInstantiatedFromMemberFunction());
95 std::string s
= cxxMethodDecl
->getReturnType().getCanonicalType().getAsString()
96 + " " + cxxMethodDecl
->getQualifiedNameAsString() + "(";
97 for (const ParmVarDecl
*pParmVarDecl
: cxxMethodDecl
->parameters()) {
98 s
+= pParmVarDecl
->getType().getCanonicalType().getAsString();
102 if (cxxMethodDecl
->isConst()) {
108 bool UnnecessaryVirtual::VisitCXXMethodDecl( const CXXMethodDecl
* methodDecl
)
110 if (ignoreLocation(methodDecl
)) {
113 if (!methodDecl
->isVirtual() || methodDecl
->isDeleted()) {
116 // ignore stuff that forms part of the stable URE interface
117 if (isInUnoIncludeFile(methodDecl
->getCanonicalDecl())) {
121 auto body
= methodDecl
->getBody();
123 auto compoundStmt
= dyn_cast
<CompoundStmt
>(body
);
125 MarkRootOverridesNonEmpty(methodDecl
->getCanonicalDecl());
126 else if (compoundStmt
->size() > 0)
127 MarkRootOverridesNonEmpty(methodDecl
->getCanonicalDecl());
130 if (!methodDecl
->isThisDeclarationADefinition())
133 methodDecl
= methodDecl
->getCanonicalDecl();
134 std::string aNiceName
= niceName(methodDecl
);
136 // for destructors, we need to check if any of the superclass' destructors are virtual
137 if (isa
<CXXDestructorDecl
>(methodDecl
)) {
138 const CXXRecordDecl
* cxxRecordDecl
= methodDecl
->getParent();
139 if (cxxRecordDecl
->getNumBases() == 0) {
140 definitionSet
.insert( { aNiceName
, toString( methodDecl
->getLocation() ) } );
143 for(auto baseSpecifier
= cxxRecordDecl
->bases_begin();
144 baseSpecifier
!= cxxRecordDecl
->bases_end(); ++baseSpecifier
)
146 if (baseSpecifier
->getType()->isRecordType())
148 const CXXRecordDecl
* superclassCXXRecordDecl
= baseSpecifier
->getType()->getAsCXXRecordDecl();
149 std::string aOverriddenNiceName
= niceName(superclassCXXRecordDecl
->getDestructor());
150 overridingSet
.insert(aOverriddenNiceName
);
156 if (methodDecl
->size_overridden_methods() == 0) {
157 definitionSet
.insert( { aNiceName
, toString( methodDecl
->getLocation() ) } );
159 for (auto iter
= methodDecl
->begin_overridden_methods();
160 iter
!= methodDecl
->end_overridden_methods(); ++iter
)
162 const CXXMethodDecl
*overriddenMethod
= *iter
;
163 // we only care about the first level override to establish that a virtual qualifier was useful.
164 if (overriddenMethod
->isPure() || overriddenMethod
->size_overridden_methods() == 0)
166 std::string aOverriddenNiceName
= niceName(overriddenMethod
);
167 overridingSet
.insert(aOverriddenNiceName
);
174 void UnnecessaryVirtual::MarkRootOverridesNonEmpty( const CXXMethodDecl
* methodDecl
)
176 if (methodDecl
->size_overridden_methods() == 0) {
177 nonEmptySet
.insert(niceName(methodDecl
));
180 for (auto iter
= methodDecl
->begin_overridden_methods();
181 iter
!= methodDecl
->end_overridden_methods(); ++iter
)
183 MarkRootOverridesNonEmpty(*iter
);
187 std::string
UnnecessaryVirtual::toString(SourceLocation loc
)
189 SourceLocation expansionLoc
= compiler
.getSourceManager().getExpansionLoc( loc
);
190 StringRef name
= getFilenameOfLocation(expansionLoc
);
191 std::string sourceLocation
= std::string(name
.substr(strlen(SRCDIR
)+1)) + ":" + std::to_string(compiler
.getSourceManager().getSpellingLineNumber(expansionLoc
));
192 loplugin::normalizeDotDotInFilePath(sourceLocation
);
193 return sourceLocation
;
197 loplugin::Plugin::Registration
< UnnecessaryVirtual
> X("unnecessaryvirtual", false);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */