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>
17 #include <sys/types.h>
25 This is intended to be run as the second stage of the "unusedfields" clang plugin.
30 class UnusedFieldsRemove
:
31 public loplugin::FilteringRewritePlugin
<UnusedFieldsRemove
>
34 explicit UnusedFieldsRemove(loplugin::InstantiationData
const & data
);
35 ~UnusedFieldsRemove();
37 virtual void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
39 bool VisitFieldDecl( const FieldDecl
* var
);
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
48 size_t getFilesize(const char* filename
)
55 UnusedFieldsRemove::UnusedFieldsRemove(loplugin::InstantiationData
const & data
): FilteringRewritePlugin(data
)
57 static const char sInputFile
[] = SRCDIR
"/result.txt";
58 mmapFilesize
= getFilesize(sInputFile
);
60 mmapFD
= open(sInputFile
, O_RDONLY
, 0);
63 mmappedData
= static_cast<char*>(mmap(NULL
, mmapFilesize
, PROT_READ
, MAP_PRIVATE
, mmapFD
, 0));
64 assert(mmappedData
!= NULL
);
67 UnusedFieldsRemove::~UnusedFieldsRemove()
70 int rc
= munmap(mmappedData
, mmapFilesize
);
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) {
87 if (ignoreLocation(fieldDecl
)) {
90 // ignore stuff that forms part of the stable URE interface
91 if (isInUnoIncludeFile(compiler
.getSourceManager().getSpellingLoc(
92 fieldDecl
->getCanonicalDecl()->getLocation()))) {
96 // don't mess with templates
97 /* if (isa<CXXRecordDecl>(fieldDecl->getParent())) {
98 if (dyn_cast<CXXRecordDecl>(fieldDecl->getParent())->getDescribedClassTemplate() != nullptr) {
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
)) {
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
, "")) {
121 DiagnosticsEngine::Warning
,
122 "Could not remove unused field (" + niceName(fieldDecl
) + ")",
123 compat::getBeginLoc(fieldDecl
))
124 << fieldDecl
->getSourceRange();
130 loplugin::Plugin::Registration
< UnusedFieldsRemove
> X("unusedfieldsremove", false);
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */