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 protected fields or members.
20 These can be made private.
22 The process goes something like this:
24 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='finalclasses' check
25 $ ./compilerplugins/clang/finalclasses.py
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
36 public RecursiveASTVisitor
<FinalClasses
>, public loplugin::Plugin
39 explicit FinalClasses(loplugin::InstantiationData
const & 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
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";
54 myfile
.open( WORKDIR
"/loplugin.finalclasses.log", std::ios::app
| std::ios::out
);
59 bool shouldVisitTemplateInstantiations () const { return true; }
61 bool shouldVisitImplicitCode() const { return true; }
63 bool VisitCXXRecordDecl( const CXXRecordDecl
* decl
);
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")
84 bool FinalClasses::VisitCXXRecordDecl(const CXXRecordDecl
* decl
)
86 if (ignoreLocation(decl
))
88 decl
= decl
->getCanonicalDecl();
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 bool bFoundProtected
= false;
104 for (auto it
= decl
->method_begin(); it
!= decl
->method_end(); ++it
) {
106 // ignore methods that are overriding base-class methods, making them private
108 if ( !i
->hasAttr
<OverrideAttr
>() && i
->getAccess() == AS_protected
) {
109 bFoundProtected
= true;
113 if (!bFoundProtected
)
115 for (auto it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
117 if ( i
->getAccess() == AS_protected
) {
118 bFoundProtected
= true;
123 if (!bFoundProtected
)
126 std::string s
= decl
->getQualifiedNameAsString();
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
) );
138 void FinalClasses::checkBase(QualType baseType
)
140 // need to look through typedefs, hence the getUnqualifiedDesugaredType
141 baseType
= baseType
.getDesugaredType(compiler
.getASTContext());
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();
149 x
= baseType
.getAsString();
150 inheritedFromSet
.insert( x
);
153 loplugin::Plugin::Registration
< FinalClasses
> X("finalclasses", false);
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */