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/.
18 Look for classes that are final i.e. nothing extends them, and have either
19 (a) protected fields or members.
23 In the case of (a), those members/fields can be made private.
24 In the case of (b), making the class final means the compiler can devirtualise
27 The process goes something like this:
29 $ make FORCE_COMPILE=all COMPILER_PLUGIN_TOOL='finalclasses' check
30 $ ./compilerplugins/clang/finalclasses.py
36 // try to limit the voluminous output a little
37 static std::set
<std::string
> inheritedFromSet
;
38 static std::map
<std::string
,std::string
> definitionMap
; // className -> filename
41 public RecursiveASTVisitor
<FinalClasses
>, public loplugin::Plugin
44 explicit FinalClasses(loplugin::InstantiationData
const & data
):
47 virtual void run() override
49 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
51 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
52 // writing to the same logfile
54 for (const std::string
& s
: inheritedFromSet
)
55 output
+= "inherited-from:\t" + s
+ "\n";
56 for (const auto & s
: definitionMap
)
57 output
+= "definition:\t" + s
.first
+ "\t" + s
.second
+ "\n";
59 myfile
.open( WORKDIR
"/loplugin.finalclasses.log", std::ios::app
| std::ios::out
);
64 bool shouldVisitTemplateInstantiations () const { return true; }
66 bool shouldVisitImplicitCode() const { return true; }
68 bool VisitCXXRecordDecl( const CXXRecordDecl
* decl
);
70 void checkBase(QualType qt
);
73 bool ignoreClass(StringRef s
)
75 // ignore stuff in the standard library, and UNO stuff we can't touch.
76 if (s
.startswith("rtl::") || s
.startswith("sal::") || s
.startswith("com::sun::")
77 || s
.startswith("std::") || s
.startswith("boost::")
78 || s
== "OString" || s
== "OUString" || s
== "bad_alloc")
85 bool FinalClasses::VisitCXXRecordDecl(const CXXRecordDecl
* decl
)
87 if (ignoreLocation(decl
))
89 if (!decl
->hasDefinition())
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 if (decl
->hasAttr
<FinalAttr
>())
105 bool bFoundVirtual
= false;
106 bool bFoundProtected
= false;
107 for (auto it
= decl
->method_begin(); it
!= decl
->method_end(); ++it
) {
109 // ignore methods that are overriding base-class methods, making them private
111 if ( !i
->hasAttr
<OverrideAttr
>() && i
->getAccess() == AS_protected
)
112 bFoundProtected
= true;
113 if ( i
->isVirtual() )
114 bFoundVirtual
= true;
117 if (!bFoundProtected
)
119 for (auto it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
121 if ( i
->getAccess() == AS_protected
) {
122 bFoundProtected
= true;
127 if (!bFoundProtected
&& !bFoundVirtual
)
130 std::string s
= decl
->getQualifiedNameAsString();
134 SourceLocation spellingLocation
= compiler
.getSourceManager().getSpellingLoc(compat::getBeginLoc(decl
));
135 auto const filename
= getFilenameOfLocation(spellingLocation
);
136 auto sourceLocation
= filename
.substr(strlen(SRCDIR
)).str() + ":"
137 + std::to_string(compiler
.getSourceManager().getSpellingLineNumber(spellingLocation
));
138 definitionMap
.insert( std::pair
<std::string
,std::string
>(s
, sourceLocation
) );
142 void FinalClasses::checkBase(QualType baseType
)
144 // need to look through typedefs, hence the getUnqualifiedDesugaredType
145 baseType
= baseType
.getDesugaredType(compiler
.getASTContext());
147 // so that we get just the template name, excluding the template parameters
148 if (baseType
->isRecordType())
149 x
= baseType
->getAsCXXRecordDecl()->getQualifiedNameAsString();
150 else if (auto templateType
= baseType
->getAs
<TemplateSpecializationType
>())
151 x
= templateType
->getTemplateName().getAsTemplateDecl()->getQualifiedNameAsString();
153 x
= baseType
.getAsString();
154 inheritedFromSet
.insert( x
);
157 loplugin::Plugin::Registration
< FinalClasses
> X("finalclasses", false);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */