cppchecksymbols.cpp 26 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
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);
467
468
469
470
471
472

                    NameAST *memberName = access->member_name;
                    if (QualifiedNameAST *q = memberName->asQualifiedName())
                        memberName = q->unqualified_name;

                    addVirtualMethodUsage(candidates, memberName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
473
474
475
476
477
                }
            }
        } else if (IdExpressionAST *idExpr = ast->base_expression->asIdExpression()) {
            if (const Name *name = idExpr->name->name) {
                if (maybeVirtualMethod(name)) {
478
479
480
481
                    NameAST *exprName = idExpr->name;
                    if (QualifiedNameAST *q = exprName->asQualifiedName())
                        exprName = q->unqualified_name;

Roberto Raggi's avatar
Roberto Raggi committed
482
483
484
485
                    Scope *scope = findScope(idExpr);
                    const QByteArray expression = textOf(idExpr);

                    const QList<LookupItem> candidates = typeOfExpression(expression, scope, TypeOfExpression::Preprocess);
486
                    addVirtualMethodUsage(candidates, exprName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
                }
            }
        }

        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
505
void CheckSymbols::checkNamespace(NameAST *name)
506
{
507
508
    if (! name)
        return;
509

510
511
    unsigned line, column;
    getTokenStartPosition(name->firstToken(), &line, &column);
512

513
    Scope *enclosingScope = _doc->scopeAt(line, column);
514
515
516
517
    if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope)) {
        foreach (Symbol *s, b->symbols()) {
            if (s->isNamespace())
                return;
518
519
520
        }
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
525
void CheckSymbols::checkName(NameAST *ast, Scope *scope)
Roberto Raggi's avatar
Roberto Raggi committed
526
{
527
    if (ast && ast->name) {
Roberto Raggi's avatar
Roberto Raggi committed
528
529
530
        if (! scope)
            scope = findScope(ast);

531
532
533
        if (const Identifier *ident = ast->name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialTypes.contains(id)) {
534
                const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
535
                addUsage(candidates, ast);
536
            } else if (_potentialMembers.contains(id)) {
537
                const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
538
539
540
541
542
543
544
545
546
547
548
549
550
                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);
551
                const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
552
                addMemberUsage(candidates, ast);
553
            }
Roberto Raggi's avatar
Roberto Raggi committed
554
555
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
556
}
Roberto Raggi's avatar
Roberto Raggi committed
557

Roberto Raggi's avatar
Roberto Raggi committed
558
bool CheckSymbols::visit(SimpleNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
559
560
{
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
561
562
563
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
564
bool CheckSymbols::visit(TemplateIdAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
565
{
Roberto Raggi's avatar
Roberto Raggi committed
566
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
567
568
569
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
570
bool CheckSymbols::visit(DestructorNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
571
{
572
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
573
574
575
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
576
bool CheckSymbols::visit(QualifiedNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
577
578
{
    if (ast->name) {
579
        Scope *scope = findScope(ast);
Roberto Raggi's avatar
Roberto Raggi committed
580
581
582
583

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

586
587
588
                if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                    accept(template_id->template_argument_list);

589
590
                const Name *name = class_or_namespace_name->name;
                b = _context.lookupType(name, scope);
591
                addUsage(b, class_or_namespace_name);
Roberto Raggi's avatar
Roberto Raggi committed
592

593
594
                for (it = it->next; b && it; it = it->next) {
                    NestedNameSpecifierAST *nested_name_specifier = it->value;
Roberto Raggi's avatar
Roberto Raggi committed
595

596
597
598
                    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);
599

600
                        b = b->findType(class_or_namespace_name->name);
601
                        addUsage(b, class_or_namespace_name);
602
                    }
Roberto Raggi's avatar
Roberto Raggi committed
603
604
605
606
607
                }
            }
        }

        if (b && ast->unqualified_name)
608
            addUsage(b->find(ast->unqualified_name->name), ast->unqualified_name);
Roberto Raggi's avatar
Roberto Raggi committed
609
610
611
612
613
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
614
bool CheckSymbols::visit(TypenameTypeParameterAST *ast)
615
{
616
    if (ast->name && ast->name->name) {
617
618
619
620
        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());
621
                const QList<LookupItem> candidates = _context.lookup(ast->name->name, scope);
622
                addUsage(candidates, ast->name);
623
624
625
626
627
628
            }
        }
    }
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
629
bool CheckSymbols::visit(TemplateTypeParameterAST *ast)
630
631
632
633
634
{
    checkName(ast->name);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
635
bool CheckSymbols::visit(TemplateDeclarationAST *ast)
636
637
638
639
640
{
    _templateDeclarationStack.append(ast);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
641
void CheckSymbols::endVisit(TemplateDeclarationAST *)
642
643
644
645
{
    _templateDeclarationStack.takeFirst();
}

Roberto Raggi's avatar
Roberto Raggi committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
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;
}

668
669
670
bool CheckSymbols::visit(FunctionDefinitionAST *ast)
{
    _functionDefinitionStack.append(ast);
671
672

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

Roberto Raggi's avatar
Roberto Raggi committed
674
    if (ast->declarator && ast->symbol && ! ast->symbol->isGenerated()) {
Roberto Raggi's avatar
Roberto Raggi committed
675
676
677
678
679
680
681
682
683
684
685
686
687
        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());
            }
        }
    }

688
689
690
691
    accept(ast->declarator);
    accept(ast->ctor_initializer);
    accept(ast->function_body);

692
693
694
695
    const LocalSymbols locals(_doc, ast);
    QList<SemanticInfo::Use> uses;
    foreach (uses, locals.uses) {
        foreach (const SemanticInfo::Use &u, uses)
696
            addUsage(u);
697
698
699
    }

    _functionDefinitionStack.removeLast();
700
701

    flush();
702
703
704
    return false;
}

705
void CheckSymbols::addUsage(const Use &use)
706
{
707
    if (_functionDefinitionStack.isEmpty()) {
708
        if (_usages.size() >= 50) {
709
710
711
712
713
714
            if (_flushRequested && use.line != _flushLine)
                flush();
            else if (! _flushRequested) {
                _flushRequested = true;
                _flushLine = use.line;
            }
715
716
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
717

718
    _usages.append(use);
719
720
}

721
void CheckSymbols::addUsage(ClassOrNamespace *b, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
722
723
724
725
{
    if (! b)
        return;

726
727
728
729
730
    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
731
732
733
734
    if (tok.generated())
        return;

    unsigned line, column;
735
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
736
    const unsigned length = tok.length();
737
    const Use use(line, column, length);
738
    addUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
739
740
741
    //qDebug() << "added use" << oo(ast->name) << line << column << length;
}

742
void CheckSymbols::addUsage(const QList<LookupItem> &candidates, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
743
{
744
745
746
747
748
    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
749
750
751
752
    if (tok.generated())
        return;

    unsigned line, column;
753
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
754
755
    const unsigned length = tok.length();

756
757
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
Roberto Raggi's avatar
Roberto Raggi committed
758
759
760
761
762
763
764
        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()) {
765
            const Use use(line, column, length);
766
            addUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
767
768
769
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
770
771
772
    }
}

773
void CheckSymbols::addMemberUsage(const QList<LookupItem> &candidates, NameAST *ast)
774
775
776
777
778
779
780
781
782
783
784
785
786
{
    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();

787
788
789
790
791
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            continue;
        else if (! c->isDeclaration())
Roberto Raggi's avatar
Roberto Raggi committed
792
793
794
795
796
            return;
        else if (! (c->enclosingSymbol() && c->enclosingSymbol()->isClass()))
            return; // shadowed
        else if (c->isTypedef() || c->type()->isFunctionType())
            return; // shadowed
797
798

        const Use use(line, column, length, Use::Field);
799
        addUsage(use);
Roberto Raggi's avatar
Roberto Raggi committed
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
848
849
850
851
852
853
854
        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
855
856
857
    }
}

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

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

Roberto Raggi's avatar
Roberto Raggi committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
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
913
void CheckSymbols::flush()
Roberto Raggi's avatar
Roberto Raggi committed
914
{
915
916
917
    _flushRequested = false;
    _flushLine = 0;

918
    if (_usages.isEmpty())
Roberto Raggi's avatar
Roberto Raggi committed
919
920
        return;

921
922
    reportResults(_usages);
    _usages.clear();
Roberto Raggi's avatar
Roberto Raggi committed
923
}