summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2015-01-20 12:38:10 +0200
committerNoel Grandin <noel@peralex.com>2015-01-26 08:42:28 +0200
commitb44cbb26efe1d0b0950b1e1613e131b506dc3876 (patch)
tree9b4d5d99e5dad0971079b997a02a6d96536709ca /compilerplugins
parent26ad60aec69310fecd918f1c2e09056aa4782320 (diff)
new loplugin: change virtual methods to non-virtual
Where we can prove that the virtual method is never overriden. In the case of pure-virtual methods, we remove the method entirely. Sometimes this leads to entire methods and fields being eliminated. Change-Id: I138ef81c95f115dbd8c023a83cfc7e9d5d6d14ae
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/removevirtuals.cxx141
-rw-r--r--compilerplugins/clang/unnecessaryvirtual.cxx119
2 files changed, 260 insertions, 0 deletions
diff --git a/compilerplugins/clang/removevirtuals.cxx b/compilerplugins/clang/removevirtuals.cxx
new file mode 100644
index 000000000000..c2bf4841d30d
--- /dev/null
+++ b/compilerplugins/clang/removevirtuals.cxx
@@ -0,0 +1,141 @@
+/* -*- 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 <string>
+#include <iostream>
+#include "plugin.hxx"
+#include "compat.hxx"
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <assert.h>
+#include <cstring>
+
+/**
+ This is intended to be run as the second stage of the "unnecessaryvirtuals" clang plugin.
+*/
+
+namespace {
+
+class RemoveVirtuals:
+ public RecursiveASTVisitor<RemoveVirtuals>, public loplugin::RewritePlugin
+{
+public:
+ explicit RemoveVirtuals(InstantiationData const & data);
+ ~RemoveVirtuals();
+
+ virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
+
+ bool VisitCXXMethodDecl( const CXXMethodDecl* var );
+private:
+ // I use a brute-force approach - mmap the results file and do a linear search on it
+ // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
+ size_t mmapFilesize;
+ int mmapFD;
+ char* mmappedData;
+};
+
+static size_t getFilesize(const char* filename)
+{
+ struct stat st;
+ stat(filename, &st);
+ return st.st_size;
+}
+
+RemoveVirtuals::RemoveVirtuals(InstantiationData const & data): RewritePlugin(data)
+{
+ static const char sInputFile[] = "/home/noel/libo4/result.txt";
+ mmapFilesize = getFilesize(sInputFile);
+ //Open file
+ mmapFD = open(sInputFile, O_RDONLY, 0);
+ assert(mmapFD != -1);
+ //Execute mmap
+ mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE | MAP_POPULATE, mmapFD, 0));
+ assert(mmappedData != NULL);
+}
+
+RemoveVirtuals::~RemoveVirtuals()
+{
+ //Cleanup
+ int rc = munmap(mmappedData, mmapFilesize);
+ assert(rc == 0);
+ close(mmapFD);
+}
+
+static std::string niceName(const CXXMethodDecl* functionDecl)
+{
+ std::string s =
+ functionDecl->getParent()->getQualifiedNameAsString() + "::"
+ + compat::getReturnType(*functionDecl).getAsString() + "-"
+ + functionDecl->getNameAsString() + "(";
+ for (const ParmVarDecl *pParmVarDecl : functionDecl->params()) {
+ s += pParmVarDecl->getType().getAsString();
+ s += ",";
+ }
+ s += ")";
+ if (functionDecl->isConst()) {
+ s += "const";
+ }
+ return s;
+}
+
+bool RemoveVirtuals::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
+{
+ if (rewriter == nullptr) {
+ return true;
+ }
+ if (ignoreLocation(functionDecl)) {
+ return true;
+ }
+ // ignore stuff that forms part of the stable URE interface
+ if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
+ functionDecl->getCanonicalDecl()->getNameInfo().getLoc()))) {
+ return true;
+ }
+
+ // don't mess with templates
+ if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) {
+ return true;
+ }
+ if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) {
+ return true;
+ }
+
+ if (!functionDecl->isVirtualAsWritten()) {
+ return true;
+ }
+ std::string aNiceName = "\n" + niceName(functionDecl) + "\n";
+ const char *aNiceNameStr = aNiceName.c_str();
+ char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
+ if(!(found < mmappedData + mmapFilesize)) {
+ return true;
+ }
+ if (functionDecl->isPure()) {
+ removeText(functionDecl->getSourceRange());
+ } else {
+ std::string aOrigText = rewriter->getRewrittenText(functionDecl->getSourceRange());
+ size_t iVirtualTokenIndex = aOrigText.find_first_of("virtual ");
+ if (iVirtualTokenIndex == std::string::npos) {
+ return true;
+ }
+ replaceText(functionDecl->getSourceRange(), aOrigText.replace(iVirtualTokenIndex, strlen("virtual "), ""));
+ }
+ return true;
+}
+
+
+
+loplugin::Plugin::Registration< RemoveVirtuals > X("removevirtuals", false);
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/unnecessaryvirtual.cxx b/compilerplugins/clang/unnecessaryvirtual.cxx
new file mode 100644
index 000000000000..0ead077473de
--- /dev/null
+++ b/compilerplugins/clang/unnecessaryvirtual.cxx
@@ -0,0 +1,119 @@
+/* -*- 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 <string>
+#include <iostream>
+#include "plugin.hxx"
+#include "compat.hxx"
+
+/**
+Dump a list of virtual methods and a list of methods overriding virtual methods.
+Then we will post-process the 2 lists and find the set of virtual methods which don't need to be virtual.
+
+The process goes something like this:
+ $ make check
+ $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='unnecessaryvirtual' check > log.txt
+ $ grep 'definition' log.txt | cut -f 2 | sort -u > definition.txt
+ $ grep 'overriding' log.txt | cut -f 2 | sort -u > overriding.txt
+ $ cat definition.txt overriding.txt | sort | uniq -u > result.txt
+ $ echo "\n" >> result.txt
+ $ for dir in *; do make FORCE_COMPILE_ALL=1 UPDATE_FILES=$dir COMPILER_PLUGIN_TOOL='removevirtuals' $dir; done
+
+Note that the actual process may involve a fair amount of undoing, hand editing, and general messing around
+to get it to work :-)
+Notably templates tend to confuse it into removing stuff that is still needed.
+*/
+
+namespace {
+
+class UnnecessaryVirtual:
+ public RecursiveASTVisitor<UnnecessaryVirtual>, public loplugin::Plugin
+{
+public:
+ explicit UnnecessaryVirtual(InstantiationData const & data): Plugin(data) {}
+
+ virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
+
+ bool VisitCXXMethodDecl( const CXXMethodDecl* var );
+
+};
+
+static std::string niceName(const CXXMethodDecl* functionDecl)
+{
+ std::string s =
+ functionDecl->getParent()->getQualifiedNameAsString() + "::"
+ + compat::getReturnType(*functionDecl).getAsString() + "-"
+ + functionDecl->getNameAsString() + "(";
+ for (const ParmVarDecl *pParmVarDecl : functionDecl->params()) {
+ s += pParmVarDecl->getType().getAsString();
+ s += ",";
+ }
+ s += ")";
+ if (functionDecl->isConst()) {
+ s += "const";
+ }
+ return s;
+}
+
+bool UnnecessaryVirtual::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
+{
+ if (ignoreLocation(functionDecl)) {
+ return true;
+ }
+ functionDecl = functionDecl->getCanonicalDecl();
+ // ignore stuff that forms part of the stable URE interface
+ if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
+ functionDecl->getNameInfo().getLoc()))) {
+ return true;
+ }
+ if (!functionDecl->isVirtual()) {
+ return true;
+ }
+ // ignore UNO interface definitions, cannot change those
+ static const char cssPrefix[] = "com::sun::star";
+ if (functionDecl->getParent()->getQualifiedNameAsString().compare(0, strlen(cssPrefix), cssPrefix) == 0) {
+ return true;
+ }
+ std::string aNiceName = niceName(functionDecl);
+ // Ignore virtual destructors for now.
+ // I cannot currently detect the case where we are overriding a pure virtual destructor.
+ if (dyn_cast<CXXDestructorDecl>(functionDecl)) {
+ return true;
+ }
+ if (functionDecl->size_overridden_methods() == 0) {
+ // ignore definition of virtual functions in templates
+// if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate
+// && functionDecl->getParent()->getDescribedClassTemplate() == nullptr)
+// {
+ cout << "definition\t" << aNiceName << endl;
+// }
+ } else {
+ for (CXXMethodDecl::method_iterator iter = functionDecl->begin_overridden_methods(); iter != functionDecl->end_overridden_methods(); ++iter) {
+ const CXXMethodDecl *pOverriddenMethod = *iter;
+ // we only care about the first level override to establish that a virtual qualifier was useful.
+ if (pOverriddenMethod->size_overridden_methods() == 0) {
+ // ignore UNO interface definitions, cannot change those
+ if (pOverriddenMethod->getParent()->getQualifiedNameAsString().compare(0, strlen(cssPrefix), cssPrefix) != 0) {
+ std::string aOverriddenNiceName = niceName(pOverriddenMethod);
+ cout << "overriding\t" << aOverriddenNiceName << endl;
+ }
+ }
+ }
+ }
+ return true;
+}
+
+
+
+loplugin::Plugin::Registration< UnnecessaryVirtual > X("unnecessaryvirtual", false);
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */