Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / unnecessaryvirtual.cxx
blobafc324cee1567d4682531e3b73430846150d26c0
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 <set>
14 #include <unordered_set>
15 #include "plugin.hxx"
16 #include "compat.hxx"
17 #include <fstream>
19 /**
20 Dump a list of virtual methods and a list of methods overriding virtual methods.
21 Then we will post-process the 2 lists and find the set of virtual methods which don't need to be virtual.
23 Also, we look for virtual methods where the bodies of all the overrides are empty i.e. this is leftover code
24 that no longer has a purpose.
26 The process goes something like this:
27 $ make check
28 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='unnecessaryvirtual' check
29 $ ./compilerplugins/clang/unnecessaryvirtual.py
30 $ for dir in *; do make FORCE_COMPILE_ALL=1 UPDATE_FILES=$dir COMPILER_PLUGIN_TOOL='removevirtuals' $dir; done
32 Note that the actual process may involve a fair amount of undoing, hand editing, and general messing around
33 to get it to work :-)
35 TODO some boost bind stuff appears to confuse it, notably in the xmloff module
38 namespace {
40 struct MyFuncInfo
42 std::string name;
43 std::string sourceLocation;
46 bool operator < (const MyFuncInfo &lhs, const MyFuncInfo &rhs)
48 return lhs.name < rhs.name;
51 // try to limit the voluminous output a little
52 static std::set<MyFuncInfo> definitionSet;
53 static std::unordered_set<std::string> overridingSet;
54 static std::unordered_set<std::string> nonEmptySet;
56 class UnnecessaryVirtual:
57 public RecursiveASTVisitor<UnnecessaryVirtual>, public loplugin::Plugin
59 public:
60 explicit UnnecessaryVirtual(loplugin::InstantiationData const & data):
61 Plugin(data) {}
63 virtual void run() override
65 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
67 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
68 // writing to the same logfile
69 std::string output;
70 for (const MyFuncInfo & s : definitionSet)
71 output += "definition:\t" + s.name + "\t" + s.sourceLocation + "\n";
72 for (const std::string & s : overridingSet)
73 output += "overriding:\t" + s + "\n";
74 for (const std::string & s : nonEmptySet)
75 output += "nonempty:\t" + s + "\n";
76 std::ofstream myfile;
77 myfile.open( WORKDIR "/loplugin.unnecessaryvirtual.log", std::ios::app | std::ios::out);
78 myfile << output;
79 myfile.close();
81 bool shouldVisitTemplateInstantiations () const { return true; }
82 bool shouldVisitImplicitCode() const { return true; }
84 bool VisitCXXMethodDecl( const CXXMethodDecl* decl );
85 private:
86 void MarkRootOverridesNonEmpty( const CXXMethodDecl* methodDecl );
87 std::string toString(SourceLocation loc);
90 std::string niceName(const CXXMethodDecl* cxxMethodDecl)
92 while (cxxMethodDecl->getTemplateInstantiationPattern())
93 cxxMethodDecl = dyn_cast<CXXMethodDecl>(cxxMethodDecl->getTemplateInstantiationPattern());
94 while (cxxMethodDecl->getInstantiatedFromMemberFunction())
95 cxxMethodDecl = dyn_cast<CXXMethodDecl>(cxxMethodDecl->getInstantiatedFromMemberFunction());
96 std::string s = cxxMethodDecl->getReturnType().getCanonicalType().getAsString()
97 + " " + cxxMethodDecl->getQualifiedNameAsString() + "(";
98 for (const ParmVarDecl *pParmVarDecl : compat::parameters(*cxxMethodDecl)) {
99 s += pParmVarDecl->getType().getCanonicalType().getAsString();
100 s += ",";
102 s += ")";
103 if (cxxMethodDecl->isConst()) {
104 s += "const";
106 return s;
109 bool UnnecessaryVirtual::VisitCXXMethodDecl( const CXXMethodDecl* methodDecl )
111 if (ignoreLocation(methodDecl)) {
112 return true;
114 if (!methodDecl->isVirtual() || methodDecl->isDeleted()) {
115 return true;
117 // ignore stuff that forms part of the stable URE interface
118 if (isInUnoIncludeFile(methodDecl->getCanonicalDecl())) {
119 return true;
122 auto body = methodDecl->getBody();
123 if (body) {
124 auto compoundStmt = dyn_cast<CompoundStmt>(body);
125 if (!compoundStmt)
126 MarkRootOverridesNonEmpty(methodDecl->getCanonicalDecl());
127 else if (compoundStmt->size() > 0)
128 MarkRootOverridesNonEmpty(methodDecl->getCanonicalDecl());
131 if (!methodDecl->isThisDeclarationADefinition())
132 return true;
134 methodDecl = methodDecl->getCanonicalDecl();
135 std::string aNiceName = niceName(methodDecl);
137 // for destructors, we need to check if any of the superclass' destructors are virtual
138 if (isa<CXXDestructorDecl>(methodDecl)) {
139 const CXXRecordDecl* cxxRecordDecl = methodDecl->getParent();
140 if (cxxRecordDecl->getNumBases() == 0) {
141 definitionSet.insert( { aNiceName, toString( methodDecl->getLocation() ) } );
142 return true;
144 for(auto baseSpecifier = cxxRecordDecl->bases_begin();
145 baseSpecifier != cxxRecordDecl->bases_end(); ++baseSpecifier)
147 if (baseSpecifier->getType()->isRecordType())
149 const CXXRecordDecl* superclassCXXRecordDecl = baseSpecifier->getType()->getAsCXXRecordDecl();
150 std::string aOverriddenNiceName = niceName(superclassCXXRecordDecl->getDestructor());
151 overridingSet.insert(aOverriddenNiceName);
154 return true;
157 if (methodDecl->size_overridden_methods() == 0) {
158 definitionSet.insert( { aNiceName, toString( methodDecl->getLocation() ) } );
159 } else {
160 for (auto iter = methodDecl->begin_overridden_methods();
161 iter != methodDecl->end_overridden_methods(); ++iter)
163 const CXXMethodDecl *overriddenMethod = *iter;
164 // we only care about the first level override to establish that a virtual qualifier was useful.
165 if (overriddenMethod->isPure() || overriddenMethod->size_overridden_methods() == 0)
167 std::string aOverriddenNiceName = niceName(overriddenMethod);
168 overridingSet.insert(aOverriddenNiceName);
172 return true;
175 void UnnecessaryVirtual::MarkRootOverridesNonEmpty( const CXXMethodDecl* methodDecl )
177 if (methodDecl->size_overridden_methods() == 0) {
178 nonEmptySet.insert(niceName(methodDecl));
179 return;
181 for (auto iter = methodDecl->begin_overridden_methods();
182 iter != methodDecl->end_overridden_methods(); ++iter)
184 MarkRootOverridesNonEmpty(*iter);
188 std::string UnnecessaryVirtual::toString(SourceLocation loc)
190 SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
191 StringRef name = compiler.getSourceManager().getFilename(expansionLoc);
192 std::string sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc));
193 loplugin::normalizeDotDotInFilePath(sourceLocation);
194 return sourceLocation;
198 loplugin::Plugin::Registration< UnnecessaryVirtual > X("unnecessaryvirtual", false);
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */