bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / unusedmethodsremove.cxx
blob417c3778edf0efb00f3ae6a6407c4d3eaa919763
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 loplugin::FilteringRewritePlugin<UnusedMethodsRemove>
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): FilteringRewritePlugin(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 (void)rc;
74 close(mmapFD);
77 std::string niceName(const CXXMethodDecl* functionDecl)
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();
92 s += ")";
93 if (functionDecl->isConst()) {
94 s += " const";
96 return s;
99 bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
101 if (rewriter == nullptr) {
102 return true;
104 if (ignoreLocation(functionDecl)) {
105 return true;
107 // ignore stuff that forms part of the stable URE interface
108 if (isInUnoIncludeFile(functionDecl)) {
109 return true;
112 // don't mess with templates
113 if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) {
114 return true;
116 if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) {
117 return true;
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;
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));
131 // remove leading spaces
132 while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ")
134 replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1));
136 if (!replaceText(replaceRange, "")) {
137 report(
138 DiagnosticsEngine::Warning,
139 "Could not remove unused method (" + niceName(functionDecl) + ")",
140 compat::getBeginLoc(functionDecl))
141 << functionDecl->getSourceRange();
143 return true;
147 loplugin::Plugin::Registration< UnusedMethodsRemove > X("unusedmethodsremove", false);
151 #endif
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */