bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / store / removevirtuals.cxx
blobc2bf4841d30d0faf43e9f430b7f3afa230a17146
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 <string>
12 #include <iostream>
13 #include "plugin.hxx"
14 #include "compat.hxx"
15 #include <sys/mman.h>
16 #include <sys/types.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <assert.h>
21 #include <cstring>
23 /**
24 This is intended to be run as the second stage of the "unnecessaryvirtuals" clang plugin.
27 namespace {
29 class RemoveVirtuals:
30 public RecursiveASTVisitor<RemoveVirtuals>, public loplugin::RewritePlugin
32 public:
33 explicit RemoveVirtuals(InstantiationData const & data);
34 ~RemoveVirtuals();
36 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
38 bool VisitCXXMethodDecl( const CXXMethodDecl* var );
39 private:
40 // I use a brute-force approach - mmap the results file and do a linear search on it
41 // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
42 size_t mmapFilesize;
43 int mmapFD;
44 char* mmappedData;
47 static size_t getFilesize(const char* filename)
49 struct stat st;
50 stat(filename, &st);
51 return st.st_size;
54 RemoveVirtuals::RemoveVirtuals(InstantiationData const & data): RewritePlugin(data)
56 static const char sInputFile[] = "/home/noel/libo4/result.txt";
57 mmapFilesize = getFilesize(sInputFile);
58 //Open file
59 mmapFD = open(sInputFile, O_RDONLY, 0);
60 assert(mmapFD != -1);
61 //Execute mmap
62 mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE | MAP_POPULATE, mmapFD, 0));
63 assert(mmappedData != NULL);
66 RemoveVirtuals::~RemoveVirtuals()
68 //Cleanup
69 int rc = munmap(mmappedData, mmapFilesize);
70 assert(rc == 0);
71 close(mmapFD);
74 static std::string niceName(const CXXMethodDecl* functionDecl)
76 std::string s =
77 functionDecl->getParent()->getQualifiedNameAsString() + "::"
78 + compat::getReturnType(*functionDecl).getAsString() + "-"
79 + functionDecl->getNameAsString() + "(";
80 for (const ParmVarDecl *pParmVarDecl : functionDecl->params()) {
81 s += pParmVarDecl->getType().getAsString();
82 s += ",";
84 s += ")";
85 if (functionDecl->isConst()) {
86 s += "const";
88 return s;
91 bool RemoveVirtuals::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
93 if (rewriter == nullptr) {
94 return true;
96 if (ignoreLocation(functionDecl)) {
97 return true;
99 // ignore stuff that forms part of the stable URE interface
100 if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
101 functionDecl->getCanonicalDecl()->getNameInfo().getLoc()))) {
102 return true;
105 // don't mess with templates
106 if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) {
107 return true;
109 if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) {
110 return true;
113 if (!functionDecl->isVirtualAsWritten()) {
114 return true;
116 std::string aNiceName = "\n" + niceName(functionDecl) + "\n";
117 const char *aNiceNameStr = aNiceName.c_str();
118 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
119 if(!(found < mmappedData + mmapFilesize)) {
120 return true;
122 if (functionDecl->isPure()) {
123 removeText(functionDecl->getSourceRange());
124 } else {
125 std::string aOrigText = rewriter->getRewrittenText(functionDecl->getSourceRange());
126 size_t iVirtualTokenIndex = aOrigText.find_first_of("virtual ");
127 if (iVirtualTokenIndex == std::string::npos) {
128 return true;
130 replaceText(functionDecl->getSourceRange(), aOrigText.replace(iVirtualTokenIndex, strlen("virtual "), ""));
132 return true;
137 loplugin::Plugin::Registration< RemoveVirtuals > X("removevirtuals", false);
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */