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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* Based on LLVM/Clang.
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*/
#ifndef LO_CLANG_SHARED_PLUGINS
#include "plugin.hxx"
#include "check.hxx"
#include "compat.hxx"
#include "config_clang.h"
#include <iostream>
#include <unordered_map>
#include "clang/AST/CXXInheritance.h"
/*
* This is a compile-time checker.
*
* Check that when we override SvXmlImportContext, and we override createFastChildContext,
* we have also overridden startFastElement, or the fast-parser stuff will not work
* correctly.
*/
namespace
{
class XmlImport : public loplugin::FilteringPlugin<XmlImport>
{
public:
explicit XmlImport(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
bool preRun() override
{
StringRef fn(handler.getMainFileName());
if (loplugin::isSamePathname(fn, SRCDIR "/xmloff/source/core/xmlictxt.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/xmloff/source/core/xmlimp.cxx"))
return false;
// These are mostly classes delegating calls to other classes
if (loplugin::isSamePathname(fn, SRCDIR "/xmloff/source/text/XMLTextFrameContext.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/xmloff/source/draw/ximpshap.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/xmloff/source/table/XMLTableImport.cxx"))
return false;
if (loplugin::isSamePathname(fn,
SRCDIR "/sc/source/filter/xml/XMLTrackedChangesContext.cxx"))
return false;
if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/xml/xmlannoi.cxx"))
return false;
// this class specifically wants to prevent some endFastElement processing happening in its superclass
if (loplugin::isSamePathname(fn, SRCDIR
"/xmloff/source/text/XMLIndexBibliographySourceContext.cxx"))
return false;
// calling mxSlaveContext
if (loplugin::isSamePathname(fn, SRCDIR "/xmloff/source/draw/XMLNumberStyles.cxx"))
return false;
return true;
}
void run() override
{
if (preRun())
{
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
}
bool VisitCXXMethodDecl(const CXXMethodDecl*);
bool VisitCXXMemberCallExpr(const CXXMemberCallExpr*);
bool VisitBinaryOperator(const BinaryOperator*);
bool VisitSwitchStmt(const SwitchStmt*);
bool VisitCallExpr(const CallExpr*);
private:
bool isXmlTokEnum(const Expr*);
bool isUInt16(const Expr*);
bool isUInt16(QualType);
std::unordered_map<const CXXRecordDecl*, const CXXMethodDecl*> startFastElementSet;
std::unordered_map<const CXXRecordDecl*, const CXXMethodDecl*> StartElementSet;
std::unordered_map<const CXXRecordDecl*, const CXXMethodDecl*> endFastElementSet;
std::unordered_map<const CXXRecordDecl*, const CXXMethodDecl*> EndElementSet;
std::unordered_map<const CXXRecordDecl*, const CXXMethodDecl*> charactersSet;
std::unordered_map<const CXXRecordDecl*, const CXXMethodDecl*> CharactersSet;
};
bool XmlImport::VisitCXXMethodDecl(const CXXMethodDecl* methodDecl)
{
auto beginLoc = methodDecl->getBeginLoc();
if (!beginLoc.isValid() || ignoreLocation(beginLoc))
return true;
if (!methodDecl->getIdentifier())
return true;
auto cxxRecordDecl = methodDecl->getParent();
if (!cxxRecordDecl || !cxxRecordDecl->getIdentifier())
return true;
if (loplugin::DeclCheck(cxxRecordDecl).Class("SvXMLImportContext"))
return true;
if (!loplugin::isDerivedFrom(cxxRecordDecl, [](Decl const* decl) -> bool {
auto const dc = loplugin::DeclCheck(decl);
return bool(dc.ClassOrStruct("SvXMLImportContext").GlobalNamespace());
}))
return true;
auto name = methodDecl->getName();
if (name == "startFastElement")
startFastElementSet.insert({ cxxRecordDecl, methodDecl });
else if (name == "StartElement")
StartElementSet.insert({ cxxRecordDecl, methodDecl });
else if (name == "endFastElement")
endFastElementSet.insert({ cxxRecordDecl, methodDecl });
else if (name == "EndElement")
EndElementSet.insert({ cxxRecordDecl, methodDecl });
else if (name == "characters")
{
if (methodDecl->getNumParams() == 1)
charactersSet.insert({ cxxRecordDecl, methodDecl });
}
else if (name == "Characters")
{
if (methodDecl->getNumParams() == 1)
CharactersSet.insert({ cxxRecordDecl, methodDecl });
}
{
auto it1 = endFastElementSet.find(cxxRecordDecl);
auto it2 = EndElementSet.find(cxxRecordDecl);
if (it1 != endFastElementSet.end() && it2 != EndElementSet.end())
{
auto methodDecl1 = it1->second;
report(DiagnosticsEngine::Warning, "cannot override both endFastElement and EndElement",
methodDecl1->getBeginLoc())
<< methodDecl1->getSourceRange();
auto methodDecl2 = it2->second;
report(DiagnosticsEngine::Warning, "cannot override both endFastElement and EndElement",
methodDecl2->getBeginLoc())
<< methodDecl2->getSourceRange();
}
}
{
auto it1 = startFastElementSet.find(cxxRecordDecl);
auto it2 = StartElementSet.find(cxxRecordDecl);
if (it1 != startFastElementSet.end() && it2 != StartElementSet.end())
{
auto methodDecl1 = it1->second;
report(DiagnosticsEngine::Warning,
"cannot override both startFastElement and StartElement",
methodDecl1->getBeginLoc())
<< methodDecl1->getSourceRange();
auto methodDecl2 = it2->second;
report(DiagnosticsEngine::Warning,
"cannot override both startFastElement and StartElement",
methodDecl2->getBeginLoc())
<< methodDecl2->getSourceRange();
}
}
{
auto it1 = charactersSet.find(cxxRecordDecl);
auto it2 = CharactersSet.find(cxxRecordDecl);
if (it1 != charactersSet.end() && it2 != CharactersSet.end())
{
auto methodDecl1 = it1->second;
report(DiagnosticsEngine::Warning, "cannot override both characters and Characters",
methodDecl1->getBeginLoc())
<< methodDecl1->getSourceRange();
auto methodDecl2 = it2->second;
report(DiagnosticsEngine::Warning, "cannot override both characters and Characters",
methodDecl2->getBeginLoc())
<< methodDecl2->getSourceRange();
}
}
auto checkEmpty = [&]() {
if (!methodDecl->isThisDeclarationADefinition())
return;
auto compoundStmt = dyn_cast_or_null<CompoundStmt>(methodDecl->getBody());
if (compoundStmt == nullptr || compoundStmt->size() > 0)
return;
report(DiagnosticsEngine::Warning, "empty, should be removed", methodDecl->getBeginLoc())
<< methodDecl->getSourceRange();
auto canonicalDecl = methodDecl->getCanonicalDecl();
if (canonicalDecl != methodDecl)
report(DiagnosticsEngine::Note, "definition here", canonicalDecl->getBeginLoc())
<< canonicalDecl->getSourceRange();
};
auto checkOnlyReturn = [&]() {
if (!methodDecl->isThisDeclarationADefinition())
return;
auto compoundStmt = dyn_cast_or_null<CompoundStmt>(methodDecl->getBody());
if (compoundStmt == nullptr || compoundStmt->size() > 1)
return;
auto returnStmt = dyn_cast_or_null<ReturnStmt>(*compoundStmt->body_begin());
if (!returnStmt)
return;
auto cxxConstructExpr
= dyn_cast_or_null<CXXConstructExpr>(returnStmt->getRetValue()->IgnoreImplicit());
if (!cxxConstructExpr)
return;
if (cxxConstructExpr->getNumArgs() != 1)
return;
if (!isa<CXXNullPtrLiteralExpr>(cxxConstructExpr->getArg(0)->IgnoreImplicit()))
return;
report(DiagnosticsEngine::Warning, "empty, should be removed", methodDecl->getBeginLoc())
<< methodDecl->getSourceRange();
auto canonicalDecl = methodDecl->getCanonicalDecl();
if (canonicalDecl != methodDecl)
report(DiagnosticsEngine::Note, "definition here", canonicalDecl->getBeginLoc())
<< canonicalDecl->getSourceRange();
};
if (name == "startFastElement")
checkEmpty();
else if (name == "endFastElement")
checkEmpty();
else if (name == "characters")
checkEmpty();
else if (name == "createFastChildContext")
checkOnlyReturn();
else if (name == "createUnknownChildContext")
checkOnlyReturn();
return true;
}
bool XmlImport::VisitCXXMemberCallExpr(const CXXMemberCallExpr* callExpr)
{
auto beginLoc = callExpr->getBeginLoc();
if (!beginLoc.isValid() || ignoreLocation(callExpr))
return true;
CXXMethodDecl* methodDecl = callExpr->getMethodDecl();
if (!methodDecl || !methodDecl->getIdentifier())
return true;
auto cxxRecordDecl = methodDecl->getParent();
if (!cxxRecordDecl || !cxxRecordDecl->getIdentifier())
return true;
if (!loplugin::DeclCheck(cxxRecordDecl).Class("SvXMLImportContext"))
return true;
auto name = methodDecl->getName();
if (name == "startFastElement" || name == "characters" || name == "endFastElement"
|| name == "createFastChildContext" || name == "createUnknownChildContext"
|| name == "StartElement" || name == "EndElement" || name == "Characters"
|| name == "CreateChildContext")
{
/**
* Calling this superclass method from a subclass method will mess with the fallback logic in the superclass.
*/
report(DiagnosticsEngine::Warning, "don't call this superclass method",
callExpr->getBeginLoc())
<< callExpr->getSourceRange();
}
return true;
}
bool XmlImport::VisitBinaryOperator(const BinaryOperator* binaryOp)
{
auto beginLoc = binaryOp->getBeginLoc();
if (!beginLoc.isValid() || ignoreLocation(binaryOp))
return true;
auto op = binaryOp->getOpcode();
if (op != BO_EQ && op != BO_NE)
return true;
auto check2 = [&](const Expr* expr) -> void {
if (!isUInt16(expr))
report(DiagnosticsEngine::Warning,
"comparing XML_TOK enum to 'sal_uInt32', expected sal_uInt16",
binaryOp->getBeginLoc())
<< binaryOp->getSourceRange();
};
if (isXmlTokEnum(binaryOp->getLHS()))
check2(binaryOp->getRHS());
else if (isXmlTokEnum(binaryOp->getRHS()))
check2(binaryOp->getLHS());
return true;
}
bool XmlImport::VisitSwitchStmt(const SwitchStmt* switchStmt)
{
auto beginLoc = switchStmt->getBeginLoc();
if (!beginLoc.isValid() || ignoreLocation(switchStmt))
return true;
if (isUInt16(switchStmt->getCond()))
return true;
// if the condition is an enum type, ignore this switch
auto condEnumType = switchStmt->getCond()
->IgnoreImplicit()
->getType()
->getUnqualifiedDesugaredType()
->getAs<EnumType>();
if (condEnumType)
return true;
auto switchCaseStmt = switchStmt->getSwitchCaseList();
for (; switchCaseStmt != nullptr; switchCaseStmt = switchCaseStmt->getNextSwitchCase())
{
auto caseStmt = dyn_cast<CaseStmt>(switchCaseStmt);
if (!caseStmt)
continue;
if (!isXmlTokEnum(caseStmt->getLHS()))
continue;
report(DiagnosticsEngine::Warning,
"comparing XML_TOK enum to 'sal_uInt32', expected sal_uInt16",
caseStmt->getBeginLoc())
<< caseStmt->getSourceRange();
}
return true;
}
bool XmlImport::VisitCallExpr(const CallExpr* callExpr)
{
auto beginLoc = callExpr->getBeginLoc();
if (!beginLoc.isValid() || ignoreLocation(callExpr))
return true;
const FunctionDecl* functionDecl;
if (isa<CXXMemberCallExpr>(callExpr))
functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
else
functionDecl = callExpr->getDirectCallee();
if (!functionDecl)
return true;
for (unsigned i = 0; i != callExpr->getNumArgs(); ++i)
{
auto argExpr = callExpr->getArg(i)->IgnoreImplicit();
if (!isXmlTokEnum(argExpr))
continue;
// if the condition is an enum type, ignore this switch
auto condEnumType = functionDecl->getParamDecl(i)
->getType()
->getUnqualifiedDesugaredType()
->getAs<EnumType>();
if (condEnumType)
continue;
if (isUInt16(functionDecl->getParamDecl(i)->getType()))
return true;
report(DiagnosticsEngine::Warning,
"passing XML_TOK enum to 'sal_Int32', wrong param or XML token type",
callExpr->getBeginLoc())
<< callExpr->getSourceRange();
}
return true;
}
bool XmlImport::isXmlTokEnum(const Expr* expr)
{
expr = expr->IgnoreImplicit();
// check that we have an unscoped enum type
auto condEnumType = expr->getType()->getUnqualifiedDesugaredType()->getAs<EnumType>();
if (!condEnumType || condEnumType->getDecl()->isScoped())
return false;
auto declRefExpr = dyn_cast<DeclRefExpr>(expr);
if (!declRefExpr)
return false;
auto enumConstant = dyn_cast<EnumConstantDecl>(declRefExpr->getDecl());
if (!enumConstant)
return false;
return enumConstant->getIdentifier()
&& compat::starts_with(enumConstant->getName(), "XML_TOK_");
}
bool XmlImport::isUInt16(const Expr* expr)
{
expr = expr->IgnoreImplicit();
return isUInt16(expr->getType());
}
bool XmlImport::isUInt16(QualType qt)
{
if (qt->isSpecificBuiltinType(BuiltinType::UShort))
return true;
return bool(loplugin::TypeCheck(qt).Typedef("sal_uInt16"));
}
loplugin::Plugin::Registration<XmlImport> xmlimport("xmlimport");
} // namespace
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|