cppchecksymbols.cpp 25.7 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
namespace {

54
55
56
57
58
59
class FriendlyThread: public QThread
{
public:
    using QThread::msleep;
};

Roberto Raggi's avatar
Roberto Raggi committed
60
61
62
63
64
class CollectTypes: protected SymbolVisitor
{
    Document::Ptr _doc;
    Snapshot _snapshot;
    QSet<QByteArray> _types;
65
    QSet<QByteArray> _members;
Roberto Raggi's avatar
Roberto Raggi committed
66
    QSet<QByteArray> _virtualMethods;
Roberto Raggi's avatar
Roberto Raggi committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    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;
    }

84
85
86
87
88
    const QSet<QByteArray> &members() const
    {
        return _members;
    }

Roberto Raggi's avatar
Roberto Raggi committed
89
90
91
92
93
    const QSet<QByteArray> &virtualMethods() const
    {
        return _virtualMethods;
    }

Roberto Raggi's avatar
Roberto Raggi committed
94
95
96
97
98
99
100
101
    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) {
102
103
104
            Scope *scope = scopes.at(i)->members();
            const unsigned start = scope->startOffset();
            const unsigned end = scope->endOffset();
Roberto Raggi's avatar
Roberto Raggi committed
105
106

            if (tokenOffset >= start && tokenOffset < end)
107
                return scope;
Roberto Raggi's avatar
Roberto Raggi committed
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
        }

        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()) {
141
142
            addType(q->base());
            addType(q->name());
Roberto Raggi's avatar
Roberto Raggi committed
143
144
145
146
147
148
149

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

        }
    }

150
151
152
153
154
155
156
157
158
159
160
161
    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
162
163
164
165
166
167
168
169
170
171
172
173
    void addVirtualMethod(const Name *name)
    {
        if (! name) {
            return;

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

        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    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)
    {
Roberto Raggi's avatar
Roberto Raggi committed
188
189
190
        if (symbol->isVirtual())
            addVirtualMethod(symbol->name());

191
192
193
194
195
196
        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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        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)
    {
Roberto Raggi's avatar
Roberto Raggi committed
215
216
217
218
219
        if (Function *funTy = symbol->type()->asFunctionType()) {
            if (funTy->isVirtual())
                addVirtualMethod(symbol->name());
        }

Roberto Raggi's avatar
Roberto Raggi committed
220
221
        if (symbol->isTypedef())
            addType(symbol->name());
222
223
        else if (! symbol->type()->isFunctionType() && symbol->enclosingSymbol()->isClass())
            addMember(symbol->name());
Roberto Raggi's avatar
Roberto Raggi committed
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

        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)
    {
250
251
252
253
254
255
        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
256
257
258
259
260
261
262
        addScope(symbol);
        addType(symbol->name());
        return true;
    }

    virtual bool visit(ForwardClassDeclaration *symbol)
    {
263
264
265
266
267
268
        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
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
        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
313
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context)
314
315
{
    Q_ASSERT(doc);
316

Roberto Raggi's avatar
Roberto Raggi committed
317
    return (new CheckSymbols(doc, context))->start();
318
319
}

Roberto Raggi's avatar
Roberto Raggi committed
320
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context)
321
    : ASTVisitor(doc->translationUnit()), _doc(doc), _context(context)
322
{
323
324
    _fileName = doc->fileName();
    CollectTypes collectTypes(doc, context.snapshot());
Roberto Raggi's avatar
Roberto Raggi committed
325
    _potentialTypes = collectTypes.types();
326
    _potentialMembers = collectTypes.members();
Roberto Raggi's avatar
Roberto Raggi committed
327
    _potentialVirtualMethods = collectTypes.virtualMethods();
Roberto Raggi's avatar
Roberto Raggi committed
328
    _scopes = collectTypes.scopes();
329
330
    _flushRequested = false;
    _flushLine = 0;
Roberto Raggi's avatar
Roberto Raggi committed
331
332

    typeOfExpression.init(_doc, _context.snapshot(), _context.bindings());
333
334
}

Roberto Raggi's avatar
Roberto Raggi committed
335
CheckSymbols::~CheckSymbols()
336
{ }
337

Roberto Raggi's avatar
Roberto Raggi committed
338
void CheckSymbols::run()
339
{
340
    _diagnosticMessages.clear();
341

342
343
344
345
346
    if (! isCanceled()) {
        if (_doc->translationUnit()) {
            accept(_doc->translationUnit()->ast());
            flush();
        }
Roberto Raggi's avatar
Roberto Raggi committed
347
    }
348
349

    reportFinished();
350
351
}

Roberto Raggi's avatar
Roberto Raggi committed
352
bool CheckSymbols::warning(unsigned line, unsigned column, const QString &text, unsigned length)
353
{
354
355
    Document::DiagnosticMessage m(Document::DiagnosticMessage::Warning, _fileName, line, column, text, length);
    _diagnosticMessages.append(m);
356
357
358
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
359
bool CheckSymbols::warning(AST *ast, const QString &text)
360
{
361
362
    const Token &firstToken = tokenAt(ast->firstToken());
    const Token &lastToken = tokenAt(ast->lastToken() - 1);
363

364
365
366
    const unsigned length = lastToken.end() - firstToken.begin();
    unsigned line = 1, column = 1;
    getTokenStartPosition(ast->firstToken(), &line, &column);
367

368
369
    warning(line, column, text, length);
    return false;
370
371
}

Roberto Raggi's avatar
Roberto Raggi committed
372
bool CheckSymbols::preVisit(AST *)
373
{
374
    if (isCanceled())
375
376
377
378
379
        return false;

    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
380
bool CheckSymbols::visit(NamespaceAST *ast)
381
{
Roberto Raggi's avatar
Roberto Raggi committed
382
383
384
385
386
387
    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());
388
            addUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
389
390
391
392
        }
    }

    return true;
393
394
}

Roberto Raggi's avatar
Roberto Raggi committed
395
bool CheckSymbols::visit(UsingDirectiveAST *)
396
397
398
399
{
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
bool CheckSymbols::visit(SimpleDeclarationAST *ast)
{
    if (ast->declarator_list && !ast->declarator_list->next) {
        if (ast->symbols && ! ast->symbols->next && !ast->symbols->value->isGenerated()) {
            Symbol *decl = ast->symbols->value;
            if (NameAST *declId = declaratorId(ast->declarator_list->value)) {
                if (Function *funTy = decl->type()->asFunctionType()) {
                    if (funTy->isVirtual()) {
                        addVirtualMethodUsage(declId);
                    } else if (maybeVirtualMethod(decl->name())) {
                        addVirtualMethodUsage(_context.lookup(decl->name(), decl->scope()), declId, funTy->argumentCount());
                    }
                }
            }
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
417
418
    return true;
}
419

Roberto Raggi's avatar
Roberto Raggi committed
420
bool CheckSymbols::visit(NamedTypeSpecifierAST *)
Roberto Raggi's avatar
Roberto Raggi committed
421
{
Roberto Raggi's avatar
Roberto Raggi committed
422
    return true;
423
}
424

425
426
427
bool CheckSymbols::visit(MemberAccessAST *ast)
{
    accept(ast->base_expression);
Roberto Raggi's avatar
Roberto Raggi committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    if (! ast->member_name)
        return false;

    if (const Name *name = ast->member_name->name) {
        if (const Identifier *ident = name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialMembers.contains(id)) {
                Scope *scope = findScope(ast);

                const Token start = tokenAt(ast->firstToken());
                const Token end = tokenAt(ast->lastToken() - 1);
                const QByteArray expression = _doc->source().mid(start.begin(), end.end() - start.begin());

                const QList<LookupItem> candidates = typeOfExpression(expression, scope, TypeOfExpression::Preprocess);
                addMemberUsage(candidates, ast->member_name);
            }
        }
    }

447
448
449
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
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
bool CheckSymbols::visit(CallAST *ast)
{
    if (ast->base_expression) {
        accept(ast->base_expression);

        unsigned argumentCount = 0;

        for (ExpressionListAST *it = ast->expression_list; it; it = it->next)
            ++argumentCount;

        if (MemberAccessAST *access = ast->base_expression->asMemberAccess()) {
            if (access->member_name && access->member_name->name) {
                if (maybeVirtualMethod(access->member_name->name)) {
                    Scope *scope = findScope(access);
                    const QByteArray expression = textOf(access);

                    const QList<LookupItem> candidates = typeOfExpression(expression, scope, TypeOfExpression::Preprocess);
                    addVirtualMethodUsage(candidates, access->member_name, argumentCount);
                }
            }
        } else if (IdExpressionAST *idExpr = ast->base_expression->asIdExpression()) {
            if (const Name *name = idExpr->name->name) {
                if (maybeVirtualMethod(name)) {
                    Scope *scope = findScope(idExpr);
                    const QByteArray expression = textOf(idExpr);

                    const QList<LookupItem> candidates = typeOfExpression(expression, scope, TypeOfExpression::Preprocess);
                    addVirtualMethodUsage(candidates, idExpr->name, argumentCount);
                }
            }
        }

        accept(ast->expression_list);
    }

    return false;
}

QByteArray CheckSymbols::textOf(AST *ast) const
{
    const Token start = tokenAt(ast->firstToken());
    const Token end = tokenAt(ast->lastToken() - 1);
    const QByteArray text = _doc->source().mid(start.begin(), end.end() - start.begin());
    return text;
}

Roberto Raggi's avatar
Roberto Raggi committed
496
void CheckSymbols::checkNamespace(NameAST *name)
497
{
498
499
    if (! name)
        return;
500

501
502
    unsigned line, column;
    getTokenStartPosition(name->firstToken(), &line, &column);
503

504
    Scope *enclosingScope = _doc->scopeAt(line, column);
505
506
507
508
    if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope)) {
        foreach (Symbol *s, b->symbols()) {
            if (s->isNamespace())
                return;
509
510
511
        }
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
516
void CheckSymbols::checkName(NameAST *ast, Scope *scope)
Roberto Raggi's avatar
Roberto Raggi committed
517
{
518
    if (ast && ast->name) {
Roberto Raggi's avatar
Roberto Raggi committed
519
520
521
        if (! scope)
            scope = findScope(ast);

522
523
524
        if (const Identifier *ident = ast->name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialTypes.contains(id)) {
525
                const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
526
                addUsage(candidates, ast);
527
            } else if (_potentialMembers.contains(id)) {
528
                const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
529
530
531
532
533
534
535
536
537
538
539
540
541
                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);
542
                const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
543
                addMemberUsage(candidates, ast);
544
            }
Roberto Raggi's avatar
Roberto Raggi committed
545
546
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
547
}
Roberto Raggi's avatar
Roberto Raggi committed
548

Roberto Raggi's avatar
Roberto Raggi committed
549
bool CheckSymbols::visit(SimpleNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
550
551
{
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
552
553
554
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
555
bool CheckSymbols::visit(TemplateIdAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
556
{
Roberto Raggi's avatar
Roberto Raggi committed
557
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
558
559
560
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
561
bool CheckSymbols::visit(DestructorNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
562
{
563
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
564
565
566
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
567
bool CheckSymbols::visit(QualifiedNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
568
569
{
    if (ast->name) {
570
        Scope *scope = findScope(ast);
Roberto Raggi's avatar
Roberto Raggi committed
571
572
573
574

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

577
578
579
                if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                    accept(template_id->template_argument_list);

580
581
                const Name *name = class_or_namespace_name->name;
                b = _context.lookupType(name, scope);
582
                addUsage(b, class_or_namespace_name);
Roberto Raggi's avatar
Roberto Raggi committed
583

584
585
                for (it = it->next; b && it; it = it->next) {
                    NestedNameSpecifierAST *nested_name_specifier = it->value;
Roberto Raggi's avatar
Roberto Raggi committed
586

587
588
589
                    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);
590

591
                        b = b->findType(class_or_namespace_name->name);
592
                        addUsage(b, class_or_namespace_name);
593
                    }
Roberto Raggi's avatar
Roberto Raggi committed
594
595
596
597
598
                }
            }
        }

        if (b && ast->unqualified_name)
599
            addUsage(b->find(ast->unqualified_name->name), ast->unqualified_name);
Roberto Raggi's avatar
Roberto Raggi committed
600
601
602
603
604
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
605
bool CheckSymbols::visit(TypenameTypeParameterAST *ast)
606
{
607
    if (ast->name && ast->name->name) {
608
609
610
611
        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());
612
                const QList<LookupItem> candidates = _context.lookup(ast->name->name, scope);
613
                addUsage(candidates, ast->name);
614
615
616
617
618
619
            }
        }
    }
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
620
bool CheckSymbols::visit(TemplateTypeParameterAST *ast)
621
622
623
624
625
{
    checkName(ast->name);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
626
bool CheckSymbols::visit(TemplateDeclarationAST *ast)
627
628
629
630
631
{
    _templateDeclarationStack.append(ast);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
632
void CheckSymbols::endVisit(TemplateDeclarationAST *)
633
634
635
636
{
    _templateDeclarationStack.takeFirst();
}

Roberto Raggi's avatar
Roberto Raggi committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
bool CheckSymbols::visit(MemInitializerAST *ast)
{
    if (_functionDefinitionStack.isEmpty())
        return false;

    if (ast->name) {
        FunctionDefinitionAST *enclosingFunction = _functionDefinitionStack.back();
        if (ClassOrNamespace *binding = _context.lookupType(enclosingFunction->symbol)) {
            foreach (Symbol *s, binding->symbols()) {
                if (Class *klass = s->asClass()){
                    checkName(ast->name, klass->members());
                    break;
                }
            }
        }
    }

    accept(ast->expression_list);

    return false;
}

659
660
661
bool CheckSymbols::visit(FunctionDefinitionAST *ast)
{
    _functionDefinitionStack.append(ast);
662
663

    accept(ast->decl_specifier_list);
Roberto Raggi's avatar
Roberto Raggi committed
664

Roberto Raggi's avatar
Roberto Raggi committed
665
    if (ast->declarator && ast->symbol && ! ast->symbol->isGenerated()) {
Roberto Raggi's avatar
Roberto Raggi committed
666
667
668
669
670
671
672
673
674
675
676
677
678
        Function *fun = ast->symbol;
        if (NameAST *declId = declaratorId(ast->declarator)) {
            if (QualifiedNameAST *q = declId->asQualifiedName())
                declId = q->unqualified_name;

            if (fun->isVirtual()) {
                addVirtualMethodUsage(declId);
            } else if (maybeVirtualMethod(fun->name())) {
                addVirtualMethodUsage(_context.lookup(fun->name(), fun->scope()), declId, fun->argumentCount());
            }
        }
    }

679
680
681
682
    accept(ast->declarator);
    accept(ast->ctor_initializer);
    accept(ast->function_body);

683
684
685
686
    const LocalSymbols locals(_doc, ast);
    QList<SemanticInfo::Use> uses;
    foreach (uses, locals.uses) {
        foreach (const SemanticInfo::Use &u, uses)
687
            addUsage(u);
688
689
690
    }

    _functionDefinitionStack.removeLast();
691
692

    flush();
693
694
695
    return false;
}

696
void CheckSymbols::addUsage(const Use &use)
697
{
698
    if (_functionDefinitionStack.isEmpty()) {
699
        if (_usages.size() >= 50) {
700
701
702
703
704
705
            if (_flushRequested && use.line != _flushLine)
                flush();
            else if (! _flushRequested) {
                _flushRequested = true;
                _flushLine = use.line;
            }
706
707
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
708

709
    _usages.append(use);
710
711
}

712
void CheckSymbols::addUsage(ClassOrNamespace *b, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
713
714
715
716
{
    if (! b)
        return;

717
718
719
720
721
    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
722
723
724
725
    if (tok.generated())
        return;

    unsigned line, column;
726
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
727
    const unsigned length = tok.length();
728
    const Use use(line, column, length);
729
    addUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
730
731
732
    //qDebug() << "added use" << oo(ast->name) << line << column << length;
}

733
void CheckSymbols::addUsage(const QList<LookupItem> &candidates, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
734
{
735
736
737
738
739
    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
740
741
742
743
    if (tok.generated())
        return;

    unsigned line, column;
744
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
745
746
    const unsigned length = tok.length();

747
748
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
Roberto Raggi's avatar
Roberto Raggi committed
749
750
751
752
753
754
755
        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()) {
756
            const Use use(line, column, length);
757
            addUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
758
759
760
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
761
762
763
    }
}

764
void CheckSymbols::addMemberUsage(const QList<LookupItem> &candidates, NameAST *ast)
765
766
767
768
769
770
771
772
773
774
775
776
777
{
    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();

778
779
780
781
782
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            continue;
        else if (! c->isDeclaration())
783
784
785
786
787
788
789
790
791
            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);
792
        addUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
        break;
    }
}

void CheckSymbols::addVirtualMethodUsage(NameAST *ast)
{
    if (! ast)
        return;

    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();

    const Use use(line, column, length, Use::VirtualMethod);
    addUsage(use);
}

void CheckSymbols::addVirtualMethodUsage(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount)
{
    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 (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            continue;

        Function *funTy = r.type()->asFunctionType();
        if (! funTy)
            continue;
        if (! funTy->isVirtual())
            continue;
        else if (argumentCount < funTy->minimumArgumentCount())
            continue;

        const Use use(line, column, length, Use::VirtualMethod);
        addUsage(use);
        break;
Roberto Raggi's avatar
Roberto Raggi committed
848
849
850
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
851
unsigned CheckSymbols::startOfTemplateDeclaration(TemplateDeclarationAST *ast) const
852
853
854
855
856
857
858
859
860
861
862
{
    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
863
Scope *CheckSymbols::findScope(AST *ast) const
864
865
866
867
868
869
870
871
872
873
874
875
876
{
    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)
877
        scope = _doc->globalSymbols();
878
879
880

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

Roberto Raggi's avatar
Roberto Raggi committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
NameAST *CheckSymbols::declaratorId(DeclaratorAST *ast) const
{
    if (ast && ast->core_declarator) {
        if (NestedDeclaratorAST *nested = ast->core_declarator->asNestedDeclarator())
            return declaratorId(nested->declarator);
        else if (DeclaratorIdAST *declId = ast->core_declarator->asDeclaratorId()) {
            return declId->name;
        }
    }

    return 0;
}

bool CheckSymbols::maybeVirtualMethod(const Name *name) const
{
    if (const Identifier *ident = name->identifier()) {
        const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
        if (_potentialVirtualMethods.contains(id))
            return true;
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
906
void CheckSymbols::flush()
Roberto Raggi's avatar
Roberto Raggi committed
907
{
908
909
910
    _flushRequested = false;
    _flushLine = 0;

911
    if (_usages.isEmpty())
Roberto Raggi's avatar
Roberto Raggi committed
912
913
        return;

914
915
    reportResults(_usages);
    _usages.clear();
Roberto Raggi's avatar
Roberto Raggi committed
916
}