cURL: follow redirects
[LibreOffice.git] / compilerplugins / clang / unusedfieldsremove.cxx
blobafe937df0ecd0152ee1761edc18950b5130cd998
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 <sys/mman.h>
15 #include <sys/types.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include <assert.h>
20 #include <cstring>
22 /**
23 This is intended to be run as the second stage of the "unusedfields" clang plugin.
26 namespace {
28 class UnusedFieldsRemove:
29 public RecursiveASTVisitor<UnusedFieldsRemove>, public loplugin::RewritePlugin
31 public:
32 explicit UnusedFieldsRemove(InstantiationData const & data);
33 ~UnusedFieldsRemove();
35 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
37 bool VisitFieldDecl( const FieldDecl* var );
38 private:
39 // I use a brute-force approach - mmap the results file and do a linear search on it
40 // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
41 size_t mmapFilesize;
42 int mmapFD;
43 char* mmappedData;
46 size_t getFilesize(const char* filename)
48 struct stat st;
49 stat(filename, &st);
50 return st.st_size;
53 UnusedFieldsRemove::UnusedFieldsRemove(InstantiationData const & data): RewritePlugin(data)
55 static const char sInputFile[] = SRCDIR "/result.txt";
56 mmapFilesize = getFilesize(sInputFile);
57 //Open file
58 mmapFD = open(sInputFile, O_RDONLY, 0);
59 assert(mmapFD != -1);
60 //Execute mmap
61 mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0));
62 assert(mmappedData != NULL);
65 UnusedFieldsRemove::~UnusedFieldsRemove()
67 //Cleanup
68 int rc = munmap(mmappedData, mmapFilesize);
69 assert(rc == 0);
70 close(mmapFD);
73 std::string niceName(const FieldDecl* fieldDecl)
75 std::string s = fieldDecl->getParent()->getQualifiedNameAsString() + " " +
76 fieldDecl->getNameAsString();
77 if (s.find("m_xExternalProgress") != std::string::npos)
78 cout << s << endl;
79 return s;
82 bool UnusedFieldsRemove::VisitFieldDecl( const FieldDecl* fieldDecl )
84 if (rewriter == nullptr) {
85 return true;
87 if (ignoreLocation(fieldDecl)) {
88 return true;
90 // ignore stuff that forms part of the stable URE interface
91 if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
92 fieldDecl->getCanonicalDecl()->getLocation()))) {
93 return true;
96 // don't mess with templates
97 /* if (isa<CXXRecordDecl>(fieldDecl->getParent())) {
98 if (dyn_cast<CXXRecordDecl>(fieldDecl->getParent())->getDescribedClassTemplate() != nullptr) {
99 return true;
103 std::string aNiceName = " " + niceName(fieldDecl) + "\n";
104 const char *aNiceNameStr = aNiceName.c_str();
105 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
106 if(!(found < mmappedData + mmapFilesize)) {
107 return true;
109 SourceRange replaceRange(fieldDecl->getSourceRange());
110 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
111 if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") {
112 replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1));
114 // remove leading spaces
115 while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ")
117 replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1));
119 if (!replaceText(replaceRange, "")) {
120 report(
121 DiagnosticsEngine::Warning,
122 "Could not remove unused field (" + niceName(fieldDecl) + ")",
123 fieldDecl->getLocStart())
124 << fieldDecl->getSourceRange();
126 return true;
130 loplugin::Plugin::Registration< UnusedFieldsRemove > X("unusedfieldsremove", false);
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */