cppchecksymbols.cpp 30.1 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
class CollectSymbols: protected SymbolVisitor
Roberto Raggi's avatar
Roberto Raggi committed
61
62
63
64
{
    Document::Ptr _doc;
    Snapshot _snapshot;
    QSet<QByteArray> _types;
65
    QSet<QByteArray> _members;
Roberto Raggi's avatar
Roberto Raggi committed
66
    QSet<QByteArray> _virtualMethods;
67
    QSet<QByteArray> _statics;
Roberto Raggi's avatar
Roberto Raggi committed
68
69
70
    bool _mainDocument;

public:
Roberto Raggi's avatar
Roberto Raggi committed
71
    CollectSymbols(Document::Ptr doc, const Snapshot &snapshot)
Roberto Raggi's avatar
Roberto Raggi committed
72
73
74
75
76
77
78
79
80
81
82
        : _doc(doc), _snapshot(snapshot), _mainDocument(false)
    {
        QSet<Namespace *> processed;
        process(doc, &processed);
    }

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

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

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

93
94
95
96
97
    const QSet<QByteArray> &statics() const
    {
        return _statics;
    }

Roberto Raggi's avatar
Roberto Raggi committed
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
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()) {
126
127
            addType(q->base());
            addType(q->name());
Roberto Raggi's avatar
Roberto Raggi committed
128
129
130
131
132
133
134

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

        }
    }

135
136
137
138
139
140
141
142
143
144
145
146
    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
147
148
149
150
151
152
153
154
155
156
157
158
    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()));

        }
    }

159
160
161
162
163
164
165
166
167
168
169
170
    void addStatic(const Name *name)
    {
        if (! name) {
            return;

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

        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
171
172
173
174
175
176
177
178
    // 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
179
180
181
        if (symbol->isVirtual())
            addVirtualMethod(symbol->name());

Roberto Raggi's avatar
Roberto Raggi committed
182
183
184
        return true;
    }

Roberto Raggi's avatar
Roberto Raggi committed
185
    virtual bool visit(Block *)
Roberto Raggi's avatar
Roberto Raggi committed
186
187
188
189
190
191
192
193
194
195
196
197
    {
        return true;
    }

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

    virtual bool visit(Declaration *symbol)
    {
Roberto Raggi's avatar
Roberto Raggi committed
198
        if (symbol->enclosingEnum() != 0)
199
200
            addStatic(symbol->name());

Roberto Raggi's avatar
Roberto Raggi committed
201
202
203
204
205
        if (Function *funTy = symbol->type()->asFunctionType()) {
            if (funTy->isVirtual())
                addVirtualMethod(symbol->name());
        }

Roberto Raggi's avatar
Roberto Raggi committed
206
207
        if (symbol->isTypedef())
            addType(symbol->name());
Roberto Raggi's avatar
Roberto Raggi committed
208
        else if (! symbol->type()->isFunctionType() && symbol->scope()->isClass())
209
            addMember(symbol->name());
Roberto Raggi's avatar
Roberto Raggi committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

        return true;
    }

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
232
233
234
235
236
    virtual bool visit(Template *)
    {
        return true;
    }

Roberto Raggi's avatar
Roberto Raggi committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    virtual bool visit(Class *symbol)
    {
        addType(symbol->name());
        return true;
    }

    virtual bool visit(ForwardClassDeclaration *symbol)
    {
        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; }
Roberto Raggi's avatar
Roberto Raggi committed
253
    virtual bool visit(ObjCMethod *) { return true; }
Roberto Raggi's avatar
Roberto Raggi committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

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

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

    virtual bool visit(ObjCProtocol *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
282
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context)
283
284
{
    Q_ASSERT(doc);
285

Roberto Raggi's avatar
Roberto Raggi committed
286
    return (new CheckSymbols(doc, context))->start();
287
288
}

Roberto Raggi's avatar
Roberto Raggi committed
289
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context)
290
    : ASTVisitor(doc->translationUnit()), _doc(doc), _context(context)
291
{
Roberto Raggi's avatar
Roberto Raggi committed
292
    CollectSymbols collectTypes(doc, context.snapshot());
Roberto Raggi's avatar
Roberto Raggi committed
293
294

    _fileName = doc->fileName();
Roberto Raggi's avatar
Roberto Raggi committed
295
    _potentialTypes = collectTypes.types();
296
    _potentialMembers = collectTypes.members();
Roberto Raggi's avatar
Roberto Raggi committed
297
    _potentialVirtualMethods = collectTypes.virtualMethods();
298
    _potentialStatics = collectTypes.statics();
299
300
    _flushRequested = false;
    _flushLine = 0;
Roberto Raggi's avatar
Roberto Raggi committed
301
302

    typeOfExpression.init(_doc, _context.snapshot(), _context.bindings());
303
304
}

Roberto Raggi's avatar
Roberto Raggi committed
305
CheckSymbols::~CheckSymbols()
306
{ }
307

Roberto Raggi's avatar
Roberto Raggi committed
308
void CheckSymbols::run()
309
{
310
    _diagnosticMessages.clear();
311

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

    reportFinished();
320
321
}

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

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

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

338
339
    warning(line, column, text, length);
    return false;
340
341
}

Roberto Raggi's avatar
Roberto Raggi committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
FunctionDefinitionAST *CheckSymbols::enclosingFunctionDefinition() const
{
    for (int index = _astStack.size() - 1; index != -1; --index) {
        AST *ast = _astStack.at(index);

        if (FunctionDefinitionAST *funDef = ast->asFunctionDefinition())
            return funDef;
    }

    return 0;
}

TemplateDeclarationAST *CheckSymbols::enclosingTemplateDeclaration() const
{
    for (int index = _astStack.size() - 1; index != -1; --index) {
        AST *ast = _astStack.at(index);

        if (TemplateDeclarationAST *funDef = ast->asTemplateDeclaration())
            return funDef;
    }

    return 0;
}

Scope *CheckSymbols::enclosingScope() const
{
    for (int index = _astStack.size() - 1; index != -1; --index) {
        AST *ast = _astStack.at(index);

        if (NamespaceAST *ns = ast->asNamespace()) {
            if (ns->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
373
                return ns->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
374
375
376

        } else if (ClassSpecifierAST *classSpec = ast->asClassSpecifier()) {
            if (classSpec->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
377
                return classSpec->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
378
379
380

        } else if (FunctionDefinitionAST *funDef = ast->asFunctionDefinition()) {
            if (funDef->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
381
                return funDef->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
382
383
384

        } else if (CompoundStatementAST *blockStmt = ast->asCompoundStatement()) {
            if (blockStmt->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
385
                return blockStmt->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
386
387
388

        } else if (IfStatementAST *ifStmt = ast->asIfStatement()) {
            if (ifStmt->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
389
                return ifStmt->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
390
391
392

        } else if (WhileStatementAST *whileStmt = ast->asWhileStatement()) {
            if (whileStmt->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
393
                return whileStmt->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
394
395
396

        } else if (ForStatementAST *forStmt = ast->asForStatement()) {
            if (forStmt->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
397
                return forStmt->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
398
399
400

        } else if (ForeachStatementAST *foreachStmt = ast->asForeachStatement()) {
            if (foreachStmt->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
401
                return foreachStmt->symbol;
Roberto Raggi's avatar
Roberto Raggi committed
402

403
404
        } else if (SwitchStatementAST *switchStmt = ast->asSwitchStatement()) {
            if (switchStmt->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
405
                return switchStmt->symbol;
406
407
408

        } else if (CatchClauseAST *catchClause = ast->asCatchClause()) {
            if (catchClause->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
409
                return catchClause->symbol;
410

Roberto Raggi's avatar
Roberto Raggi committed
411
412
413
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
414
    return _doc->globalNamespace();
Roberto Raggi's avatar
Roberto Raggi committed
415
416
417
}

bool CheckSymbols::preVisit(AST *ast)
418
{
Roberto Raggi's avatar
Roberto Raggi committed
419
420
    _astStack.append(ast);

421
    if (isCanceled())
422
423
424
425
426
        return false;

    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
427
428
429
430
431
void CheckSymbols::postVisit(AST *)
{
    _astStack.takeLast();
}

Roberto Raggi's avatar
Roberto Raggi committed
432
bool CheckSymbols::visit(NamespaceAST *ast)
433
{
Roberto Raggi's avatar
Roberto Raggi committed
434
435
436
437
438
439
    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());
Roberto Raggi's avatar
Roberto Raggi committed
440
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
441
442
443
444
        }
    }

    return true;
445
446
}

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

452
453
454
455
456
457
bool CheckSymbols::visit(EnumeratorAST *ast)
{
    addUse(ast->identifier_token, Use::Static);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
458
459
460
461
462
463
464
465
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()) {
Roberto Raggi's avatar
Roberto Raggi committed
466
                        addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
467
                    } else if (maybeVirtualMethod(decl->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
468
                        addVirtualMethod(_context.lookup(decl->name(), decl->scope()), declId, funTy->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
469
470
471
472
473
474
                    }
                }
            }
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
475
476
    return true;
}
477

Roberto Raggi's avatar
Roberto Raggi committed
478
bool CheckSymbols::visit(NamedTypeSpecifierAST *)
Roberto Raggi's avatar
Roberto Raggi committed
479
{
Roberto Raggi's avatar
Roberto Raggi committed
480
    return true;
481
}
482

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
bool CheckSymbols::visit(ElaboratedTypeSpecifierAST *ast)
{
    accept(ast->attribute_list);

    if (ast->name) {
        if (const Name *name = ast->name->name) {
            if (name->isNameId() || name->isTemplateNameId()) {
                addUse(ast->name, Use::Type);
                return false;
            }
        }
    }

    accept(ast->name);
    return false;
}

500
501
502
bool CheckSymbols::visit(MemberAccessAST *ast)
{
    accept(ast->base_expression);
Roberto Raggi's avatar
Roberto Raggi committed
503
504
505
506
507
508
509
510
511
512
513
    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)) {
                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());

Roberto Raggi's avatar
Roberto Raggi committed
514
515
                const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(), TypeOfExpression::Preprocess);
                addClassMember(candidates, ast->member_name);
Roberto Raggi's avatar
Roberto Raggi committed
516
517
518
519
            }
        }
    }

520
521
522
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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)) {
                    const QByteArray expression = textOf(access);

Roberto Raggi's avatar
Roberto Raggi committed
538
539
                    const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(),
                                                                          TypeOfExpression::Preprocess);
540
541
542
543
544

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

Roberto Raggi's avatar
Roberto Raggi committed
545
                    addVirtualMethod(candidates, memberName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
546
547
548
549
550
                }
            }
        } else if (IdExpressionAST *idExpr = ast->base_expression->asIdExpression()) {
            if (const Name *name = idExpr->name->name) {
                if (maybeVirtualMethod(name)) {
551
552
553
554
                    NameAST *exprName = idExpr->name;
                    if (QualifiedNameAST *q = exprName->asQualifiedName())
                        exprName = q->unqualified_name;

Roberto Raggi's avatar
Roberto Raggi committed
555
556
                    const QList<LookupItem> candidates = typeOfExpression(textOf(idExpr), enclosingScope(),
                                                                          TypeOfExpression::Preprocess);
Roberto Raggi's avatar
Roberto Raggi committed
557

Roberto Raggi's avatar
Roberto Raggi committed
558
                    addVirtualMethod(candidates, exprName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
                }
            }
        }

        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
577
void CheckSymbols::checkNamespace(NameAST *name)
578
{
579
580
    if (! name)
        return;
581

582
583
    unsigned line, column;
    getTokenStartPosition(name->firstToken(), &line, &column);
584

Roberto Raggi's avatar
Roberto Raggi committed
585
    if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope())) {
586
587
588
        foreach (Symbol *s, b->symbols()) {
            if (s->isNamespace())
                return;
589
590
591
        }
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
596
597
598
599
600
601
602
bool CheckSymbols::hasVirtualDestructor(Class *klass) const
{
    if (! klass)
        return false;
    const Identifier *id = klass->identifier();
    if (! id)
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
603
    for (Symbol *s = klass->find(id); s; s = s->next()) {
Roberto Raggi's avatar
Roberto Raggi committed
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
        if (! s->name())
            continue;
        else if (s->name()->isDestructorNameId()) {
            if (Function *funTy = s->type()->asFunctionType()) {
                if (funTy->isVirtual() && id->isEqualTo(s->identifier()))
                    return true;
            }
        }
    }
    return false;
}

bool CheckSymbols::hasVirtualDestructor(ClassOrNamespace *binding) const
{
    QSet<ClassOrNamespace *> processed;
    QList<ClassOrNamespace *> todo;
    todo.append(binding);

    while (! todo.isEmpty()) {
        ClassOrNamespace *b = todo.takeFirst();
        if (b && ! processed.contains(b)) {
            processed.insert(b);
            foreach (Symbol *s, b->symbols()) {
                if (Class *k = s->asClass()) {
                    if (hasVirtualDestructor(k))
                        return true;
                }
            }

            todo += b->usings();
        }
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
640
void CheckSymbols::checkName(NameAST *ast, Scope *scope)
Roberto Raggi's avatar
Roberto Raggi committed
641
{
642
    if (ast && ast->name) {
Roberto Raggi's avatar
Roberto Raggi committed
643
        if (! scope)
Roberto Raggi's avatar
Roberto Raggi committed
644
645
            scope = enclosingScope();

Roberto Raggi's avatar
Roberto Raggi committed
646
647
        if (ast->asDestructorName() != 0 && scope->isClass()) {
            Class *klass = scope->asClass();
Roberto Raggi's avatar
Roberto Raggi committed
648
649
            if (hasVirtualDestructor(_context.lookupType(klass)))
                addUse(ast, Use::VirtualMethod);
650
        } else if (maybeType(ast->name) || maybeStatic(ast->name)) {
Roberto Raggi's avatar
Roberto Raggi committed
651
            const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
652
            addTypeOrStatic(candidates, ast);
Roberto Raggi's avatar
Roberto Raggi committed
653
654
655
        } else if (maybeMember(ast->name)) {
            const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
            addClassMember(candidates, ast);
Roberto Raggi's avatar
Roberto Raggi committed
656
657
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
658
}
Roberto Raggi's avatar
Roberto Raggi committed
659

Roberto Raggi's avatar
Roberto Raggi committed
660
bool CheckSymbols::visit(SimpleNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
661
662
{
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
663
664
665
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
666
bool CheckSymbols::visit(TemplateIdAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
667
{
Roberto Raggi's avatar
Roberto Raggi committed
668
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
669
670
671
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
672
bool CheckSymbols::visit(DestructorNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
673
{
674
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
675
676
677
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
678
bool CheckSymbols::visit(QualifiedNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
679
680
681
682
683
{
    if (ast->name) {
        ClassOrNamespace *b = 0;
        if (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list) {
            NestedNameSpecifierAST *nested_name_specifier = it->value;
684
            if (NameAST *class_or_namespace_name = nested_name_specifier->class_or_namespace_name) { // ### remove shadowing
Roberto Raggi's avatar
Roberto Raggi committed
685

686
687
688
                if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                    accept(template_id->template_argument_list);

689
                const Name *name = class_or_namespace_name->name;
Roberto Raggi's avatar
Roberto Raggi committed
690
691
                b = _context.lookupType(name, enclosingScope());
                addType(b, class_or_namespace_name);
Roberto Raggi's avatar
Roberto Raggi committed
692

693
694
                for (it = it->next; b && it; it = it->next) {
                    NestedNameSpecifierAST *nested_name_specifier = it->value;
Roberto Raggi's avatar
Roberto Raggi committed
695

696
697
698
                    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);
699

700
                        b = b->findType(class_or_namespace_name->name);
Roberto Raggi's avatar
Roberto Raggi committed
701
                        addType(b, class_or_namespace_name);
702
                    }
Roberto Raggi's avatar
Roberto Raggi committed
703
704
705
706
                }
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
707
        if (b && ast->unqualified_name) {
Roberto Raggi's avatar
Roberto Raggi committed
708
709
710
711
            if (ast->unqualified_name->asDestructorName() != 0) {
                if (hasVirtualDestructor(b))
                    addUse(ast->unqualified_name, Use::VirtualMethod);
            } else {
712
                addTypeOrStatic(b->find(ast->unqualified_name->name), ast->unqualified_name);
Roberto Raggi's avatar
Roberto Raggi committed
713
            }
Roberto Raggi's avatar
Roberto Raggi committed
714
        }
Roberto Raggi's avatar
Roberto Raggi committed
715
716
717
718
719
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
720
bool CheckSymbols::visit(TypenameTypeParameterAST *ast)
721
{
Roberto Raggi's avatar
Roberto Raggi committed
722
723
724
    addUse(ast->name, Use::Type);
    accept(ast->type_id);
    return false;
725
726
}

Roberto Raggi's avatar
Roberto Raggi committed
727
bool CheckSymbols::visit(TemplateTypeParameterAST *ast)
728
{
Roberto Raggi's avatar
Roberto Raggi committed
729
730
731
732
    accept(ast->template_parameter_list);
    addUse(ast->name, Use::Type);
    accept(ast->type_id);
    return false;
733
734
}

Roberto Raggi's avatar
Roberto Raggi committed
735
736
bool CheckSymbols::visit(MemInitializerAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
737
738
739
740
741
    if (FunctionDefinitionAST *enclosingFunction = enclosingFunctionDefinition()) {
        if (ast->name && enclosingFunction->symbol) {
            if (ClassOrNamespace *binding = _context.lookupType(enclosingFunction->symbol)) {
                foreach (Symbol *s, binding->symbols()) {
                    if (Class *klass = s->asClass()){
Roberto Raggi's avatar
Roberto Raggi committed
742
                        checkName(ast->name, klass);
Roberto Raggi's avatar
Roberto Raggi committed
743
744
                        break;
                    }
Roberto Raggi's avatar
Roberto Raggi committed
745
746
747
748
                }
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
749
750
        accept(ast->expression_list);
    }
Roberto Raggi's avatar
Roberto Raggi committed
751
752
753
754

    return false;
}

755
756
bool CheckSymbols::visit(FunctionDefinitionAST *ast)
{
757
    AST *thisFunction = _astStack.takeLast();
758
    accept(ast->decl_specifier_list);
759
    _astStack.append(thisFunction);
Roberto Raggi's avatar
Roberto Raggi committed
760

Roberto Raggi's avatar
Roberto Raggi committed
761
    if (ast->declarator && ast->symbol && ! ast->symbol->isGenerated()) {
Roberto Raggi's avatar
Roberto Raggi committed
762
763
764
765
766
767
        Function *fun = ast->symbol;
        if (NameAST *declId = declaratorId(ast->declarator)) {
            if (QualifiedNameAST *q = declId->asQualifiedName())
                declId = q->unqualified_name;

            if (fun->isVirtual()) {
Roberto Raggi's avatar
Roberto Raggi committed
768
                addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
769
            } else if (maybeVirtualMethod(fun->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
770
                addVirtualMethod(_context.lookup(fun->name(), fun->scope()), declId, fun->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
771
772
773
774
            }
        }
    }

775
776
777
778
    accept(ast->declarator);
    accept(ast->ctor_initializer);
    accept(ast->function_body);

779
780
781
782
    const LocalSymbols locals(_doc, ast);
    QList<SemanticInfo::Use> uses;
    foreach (uses, locals.uses) {
        foreach (const SemanticInfo::Use &u, uses)
Roberto Raggi's avatar
Roberto Raggi committed
783
            addUse(u);
784
785
    }

786
    flush();
787
788
789
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
void CheckSymbols::addUse(NameAST *ast, Use::Kind kind)
{
    if (! ast)
        return;

    if (QualifiedNameAST *q = ast->asQualifiedName())
        ast = q->unqualified_name;

    if (! ast)
        return; // nothing to do
    else if (ast->asOperatorFunctionId() != 0 || ast->asConversionFunctionId() != 0)
        return; // nothing to do

    unsigned startToken = ast->firstToken();

    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

808
809
810
811
812
813
814
815
816
    addUse(startToken, kind);
}

void CheckSymbols::addUse(unsigned tokenIndex, Use::Kind kind)
{
    if (! tokenIndex)
        return;

    const Token &tok = tokenAt(tokenIndex);
Roberto Raggi's avatar
Roberto Raggi committed
817
818
819
820
    if (tok.generated())
        return;

    unsigned line, column;
821
    getTokenStartPosition(tokenIndex, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
822
823
824
825
826
827
828
    const unsigned length = tok.length();

    const Use use(line, column, length, kind);
    addUse(use);
}

void CheckSymbols::addUse(const Use &use)
829
{
Roberto Raggi's avatar
Roberto Raggi committed
830
    if (! enclosingFunctionDefinition()) {
831
        if (_usages.size() >= 50) {
832
833
834
835
836
837
            if (_flushRequested && use.line != _flushLine)
                flush();
            else if (! _flushRequested) {
                _flushRequested = true;
                _flushLine = use.line;
            }
838
839
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
840

841
    _usages.append(use);
842
843
}

Roberto Raggi's avatar
Roberto Raggi committed
844
void CheckSymbols::addType(ClassOrNamespace *b, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
845
846
847
848
{
    if (! b)
        return;

849
850
851
852
853
    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
854
855
856
857
    if (tok.generated())
        return;

    unsigned line, column;
858
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
859
    const unsigned length = tok.length();
Roberto Raggi's avatar
Roberto Raggi committed
860
861
    const Use use(line, column, length, Use::Type);
    addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
862
863
864
    //qDebug() << "added use" << oo(ast->name) << line << column << length;
}

Roberto Raggi's avatar
Roberto Raggi committed
865
866
867
868
869
bool CheckSymbols::isTemplateClass(Symbol *symbol) const
{
    if (symbol) {
        if (Template *templ = symbol->asTemplate()) {
            if (Symbol *declaration = templ->declaration()) {
870
                if (declaration->isClass() || declaration->isForwardClassDeclaration())
Roberto Raggi's avatar
Roberto Raggi committed
871
872
873
874
875
876
877
                    return true;
            }
        }
    }
    return false;
}

878
void CheckSymbols::addTypeOrStatic(const QList<LookupItem> &candidates, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
879
{
880
881
882
883
884
    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
885
886
887
    if (tok.generated())
        return;

888
889
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
Roberto Raggi's avatar
Roberto Raggi committed
890
891
892
893
894
        if (c->isUsingDeclaration()) // skip using declarations...
            continue;
        else if (c->isUsingNamespaceDirective()) // ... and using namespace directives.
            continue;
        else if (c->isTypedef() || c->isNamespace() ||
Roberto Raggi's avatar
Roberto Raggi committed
895
                 c->isClass() || c->isEnum() || isTemplateClass(c) ||
Roberto Raggi's avatar
Roberto Raggi committed
896
                 c->isForwardClassDeclaration() || c->isTypenameArgument() || c->enclosingEnum() != 0) {
897
898
899
900
901

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

902
903
            Use::Kind kind = Use::Type;

Roberto Raggi's avatar
Roberto Raggi committed
904
            if (c->enclosingEnum() != 0)
905
906
907
                kind = Use::Static;

            const Use use(line, column, length, kind);
Roberto Raggi's avatar
Roberto Raggi committed
908
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
909
910
911
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
912
913
914
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
915
void CheckSymbols::addClassMember(const QList<LookupItem> &candidates, NameAST *ast)
916
917
918
919
920
921
922
923
924
{
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

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

925
926
927
928
929
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            continue;
        else if (! c->isDeclaration())
Roberto Raggi's avatar
Roberto Raggi committed
930
            return;
Roberto Raggi's avatar
Roberto Raggi committed
931
        else if (! (c->scope() && c->scope()->isClass()))
Roberto Raggi's avatar
Roberto Raggi committed
932
933
934
            return; // shadowed
        else if (c->isTypedef() || c->type()->isFunctionType())
            return; // shadowed
935

936
937
938
939
        unsigned line, column;
        getTokenStartPosition(startToken, &line, &column);
        const unsigned length = tok.length();

940
        const Use use(line, column, length, Use::Field);
Roberto Raggi's avatar
Roberto Raggi committed
941
        addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
942
943
944
945
        break;
    }
}

946
947
948
949
950
951
952
953
954
955
956
957
958
959
void CheckSymbols::addStatic(const QList<LookupItem> &candidates, NameAST *ast)
{
    if (ast->asDestructorName() != 0)
        return;

    unsigned startToken = ast->firstToken();
    const Token &tok = tokenAt(startToken);
    if (tok.generated())
        return;

    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            return;
Roberto Raggi's avatar
Roberto Raggi committed
960
        if (c->scope()->isEnum()) {
961
962
963
964
965
966
967
968
969
970
971
972
            unsigned line, column;
            getTokenStartPosition(startToken, &line, &column);
            const unsigned length = tok.length();

            const Use use(line, column, length, Use::Static);
            addUse(use);
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
973
void CheckSymbols::addVirtualMethod(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount)
Roberto Raggi's avatar
Roberto Raggi committed
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
{
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

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

    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;
995
996
997
998
999
1000
        else if (argumentCount > funTy->argumentCount()) {
            if (! funTy->isVariadic())
                continue;
        }

        unsigned line, column;
For faster browsing, not all history is shown. View entire blame