bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / unusedfieldsremove.cxx
blob62c5fc7bb271f4a7c5ea5a70d4aa19ff1a53eae1
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 close(mmapFD);
75 std::string niceName(const FieldDecl* fieldDecl)
77 return fieldDecl->getParent()->getQualifiedNameAsString() + " " +
78 fieldDecl->getNameAsString();
81 bool UnusedFieldsRemove::VisitFieldDecl( const FieldDecl* fieldDecl )
83 if (rewriter == nullptr) {
84 return true;
86 if (ignoreLocation(fieldDecl)) {
87 return true;
89 // ignore stuff that forms part of the stable URE interface
90 if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
91 fieldDecl->getCanonicalDecl()->getLocation()))) {
92 return true;
95 // don't mess with templates
96 /* if (isa<CXXRecordDecl>(fieldDecl->getParent())) {
97 if (dyn_cast<CXXRecordDecl>(fieldDecl->getParent())->getDescribedClassTemplate() != nullptr) {
98 return true;
102 std::string aNiceName = " " + niceName(fieldDecl) + "\n";
103 const char *aNiceNameStr = aNiceName.c_str();
104 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
105 if(!(found < mmappedData + mmapFilesize)) {
106 return true;
108 SourceRange replaceRange(fieldDecl->getSourceRange());
109 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
110 if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") {
111 replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1));
113 // remove leading spaces
114 while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ")
116 replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1));
118 if (!replaceText(replaceRange, "")) {
119 report(
120 DiagnosticsEngine::Warning,
121 "Could not remove unused field (" + niceName(fieldDecl) + ")",
122 compat::getBeginLoc(fieldDecl))
123 << fieldDecl->getSourceRange();
125 return true;
129 loplugin::Plugin::Registration< UnusedFieldsRemove > X("unusedfieldsremove", false);
133 #endif
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */