1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <string>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include "plugin.hxx"
#include "check.hxx"
#include "clang/AST/CXXInheritance.h"
// Original idea from tml.
// Look for variables that are (a) initialised from zero or one constants. (b) only used in one spot.
// In which case, we might as well inline it.
namespace
{
bool startsWith(const std::string& rStr, const char* pSubStr) {
return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
}
class OnceVar:
public RecursiveASTVisitor<OnceVar>, public loplugin::Plugin
{
public:
explicit OnceVar(InstantiationData const & data): Plugin(data) {}
virtual void run() override {
// ignore some files with problematic macros
std::string fn( compiler.getSourceManager().getFileEntryForID(
compiler.getSourceManager().getMainFileID())->getName() );
normalizeDotDotInFilePath(fn);
// platform-specific stuff
if (fn == SRCDIR "/sal/osl/unx/thread.cxx"
|| fn == SRCDIR "/sot/source/base/formats.cxx"
|| fn == SRCDIR "/svl/source/config/languageoptions.cxx"
|| fn == SRCDIR "/sfx2/source/appl/appdde.cxx"
|| fn == SRCDIR "/configmgr/source/components.cxx"
|| fn == SRCDIR "/embeddedobj/source/msole/oleembed.cxx")
return;
// some of this is necessary
if (startsWith( fn, SRCDIR "/sal/qa/"))
return;
if (startsWith( fn, SRCDIR "/comphelper/qa/"))
return;
// TODO need to check calls via function pointer
if (fn == SRCDIR "/i18npool/source/textconversion/textconversion_zh.cxx"
|| fn == SRCDIR "/i18npool/source/localedata/localedata.cxx")
return;
// debugging stuff
if (fn == SRCDIR "/sc/source/core/data/dpcache.cxx"
|| fn == SRCDIR "/sw/source/core/layout/dbg_lay.cxx"
|| fn == SRCDIR "/sw/source/core/layout/ftnfrm.cxx")
return;
// TODO taking local reference to variable
if (fn == SRCDIR "/sc/source/filter/excel/xechart.cxx")
return;
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
for (auto const & varDecl : maVarDeclSet)
{
if (maVarDeclToIgnoreSet.find(varDecl) != maVarDeclToIgnoreSet.end())
continue;
int noUses = 0;
auto it = maVarUsesMap.find(varDecl);
if (it != maVarUsesMap.end())
noUses = it->second;
if (noUses > 1)
continue;
report(DiagnosticsEngine::Warning,
"var used only once, should be inlined or declared const",
varDecl->getLocation())
<< varDecl->getSourceRange();
if (it != maVarUsesMap.end())
report(DiagnosticsEngine::Note,
"used here",
maVarUseSourceRangeMap[varDecl].getBegin())
<< maVarUseSourceRangeMap[varDecl];
}
}
bool VisitDeclRefExpr( const DeclRefExpr* );
bool VisitVarDecl( const VarDecl* );
private:
std::unordered_set<VarDecl const *> maVarDeclSet;
std::unordered_set<VarDecl const *> maVarDeclToIgnoreSet;
std::unordered_map<VarDecl const *, int> maVarUsesMap;
std::unordered_map<VarDecl const *, SourceRange> maVarUseSourceRangeMap;
};
bool OnceVar::VisitVarDecl( const VarDecl* varDecl )
{
if (ignoreLocation(varDecl)) {
return true;
}
if (varDecl->isExceptionVariable() || isa<ParmVarDecl>(varDecl)) {
return true;
}
// ignore stuff in header files (which should really not be there, but anyhow)
if (!compiler.getSourceManager().isInMainFile(varDecl->getLocation())) {
return true;
}
// Ignore macros like FD_ZERO
if (compiler.getSourceManager().isMacroBodyExpansion(varDecl->getLocStart())) {
return true;
}
if (varDecl->hasGlobalStorage()) {
return true;
}
auto const tc = loplugin::TypeCheck(varDecl->getType());
if (!varDecl->getType()->isScalarType()
&& !varDecl->getType()->isBooleanType()
&& !varDecl->getType()->isEnumeralType()
&& !tc.Class("OString").Namespace("rtl").GlobalNamespace()
&& !tc.Class("OUString").Namespace("rtl").GlobalNamespace()
&& !tc.Class("OStringBuffer").Namespace("rtl").GlobalNamespace()
&& !tc.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace())
{
return true;
}
if (varDecl->getType()->isPointerType())
return true;
// if it's declared const, ignore it, it's there to make the code easier to read
if (tc.Const())
return true;
if (!varDecl->hasInit())
return true;
// check for string or scalar literals
bool foundStringLiteral = false;
const Expr * initExpr = varDecl->getInit();
if (auto e = dyn_cast<ExprWithCleanups>(initExpr)) {
initExpr = e->getSubExpr();
}
if (auto stringLit = dyn_cast<clang::StringLiteral>(initExpr)) {
foundStringLiteral = true;
// ignore long literals, helps to make the code more legible
if (stringLit->getLength() > 40) {
return true;
}
} else if (auto constructExpr = dyn_cast<CXXConstructExpr>(initExpr)) {
if (constructExpr->getNumArgs() > 0) {
auto stringLit2 = dyn_cast<clang::StringLiteral>(constructExpr->getArg(0));
foundStringLiteral = stringLit2 != nullptr;
// ignore long literals, helps to make the code more legible
if (stringLit2 && stringLit2->getLength() > 40) {
return true;
}
}
}
if (!foundStringLiteral
&& !varDecl->getInit()->isConstantInitializer(compiler.getASTContext(), false/*ForRef*/))
{
return true;
}
maVarDeclSet.insert(varDecl);
return true;
}
bool OnceVar::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
{
if (ignoreLocation(declRefExpr)) {
return true;
}
const Decl* decl = declRefExpr->getDecl();
if (!isa<VarDecl>(decl) || isa<ParmVarDecl>(decl)) {
return true;
}
const VarDecl * varDecl = dyn_cast<VarDecl>(decl)->getCanonicalDecl();
// ignore stuff in header files (which should really not be there, but anyhow)
if (!compiler.getSourceManager().isInMainFile(varDecl->getLocation())) {
return true;
}
Stmt const * parent = parentStmt(declRefExpr);
// ignore cases like:
// const OUString("xxx") xxx;
// rtl_something(xxx.pData);
// and
// foo(&xxx);
// where we cannot inline the declaration.
auto const tc = loplugin::TypeCheck(varDecl->getType());
if (tc.Class("OUString").Namespace("rtl").GlobalNamespace()
&& parent && (isa<MemberExpr>(parent) || isa<UnaryOperator>(parent)))
{
maVarDeclToIgnoreSet.insert(varDecl);
return true;
}
// if we take the address of it, or we modify it, ignore it
if (auto unaryOp = dyn_cast_or_null<UnaryOperator>(parent)) {
UnaryOperator::Opcode op = unaryOp->getOpcode();
if (op == UO_AddrOf || op == UO_PreInc || op == UO_PostInc
|| op == UO_PreDec || op == UO_PostDec)
{
maVarDeclToIgnoreSet.insert(varDecl);
return true;
}
}
// if we assign it another value, or modify it, ignore it
if (auto binaryOp = dyn_cast_or_null<BinaryOperator>(parent)) {
if (binaryOp->getLHS() == declRefExpr)
{
BinaryOperator::Opcode op = binaryOp->getOpcode();
if (op == BO_Assign || op == BO_PtrMemD || op == BO_PtrMemI || op == BO_MulAssign
|| op == BO_DivAssign || op == BO_RemAssign || op == BO_AddAssign
|| op == BO_SubAssign || op == BO_ShlAssign || op == BO_ShrAssign
|| op == BO_AndAssign || op == BO_XorAssign || op == BO_OrAssign)
{
maVarDeclToIgnoreSet.insert(varDecl);
return true;
}
}
}
// ignore those ones we are passing by reference
if (auto callExpr = dyn_cast_or_null<CallExpr>(parent)) {
const FunctionDecl* calleeFunctionDecl = callExpr->getDirectCallee();
if (calleeFunctionDecl) {
for (unsigned i = 0; i < callExpr->getNumArgs(); ++i) {
if (callExpr->getArg(i) == declRefExpr) {
if (i < calleeFunctionDecl->getNumParams()) {
QualType qt { calleeFunctionDecl->getParamDecl(i)->getType() };
if (loplugin::TypeCheck(qt).LvalueReference()) {
maVarDeclToIgnoreSet.insert(varDecl);
return true;
}
}
break;
}
}
}
}
// ignore those ones we are passing by reference
if (auto cxxConstructExpr = dyn_cast_or_null<CXXConstructExpr>(parent)) {
const CXXConstructorDecl* cxxConstructorDecl = cxxConstructExpr->getConstructor();
for (unsigned i = 0; i < cxxConstructExpr->getNumArgs(); ++i) {
if (cxxConstructExpr->getArg(i) == declRefExpr) {
if (i < cxxConstructorDecl->getNumParams()) {
QualType qt { cxxConstructorDecl->getParamDecl(i)->getType() };
if (loplugin::TypeCheck(qt).LvalueReference()) {
maVarDeclToIgnoreSet.insert(varDecl);
return true;
}
}
break;
}
}
return true;
}
if (maVarUsesMap.find(varDecl) == maVarUsesMap.end()) {
maVarUsesMap[varDecl] = 1;
maVarUseSourceRangeMap[varDecl] = declRefExpr->getSourceRange();
} else {
maVarUsesMap[varDecl]++;
}
return true;
}
loplugin::Plugin::Registration< OnceVar > X("oncevar", false);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|