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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
/* -*- 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 <algorithm>
#include <cassert>
#include <set>
#include <string>
#include "clang/AST/Attr.h"
#include "compat.hxx"
#include "plugin.hxx"
namespace {
bool isSalBool(QualType type) {
TypedefType const * t = type->getAs<TypedefType>();
return t != nullptr && t->getDecl()->getNameAsString() == "sal_Bool";
}
// Clang 3.2 FunctionDecl::isInlined doesn't work as advertised ("Determine
// whether this function should be inlined, because it is either marked 'inline'
// or 'constexpr' or is a member function of a class that was defined in the
// class body.") but mis-classifies salhelper::Timer's isTicking, isExpired, and
// expiresBefore members as defined in salhelper/source/timer.cxx as inlined:
bool isInlined(FunctionDecl const & decl) {
#if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
return decl.isInlined();
#else
(void)decl;
return false;
#endif
}
// It appears that, given a function declaration, there is no way to determine
// the language linkage of the function's type, only of the function's name
// (via FunctionDecl::isExternC); however, in a case like
//
// extern "C" { static void f(); }
//
// the function's name does not have C language linkage while the function's
// type does (as clarified in C++11 [decl.link]); cf. <http://clang-developers.
// 42468.n3.nabble.com/Language-linkage-of-function-type-tt4037248.html>
// "Language linkage of function type":
bool hasCLanguageLinkageType(FunctionDecl const * decl) {
assert(decl != nullptr);
if (decl->isExternC()) {
return true;
}
#if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3
if (decl->isInExternCContext()) {
return true;
}
#else
if (decl->getCanonicalDecl()->getDeclContext()->isExternCContext()) {
return true;
}
#endif
return false;
}
enum class OverrideKind { NO, YES, MAYBE };
OverrideKind getOverrideKind(FunctionDecl const * decl) {
CXXMethodDecl const * m = dyn_cast<CXXMethodDecl>(decl);
if (m == nullptr) {
return OverrideKind::NO;
}
if (m->size_overridden_methods() != 0 || m->hasAttr<OverrideAttr>()) {
return OverrideKind::YES;
}
if (!dyn_cast<CXXRecordDecl>(m->getDeclContext())->hasAnyDependentBases()) {
return OverrideKind::NO;
}
return OverrideKind::MAYBE;
}
//TODO: current implementation is not at all general, just tests what we
// encounter in practice:
bool hasBoolOverload(FunctionDecl const * decl, bool mustBeDeleted) {
unsigned n = decl->getNumParams();
auto res = decl->getDeclContext()->lookup(decl->getDeclName());
for (auto d = compat::begin(res); d != compat::end(res); ++d) {
FunctionDecl const * f = dyn_cast<FunctionDecl>(*d);
if (f != nullptr && (!mustBeDeleted || f->isDeleted())) {
if (f->getNumParams() == n) {
bool hasSB = false;
for (unsigned i = 0; i != n; ++i) {
QualType t1 { decl->getParamDecl(i)->getType() };
bool isSB = isSalBool(t1);
bool isSBRef = !isSB && t1->isReferenceType()
&& isSalBool(t1.getNonReferenceType());
QualType t2 { f->getParamDecl(i)->getType() };
if (!(isSB
? t2->isBooleanType()
: isSBRef
? (t2->isReferenceType()
&& t2.getNonReferenceType()->isBooleanType())
: t2 == t1))
{
goto next;
}
hasSB |= isSB || isSBRef;
}
return hasSB;
// cheaply protect against the case where decl would have no
// sal_Bool parameters at all and would match itself
next:;
}
}
}
return false;
}
class SalBool:
public RecursiveASTVisitor<SalBool>, public loplugin::RewritePlugin
{
public:
explicit SalBool(InstantiationData const & data): RewritePlugin(data) {}
virtual void run() override;
bool VisitUnaryAddrOf(UnaryOperator const * op);
bool VisitCallExpr(CallExpr * expr);
bool VisitCStyleCastExpr(CStyleCastExpr * expr);
bool VisitCXXStaticCastExpr(CXXStaticCastExpr * expr);
bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr * expr);
bool WalkUpFromParmVarDecl(ParmVarDecl const * decl);
bool VisitParmVarDecl(ParmVarDecl const * decl);
bool WalkUpFromVarDecl(VarDecl const * decl);
bool VisitVarDecl(VarDecl const * decl);
bool WalkUpFromFieldDecl(FieldDecl const * decl);
bool VisitFieldDecl(FieldDecl const * decl);
bool WalkUpFromFunctionDecl(FunctionDecl const * decl);
bool VisitFunctionDecl(FunctionDecl const * decl);
bool VisitValueDecl(ValueDecl const * decl);
private:
bool isInSpecialMainFile(SourceLocation spellingLocation) const;
bool rewrite(SourceLocation location);
std::set<VarDecl const *> varDecls_;
};
void SalBool::run() {
if (compiler.getLangOpts().CPlusPlus) {
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
for (auto decl: varDecls_) {
SourceLocation loc { decl->getLocStart() };
TypeSourceInfo * tsi = decl->getTypeSourceInfo();
if (tsi != nullptr) {
SourceLocation l {
compiler.getSourceManager().getExpansionLoc(
tsi->getTypeLoc().getBeginLoc()) };
SourceLocation end {
compiler.getSourceManager().getExpansionLoc(
tsi->getTypeLoc().getEndLoc()) };
assert(l.isFileID() && end.isFileID());
if (l == end
|| compiler.getSourceManager().isBeforeInTranslationUnit(
l, end))
{
for (;;) {
unsigned n = Lexer::MeasureTokenLength(
l, compiler.getSourceManager(),
compiler.getLangOpts());
std::string s {
compiler.getSourceManager().getCharacterData(l),
n };
if (s == "sal_Bool") {
loc = l;
break;
}
if (l == end) {
break;
}
l = l.getLocWithOffset(std::max<unsigned>(n, 1));
}
}
}
if (!rewrite(loc)) {
report(
DiagnosticsEngine::Warning,
"VarDecl, use \"bool\" instead of \"sal_Bool\"", loc)
<< decl->getSourceRange();
}
}
}
}
bool SalBool::VisitUnaryAddrOf(UnaryOperator const * op) {
Expr const * e1 = op->getSubExpr()->IgnoreParenCasts();
if (isSalBool(e1->getType())) {
DeclRefExpr const * e2 = dyn_cast<DeclRefExpr>(e1);
if (e2 != nullptr) {
VarDecl const * d = dyn_cast<VarDecl>(e2->getDecl());
if (d != nullptr) {
varDecls_.erase(d);
}
}
}
return true;
}
bool SalBool::VisitCallExpr(CallExpr * expr) {
Decl const * d = expr->getCalleeDecl();
FunctionProtoType const * ft = nullptr;
if (d != nullptr) {
FunctionDecl const * fd = dyn_cast<FunctionDecl>(d);
if (fd != nullptr) {
PointerType const * pt = fd->getType()->getAs<PointerType>();
QualType t2(pt == nullptr ? fd->getType() : pt->getPointeeType());
ft = t2->getAs<FunctionProtoType>();
assert(
ft != nullptr || !compiler.getLangOpts().CPlusPlus
|| (fd->getBuiltinID() != Builtin::NotBuiltin
&& isa<FunctionNoProtoType>(t2)));
// __builtin_*s have no proto type?
} else {
VarDecl const * vd = dyn_cast<VarDecl>(d);
if (vd != nullptr) {
PointerType const * pt = vd->getType()->getAs<PointerType>();
ft = (pt == nullptr ? vd->getType() : pt->getPointeeType())
->getAs<FunctionProtoType>();
}
}
}
if (ft != nullptr) {
for (unsigned i = 0; i != compat::getNumParams(*ft); ++i) {
QualType t(compat::getParamType(*ft, i));
if (t->isLValueReferenceType()) {
t = t.getNonReferenceType();
if (!t.isConstQualified() && isSalBool(t)
&& i < expr->getNumArgs())
{
DeclRefExpr * ref = dyn_cast<DeclRefExpr>(expr->getArg(i));
if (ref != nullptr) {
VarDecl const * d = dyn_cast<VarDecl>(ref->getDecl());
if (d != nullptr) {
varDecls_.erase(d);
}
}
}
}
}
}
return true;
}
bool SalBool::VisitCStyleCastExpr(CStyleCastExpr * expr) {
if (ignoreLocation(expr)) {
return true;
}
if (isSalBool(expr->getType())) {
SourceLocation loc { expr->getLocStart() };
while (compiler.getSourceManager().isMacroArgExpansion(loc)) {
loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc);
}
if (compat::isMacroBodyExpansion(compiler, loc)) {
StringRef name { Lexer::getImmediateMacroName(
loc, compiler.getSourceManager(), compiler.getLangOpts()) };
if (name == "sal_False" || name == "sal_True") {
return true;
}
}
report(
DiagnosticsEngine::Warning,
"CStyleCastExpr, suspicious cast from %0 to %1",
expr->getLocStart())
<< expr->getSubExpr()->IgnoreParenImpCasts()->getType()
<< expr->getType() << expr->getSourceRange();
}
return true;
}
bool SalBool::VisitCXXStaticCastExpr(CXXStaticCastExpr * expr) {
if (ignoreLocation(expr)) {
return true;
}
if (isSalBool(expr->getType())) {
report(
DiagnosticsEngine::Warning,
"CXXStaticCastExpr, suspicious cast from %0 to %1",
expr->getLocStart())
<< expr->getSubExpr()->IgnoreParenImpCasts()->getType()
<< expr->getType() << expr->getSourceRange();
}
return true;
}
bool SalBool::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr * expr) {
if (ignoreLocation(expr)) {
return true;
}
if (isSalBool(expr->getType())) {
report(
DiagnosticsEngine::Warning,
"CXXFunctionalCastExpr, suspicious cast from %0 to %1",
expr->getLocStart())
<< expr->getSubExpr()->IgnoreParenImpCasts()->getType()
<< expr->getType() << expr->getSourceRange();
}
return true;
}
bool SalBool::WalkUpFromParmVarDecl(ParmVarDecl const * decl) {
return VisitParmVarDecl(decl);
}
bool SalBool::VisitParmVarDecl(ParmVarDecl const * decl) {
if (ignoreLocation(decl)) {
return true;
}
if (isSalBool(decl->getType().getNonReferenceType())) {
FunctionDecl const * f = dyn_cast<FunctionDecl>(decl->getDeclContext());
if (f != nullptr) { // e.g.: typedef sal_Bool (* FuncPtr )( sal_Bool );
f = f->getCanonicalDecl();
if (!(hasCLanguageLinkageType(f)
|| (isInUnoIncludeFile(
compiler.getSourceManager().getSpellingLoc(
f->getNameInfo().getLoc()))
&& (!isInlined(*f) || f->hasAttr<DeprecatedAttr>()
|| decl->getType()->isReferenceType()
|| hasBoolOverload(f, false)))
|| f->isDeleted() || hasBoolOverload(f, true)))
{
OverrideKind k = getOverrideKind(f);
if (k != OverrideKind::YES) {
SourceLocation loc { decl->getLocStart() };
TypeSourceInfo * tsi = decl->getTypeSourceInfo();
if (tsi != nullptr) {
SourceLocation l {
compiler.getSourceManager().getExpansionLoc(
tsi->getTypeLoc().getBeginLoc()) };
SourceLocation end {
compiler.getSourceManager().getExpansionLoc(
tsi->getTypeLoc().getEndLoc()) };
assert(l.isFileID() && end.isFileID());
if (l == end
|| (compiler.getSourceManager()
.isBeforeInTranslationUnit(l, end)))
{
for (;;) {
unsigned n = Lexer::MeasureTokenLength(
l, compiler.getSourceManager(),
compiler.getLangOpts());
std::string s {
compiler.getSourceManager().getCharacterData(l),
n };
if (s == "sal_Bool") {
loc = l;
break;
}
if (l == end) {
break;
}
l = l.getLocWithOffset(std::max<unsigned>(n, 1));
}
}
}
// Only rewrite declarations in include files if a
// definition is also seen, to avoid compilation of a
// definition (in a main file only processed later) to fail
// with a "mismatch" error before the rewriter had a chance
// to act upon the definition (but use the heuristic of
// assuming pure virtual functions do not have definitions);
// also, do not automatically rewrite functions that could
// implicitly override depend base functions (and thus stop
// doing so after the rewrite; note that this is less
// dangerous for return types than for parameter types,
// where the function would still implicitly override and
// cause a compilation error due to the incompatible return
// type):
if (!((compat::isInMainFile(
compiler.getSourceManager(),
compiler.getSourceManager().getSpellingLoc(
dyn_cast<FunctionDecl>(
decl->getDeclContext())
->getNameInfo().getLoc()))
|| f->isDefined() || f->isPure())
&& k != OverrideKind::MAYBE && rewrite(loc)))
{
report(
DiagnosticsEngine::Warning,
("ParmVarDecl, use \"bool\" instead of"
" \"sal_Bool\"%0"),
loc)
<< (k == OverrideKind::MAYBE
? (" (unless this member function overrides a"
" dependent base member function, even"
" though it is not marked 'override')")
: "")
<< decl->getSourceRange();
}
}
}
}
}
return true;
}
bool SalBool::WalkUpFromVarDecl(VarDecl const * decl) {
return VisitVarDecl(decl);
}
bool SalBool::VisitVarDecl(VarDecl const * decl) {
if (ignoreLocation(decl)) {
return true;
}
if (!decl->isExternC() && isSalBool(decl->getType())
&& !isInSpecialMainFile(
compiler.getSourceManager().getSpellingLoc(decl->getLocStart())))
{
varDecls_.insert(decl);
}
return true;
}
bool SalBool::WalkUpFromFieldDecl(FieldDecl const * decl) {
return VisitFieldDecl(decl);
}
bool SalBool::VisitFieldDecl(FieldDecl const * decl) {
if (ignoreLocation(decl)) {
return true;
}
if (isSalBool(decl->getType())) {
TagDecl const * td = dyn_cast<TagDecl>(decl->getDeclContext());
assert(td != nullptr);
if (!(((td->isStruct() || td->isUnion())
&& compat::isExternCContext(*td))
|| isInUnoIncludeFile(
compiler.getSourceManager().getSpellingLoc(
decl->getLocation()))))
{
SourceLocation loc { decl->getLocStart() };
TypeSourceInfo * tsi = decl->getTypeSourceInfo();
if (tsi != nullptr) {
SourceLocation l {
compiler.getSourceManager().getExpansionLoc(
tsi->getTypeLoc().getBeginLoc()) };
SourceLocation end {
compiler.getSourceManager().getExpansionLoc(
tsi->getTypeLoc().getEndLoc()) };
assert(l.isFileID() && end.isFileID());
if (l == end
|| compiler.getSourceManager().isBeforeInTranslationUnit(
l, end))
{
for (;;) {
unsigned n = Lexer::MeasureTokenLength(
l, compiler.getSourceManager(),
compiler.getLangOpts());
std::string s {
compiler.getSourceManager().getCharacterData(l),
n };
if (s == "sal_Bool") {
loc = l;
break;
}
if (l == end) {
break;
}
l = l.getLocWithOffset(std::max<unsigned>(n, 1));
}
}
}
if (!rewrite(loc)) {
report(
DiagnosticsEngine::Warning,
"FieldDecl, use \"bool\" instead of \"sal_Bool\"", loc)
<< decl->getSourceRange();
}
}
}
return true;
}
bool SalBool::WalkUpFromFunctionDecl(FunctionDecl const * decl) {
return VisitFunctionDecl(decl);
}
bool SalBool::VisitFunctionDecl(FunctionDecl const * decl) {
if (ignoreLocation(decl)) {
return true;
}
if (isSalBool(compat::getReturnType(*decl).getNonReferenceType())) {
FunctionDecl const * f = decl->getCanonicalDecl();
OverrideKind k = getOverrideKind(f);
if (k != OverrideKind::YES
&& !(hasCLanguageLinkageType(f)
|| (isInUnoIncludeFile(
compiler.getSourceManager().getSpellingLoc(
f->getNameInfo().getLoc()))
&& (!isInlined(*f) || f->hasAttr<DeprecatedAttr>()))))
{
SourceLocation loc { decl->getLocStart() };
SourceLocation l { compiler.getSourceManager().getExpansionLoc(
loc) };
SourceLocation end { compiler.getSourceManager().getExpansionLoc(
decl->getNameInfo().getLoc()) };
assert(l.isFileID() && end.isFileID());
if (compiler.getSourceManager().isBeforeInTranslationUnit(l, end)) {
while (l != end) {
unsigned n = Lexer::MeasureTokenLength(
l, compiler.getSourceManager(), compiler.getLangOpts());
std::string s {
compiler.getSourceManager().getCharacterData(l), n };
if (s == "sal_Bool") {
loc = l;
break;
}
l = l.getLocWithOffset(std::max<unsigned>(n, 1));
}
}
// Only rewrite declarations in include files if a definition is
// also seen, to avoid compilation of a definition (in a main file
// only processed later) to fail with a "mismatch" error before the
// rewriter had a chance to act upon the definition (but use the
// heuristic of assuming pure virtual functions do not have
// definitions):
if (!((compat::isInMainFile(
compiler.getSourceManager(),
compiler.getSourceManager().getSpellingLoc(
decl->getNameInfo().getLoc()))
|| f->isDefined() || f->isPure())
&& rewrite(loc)))
{
report(
DiagnosticsEngine::Warning,
"use \"bool\" instead of \"sal_Bool\" as return type%0",
loc)
<< (k == OverrideKind::MAYBE
? (" (unless this member function overrides a dependent"
" base member function, even though it is not marked"
" 'override')")
: "")
<< decl->getSourceRange();
}
}
}
return true;
}
bool SalBool::VisitValueDecl(ValueDecl const * decl) {
if (ignoreLocation(decl)) {
return true;
}
if (isSalBool(decl->getType()) && !rewrite(decl->getLocStart())) {
report(
DiagnosticsEngine::Warning,
"ValueDecl, use \"bool\" instead of \"sal_Bool\"",
decl->getLocStart())
<< decl->getSourceRange();
}
return true;
}
bool SalBool::isInSpecialMainFile(SourceLocation spellingLocation) const {
return compat::isInMainFile(compiler.getSourceManager(), spellingLocation)
&& (compiler.getSourceManager().getFilename(spellingLocation)
== SRCDIR "/cppu/qa/test_any.cxx");
}
bool SalBool::rewrite(SourceLocation location) {
if (rewriter != nullptr) {
//TODO: "::sal_Bool" -> "bool", not "::bool"
SourceLocation loc { compiler.getSourceManager().getExpansionLoc(
location) };
unsigned n = Lexer::MeasureTokenLength(
loc, compiler.getSourceManager(), compiler.getLangOpts());
if (std::string(compiler.getSourceManager().getCharacterData(loc), n)
== "sal_Bool")
{
return replaceText(loc, n, "bool");
}
}
return false;
}
loplugin::Plugin::Registration<SalBool> X("salbool", true);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|