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>
18 #include <sys/types.h>
26 This is intended to be run as the second stage of the "unusedmethods" clang plugin.
31 class UnusedMethodsRemove
:
32 public loplugin::FilteringRewritePlugin
<UnusedMethodsRemove
>
35 explicit UnusedMethodsRemove(loplugin::InstantiationData
const & data
);
36 ~UnusedMethodsRemove();
38 virtual void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
40 bool VisitCXXMethodDecl( const CXXMethodDecl
* 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 UnusedMethodsRemove::UnusedMethodsRemove(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 UnusedMethodsRemove::~UnusedMethodsRemove()
71 int rc
= munmap(mmappedData
, mmapFilesize
);
77 std::string
niceName(const CXXMethodDecl
* functionDecl
)
80 functionDecl
->getReturnType().getCanonicalType().getAsString()
81 + " " + functionDecl
->getParent()->getQualifiedNameAsString()
82 + "::" + functionDecl
->getNameAsString()
85 for (const ParmVarDecl
*pParmVarDecl
: functionDecl
->parameters()) {
90 s
+= pParmVarDecl
->getType().getCanonicalType().getAsString();
93 if (functionDecl
->isConst()) {
99 bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl
* functionDecl
)
101 if (rewriter
== nullptr) {
104 if (ignoreLocation(functionDecl
)) {
107 // ignore stuff that forms part of the stable URE interface
108 if (isInUnoIncludeFile(functionDecl
)) {
112 // don't mess with templates
113 if (functionDecl
->getParent()->getDescribedClassTemplate() != nullptr) {
116 if (functionDecl
->getTemplatedKind() != FunctionDecl::TK_NonTemplate
) {
120 std::string aNiceName
= "\n" + niceName(functionDecl
) + "\n";
121 const char *aNiceNameStr
= aNiceName
.c_str();
122 char* found
= std::search(mmappedData
, mmappedData
+ mmapFilesize
, aNiceNameStr
, aNiceNameStr
+ strlen(aNiceNameStr
));
123 if(!(found
< mmappedData
+ mmapFilesize
)) {
126 SourceRange
replaceRange(functionDecl
->getSourceRange());
127 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
128 if (rewriter
->getRewrittenText(SourceRange(replaceRange
.getEnd(), replaceRange
.getEnd().getLocWithOffset(1))) == ";") {
129 replaceRange
.setEnd(replaceRange
.getEnd().getLocWithOffset(1));
131 // remove leading spaces
132 while (rewriter
->getRewrittenText(SourceRange(replaceRange
.getBegin().getLocWithOffset(-1), replaceRange
.getBegin())) == " ")
134 replaceRange
.setBegin(replaceRange
.getBegin().getLocWithOffset(-1));
136 if (!replaceText(replaceRange
, "")) {
138 DiagnosticsEngine::Warning
,
139 "Could not remove unused method (" + niceName(functionDecl
) + ")",
140 compat::getBeginLoc(functionDecl
))
141 << functionDecl
->getSourceRange();
147 loplugin::Plugin::Registration
< UnusedMethodsRemove
> X("unusedmethodsremove", false);
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */