bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / unusedfieldsremove.cxx
blobb8c339edcbcd32c44ed80aaf9faecde8dbac2d29
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 <sys/mman.h>
17 #include <sys/types.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <assert.h>
22 #include <cstring>
24 /**
25 This is intended to be run as the second stage of the "unusedfields" clang plugin.
28 namespace {
30 class UnusedFieldsRemove:
31 public loplugin::FilteringRewritePlugin<UnusedFieldsRemove>
33 public:
34 explicit UnusedFieldsRemove(loplugin::InstantiationData const & data);
35 ~UnusedFieldsRemove();
37 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
39 bool VisitFieldDecl( const FieldDecl* var );
40 private:
41 // I use a brute-force approach - mmap the results file and do a linear search on it
42 // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
43 size_t mmapFilesize;
44 int mmapFD;
45 char* mmappedData;
48 size_t getFilesize(const char* filename)
50 struct stat st;
51 stat(filename, &st);
52 return st.st_size;
55 UnusedFieldsRemove::UnusedFieldsRemove(loplugin::InstantiationData const & data): FilteringRewritePlugin(data)
57 static const char sInputFile[] = SRCDIR "/result.txt";
58 mmapFilesize = getFilesize(sInputFile);
59 //Open file
60 mmapFD = open(sInputFile, O_RDONLY, 0);
61 assert(mmapFD != -1);
62 //Execute mmap
63 mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0));
64 assert(mmappedData != NULL);
67 UnusedFieldsRemove::~UnusedFieldsRemove()
69 //Cleanup
70 int rc = munmap(mmappedData, mmapFilesize);
71 assert(rc == 0);
72 (void)rc;
73 close(mmapFD);
76 std::string niceName(const FieldDecl* fieldDecl)
78 return fieldDecl->getParent()->getQualifiedNameAsString() + " " +
79 fieldDecl->getNameAsString();
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 compat::getBeginLoc(fieldDecl))
124 << fieldDecl->getSourceRange();
126 return true;
130 loplugin::Plugin::Registration< UnusedFieldsRemove > X("unusedfieldsremove", false);
134 #endif
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */