cppchecksymbols.cpp 18.6 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
**
**************************************************************************/

Roberto Raggi's avatar
Roberto Raggi committed
30
#include "cppchecksymbols.h"
31
32
#include "cpplocalsymbols.h"

33
#include <cplusplus/Overview.h>
34
35
36
37
38
39
40

#include <Names.h>
#include <Literals.h>
#include <Symbols.h>
#include <TranslationUnit.h>
#include <Scope.h>
#include <AST.h>
Roberto Raggi's avatar
Roberto Raggi committed
41
#include <SymbolVisitor.h>
42

43
44
45
46
47
#include <QtCore/QCoreApplication>
#include <QtCore/QThreadPool>
#include <QtCore/QDebug>

#include <qtconcurrent/runextensions.h>
48

49
using namespace CPlusPlus;
50
using namespace CppEditor::Internal;
51

Roberto Raggi's avatar
Roberto Raggi committed
52
53
54
55
56
57
58
namespace {

class CollectTypes: protected SymbolVisitor
{
    Document::Ptr _doc;
    Snapshot _snapshot;
    QSet<QByteArray> _types;
59
    QSet<QByteArray> _members;
Roberto Raggi's avatar
Roberto Raggi committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    QList<ScopedSymbol *> _scopes;
    QList<NameAST *> _names;
    bool _mainDocument;

public:
    CollectTypes(Document::Ptr doc, const Snapshot &snapshot)
        : _doc(doc), _snapshot(snapshot), _mainDocument(false)
    {
        QSet<Namespace *> processed;
        process(doc, &processed);
    }

    const QSet<QByteArray> &types() const
    {
        return _types;
    }

77
78
79
80
81
    const QSet<QByteArray> &members() const
    {
        return _members;
    }

Roberto Raggi's avatar
Roberto Raggi committed
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
    const QList<ScopedSymbol *> &scopes() const
    {
        return _scopes;
    }

    static Scope *findScope(unsigned tokenOffset, const QList<ScopedSymbol *> &scopes)
    {
        for (int i = scopes.size() - 1; i != -1; --i) {
            ScopedSymbol *symbol = scopes.at(i);
            const unsigned start = symbol->startOffset();
            const unsigned end = symbol->endOffset();

            if (tokenOffset >= start && tokenOffset < end)
                return symbol->members();
        }

        return 0;
    }

protected:
    void process(Document::Ptr doc, QSet<Namespace *> *processed)
    {
        if (! doc)
            return;
        else if (! processed->contains(doc->globalNamespace())) {
            processed->insert(doc->globalNamespace());

            foreach (const Document::Include &i, doc->includes())
                process(_snapshot.document(i.fileName()), processed);

            _mainDocument = (doc == _doc); // ### improve
            accept(doc->globalNamespace());
        }
    }

    void addType(const Identifier *id)
    {
        if (id)
            _types.insert(QByteArray::fromRawData(id->chars(), id->size()));
    }

    void addType(const Name *name)
    {
        if (! name) {
            return;

        } else if (const QualifiedNameId *q = name->asQualifiedNameId()) {
129
130
            addType(q->base());
            addType(q->name());
Roberto Raggi's avatar
Roberto Raggi committed
131
132
133
134
135
136
137

        } else if (name->isNameId() || name->isTemplateNameId()) {
            addType(name->identifier());

        }
    }

138
139
140
141
142
143
144
145
146
147
148
149
    void addMember(const Name *name)
    {
        if (! name) {
            return;

        } else if (name->isNameId()) {
            const Identifier *id = name->identifier();
            _members.insert(QByteArray::fromRawData(id->chars(), id->size()));

        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    void addScope(ScopedSymbol *symbol)
    {
        if (_mainDocument)
            _scopes.append(symbol);
    }

    // nothing to do
    virtual bool visit(UsingNamespaceDirective *) { return true; }
    virtual bool visit(UsingDeclaration *) { return true; }
    virtual bool visit(Argument *) { return true; }
    virtual bool visit(BaseClass *) { return true; }

    virtual bool visit(Function *symbol)
    {
164
165
166
167
168
169
        for (TemplateParameters *p = symbol->templateParameters(); p; p = p->previous()) {
            Scope *scope = p->scope();
            for (unsigned i = 0; i < scope->symbolCount(); ++i)
                accept(scope->symbolAt(i));
        }

Roberto Raggi's avatar
Roberto Raggi committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        addScope(symbol);
        return true;
    }

    virtual bool visit(Block *symbol)
    {
        addScope(symbol);
        return true;
    }

    virtual bool visit(NamespaceAlias *symbol)
    {
        addType(symbol->name());
        return true;
    }

    virtual bool visit(Declaration *symbol)
    {
        if (symbol->isTypedef())
            addType(symbol->name());
190
191
        else if (! symbol->type()->isFunctionType() && symbol->enclosingSymbol()->isClass())
            addMember(symbol->name());
Roberto Raggi's avatar
Roberto Raggi committed
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

        return true;
    }

    virtual bool visit(TypenameArgument *symbol)
    {
        addType(symbol->name());
        return true;
    }

    virtual bool visit(Enum *symbol)
    {
        addScope(symbol);
        addType(symbol->name());
        return true;
    }

    virtual bool visit(Namespace *symbol)
    {
        addScope(symbol);
        addType(symbol->name());
        return true;
    }

    virtual bool visit(Class *symbol)
    {
218
219
220
221
222
223
        for (TemplateParameters *p = symbol->templateParameters(); p; p = p->previous()) {
            Scope *scope = p->scope();
            for (unsigned i = 0; i < scope->symbolCount(); ++i)
                accept(scope->symbolAt(i));
        }

Roberto Raggi's avatar
Roberto Raggi committed
224
225
226
227
228
229
230
        addScope(symbol);
        addType(symbol->name());
        return true;
    }

    virtual bool visit(ForwardClassDeclaration *symbol)
    {
231
232
233
234
235
236
        for (TemplateParameters *p = symbol->templateParameters(); p; p = p->previous()) {
            Scope *scope = p->scope();
            for (unsigned i = 0; i < scope->symbolCount(); ++i)
                accept(scope->symbolAt(i));
        }

Roberto Raggi's avatar
Roberto Raggi committed
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
        addType(symbol->name());
        return true;
    }

    // Objective-C
    virtual bool visit(ObjCBaseClass *) { return true; }
    virtual bool visit(ObjCBaseProtocol *) { return true; }
    virtual bool visit(ObjCPropertyDeclaration *) { return true; }

    virtual bool visit(ObjCMethod *symbol)
    {
        addScope(symbol);
        return true;
    }

    virtual bool visit(ObjCClass *symbol)
    {
        addScope(symbol);
        addType(symbol->name());
        return true;
    }

    virtual bool visit(ObjCForwardClassDeclaration *symbol)
    {
        addType(symbol->name());
        return true;
    }

    virtual bool visit(ObjCProtocol *symbol)
    {
        addScope(symbol);
        addType(symbol->name());
        return true;
    }

    virtual bool visit(ObjCForwardProtocolDeclaration *symbol)
    {
        addType(symbol->name());
        return true;
    }
};

} // end of anonymous namespace

Roberto Raggi's avatar
Roberto Raggi committed
281
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context)
282
283
{
    Q_ASSERT(doc);
Roberto Raggi's avatar
Roberto Raggi committed
284
    return (new CheckSymbols(doc, context))->start();
285
286
}

Roberto Raggi's avatar
Roberto Raggi committed
287
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context)
288
    : ASTVisitor(doc->translationUnit()), _doc(doc), _context(context)
289
{
290
291
    _fileName = doc->fileName();
    CollectTypes collectTypes(doc, context.snapshot());
Roberto Raggi's avatar
Roberto Raggi committed
292
    _potentialTypes = collectTypes.types();
293
    _potentialMembers = collectTypes.members();
Roberto Raggi's avatar
Roberto Raggi committed
294
    _scopes = collectTypes.scopes();
295
296
    _flushRequested = false;
    _flushLine = 0;
297
298
}

Roberto Raggi's avatar
Roberto Raggi committed
299
CheckSymbols::~CheckSymbols()
300
{ }
301

Roberto Raggi's avatar
Roberto Raggi committed
302
void CheckSymbols::run()
303
304
305
306
307
308
309
{
    if (! isCanceled())
        runFunctor();

    reportFinished();
}

Roberto Raggi's avatar
Roberto Raggi committed
310
void CheckSymbols::runFunctor()
311
{
312
    _diagnosticMessages.clear();
313

Roberto Raggi's avatar
Roberto Raggi committed
314
    if (_doc->translationUnit()) {
315
        accept(_doc->translationUnit()->ast());
Roberto Raggi's avatar
Roberto Raggi committed
316
317
        flush();
    }
318
319
}

Roberto Raggi's avatar
Roberto Raggi committed
320
bool CheckSymbols::warning(unsigned line, unsigned column, const QString &text, unsigned length)
321
{
322
323
    Document::DiagnosticMessage m(Document::DiagnosticMessage::Warning, _fileName, line, column, text, length);
    _diagnosticMessages.append(m);
324
325
326
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
327
bool CheckSymbols::warning(AST *ast, const QString &text)
328
{
329
330
    const Token &firstToken = tokenAt(ast->firstToken());
    const Token &lastToken = tokenAt(ast->lastToken() - 1);
331

332
333
334
    const unsigned length = lastToken.end() - firstToken.begin();
    unsigned line = 1, column = 1;
    getTokenStartPosition(ast->firstToken(), &line, &column);
335

336
337
    warning(line, column, text, length);
    return false;
338
339
}

Roberto Raggi's avatar
Roberto Raggi committed
340
bool CheckSymbols::preVisit(AST *)
341
{
342
    if (isCanceled())
343
344
345
346
347
        return false;

    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
348
bool CheckSymbols::visit(NamespaceAST *ast)
349
{
Roberto Raggi's avatar
Roberto Raggi committed
350
351
352
353
354
355
    if (ast->identifier_token) {
        const Token &tok = tokenAt(ast->identifier_token);
        if (! tok.generated()) {
            unsigned line, column;
            getTokenStartPosition(ast->identifier_token, &line, &column);
            Use use(line, column, tok.length());
356
            addTypeUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
357
358
359
360
        }
    }

    return true;
361
362
}

Roberto Raggi's avatar
Roberto Raggi committed
363
bool CheckSymbols::visit(UsingDirectiveAST *)
364
365
366
367
{
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
368
bool CheckSymbols::visit(SimpleDeclarationAST *)
369
{
Roberto Raggi's avatar
Roberto Raggi committed
370
371
    return true;
}
372

Roberto Raggi's avatar
Roberto Raggi committed
373
bool CheckSymbols::visit(NamedTypeSpecifierAST *)
Roberto Raggi's avatar
Roberto Raggi committed
374
{
Roberto Raggi's avatar
Roberto Raggi committed
375
    return true;
376
}
377

378
379
380
381
382
383
bool CheckSymbols::visit(MemberAccessAST *ast)
{
    accept(ast->base_expression);
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
384
void CheckSymbols::checkNamespace(NameAST *name)
385
{
386
387
    if (! name)
        return;
388

389
390
    unsigned line, column;
    getTokenStartPosition(name->firstToken(), &line, &column);
391

392
    Scope *enclosingScope = _doc->scopeAt(line, column);
393
394
395
396
    if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope)) {
        foreach (Symbol *s, b->symbols()) {
            if (s->isNamespace())
                return;
397
398
399
        }
    }

400
401
    const unsigned length = tokenAt(name->lastToken() - 1).end() - tokenAt(name->firstToken()).begin();
    warning(line, column, QCoreApplication::translate("CheckUndefinedSymbols", "Expected a namespace-name"), length);
402
}
Roberto Raggi's avatar
Roberto Raggi committed
403

Roberto Raggi's avatar
Roberto Raggi committed
404
void CheckSymbols::checkName(NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
405
{
406
    if (ast && ast->name) {
407
408
409
410
411
412
        if (const Identifier *ident = ast->name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialTypes.contains(id)) {
                Scope *scope = findScope(ast);
                const QList<Symbol *> candidates = _context.lookup(ast->name, scope);
                addTypeUsage(candidates, ast);
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
            } else if (_potentialMembers.contains(id)) {
                Scope *scope = findScope(ast);
                const QList<Symbol *> candidates = _context.lookup(ast->name, scope);
                addMemberUsage(candidates, ast);
            }
        }
    }
}

void CheckSymbols::checkMemberName(NameAST *ast)
{
    if (ast && ast->name) {
        if (const Identifier *ident = ast->name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialMembers.contains(id)) {
                Scope *scope = findScope(ast);
                const QList<Symbol *> candidates = _context.lookup(ast->name, scope);
                addMemberUsage(candidates, ast);
            } else if (_potentialMembers.contains(id)) {
                Scope *scope = findScope(ast);
                const QList<Symbol *> candidates = _context.lookup(ast->name, scope);
                addMemberUsage(candidates, ast);
435
            }
Roberto Raggi's avatar
Roberto Raggi committed
436
437
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
438
}
Roberto Raggi's avatar
Roberto Raggi committed
439

Roberto Raggi's avatar
Roberto Raggi committed
440
bool CheckSymbols::visit(SimpleNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
441
442
{
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
443
444
445
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
446
bool CheckSymbols::visit(TemplateIdAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
447
{
Roberto Raggi's avatar
Roberto Raggi committed
448
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
449
450
451
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
452
bool CheckSymbols::visit(DestructorNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
453
{
454
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
455
456
457
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
458
bool CheckSymbols::visit(QualifiedNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
459
460
{
    if (ast->name) {
461
        Scope *scope = findScope(ast);
Roberto Raggi's avatar
Roberto Raggi committed
462
463
464
465

        ClassOrNamespace *b = 0;
        if (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list) {
            NestedNameSpecifierAST *nested_name_specifier = it->value;
466
            if (NameAST *class_or_namespace_name = nested_name_specifier->class_or_namespace_name) { // ### remove shadowing
Roberto Raggi's avatar
Roberto Raggi committed
467

468
469
470
                if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                    accept(template_id->template_argument_list);

471
472
473
                const Name *name = class_or_namespace_name->name;
                b = _context.lookupType(name, scope);
                addTypeUsage(b, class_or_namespace_name);
Roberto Raggi's avatar
Roberto Raggi committed
474

475
476
                for (it = it->next; b && it; it = it->next) {
                    NestedNameSpecifierAST *nested_name_specifier = it->value;
Roberto Raggi's avatar
Roberto Raggi committed
477

478
479
480
                    if (NameAST *class_or_namespace_name = nested_name_specifier->class_or_namespace_name) {
                        if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                            accept(template_id->template_argument_list);
481

482
483
484
                        b = b->findType(class_or_namespace_name->name);
                        addTypeUsage(b, class_or_namespace_name);
                    }
Roberto Raggi's avatar
Roberto Raggi committed
485
486
487
488
489
490
491
492
493
494
495
                }
            }
        }

        if (b && ast->unqualified_name)
            addTypeUsage(b->find(ast->unqualified_name->name), ast->unqualified_name);
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
496
bool CheckSymbols::visit(TypenameTypeParameterAST *ast)
497
{
498
    if (ast->name && ast->name->name) {
499
500
501
502
503
504
505
506
507
508
509
510
        if (const Identifier *templId = ast->name->name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(templId->chars(), templId->size());
            if (_potentialTypes.contains(id)) {
                Scope *scope = findScope(_templateDeclarationStack.back());
                const QList<Symbol *> candidates = _context.lookup(ast->name->name, scope);
                addTypeUsage(candidates, ast->name);
            }
        }
    }
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
511
bool CheckSymbols::visit(TemplateTypeParameterAST *ast)
512
513
514
515
516
{
    checkName(ast->name);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
517
bool CheckSymbols::visit(TemplateDeclarationAST *ast)
518
519
520
521
522
{
    _templateDeclarationStack.append(ast);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
523
void CheckSymbols::endVisit(TemplateDeclarationAST *)
524
525
526
527
{
    _templateDeclarationStack.takeFirst();
}

528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
bool CheckSymbols::visit(FunctionDefinitionAST *ast)
{
    _functionDefinitionStack.append(ast);
    const LocalSymbols locals(_doc, ast);
    QList<SemanticInfo::Use> uses;
    foreach (uses, locals.uses) {
        foreach (const SemanticInfo::Use &u, uses)
            addTypeUsage(u);
    }

    accept(ast->decl_specifier_list);
    accept(ast->declarator);
    accept(ast->ctor_initializer);
    accept(ast->function_body);

    _functionDefinitionStack.removeLast();
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
547
void CheckSymbols::addTypeUsage(const Use &use)
548
{
549
550
551
552
553
554
555
556
    if (_functionDefinitionStack.isEmpty()) {
        if (_typeUsages.size() >= 50) {
            if (_flushRequested && use.line != _flushLine)
                flush();
            else if (! _flushRequested) {
                _flushRequested = true;
                _flushLine = use.line;
            }
557
558
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
559

560
    _typeUsages.append(use);
561
562
}

Roberto Raggi's avatar
Roberto Raggi committed
563
void CheckSymbols::addTypeUsage(ClassOrNamespace *b, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
564
565
566
567
{
    if (! b)
        return;

568
569
570
571
572
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

    const Token &tok = tokenAt(startToken);
Roberto Raggi's avatar
Roberto Raggi committed
573
574
575
576
    if (tok.generated())
        return;

    unsigned line, column;
577
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
578
    const unsigned length = tok.length();
579
580
    const Use use(line, column, length);
    addTypeUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
581
582
583
    //qDebug() << "added use" << oo(ast->name) << line << column << length;
}

Roberto Raggi's avatar
Roberto Raggi committed
584
void CheckSymbols::addTypeUsage(const QList<Symbol *> &candidates, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
585
{
586
587
588
589
590
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

    const Token &tok = tokenAt(startToken);
Roberto Raggi's avatar
Roberto Raggi committed
591
592
593
594
    if (tok.generated())
        return;

    unsigned line, column;
595
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
596
597
598
    const unsigned length = tok.length();

    foreach (Symbol *c, candidates) {
Roberto Raggi's avatar
Roberto Raggi committed
599
600
601
602
603
604
605
        if (c->isUsingDeclaration()) // skip using declarations...
            continue;
        else if (c->isUsingNamespaceDirective()) // ... and using namespace directives.
            continue;
        else if (c->isTypedef() || c->isNamespace() ||
                 c->isClass() || c->isEnum() ||
                 c->isForwardClassDeclaration() || c->isTypenameArgument()) {
606
607
            const Use use(line, column, length);
            addTypeUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
608
609
610
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
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
    }
}

void CheckSymbols::addMemberUsage(const QList<Symbol *> &candidates, NameAST *ast)
{
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

    const Token &tok = tokenAt(startToken);
    if (tok.generated())
        return;

    unsigned line, column;
    getTokenStartPosition(startToken, &line, &column);
    const unsigned length = tok.length();

    foreach (Symbol *c, candidates) {
        if (! c->isDeclaration())
            continue;
        else if (c->isTypedef())
            continue;
        else if (c->type()->isFunctionType())
            continue;
        else if (! c->enclosingSymbol()->isClass())
            continue;

        const Use use(line, column, length, Use::Field);
        addTypeUsage(use);
        //qDebug() << "added use" << oo(ast->name) << line << column << length;
Roberto Raggi's avatar
Roberto Raggi committed
641
642
643
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
644
unsigned CheckSymbols::startOfTemplateDeclaration(TemplateDeclarationAST *ast) const
645
646
647
648
649
650
651
652
653
654
655
{
    if (ast->declaration) {
        if (TemplateDeclarationAST *templ = ast->declaration->asTemplateDeclaration())
            return startOfTemplateDeclaration(templ);

        return ast->declaration->firstToken();
    }

    return ast->firstToken();
}

Roberto Raggi's avatar
Roberto Raggi committed
656
Scope *CheckSymbols::findScope(AST *ast) const
657
658
659
660
661
662
663
664
665
666
667
668
669
{
    Scope *scope = 0;

    if (ast) {
        unsigned startToken = ast->firstToken();
        if (TemplateDeclarationAST *templ = ast->asTemplateDeclaration())
            startToken = startOfTemplateDeclaration(templ);

        const unsigned tokenOffset = tokenAt(startToken).offset;
        scope = CollectTypes::findScope(tokenOffset, _scopes);
    }

    if (! scope)
670
        scope = _doc->globalSymbols();
671
672
673

    return scope;
}
Roberto Raggi's avatar
Roberto Raggi committed
674

Roberto Raggi's avatar
Roberto Raggi committed
675
void CheckSymbols::flush()
Roberto Raggi's avatar
Roberto Raggi committed
676
{
677
678
679
    _flushRequested = false;
    _flushLine = 0;

Roberto Raggi's avatar
Roberto Raggi committed
680
681
682
    if (_typeUsages.isEmpty())
        return;

683
    reportResults(_typeUsages);
Roberto Raggi's avatar
Roberto Raggi committed
684
685
    _typeUsages.clear();
}