Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / unusedmethodsremove.cxx
blob33c7ee7a1fecffe533f36a0d27f778dfd00e79b1
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 #if !defined _WIN32 //TODO, #include <sys/mman.h>
12 #include <cassert>
13 #include <string>
14 #include <iostream>
15 #include "plugin.hxx"
16 #include "compat.hxx"
17 #include <sys/mman.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <assert.h>
23 #include <cstring>
25 /**
26 This is intended to be run as the second stage of the "unusedmethods" clang plugin.
29 namespace {
31 class UnusedMethodsRemove:
32 public RecursiveASTVisitor<UnusedMethodsRemove>, public loplugin::RewritePlugin
34 public:
35 explicit UnusedMethodsRemove(loplugin::InstantiationData const & data);
36 ~UnusedMethodsRemove();
38 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
40 bool VisitCXXMethodDecl( const CXXMethodDecl* var );
41 private:
42 // I use a brute-force approach - mmap the results file and do a linear search on it
43 // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
44 size_t mmapFilesize;
45 int mmapFD;
46 char* mmappedData;
49 size_t getFilesize(const char* filename)
51 struct stat st;
52 stat(filename, &st);
53 return st.st_size;
56 UnusedMethodsRemove::UnusedMethodsRemove(loplugin::InstantiationData const & data): RewritePlugin(data)
58 static const char sInputFile[] = SRCDIR "/result.txt";
59 mmapFilesize = getFilesize(sInputFile);
60 //Open file
61 mmapFD = open(sInputFile, O_RDONLY, 0);
62 assert(mmapFD != -1);
63 //Execute mmap
64 mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0));
65 assert(mmappedData != NULL);
68 UnusedMethodsRemove::~UnusedMethodsRemove()
70 //Cleanup
71 int rc = munmap(mmappedData, mmapFilesize);
72 assert(rc == 0);
73 close(mmapFD);
76 std::string niceName(const CXXMethodDecl* functionDecl)
78 std::string s =
79 functionDecl->getReturnType().getCanonicalType().getAsString()
80 + " " + functionDecl->getParent()->getQualifiedNameAsString()
81 + "::" + functionDecl->getNameAsString()
82 + "(";
83 bool bFirst = true;
84 for (const ParmVarDecl *pParmVarDecl : compat::parameters(*functionDecl)) {
85 if (bFirst)
86 bFirst = false;
87 else
88 s += ",";
89 s += pParmVarDecl->getType().getCanonicalType().getAsString();
91 s += ")";
92 if (functionDecl->isConst()) {
93 s += " const";
95 return s;
98 bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
100 if (rewriter == nullptr) {
101 return true;
103 if (ignoreLocation(functionDecl)) {
104 return true;
106 // ignore stuff that forms part of the stable URE interface
107 if (isInUnoIncludeFile(functionDecl)) {
108 return true;
111 // don't mess with templates
112 if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) {
113 return true;
115 if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) {
116 return true;
119 std::string aNiceName = "\n" + niceName(functionDecl) + "\n";
120 const char *aNiceNameStr = aNiceName.c_str();
121 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
122 if(!(found < mmappedData + mmapFilesize)) {
123 return true;
125 SourceRange replaceRange(functionDecl->getSourceRange());
126 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
127 if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") {
128 replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1));
130 // remove leading spaces
131 while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ")
133 replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1));
135 if (!replaceText(replaceRange, "")) {
136 report(
137 DiagnosticsEngine::Warning,
138 "Could not remove unused method (" + niceName(functionDecl) + ")",
139 functionDecl->getLocStart())
140 << functionDecl->getSourceRange();
142 return true;
146 loplugin::Plugin::Registration< UnusedMethodsRemove > X("unusedmethodsremove", false);
150 #endif
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */