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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/* -*- 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 <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include <clang/AST/CXXInheritance.h>
#include "compat.hxx"
#include "plugin.hxx"
/**
look for methods where all they do is call their superclass method
*/
namespace {
class UnnecessaryOverride:
public RecursiveASTVisitor<UnnecessaryOverride>, public loplugin::Plugin
{
public:
explicit UnnecessaryOverride(InstantiationData const & data): Plugin(data) {}
virtual void run() override
{
// ignore some files with problematic macros
StringRef fn( compiler.getSourceManager().getFileEntryForID(
compiler.getSourceManager().getMainFileID())->getName() );
if (fn == SRCDIR "/sd/source/ui/framework/factories/ChildWindowPane.cxx")
return;
if (fn == SRCDIR "/forms/source/component/Date.cxx")
return;
if (fn == SRCDIR "/forms/source/component/Time.cxx")
return;
if (fn == SRCDIR "/svx/source/dialog/hyperdlg.cxx")
return;
if (fn == SRCDIR "/svx/source/dialog/rubydialog.cxx")
return;
if (fn.startswith(SRCDIR "/canvas"))
return;
if (fn == SRCDIR "/sc/source/ui/view/spelldialog.cxx")
return;
if (fn == SRCDIR "/sd/source/ui/dlg/SpellDialogChildWindow.cxx")
return;
// HAVE_ODBC_ADMINISTRATION
if (fn == SRCDIR "/dbaccess/source/ui/dlg/dsselect.cxx")
return;
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitCXXMethodDecl(const CXXMethodDecl *);
private:
const CXXMethodDecl * findOverriddenOrSimilarMethodInSuperclasses(const CXXMethodDecl *);
bool BaseCheckCallback(
const CXXRecordDecl *BaseDefinition
#if CLANG_VERSION < 30800
, void *
#endif
);
};
bool UnnecessaryOverride::VisitCXXMethodDecl(const CXXMethodDecl* methodDecl)
{
if (ignoreLocation(methodDecl->getCanonicalDecl())) {
return true;
}
if (isa<CXXConstructorDecl>(methodDecl)) {
return true;
}
if (isa<CXXDestructorDecl>(methodDecl)) {
// Warn about unnecessarily user-declared overriding virtual
// destructors; such a destructor is deemed unnecessary if
// * it is public;
// * its class is only defined in the .cxx file (i.e., the virtual
// destructor is neither used to controll the place of vtable
// emission, nor is its definition depending on types that may still
// be incomplete);
// * it either does not have an explicit exception specification, or has
// a non-dependent explicit exception specification that is compatible
// with a non-dependent exception specification the destructor would
// have if it did not have an explicit one (TODO);
// * it is either defined as defaulted or with an empty body.
// Removing the user-declared destructor may cause the class to get an
// implicitly declared move constructor and/or move assignment operator;
// that is considered acceptable: If any subobject cannot be moved, the
// implicitly declared function will be defined as deleted (which is in
// practice not much different from not having it declared), and
// otherwise offering movability is likely even an improvement over not
// offering it due to a "pointless" user-declared destructor.
// Similarly, removing the user-declared destructor may cause the
// implicit definition of a copy constructor and/or copy assignment
// operator to change from being an obsolete feature to being a standard
// feature. That difference is not taken into account here.
if ((methodDecl->begin_overridden_methods()
== methodDecl->end_overridden_methods())
|| methodDecl->getAccess() != AS_public)
{
return true;
}
if (!compiler.getSourceManager().isInMainFile(
methodDecl->getCanonicalDecl()->getLocation()))
{
return true;
}
if (!methodDecl->isExplicitlyDefaulted()) {
if (!methodDecl->doesThisDeclarationHaveABody()) {
return true;
}
auto stmt = dyn_cast<CompoundStmt>(methodDecl->getBody());
if (stmt == nullptr || stmt->size() != 0) {
return true;
}
}
//TODO: exception specification
auto cls = methodDecl->getParent();
if (!(cls->hasUserDeclaredCopyConstructor()
|| cls->hasUserDeclaredCopyAssignment()
|| cls->hasUserDeclaredMoveConstructor()
|| cls->hasUserDeclaredMoveAssignment()))
{
}
if ((cls->needsImplicitMoveConstructor()
&& !(cls->hasUserDeclaredCopyConstructor()
|| cls->hasUserDeclaredCopyAssignment()
|| cls->hasUserDeclaredMoveAssignment()))
|| (cls->needsImplicitMoveAssignment()
&& !(cls->hasUserDeclaredCopyConstructor()
|| cls->hasUserDeclaredCopyAssignment()
|| cls->hasUserDeclaredMoveConstructor())))
{
report(DiagnosticsEngine::Fatal, "TODO", methodDecl->getLocation());
return true;
}
report(
DiagnosticsEngine::Warning, "unnecessary user-declared destructor",
methodDecl->getLocation())
<< methodDecl->getSourceRange();
auto cd = methodDecl->getCanonicalDecl();
if (cd->getLocation() != methodDecl->getLocation()) {
report(DiagnosticsEngine::Note, "declared here", cd->getLocation())
<< cd->getSourceRange();
}
return true;
}
if (!methodDecl->doesThisDeclarationHaveABody()) {
return true;
}
// if we are overriding more than one method, then this is a disambiguating override
if (methodDecl->isVirtual()) {
if (methodDecl->size_overridden_methods() != 1)
{
return true;
}
}
// sometimes the disambiguation happens in a base class
StringRef aFileName = compiler.getSourceManager().getFilename(compiler.getSourceManager().getSpellingLoc(methodDecl->getLocStart()));
if (aFileName == SRCDIR "/comphelper/source/property/propertycontainer.cxx")
return true;
// not sure what is happening here
if (aFileName == SRCDIR "/extensions/source/bibliography/datman.cxx")
return true;
// some very creative method hiding going on here
if (aFileName == SRCDIR "/svx/source/dialog/checklbx.cxx")
return true;
// entertaining template magic
if (aFileName == SRCDIR "/sc/source/ui/vba/vbaformatcondition.cxx")
return true;
// not sure what is going on here, but removing the override causes a crash
if (methodDecl->getQualifiedNameAsString() == "SwXTextDocument::queryAdapter")
return true;
const CXXMethodDecl* overriddenMethodDecl = findOverriddenOrSimilarMethodInSuperclasses(methodDecl);
if (!overriddenMethodDecl) {
return true;
}
if (compat::getReturnType(*methodDecl).getCanonicalType()
!= compat::getReturnType(*overriddenMethodDecl).getCanonicalType())
{
return true;
}
//TODO: check for identical exception specifications
const CompoundStmt* compoundStmt = dyn_cast<CompoundStmt>(methodDecl->getBody());
if (!compoundStmt || compoundStmt->size() != 1)
return true;
const CXXMemberCallExpr* callExpr;
if (compat::getReturnType(*methodDecl).getCanonicalType()->isVoidType())
{
callExpr = dyn_cast<CXXMemberCallExpr>(*compoundStmt->body_begin());
}
else
{
auto returnStmt = dyn_cast<ReturnStmt>(*compoundStmt->body_begin());
if (returnStmt == nullptr) {
return true;
}
auto returnExpr = returnStmt->getRetValue();
if (returnExpr == nullptr) {
return true;
}
returnExpr = returnExpr->IgnoreImplicit();
// In something like
//
// Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery(
// const rtl::OUString& sql)
// throw(SQLException, RuntimeException, std::exception)
// {
// return OCommonStatement::executeQuery( sql );
// }
//
// look down through all the
//
// ReturnStmt
// `-ExprWithCleanups
// `-CXXConstructExpr
// `-MaterializeTemporaryExpr
// `-ImplicitCastExpr
// `-CXXBindTemporaryExpr
// `-CXXMemberCallExpr
//
// where the fact that the overriding and overridden function have identical
// return types makes us confident that all we need to check here is whether
// there's an (arbitrary, one-argument) CXXConstructorExpr and
// CXXBindTemporaryExpr in between:
if (auto ctorExpr = dyn_cast<CXXConstructExpr>(returnExpr)) {
if (ctorExpr->getNumArgs() == 1) {
auto tempExpr1 = ctorExpr->getArg(0)->IgnoreImplicit();
if (auto tempExpr2 = dyn_cast<CXXBindTemporaryExpr>(tempExpr1))
{
returnExpr = tempExpr2->getSubExpr();
}
else if (auto tempExpr2 = dyn_cast<CXXMemberCallExpr>(tempExpr1))
{
returnExpr = tempExpr2;
}
}
}
callExpr = dyn_cast<CXXMemberCallExpr>(returnExpr->IgnoreParenImpCasts());
}
if (!callExpr || callExpr->getMethodDecl() != overriddenMethodDecl)
return true;
const Expr* expr1 = callExpr->getImplicitObjectArgument()->IgnoreImpCasts();
if (!expr1)
return true;
const CXXThisExpr* expr2 = dyn_cast_or_null<CXXThisExpr>(expr1);
if (!expr2)
return true;
for (unsigned i = 0; i<callExpr->getNumArgs(); ++i) {
// ignore ImplicitCastExpr
const DeclRefExpr * declRefExpr = dyn_cast<DeclRefExpr>(callExpr->getArg(i)->IgnoreImplicit());
if (!declRefExpr || declRefExpr->getDecl() != methodDecl->getParamDecl(i))
return true;
}
report(
DiagnosticsEngine::Warning, "%0%1 function just calls %2 parent",
methodDecl->getSourceRange().getBegin())
<< methodDecl->getAccess()
<< (methodDecl->isVirtual() ? " virtual" : "")
<< overriddenMethodDecl->getAccess()
<< methodDecl->getSourceRange();
if (methodDecl->getCanonicalDecl()->getLocation() != methodDecl->getLocation()) {
const CXXMethodDecl* pOther = methodDecl->getCanonicalDecl();
report(
DiagnosticsEngine::Note,
"method declaration here",
pOther->getLocStart())
<< pOther->getSourceRange();
}
return true;
}
const CXXMethodDecl* UnnecessaryOverride::findOverriddenOrSimilarMethodInSuperclasses(const CXXMethodDecl* methodDecl)
{
if (methodDecl->isVirtual()) {
return *methodDecl->begin_overridden_methods();
}
if (!methodDecl->getDeclName().isIdentifier()) {
return nullptr;
}
#if CLANG_VERSION < 30800
return nullptr;
#else
std::vector<const CXXMethodDecl*> maSimilarMethods;
auto BaseMatchesCallback = [&](const CXXBaseSpecifier *cxxBaseSpecifier, CXXBasePath& )
{
if (cxxBaseSpecifier->getAccessSpecifier() != AS_public && cxxBaseSpecifier->getAccessSpecifier() != AS_protected)
return false;
if (!cxxBaseSpecifier->getType().getTypePtr())
return false;
const CXXRecordDecl* baseCXXRecordDecl = cxxBaseSpecifier->getType()->getAsCXXRecordDecl();
if (!baseCXXRecordDecl)
return false;
if (baseCXXRecordDecl->isInvalidDecl())
return false;
for (const CXXMethodDecl* baseMethod : baseCXXRecordDecl->methods())
{
if (!baseMethod->getDeclName().isIdentifier() || methodDecl->getName() != baseMethod->getName()) {
continue;
}
if (compat::getReturnType(*methodDecl).getCanonicalType()
!= compat::getReturnType(*baseMethod).getCanonicalType())
{
continue;
}
if (methodDecl->param_size() != baseMethod->param_size())
continue;
if (methodDecl->getNumParams() != baseMethod->getNumParams())
continue;
bool bParamsMatch = true;
for (unsigned i=0; i<methodDecl->param_size(); ++i)
{
if (methodDecl->parameters()[i]->getType() != baseMethod->parameters()[i]->getType())
{
bParamsMatch = false;
break;
}
}
if (bParamsMatch)
maSimilarMethods.push_back(baseMethod);
}
return false;
};
CXXBasePaths aPaths;
methodDecl->getParent()->lookupInBases(BaseMatchesCallback, aPaths);
if (maSimilarMethods.size() == 1) {
return maSimilarMethods[0];
}
return nullptr;
#endif
}
loplugin::Plugin::Registration< UnnecessaryOverride > X("unnecessaryoverride", true);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|