1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10 #if !defined _WIN32 //TODO, #include <sys/mman.h>
11
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>
24
25 /**
26 This is intended to be run as the second stage of the "unusedmethods" clang plugin.
27 */
28
29 namespace {
30
31 class UnusedMethodsRemove:
32 public loplugin::FilteringRewritePlugin<UnusedMethodsRemove>
33 {
34 public:
35 explicit UnusedMethodsRemove(loplugin::InstantiationData const & data);
36 ~UnusedMethodsRemove();
37
run()38 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
39
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;
47 };
48
getFilesize(const char * filename)49 size_t getFilesize(const char* filename)
50 {
51 struct stat st;
52 stat(filename, &st);
53 return st.st_size;
54 }
55
UnusedMethodsRemove(loplugin::InstantiationData const & data)56 UnusedMethodsRemove::UnusedMethodsRemove(loplugin::InstantiationData const & data): FilteringRewritePlugin(data)
57 {
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);
66 }
67
~UnusedMethodsRemove()68 UnusedMethodsRemove::~UnusedMethodsRemove()
69 {
70 //Cleanup
71 int rc = munmap(mmappedData, mmapFilesize);
72 assert(rc == 0);
73 (void)rc;
74 close(mmapFD);
75 }
76
niceName(const CXXMethodDecl * functionDecl)77 std::string niceName(const CXXMethodDecl* functionDecl)
78 {
79 std::string s =
80 functionDecl->getReturnType().getCanonicalType().getAsString()
81 + " " + functionDecl->getParent()->getQualifiedNameAsString()
82 + "::" + functionDecl->getNameAsString()
83 + "(";
84 bool bFirst = true;
85 for (const ParmVarDecl *pParmVarDecl : functionDecl->parameters()) {
86 if (bFirst)
87 bFirst = false;
88 else
89 s += ",";
90 s += pParmVarDecl->getType().getCanonicalType().getAsString();
91 }
92 s += ")";
93 if (functionDecl->isConst()) {
94 s += " const";
95 }
96 return s;
97 }
98
VisitCXXMethodDecl(const CXXMethodDecl * functionDecl)99 bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
100 {
101 if (rewriter == nullptr) {
102 return true;
103 }
104 if (ignoreLocation(functionDecl)) {
105 return true;
106 }
107 // ignore stuff that forms part of the stable URE interface
108 if (isInUnoIncludeFile(functionDecl)) {
109 return true;
110 }
111
112 // don't mess with templates
113 if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) {
114 return true;
115 }
116 if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) {
117 return true;
118 }
119
120 std::string aNiceName = "\n" + niceName(functionDecl) + "\n";
121 const char *aNiceNameStr = aNiceName.c_str();
122 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
123 if(!(found < mmappedData + mmapFilesize)) {
124 return true;
125 }
126 SourceRange replaceRange(functionDecl->getSourceRange());
127 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
128 if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") {
129 replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1));
130 }
131 // remove leading spaces
132 while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ")
133 {
134 replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1));
135 }
136 if (!replaceText(replaceRange, "")) {
137 report(
138 DiagnosticsEngine::Warning,
139 "Could not remove unused method (" + niceName(functionDecl) + ")",
140 compat::getBeginLoc(functionDecl))
141 << functionDecl->getSourceRange();
142 }
143 return true;
144 }
145
146
147 loplugin::Plugin::Registration< UnusedMethodsRemove > X("unusedmethodsremove", false);
148
149 }
150
151 #endif
152
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
154