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 RecursiveASTVisitor
<UnusedMethodsRemove
>, public loplugin::RewritePlugin
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
): RewritePlugin(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
);
76 std::string
niceName(const CXXMethodDecl
* functionDecl
)
79 functionDecl
->getReturnType().getCanonicalType().getAsString()
80 + " " + functionDecl
->getParent()->getQualifiedNameAsString()
81 + "::" + functionDecl
->getNameAsString()
84 for (const ParmVarDecl
*pParmVarDecl
: compat::parameters(*functionDecl
)) {
89 s
+= pParmVarDecl
->getType().getCanonicalType().getAsString();
92 if (functionDecl
->isConst()) {
98 bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl
* functionDecl
)
100 if (rewriter
== nullptr) {
103 if (ignoreLocation(functionDecl
)) {
106 // ignore stuff that forms part of the stable URE interface
107 if (isInUnoIncludeFile(functionDecl
)) {
111 // don't mess with templates
112 if (functionDecl
->getParent()->getDescribedClassTemplate() != nullptr) {
115 if (functionDecl
->getTemplatedKind() != FunctionDecl::TK_NonTemplate
) {
119 std::string aNiceName
= "\n" + niceName(functionDecl
) + "\n";
120 const char *aNiceNameStr
= aNiceName
.c_str();
121 char* found
= std::search(mmappedData
, mmappedData
+ mmapFilesize
, aNiceNameStr
, aNiceNameStr
+ strlen(aNiceNameStr
));
122 if(!(found
< mmappedData
+ mmapFilesize
)) {
125 SourceRange
replaceRange(functionDecl
->getSourceRange());
126 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
127 if (rewriter
->getRewrittenText(SourceRange(replaceRange
.getEnd(), replaceRange
.getEnd().getLocWithOffset(1))) == ";") {
128 replaceRange
.setEnd(replaceRange
.getEnd().getLocWithOffset(1));
130 // remove leading spaces
131 while (rewriter
->getRewrittenText(SourceRange(replaceRange
.getBegin().getLocWithOffset(-1), replaceRange
.getBegin())) == " ")
133 replaceRange
.setBegin(replaceRange
.getBegin().getLocWithOffset(-1));
135 if (!replaceText(replaceRange
, "")) {
137 DiagnosticsEngine::Warning
,
138 "Could not remove unused method (" + niceName(functionDecl
) + ")",
139 functionDecl
->getLocStart())
140 << functionDecl
->getSourceRange();
146 loplugin::Plugin::Registration
< UnusedMethodsRemove
> X("unusedmethodsremove", false);
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */