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/.
16 #include <sys/types.h>
24 This is intended to be run as the second stage of the "unusedmethods" clang plugin.
29 class UnusedMethodsRemove
:
30 public RecursiveASTVisitor
<UnusedMethodsRemove
>, public loplugin::RewritePlugin
33 explicit UnusedMethodsRemove(InstantiationData
const & data
);
34 ~UnusedMethodsRemove();
36 virtual void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
38 bool VisitCXXMethodDecl( const CXXMethodDecl
* var
);
40 // I use a brute-force approach - mmap the results file and do a linear search on it
41 // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
47 size_t getFilesize(const char* filename
)
54 UnusedMethodsRemove::UnusedMethodsRemove(InstantiationData
const & data
): RewritePlugin(data
)
56 static const char sInputFile
[] = SRCDIR
"/result.txt";
57 mmapFilesize
= getFilesize(sInputFile
);
59 mmapFD
= open(sInputFile
, O_RDONLY
, 0);
62 mmappedData
= static_cast<char*>(mmap(NULL
, mmapFilesize
, PROT_READ
, MAP_PRIVATE
, mmapFD
, 0));
63 assert(mmappedData
!= NULL
);
66 UnusedMethodsRemove::~UnusedMethodsRemove()
69 int rc
= munmap(mmappedData
, mmapFilesize
);
74 std::string
niceName(const CXXMethodDecl
* functionDecl
)
77 compat::getReturnType(*functionDecl
).getCanonicalType().getAsString()
78 + " " + functionDecl
->getParent()->getQualifiedNameAsString()
79 + "::" + functionDecl
->getNameAsString()
82 for (const ParmVarDecl
*pParmVarDecl
: compat::parameters(*functionDecl
)) {
87 s
+= pParmVarDecl
->getType().getCanonicalType().getAsString();
90 if (functionDecl
->isConst()) {
96 bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl
* functionDecl
)
98 if (rewriter
== nullptr) {
101 if (ignoreLocation(functionDecl
)) {
104 // ignore stuff that forms part of the stable URE interface
105 if (isInUnoIncludeFile(functionDecl
)) {
109 // don't mess with templates
110 if (functionDecl
->getParent()->getDescribedClassTemplate() != nullptr) {
113 if (functionDecl
->getTemplatedKind() != FunctionDecl::TK_NonTemplate
) {
117 std::string aNiceName
= "\n" + niceName(functionDecl
) + "\n";
118 const char *aNiceNameStr
= aNiceName
.c_str();
119 char* found
= std::search(mmappedData
, mmappedData
+ mmapFilesize
, aNiceNameStr
, aNiceNameStr
+ strlen(aNiceNameStr
));
120 if(!(found
< mmappedData
+ mmapFilesize
)) {
123 SourceRange
replaceRange(functionDecl
->getSourceRange());
124 // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
125 if (rewriter
->getRewrittenText(SourceRange(replaceRange
.getEnd(), replaceRange
.getEnd().getLocWithOffset(1))) == ";") {
126 replaceRange
.setEnd(replaceRange
.getEnd().getLocWithOffset(1));
128 // remove leading spaces
129 while (rewriter
->getRewrittenText(SourceRange(replaceRange
.getBegin().getLocWithOffset(-1), replaceRange
.getBegin())) == " ")
131 replaceRange
.setBegin(replaceRange
.getBegin().getLocWithOffset(-1));
133 if (!replaceText(replaceRange
, "")) {
135 DiagnosticsEngine::Warning
,
136 "Could not remove unused method (" + niceName(functionDecl
) + ")",
137 functionDecl
->getLocStart())
138 << functionDecl
->getSourceRange();
144 loplugin::Plugin::Registration
< UnusedMethodsRemove
> X("unusedmethodsremove", false);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */