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
);
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) {
86 if (ignoreLocation(fieldDecl
)) {
89 // ignore stuff that forms part of the stable URE interface
90 if (isInUnoIncludeFile(compiler
.getSourceManager().getSpellingLoc(
91 fieldDecl
->getCanonicalDecl()->getLocation()))) {
95 // don't mess with templates
96 /* if (isa<CXXRecordDecl>(fieldDecl->getParent())) {
97 if (dyn_cast<CXXRecordDecl>(fieldDecl->getParent())->getDescribedClassTemplate() != nullptr) {
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
)) {
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
, "")) {
120 DiagnosticsEngine::Warning
,
121 "Could not remove unused field (" + niceName(fieldDecl
) + ")",
122 compat::getBeginLoc(fieldDecl
))
123 << fieldDecl
->getSourceRange();
129 loplugin::Plugin::Registration
< UnusedFieldsRemove
> X("unusedfieldsremove", false);
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */