summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/toolslong.cxx
blob: 26105a2697d8702de9ba1695d91582aa56870f24 (plain)
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* -*- 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 <limits>
#include <map>
#include <string>
#include <iostream>

#include "clang/AST/Attr.h"
#include "clang/Basic/Builtins.h"

#include "config_clang.h"

#include "check.hxx"
#include "compat.hxx"
#include "plugin.hxx"

namespace
{
bool isLong(QualType type)
{
    type = type.getNonReferenceType();
    // ignore sal_Int64
    if (type->getAs<TypedefType>())
        return false;
    // some parts of the STL have ::difference_type => long
    if (type->getAs<AutoType>() || type->getAs<DecltypeType>())
        return false;
#if CLANG_VERSION < 80000
    // Prior to <https://github.com/llvm/llvm-project/commit/
    // c50240dac133451b3eae5b89cecca4c1c4af9fd4> "[AST] Get aliased type info from an aliased
    // TemplateSpecialization" in Clang 8, if type is a TemplateSpecializationType on top of a
    // TypedefType, the above getAs<TypedefType> returned null (as it unconditionally desugared the
    // TemplateSpecializationType to the underlying canonic type, not to any aliased type), so re-
    // check with the TemplateSpecializationType's aliased type:
    if (auto const t = type->getAs<TemplateSpecializationType>())
    {
        if (t->isTypeAlias())
        {
            return isLong(t->getAliasedType());
        }
    }
#endif
    if (type->isSpecificBuiltinType(BuiltinType::Kind::Long))
        return true;
    auto arrayType = type->getAsArrayTypeUnsafe();
    if (arrayType)
        return isLong(arrayType->getElementType());
    if (type->isPointerType())
        return isLong(type->getPointeeType());
    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;
}

class ToolsLong : public loplugin::FilteringRewritePlugin<ToolsLong>
{
public:
    explicit ToolsLong(loplugin::InstantiationData const& data)
        : loplugin::FilteringRewritePlugin<ToolsLong>(data)
    {
    }

    virtual void run() override;

    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 VisitCallExpr(CallExpr const* expr);

private:
    bool rewrite(SourceLocation location);
    bool isExcludedFile(SourceLocation spellingLocation) const;
    /** sort by the reverse of source order, so we can do replacing from the end of the file backwards,
        which means we reduce the chances of having overlapping changes. */
    template <class T>
    std::vector<std::pair<T, bool>> reverseSourceOrder(std::map<T, bool> const& map) const
    {
        std::vector<std::pair<T, bool>> vec(map.begin(), map.end());
        std::sort(vec.begin(), vec.end(),
                  [&](std::pair<T, bool> const& lhs, std::pair<T, bool> const& rhs) {
                      return compiler.getSourceManager().getCharacterData(
                                 compat::getBeginLoc(lhs.first))
                             > compiler.getSourceManager().getCharacterData(
                                   compat::getBeginLoc(rhs.first));
                  });
        return vec;
    }

    std::map<VarDecl const*, bool> varDecls_;
    std::map<FieldDecl const*, bool> fieldDecls_;
    std::map<ParmVarDecl const*, bool> parmVarDecls_;
    std::map<FunctionDecl const*, bool> functionDecls_;
    std::map<CXXStaticCastExpr const*, bool> staticCasts_;
    std::map<CXXFunctionalCastExpr const*, bool> functionalCasts_;
};

void ToolsLong::run()
{
    if (!compiler.getLangOpts().CPlusPlus)
        return;

    StringRef fn(handler.getMainFileName());
    // sberg says this is fine
    if (loplugin::isSamePathname(fn, SRCDIR "/pyuno/source/module/pyuno.cxx")
        || loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/webdav-neon/NeonSession.cxx"))
        return;
    // these are places where the external API is actually "long"
    if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/filter/jpeg/JpegReader.cxx"))
        return;
    if (loplugin::isSamePathname(fn, SRCDIR "/writerperfect/source/common/DirectoryStream.cxx"))
        return;
    if (loplugin::isSamePathname(fn, SRCDIR "/writerperfect/source/common/WPXSvInputStream.cxx"))
        return;
    if (loplugin::isSamePathname(fn,
                                 SRCDIR "/writerperfect/source/calc/MSWorksCalcImportFilter.cxx"))
        return;
    if (loplugin::isSamePathname(fn, SRCDIR "/writerperfect/qa/unit/WPXSvStreamTest.cxx"))
        return;
    if (loplugin::isSamePathname(fn, SRCDIR "/desktop/source/lib/init.cxx"))
        return;

    TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());

    for (auto const& dcl : reverseSourceOrder(varDecls_))
    {
        auto const decl = dcl.first;
        SourceLocation loc{ compat::getBeginLoc(decl) };
        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 == "long")
                    {
                        loc = l;
                        break;
                    }
                    if (l == end)
                    {
                        break;
                    }
                    l = l.getLocWithOffset(std::max<unsigned>(n, 1));
                }
            }
        }
        if (!rewrite(loc))
        {
            report(DiagnosticsEngine::Warning, "VarDecl, use \"tools::Long\" instead of %0", loc)
                << decl->getType().getLocalUnqualifiedType() << decl->getSourceRange();
        }
    }
    for (auto const& dcl : reverseSourceOrder(fieldDecls_))
    {
        auto const decl = dcl.first;
        SourceLocation loc{ compat::getBeginLoc(decl) };
        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 == "long")
                    {
                        loc = l;
                        break;
                    }
                    if (l == end)
                    {
                        break;
                    }
                    l = l.getLocWithOffset(std::max<unsigned>(n, 1));
                }
            }
        }
        if (!rewrite(loc))
        {
            report(DiagnosticsEngine::Warning, "FieldDecl, use \"tools::Long\" instead of %0", loc)
                << decl->getType().getLocalUnqualifiedType() << decl->getSourceRange();
        }
    }
    for (auto const& dcl : reverseSourceOrder(parmVarDecls_))
    {
        auto const decl = dcl.first;
        SourceLocation loc{ compat::getBeginLoc(decl) };
        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 == "long")
                    {
                        loc = l;
                        break;
                    }
                    if (l == end)
                    {
                        break;
                    }
                    l = l.getLocWithOffset(std::max<unsigned>(n, 1));
                }
            }
        }
        FunctionDecl const* f = dyn_cast_or_null<FunctionDecl>(decl->getDeclContext());
        if (f)
            f = f->getCanonicalDecl();
        OverrideKind k = f ? getOverrideKind(f) : OverrideKind::NO;
        if (k == OverrideKind::MAYBE || !rewrite(loc))
        {
            report(DiagnosticsEngine::Warning,
                   ("ParmVarDecl, use \"tools::Long\" instead of"
                    " %0%1"),
                   loc)
                << decl->getType().getNonReferenceType().getLocalUnqualifiedType()
                << (k == OverrideKind::MAYBE ? (" (unless this member function overrides a"
                                                " dependent base member function, even"
                                                " though it is not marked 'override')")
                                             : "")
                << decl->getSourceRange();
        }
    }
    for (auto const& dcl : functionDecls_)
    {
        auto const decl = dcl.first;
        SourceLocation loc{ compat::getBeginLoc(decl) };
        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 == "long")
                {
                    loc = l;
                    break;
                }
                l = l.getLocWithOffset(std::max<unsigned>(n, 1));
            }
        }
        if (rewrite(loc))
            continue;
        report(DiagnosticsEngine::Warning, "use \"tools::Long\" instead of %0 as return type%1",
               loc)
            << decl->getReturnType().getNonReferenceType().getLocalUnqualifiedType()
            << (getOverrideKind(decl) == OverrideKind::MAYBE
                    ? (" (unless this member function overrides a dependent"
                       " base member function, even though it is not marked"
                       " 'override')")
                    : "")
            << decl->getSourceRange();
    }

    for (auto const& dcl : staticCasts_)
    {
        auto const expr = dcl.first;
        SourceLocation loc{ compat::getBeginLoc(expr) };
        TypeSourceInfo* tsi = expr->getTypeInfoAsWritten();
        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 == "long")
                    {
                        loc = l;
                        break;
                    }
                    if (l == end)
                    {
                        break;
                    }
                    l = l.getLocWithOffset(std::max<unsigned>(n, 1));
                }
            }
        }
        if (!rewrite(loc))
        {
            report(DiagnosticsEngine::Warning, "CXXStaticCastExpr, suspicious cast from %0 to %1",
                   compat::getBeginLoc(expr))
                << expr->getSubExpr()->IgnoreParenImpCasts()->getType() << expr->getType()
                << expr->getSourceRange();
        }
    }

    for (auto const& dcl : functionalCasts_)
    {
        auto const expr = dcl.first;
        SourceLocation loc{ compat::getBeginLoc(expr) };
        TypeSourceInfo* tsi = expr->getTypeInfoAsWritten();
        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 == "long")
                    {
                        loc = l;
                        break;
                    }
                    if (l == end)
                    {
                        break;
                    }
                    l = l.getLocWithOffset(std::max<unsigned>(n, 1));
                }
            }
        }
        if (!rewrite(loc))
        {
            report(DiagnosticsEngine::Warning,
                   "CXXFunctionalCastExpr, suspicious cast from %0 to %1",
                   compat::getBeginLoc(expr))
                << expr->getSubExpr()->IgnoreParenImpCasts()->getType() << expr->getType()
                << expr->getSourceRange();
        }
    }
}

bool ToolsLong::VisitCStyleCastExpr(CStyleCastExpr* expr)
{
    if (ignoreLocation(expr))
        return true;
    if (isExcludedFile(compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(expr))))
        return true;
    auto const k = isLong(expr->getType());
    if (!k)
        return true;
    SourceLocation loc{ compat::getBeginLoc(expr) };
    while (compiler.getSourceManager().isMacroArgExpansion(loc))
        loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc);
    report(DiagnosticsEngine::Warning, "CStyleCastExpr, suspicious cast from %0 to %1",
           compat::getBeginLoc(expr))
        << expr->getSubExpr()->IgnoreParenImpCasts()->getType() << expr->getType()
        << expr->getSourceRange();
    return true;
}

bool ToolsLong::VisitCXXStaticCastExpr(CXXStaticCastExpr* expr)
{
    if (ignoreLocation(expr))
        return true;
    if (isExcludedFile(compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(expr))))
        return true;
    auto const k = isLong(expr->getType());
    if (!k)
        return true;
    staticCasts_.insert({ expr, k });
    return true;
}

bool ToolsLong::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr* expr)
{
    if (ignoreLocation(expr))
        return true;
    if (isExcludedFile(compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(expr))))
        return true;
    auto const k = isLong(expr->getType());
    if (!k)
        return true;
    functionalCasts_.insert({ expr, k });
    return true;
}

bool ToolsLong::WalkUpFromParmVarDecl(ParmVarDecl const* decl) { return VisitParmVarDecl(decl); }

bool ToolsLong::VisitParmVarDecl(ParmVarDecl const* decl)
{
    if (ignoreLocation(decl))
        return true;
    if (isExcludedFile(compiler.getSourceManager().getSpellingLoc(decl->getLocation())))
        return true;
    auto const fbk = isLong(decl->getType());
    if (!fbk)
        return true;
    FunctionDecl const* f = dyn_cast<FunctionDecl>(decl->getDeclContext());
    if (f) // e.g.: typedef sal_Bool (* FuncPtr )( sal_Bool );
    {
        // ignore the function in include/test/cppunitasserthelper.hxx
        if (f->getIdentifier() && f->getName() == "assertEquals")
            return true;
        auto canonicalF = f->getCanonicalDecl();
        if (canonicalF->isDeletedAsWritten() && isa<CXXConversionDecl>(canonicalF))
            return true;
        // 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):
        bool ok = canonicalF->isDefined() || canonicalF->isPure()
                  || compiler.getSourceManager().isInMainFile(
                         compiler.getSourceManager().getSpellingLoc(f->getNameInfo().getLoc()));
        if (!ok)
            return true;
    }
    parmVarDecls_.insert({ decl, fbk });
    return true;
}

bool ToolsLong::WalkUpFromVarDecl(VarDecl const* decl) { return VisitVarDecl(decl); }

bool ToolsLong::VisitVarDecl(VarDecl const* decl)
{
    if (ignoreLocation(decl))
        return true;
    if (isExcludedFile(compiler.getSourceManager().getSpellingLoc(decl->getLocation())))
        return true;
    auto k = isLong(decl->getType());
    if (!k)
        return true;
    varDecls_.insert({ decl, k });
    return true;
}

bool ToolsLong::WalkUpFromFieldDecl(FieldDecl const* decl) { return VisitFieldDecl(decl); }

bool ToolsLong::VisitFieldDecl(FieldDecl const* decl)
{
    if (ignoreLocation(decl))
        return true;
    if (isExcludedFile(compiler.getSourceManager().getSpellingLoc(decl->getLocation())))
        return true;
    auto k = isLong(decl->getType());
    if (!k)
        return true;
    TagDecl const* td = dyn_cast<TagDecl>(decl->getDeclContext());
    if (td == nullptr)
    {
        //TODO: ObjCInterface
        return true;
    }
    fieldDecls_.insert({ decl, k });
    return true;
}

bool ToolsLong::WalkUpFromFunctionDecl(FunctionDecl const* decl) { return VisitFunctionDecl(decl); }

bool ToolsLong::VisitFunctionDecl(FunctionDecl const* decl)
{
    if (ignoreLocation(decl))
        return true;
    if (isExcludedFile(compiler.getSourceManager().getSpellingLoc(decl->getLocation())))
        return true;
    auto const fbk = isLong(decl->getReturnType());
    if (!fbk)
        return true;
    if (decl->isDeletedAsWritten() && isa<CXXConversionDecl>(decl))
        return true;
    if (decl->isPure() || decl->isDefined()
        || compiler.getSourceManager().isInMainFile(
               compiler.getSourceManager().getSpellingLoc(decl->getNameInfo().getLoc())))
    {
        functionDecls_.insert({ decl, fbk });
    }
    return true;
}

bool ToolsLong::VisitCallExpr(CallExpr const* expr)
{
    if (ignoreLocation(expr))
    {
        return true;
    }
    auto const d1 = expr->getDirectCallee();
    if (d1 == nullptr || !loplugin::DeclCheck(d1).Function("curl_easy_getinfo").GlobalNamespace())
    {
        return true;
    }
    if (expr->getNumArgs() != 3)
    {
        return true;
    }
    //TODO: Check expr->getArg(1) is CURLINFO_RESPONSE_CODE
    auto const e1 = dyn_cast<UnaryOperator>(expr->getArg(2)->IgnoreParenImpCasts());
    if (e1 == nullptr || e1->getOpcode() != UO_AddrOf)
    {
        return true;
    }
    auto const e2 = dyn_cast<DeclRefExpr>(e1->getSubExpr()->IgnoreParenImpCasts());
    if (e2 == nullptr)
    {
        return true;
    }
    auto const d2 = e2->getDecl();
    if (auto const d3 = dyn_cast<ParmVarDecl>(d2))
    {
        parmVarDecls_.erase(d3);
    }
    else if (auto const d4 = dyn_cast<VarDecl>(d2))
    {
        varDecls_.erase(d4);
    }
    else if (auto const d5 = dyn_cast<FieldDecl>(d2))
    {
        fieldDecls_.erase(d5);
    }
    return true;
}

bool ToolsLong::rewrite(SourceLocation location)
{
    if (rewriter != nullptr)
    {
        SourceLocation loc{ compiler.getSourceManager().getExpansionLoc(location) };
        unsigned n
            = Lexer::MeasureTokenLength(loc, compiler.getSourceManager(), compiler.getLangOpts());
        if (std::string(compiler.getSourceManager().getCharacterData(loc), n) == "long")
        {
            return replaceText(loc, n, "tools::Long");
        }
    }
    return false;
}

bool ToolsLong::isExcludedFile(SourceLocation spellingLocation) const
{
    if (isInUnoIncludeFile(spellingLocation))
        return true;
    auto f = getFilenameOfLocation(spellingLocation);
    return loplugin::hasPathnamePrefix(f, SRCDIR "/include/cppu/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/cppuhelper/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/registry/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/osl/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/rtl/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/sal/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/salhelper/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/typelib/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/include/LibreOfficeKit/") // TODO
           || loplugin::hasPathnamePrefix(f, SRCDIR "/bridges/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/codemaker/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/configmgr/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/cppu/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/cppuhelper/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/external/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/libreofficekit/") // TODO
           || loplugin::hasPathnamePrefix(f, SRCDIR "/registry/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/rtl/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/sal/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/salhelper/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/soltools/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/unoidl/")
           || loplugin::hasPathnamePrefix(f, SRCDIR "/workdir/");
}

loplugin::Plugin::Registration<ToolsLong> X("toolslong", true);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */