bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / store / unnecessaryvirtual.cxx
blob0ead077473dec18bcd9bd5c5187a0506cbd3b29a
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 "plugin.hxx"
14 #include "compat.hxx"
16 /**
17 Dump a list of virtual methods and a list of methods overriding virtual methods.
18 Then we will post-process the 2 lists and find the set of virtual methods which don't need to be virtual.
20 The process goes something like this:
21 $ make check
22 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='unnecessaryvirtual' check > log.txt
23 $ grep 'definition' log.txt | cut -f 2 | sort -u > definition.txt
24 $ grep 'overriding' log.txt | cut -f 2 | sort -u > overriding.txt
25 $ cat definition.txt overriding.txt | sort | uniq -u > result.txt
26 $ echo "\n" >> result.txt
27 $ for dir in *; do make FORCE_COMPILE_ALL=1 UPDATE_FILES=$dir COMPILER_PLUGIN_TOOL='removevirtuals' $dir; done
29 Note that the actual process may involve a fair amount of undoing, hand editing, and general messing around
30 to get it to work :-)
31 Notably templates tend to confuse it into removing stuff that is still needed.
34 namespace {
36 class UnnecessaryVirtual:
37 public RecursiveASTVisitor<UnnecessaryVirtual>, public loplugin::Plugin
39 public:
40 explicit UnnecessaryVirtual(InstantiationData const & data): Plugin(data) {}
42 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
44 bool VisitCXXMethodDecl( const CXXMethodDecl* var );
48 static std::string niceName(const CXXMethodDecl* functionDecl)
50 std::string s =
51 functionDecl->getParent()->getQualifiedNameAsString() + "::"
52 + compat::getReturnType(*functionDecl).getAsString() + "-"
53 + functionDecl->getNameAsString() + "(";
54 for (const ParmVarDecl *pParmVarDecl : functionDecl->params()) {
55 s += pParmVarDecl->getType().getAsString();
56 s += ",";
58 s += ")";
59 if (functionDecl->isConst()) {
60 s += "const";
62 return s;
65 bool UnnecessaryVirtual::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
67 if (ignoreLocation(functionDecl)) {
68 return true;
70 functionDecl = functionDecl->getCanonicalDecl();
71 // ignore stuff that forms part of the stable URE interface
72 if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
73 functionDecl->getNameInfo().getLoc()))) {
74 return true;
76 if (!functionDecl->isVirtual()) {
77 return true;
79 // ignore UNO interface definitions, cannot change those
80 static const char cssPrefix[] = "com::sun::star";
81 if (functionDecl->getParent()->getQualifiedNameAsString().compare(0, strlen(cssPrefix), cssPrefix) == 0) {
82 return true;
84 std::string aNiceName = niceName(functionDecl);
85 // Ignore virtual destructors for now.
86 // I cannot currently detect the case where we are overriding a pure virtual destructor.
87 if (dyn_cast<CXXDestructorDecl>(functionDecl)) {
88 return true;
90 if (functionDecl->size_overridden_methods() == 0) {
91 // ignore definition of virtual functions in templates
92 // if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate
93 // && functionDecl->getParent()->getDescribedClassTemplate() == nullptr)
94 // {
95 cout << "definition\t" << aNiceName << endl;
96 // }
97 } else {
98 for (CXXMethodDecl::method_iterator iter = functionDecl->begin_overridden_methods(); iter != functionDecl->end_overridden_methods(); ++iter) {
99 const CXXMethodDecl *pOverriddenMethod = *iter;
100 // we only care about the first level override to establish that a virtual qualifier was useful.
101 if (pOverriddenMethod->size_overridden_methods() == 0) {
102 // ignore UNO interface definitions, cannot change those
103 if (pOverriddenMethod->getParent()->getQualifiedNameAsString().compare(0, strlen(cssPrefix), cssPrefix) != 0) {
104 std::string aOverriddenNiceName = niceName(pOverriddenMethod);
105 cout << "overriding\t" << aOverriddenNiceName << endl;
110 return true;
115 loplugin::Plugin::Registration< UnnecessaryVirtual > X("unnecessaryvirtual", false);
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */