1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #if !defined _WIN32 //TODO, #include <sys/mman.h>
15 #include "config_clang.h"
18 #include <sys/types.h>
26 This is intended to be run as the second stage of the "unusedfields" clang plugin.
31 class UnusedFieldsRemove
:
32 public loplugin::FilteringRewritePlugin
<UnusedFieldsRemove
>
35 explicit UnusedFieldsRemove(loplugin::InstantiationData
const & data
);
36 ~UnusedFieldsRemove();
38 virtual void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
40 bool VisitFieldDecl( const FieldDecl
* var
);
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
49 size_t getFilesize(const char* filename
)
56 UnusedFieldsRemove::UnusedFieldsRemove(loplugin::InstantiationData
const & data
): FilteringRewritePlugin(data
)
58 static const char sInputFile
[] = SRCDIR
"/result.txt";
59 mmapFilesize
= getFilesize(sInputFile
);
61 mmapFD
= open(sInputFile
, O_RDONLY
, 0);
64 mmappedData
= static_cast<char*>(mmap(NULL
, mmapFilesize
, PROT_READ
, MAP_PRIVATE
, mmapFD
, 0));
65 assert(mmappedData
!= NULL
);
68 UnusedFieldsRemove::~UnusedFieldsRemove()
71 int rc
= munmap(mmappedData
, mmapFilesize
);
77 std::string
niceName(const FieldDecl
* fieldDecl
)
79 return fieldDecl
->getParent()->getQualifiedNameAsString() + " " +
80 fieldDecl
->getNameAsString();
83 bool UnusedFieldsRemove::VisitFieldDecl( const FieldDecl
* fieldDecl
)
85 if (rewriter
== nullptr) {
88 if (ignoreLocation(fieldDecl
)) {
91 // ignore stuff that forms part of the stable URE interface
92 if (isInUnoIncludeFile(compiler
.getSourceManager().getSpellingLoc(
93 fieldDecl
->getCanonicalDecl()->getLocation()))) {
97 // don't mess with templates
98 /* if (isa<CXXRecordDecl>(fieldDecl->getParent())) {
99 if (dyn_cast<CXXRecordDecl>(fieldDecl->getParent())->getDescribedClassTemplate() != nullptr) {
104 std::string aNiceName
= " " + niceName(fieldDecl
) + "\n";
105 const char *aNiceNameStr
= aNiceName
.c_str();
106 char* found
= std::search(mmappedData
, mmappedData
+ mmapFilesize
, aNiceNameStr
, aNiceNameStr
+ strlen(aNiceNameStr
));
107 if(!(found
< mmappedData
+ mmapFilesize
)) {
110 SourceRange
replaceRange(fieldDecl
->getSourceRange());
111 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
112 if (rewriter
->getRewrittenText(SourceRange(replaceRange
.getEnd(), replaceRange
.getEnd().getLocWithOffset(1))) == ";") {
113 replaceRange
.setEnd(replaceRange
.getEnd().getLocWithOffset(1));
115 // remove leading spaces
116 while (rewriter
->getRewrittenText(SourceRange(replaceRange
.getBegin().getLocWithOffset(-1), replaceRange
.getBegin())) == " ")
118 replaceRange
.setBegin(replaceRange
.getBegin().getLocWithOffset(-1));
120 if (!replaceText(replaceRange
, "")) {
122 DiagnosticsEngine::Warning
,
123 "Could not remove unused field (" + niceName(fieldDecl
) + ")",
124 fieldDecl
->getBeginLoc())
125 << fieldDecl
->getSourceRange();
131 loplugin::Plugin::Registration
< UnusedFieldsRemove
> X("unusedfieldsremove", false);
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */