cURL: follow redirects
[LibreOffice.git] / compilerplugins / clang / unusedmethodsremove.cxx
blob09187edfc7c054af00b0f39bffb85181022350f5
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 "unusedmethods" clang plugin.
27 namespace {
29 class UnusedMethodsRemove:
30 public RecursiveASTVisitor<UnusedMethodsRemove>, public loplugin::RewritePlugin
32 public:
33 explicit UnusedMethodsRemove(InstantiationData const & data);
34 ~UnusedMethodsRemove();
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 size_t getFilesize(const char* filename)
49 struct stat st;
50 stat(filename, &st);
51 return st.st_size;
54 UnusedMethodsRemove::UnusedMethodsRemove(InstantiationData const & data): RewritePlugin(data)
56 static const char sInputFile[] = SRCDIR "/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, mmapFD, 0));
63 assert(mmappedData != NULL);
66 UnusedMethodsRemove::~UnusedMethodsRemove()
68 //Cleanup
69 int rc = munmap(mmappedData, mmapFilesize);
70 assert(rc == 0);
71 close(mmapFD);
74 std::string niceName(const CXXMethodDecl* functionDecl)
76 std::string s =
77 compat::getReturnType(*functionDecl).getCanonicalType().getAsString()
78 + " " + functionDecl->getParent()->getQualifiedNameAsString()
79 + "::" + functionDecl->getNameAsString()
80 + "(";
81 bool bFirst = true;
82 for (const ParmVarDecl *pParmVarDecl : compat::parameters(*functionDecl)) {
83 if (bFirst)
84 bFirst = false;
85 else
86 s += ",";
87 s += pParmVarDecl->getType().getCanonicalType().getAsString();
89 s += ")";
90 if (functionDecl->isConst()) {
91 s += " const";
93 return s;
96 bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl )
98 if (rewriter == nullptr) {
99 return true;
101 if (ignoreLocation(functionDecl)) {
102 return true;
104 // ignore stuff that forms part of the stable URE interface
105 if (isInUnoIncludeFile(functionDecl)) {
106 return true;
109 // don't mess with templates
110 if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) {
111 return true;
113 if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) {
114 return true;
117 std::string aNiceName = "\n" + niceName(functionDecl) + "\n";
118 const char *aNiceNameStr = aNiceName.c_str();
119 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
120 if(!(found < mmappedData + mmapFilesize)) {
121 return true;
123 SourceRange replaceRange(functionDecl->getSourceRange());
124 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
125 if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") {
126 replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1));
128 // remove leading spaces
129 while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ")
131 replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1));
133 if (!replaceText(replaceRange, "")) {
134 report(
135 DiagnosticsEngine::Warning,
136 "Could not remove unused method (" + niceName(functionDecl) + ")",
137 functionDecl->getLocStart())
138 << functionDecl->getSourceRange();
140 return true;
144 loplugin::Plugin::Registration< UnusedMethodsRemove > X("unusedmethodsremove", false);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */