2017-10-03 09:17:35 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include "plugin.hxx"
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
/**
|
2019-10-28 18:21:37 +02:00
|
|
|
Look for classes that are final i.e. nothing extends them, and have either
|
|
|
|
(a) protected fields or members.
|
|
|
|
or
|
|
|
|
(b) virtual members
|
2017-10-03 09:17:35 +02:00
|
|
|
|
2019-10-28 18:21:37 +02:00
|
|
|
In the case of (a), those members/fields can be made private.
|
|
|
|
In the case of (b), making the class final means the compiler can devirtualise
|
|
|
|
some method class.
|
2017-10-03 09:17:35 +02:00
|
|
|
|
|
|
|
The process goes something like this:
|
|
|
|
$ make check
|
2018-03-04 15:00:48 +01:00
|
|
|
$ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='finalclasses' check
|
2017-10-03 09:17:35 +02:00
|
|
|
$ ./compilerplugins/clang/finalclasses.py
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// try to limit the voluminous output a little
|
|
|
|
static std::set<std::string> inheritedFromSet;
|
|
|
|
static std::map<std::string,std::string> definitionMap; // className -> filename
|
|
|
|
|
|
|
|
class FinalClasses:
|
|
|
|
public RecursiveASTVisitor<FinalClasses>, public loplugin::Plugin
|
|
|
|
{
|
|
|
|
public:
|
2017-11-07 11:50:47 +01:00
|
|
|
explicit FinalClasses(loplugin::InstantiationData const & data):
|
|
|
|
Plugin(data) {}
|
2017-10-03 09:17:35 +02:00
|
|
|
|
|
|
|
virtual void run() override
|
|
|
|
{
|
|
|
|
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
|
|
|
|
|
|
|
|
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
|
|
|
|
// writing to the same logfile
|
|
|
|
std::string output;
|
|
|
|
for (const std::string & s : inheritedFromSet)
|
|
|
|
output += "inherited-from:\t" + s + "\n";
|
-Werror,-Wrange-loop-analysis
> compilerplugins/clang/finalclasses.cxx:56:57: error: loop variable 's' has type 'const std::pair<std::string, std::string> &' (aka 'const pair<basic_string<char>, basic_string<char> > &') but is initialized with type 'std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >' resulting in a copy [-Werror,-Wrange-loop-analysis]
> for (const std::pair<std::string,std::string> & s : definitionMap)
> ^
> compilerplugins/clang/finalclasses.cxx:56:14: note: use non-reference type 'std::pair<std::string, std::string>' (aka 'pair<basic_string<char>, basic_string<char> >') to keep the copy or type 'const std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > &' to prevent copying
> for (const std::pair<std::string,std::string> & s : definitionMap)
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
etc.
Change-Id: If3f0599ef79d49e4bb4bbd245ca7456c09d13975
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86893
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2020-01-15 23:12:25 +01:00
|
|
|
for (const auto & s : definitionMap)
|
2017-10-03 09:17:35 +02:00
|
|
|
output += "definition:\t" + s.first + "\t" + s.second + "\n";
|
|
|
|
std::ofstream myfile;
|
2018-04-10 15:41:12 +02:00
|
|
|
myfile.open( WORKDIR "/loplugin.finalclasses.log", std::ios::app | std::ios::out);
|
2017-10-03 09:17:35 +02:00
|
|
|
myfile << output;
|
|
|
|
myfile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldVisitTemplateInstantiations () const { return true; }
|
|
|
|
|
2017-10-05 07:50:24 +02:00
|
|
|
bool shouldVisitImplicitCode() const { return true; }
|
|
|
|
|
2017-10-03 09:17:35 +02:00
|
|
|
bool VisitCXXRecordDecl( const CXXRecordDecl* decl);
|
2017-10-05 07:50:24 +02:00
|
|
|
private:
|
|
|
|
void checkBase(QualType qt);
|
2017-10-03 09:17:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool ignoreClass(StringRef s)
|
|
|
|
{
|
|
|
|
// ignore stuff in the standard library, and UNO stuff we can't touch.
|
2020-02-13 08:30:43 +01:00
|
|
|
if (s.startswith("rtl::") || s.startswith("sal::") || s.startswith("com::sun::")
|
|
|
|
|| s.startswith("std::") || s.startswith("boost::")
|
2017-10-03 09:17:35 +02:00
|
|
|
|| s == "OString" || s == "OUString" || s == "bad_alloc")
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FinalClasses::VisitCXXRecordDecl(const CXXRecordDecl* decl)
|
|
|
|
{
|
|
|
|
if (ignoreLocation(decl))
|
|
|
|
return true;
|
|
|
|
decl = decl->getCanonicalDecl();
|
2017-10-05 07:50:24 +02:00
|
|
|
if (!decl->hasDefinition())
|
2017-10-03 09:17:35 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto it = decl->bases_begin(); it != decl->bases_end(); ++it)
|
|
|
|
{
|
|
|
|
const CXXBaseSpecifier spec = *it;
|
2017-10-05 07:50:24 +02:00
|
|
|
checkBase(spec.getType());
|
|
|
|
}
|
|
|
|
for (auto it = decl->vbases_begin(); it != decl->vbases_end(); ++it)
|
|
|
|
{
|
|
|
|
const CXXBaseSpecifier spec = *it;
|
|
|
|
checkBase(spec.getType());
|
2017-10-03 09:17:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 08:08:38 +02:00
|
|
|
if (decl->hasAttr<FinalAttr>())
|
|
|
|
return true;
|
2019-10-28 18:21:37 +02:00
|
|
|
bool bFoundVirtual = false;
|
2017-10-03 09:17:35 +02:00
|
|
|
bool bFoundProtected = false;
|
|
|
|
for (auto it = decl->method_begin(); it != decl->method_end(); ++it) {
|
|
|
|
auto i = *it;
|
|
|
|
// ignore methods that are overriding base-class methods, making them private
|
|
|
|
// isn't useful
|
2019-10-28 18:21:37 +02:00
|
|
|
if ( !i->hasAttr<OverrideAttr>() && i->getAccess() == AS_protected )
|
2017-10-03 09:17:35 +02:00
|
|
|
bFoundProtected = true;
|
2019-10-28 18:21:37 +02:00
|
|
|
if ( i->isVirtual() )
|
|
|
|
bFoundVirtual = true;
|
2017-10-03 09:17:35 +02:00
|
|
|
}
|
|
|
|
if (!bFoundProtected)
|
|
|
|
{
|
|
|
|
for (auto it = decl->field_begin(); it != decl->field_end(); ++it) {
|
|
|
|
auto i = *it;
|
|
|
|
if ( i->getAccess() == AS_protected ) {
|
|
|
|
bFoundProtected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 18:21:37 +02:00
|
|
|
if (!bFoundProtected && !bFoundVirtual)
|
2017-10-03 09:17:35 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
std::string s = decl->getQualifiedNameAsString();
|
|
|
|
if (ignoreClass(s))
|
|
|
|
return true;
|
|
|
|
|
2018-08-10 12:35:21 +02:00
|
|
|
SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(decl));
|
2020-02-13 08:30:43 +01:00
|
|
|
auto const filename = getFilenameOfLocation(spellingLocation);
|
|
|
|
auto sourceLocation = filename.substr(strlen(SRCDIR)).str() + ":"
|
2017-10-03 09:17:35 +02:00
|
|
|
+ std::to_string(compiler.getSourceManager().getSpellingLineNumber(spellingLocation));
|
|
|
|
definitionMap.insert( std::pair<std::string,std::string>(s, sourceLocation) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-05 07:50:24 +02:00
|
|
|
void FinalClasses::checkBase(QualType baseType)
|
|
|
|
{
|
|
|
|
// need to look through typedefs, hence the getUnqualifiedDesugaredType
|
|
|
|
baseType = baseType.getDesugaredType(compiler.getASTContext());
|
|
|
|
std::string x;
|
|
|
|
// so that we get just the template name, excluding the template parameters
|
|
|
|
if (baseType->isRecordType())
|
|
|
|
x = baseType->getAsCXXRecordDecl()->getQualifiedNameAsString();
|
|
|
|
else if (auto templateType = baseType->getAs<TemplateSpecializationType>())
|
|
|
|
x = templateType->getTemplateName().getAsTemplateDecl()->getQualifiedNameAsString();
|
|
|
|
else
|
|
|
|
x = baseType.getAsString();
|
|
|
|
inheritedFromSet.insert( x );
|
|
|
|
}
|
|
|
|
|
2017-10-03 09:17:35 +02:00
|
|
|
loplugin::Plugin::Registration< FinalClasses > X("finalclasses", false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|