Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / finalclasses.cxx
blob6e8596a78421fa29ba187076405b05c532245857
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 <set>
12 #include <string>
13 #include <iostream>
14 #include "plugin.hxx"
15 #include <fstream>
17 /**
18 Look for classes that are final i.e. nothing extends them, and have protected fields or members.
20 These can be made private.
22 The process goes something like this:
23 $ make check
24 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='finalclasses' check
25 $ ./compilerplugins/clang/finalclasses.py
29 namespace {
31 // try to limit the voluminous output a little
32 static std::set<std::string> inheritedFromSet;
33 static std::map<std::string,std::string> definitionMap; // className -> filename
35 class FinalClasses:
36 public RecursiveASTVisitor<FinalClasses>, public loplugin::Plugin
38 public:
39 explicit FinalClasses(loplugin::InstantiationData const & data):
40 Plugin(data) {}
42 virtual void run() override
44 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
46 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
47 // writing to the same logfile
48 std::string output;
49 for (const std::string & s : inheritedFromSet)
50 output += "inherited-from:\t" + s + "\n";
51 for (const std::pair<std::string,std::string> & s : definitionMap)
52 output += "definition:\t" + s.first + "\t" + s.second + "\n";
53 std::ofstream myfile;
54 myfile.open( WORKDIR "/loplugin.finalclasses.log", std::ios::app | std::ios::out);
55 myfile << output;
56 myfile.close();
59 bool shouldVisitTemplateInstantiations () const { return true; }
61 bool shouldVisitImplicitCode() const { return true; }
63 bool VisitCXXRecordDecl( const CXXRecordDecl* decl);
64 private:
65 void checkBase(QualType qt);
68 bool startsWith(const std::string& rStr, const char* pSubStr) {
69 return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
72 bool ignoreClass(StringRef s)
74 // ignore stuff in the standard library, and UNO stuff we can't touch.
75 if (startsWith(s, "rtl::") || startsWith(s, "sal::") || startsWith(s, "com::sun::")
76 || startsWith(s, "std::") || startsWith(s, "boost::")
77 || s == "OString" || s == "OUString" || s == "bad_alloc")
79 return true;
81 return false;
84 bool FinalClasses::VisitCXXRecordDecl(const CXXRecordDecl* decl)
86 if (ignoreLocation(decl))
87 return true;
88 decl = decl->getCanonicalDecl();
89 if (!decl->hasDefinition())
90 return true;
92 for (auto it = decl->bases_begin(); it != decl->bases_end(); ++it)
94 const CXXBaseSpecifier spec = *it;
95 checkBase(spec.getType());
97 for (auto it = decl->vbases_begin(); it != decl->vbases_end(); ++it)
99 const CXXBaseSpecifier spec = *it;
100 checkBase(spec.getType());
103 bool bFoundProtected = false;
104 for (auto it = decl->method_begin(); it != decl->method_end(); ++it) {
105 auto i = *it;
106 // ignore methods that are overriding base-class methods, making them private
107 // isn't useful
108 if ( !i->hasAttr<OverrideAttr>() && i->getAccess() == AS_protected ) {
109 bFoundProtected = true;
110 break;
113 if (!bFoundProtected)
115 for (auto it = decl->field_begin(); it != decl->field_end(); ++it) {
116 auto i = *it;
117 if ( i->getAccess() == AS_protected ) {
118 bFoundProtected = true;
119 break;
123 if (!bFoundProtected)
124 return true;
126 std::string s = decl->getQualifiedNameAsString();
127 if (ignoreClass(s))
128 return true;
130 SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(decl->getLocStart());
131 std::string filename = compiler.getSourceManager().getFilename(spellingLocation);
132 auto sourceLocation = filename.substr(strlen(SRCDIR)) + ":"
133 + std::to_string(compiler.getSourceManager().getSpellingLineNumber(spellingLocation));
134 definitionMap.insert( std::pair<std::string,std::string>(s, sourceLocation) );
135 return true;
138 void FinalClasses::checkBase(QualType baseType)
140 // need to look through typedefs, hence the getUnqualifiedDesugaredType
141 baseType = baseType.getDesugaredType(compiler.getASTContext());
142 std::string x;
143 // so that we get just the template name, excluding the template parameters
144 if (baseType->isRecordType())
145 x = baseType->getAsCXXRecordDecl()->getQualifiedNameAsString();
146 else if (auto templateType = baseType->getAs<TemplateSpecializationType>())
147 x = templateType->getTemplateName().getAsTemplateDecl()->getQualifiedNameAsString();
148 else
149 x = baseType.getAsString();
150 inheritedFromSet.insert( x );
153 loplugin::Plugin::Registration< FinalClasses > X("finalclasses", false);
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */