cppchecksymbols.cpp 30.2 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());

182
183
184
185
186
187
        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
188
189
190
        return true;
    }

Roberto Raggi's avatar
Roberto Raggi committed
191
    virtual bool visit(Block *)
Roberto Raggi's avatar
Roberto Raggi committed
192
193
194
195
196
197
198
199
200
201
202
203
    {
        return true;
    }

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

    virtual bool visit(Declaration *symbol)
    {
204
205
206
        if (symbol->enclosingEnumScope() != 0)
            addStatic(symbol->name());

Roberto Raggi's avatar
Roberto Raggi committed
207
208
209
210
211
        if (Function *funTy = symbol->type()->asFunctionType()) {
            if (funTy->isVirtual())
                addVirtualMethod(symbol->name());
        }

Roberto Raggi's avatar
Roberto Raggi committed
212
213
        if (symbol->isTypedef())
            addType(symbol->name());
214
215
        else if (! symbol->type()->isFunctionType() && symbol->enclosingSymbol()->isClass())
            addMember(symbol->name());
Roberto Raggi's avatar
Roberto Raggi committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

        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;
    }

    virtual bool visit(Class *symbol)
    {
240
241
242
243
244
245
        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
246
247
248
249
250
251
        addType(symbol->name());
        return true;
    }

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

    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
295
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context)
296
297
{
    Q_ASSERT(doc);
298

Roberto Raggi's avatar
Roberto Raggi committed
299
    return (new CheckSymbols(doc, context))->start();
300
301
}

Roberto Raggi's avatar
Roberto Raggi committed
302
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context)
303
    : ASTVisitor(doc->translationUnit()), _doc(doc), _context(context)
304
{
Roberto Raggi's avatar
Roberto Raggi committed
305
    CollectSymbols collectTypes(doc, context.snapshot());
Roberto Raggi's avatar
Roberto Raggi committed
306
307

    _fileName = doc->fileName();
Roberto Raggi's avatar
Roberto Raggi committed
308
    _potentialTypes = collectTypes.types();
309
    _potentialMembers = collectTypes.members();
Roberto Raggi's avatar
Roberto Raggi committed
310
    _potentialVirtualMethods = collectTypes.virtualMethods();
311
    _potentialStatics = collectTypes.statics();
312
313
    _flushRequested = false;
    _flushLine = 0;
Roberto Raggi's avatar
Roberto Raggi committed
314
315

    typeOfExpression.init(_doc, _context.snapshot(), _context.bindings());
316
317
}

Roberto Raggi's avatar
Roberto Raggi committed
318
CheckSymbols::~CheckSymbols()
319
{ }
320

Roberto Raggi's avatar
Roberto Raggi committed
321
void CheckSymbols::run()
322
{
323
    _diagnosticMessages.clear();
324

325
326
327
328
329
    if (! isCanceled()) {
        if (_doc->translationUnit()) {
            accept(_doc->translationUnit()->ast());
            flush();
        }
Roberto Raggi's avatar
Roberto Raggi committed
330
    }
331
332

    reportFinished();
333
334
}

Roberto Raggi's avatar
Roberto Raggi committed
335
bool CheckSymbols::warning(unsigned line, unsigned column, const QString &text, unsigned length)
336
{
337
338
    Document::DiagnosticMessage m(Document::DiagnosticMessage::Warning, _fileName, line, column, text, length);
    _diagnosticMessages.append(m);
339
340
341
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
342
bool CheckSymbols::warning(AST *ast, const QString &text)
343
{
344
345
    const Token &firstToken = tokenAt(ast->firstToken());
    const Token &lastToken = tokenAt(ast->lastToken() - 1);
346

347
348
349
    const unsigned length = lastToken.end() - firstToken.begin();
    unsigned line = 1, column = 1;
    getTokenStartPosition(ast->firstToken(), &line, &column);
350

351
352
    warning(line, column, text, length);
    return false;
353
354
}

Roberto Raggi's avatar
Roberto Raggi committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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)
                return ns->symbol->members();

        } else if (ClassSpecifierAST *classSpec = ast->asClassSpecifier()) {
            if (classSpec->symbol)
                return classSpec->symbol->members();

        } else if (FunctionDefinitionAST *funDef = ast->asFunctionDefinition()) {
            if (funDef->symbol)
                return funDef->symbol->members();

        } else if (CompoundStatementAST *blockStmt = ast->asCompoundStatement()) {
            if (blockStmt->symbol)
                return blockStmt->symbol->members();

        } else if (IfStatementAST *ifStmt = ast->asIfStatement()) {
            if (ifStmt->symbol)
                return ifStmt->symbol->members();

        } else if (WhileStatementAST *whileStmt = ast->asWhileStatement()) {
            if (whileStmt->symbol)
                return whileStmt->symbol->members();

        } else if (ForStatementAST *forStmt = ast->asForStatement()) {
            if (forStmt->symbol)
                return forStmt->symbol->members();

        } else if (ForeachStatementAST *foreachStmt = ast->asForeachStatement()) {
            if (foreachStmt->symbol)
                return foreachStmt->symbol->members();

416
417
418
419
420
421
422
423
        } else if (SwitchStatementAST *switchStmt = ast->asSwitchStatement()) {
            if (switchStmt->symbol)
                return switchStmt->symbol->members();

        } else if (CatchClauseAST *catchClause = ast->asCatchClause()) {
            if (catchClause->symbol)
                return catchClause->symbol->members();

Roberto Raggi's avatar
Roberto Raggi committed
424
425
426
427
428
429
430
        }
    }

    return _doc->globalSymbols();
}

bool CheckSymbols::preVisit(AST *ast)
431
{
Roberto Raggi's avatar
Roberto Raggi committed
432
433
    _astStack.append(ast);

434
    if (isCanceled())
435
436
437
438
439
        return false;

    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
440
441
442
443
444
void CheckSymbols::postVisit(AST *)
{
    _astStack.takeLast();
}

Roberto Raggi's avatar
Roberto Raggi committed
445
bool CheckSymbols::visit(NamespaceAST *ast)
446
{
Roberto Raggi's avatar
Roberto Raggi committed
447
448
449
450
451
452
    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
453
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
454
455
456
457
        }
    }

    return true;
458
459
}

Roberto Raggi's avatar
Roberto Raggi committed
460
bool CheckSymbols::visit(UsingDirectiveAST *)
461
462
463
464
{
    return true;
}

465
466
467
468
469
470
bool CheckSymbols::visit(EnumeratorAST *ast)
{
    addUse(ast->identifier_token, Use::Static);
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
471
472
473
474
475
476
477
478
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
479
                        addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
480
                    } else if (maybeVirtualMethod(decl->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
481
                        addVirtualMethod(_context.lookup(decl->name(), decl->scope()), declId, funTy->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
482
483
484
485
486
487
                    }
                }
            }
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
488
489
    return true;
}
490

Roberto Raggi's avatar
Roberto Raggi committed
491
bool CheckSymbols::visit(NamedTypeSpecifierAST *)
Roberto Raggi's avatar
Roberto Raggi committed
492
{
Roberto Raggi's avatar
Roberto Raggi committed
493
    return true;
494
}
495

496
497
498
bool CheckSymbols::visit(MemberAccessAST *ast)
{
    accept(ast->base_expression);
Roberto Raggi's avatar
Roberto Raggi committed
499
500
501
502
503
504
505
506
507
508
509
    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
510
511
                const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(), TypeOfExpression::Preprocess);
                addClassMember(candidates, ast->member_name);
Roberto Raggi's avatar
Roberto Raggi committed
512
513
514
515
            }
        }
    }

516
517
518
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
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
534
535
                    const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(),
                                                                          TypeOfExpression::Preprocess);
536
537
538
539
540

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
554
                    addVirtualMethod(candidates, exprName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
                }
            }
        }

        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
573
void CheckSymbols::checkNamespace(NameAST *name)
574
{
575
576
    if (! name)
        return;
577

578
579
    unsigned line, column;
    getTokenStartPosition(name->firstToken(), &line, &column);
580

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

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

Roberto Raggi's avatar
Roberto Raggi committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
bool CheckSymbols::hasVirtualDestructor(Class *klass) const
{
    if (! klass)
        return false;
    const Identifier *id = klass->identifier();
    if (! id)
        return false;
    for (Symbol *s = klass->members()->lookat(id); s; s = s->next()) {
        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
636
void CheckSymbols::checkName(NameAST *ast, Scope *scope)
Roberto Raggi's avatar
Roberto Raggi committed
637
{
638
    if (ast && ast->name) {
Roberto Raggi's avatar
Roberto Raggi committed
639
        if (! scope)
Roberto Raggi's avatar
Roberto Raggi committed
640
641
            scope = enclosingScope();

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

Roberto Raggi's avatar
Roberto Raggi committed
656
bool CheckSymbols::visit(SimpleNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
657
658
{
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
659
660
661
    return true;
}

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

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

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

682
683
684
                if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                    accept(template_id->template_argument_list);

685
                const Name *name = class_or_namespace_name->name;
Roberto Raggi's avatar
Roberto Raggi committed
686
687
                b = _context.lookupType(name, enclosingScope());
                addType(b, class_or_namespace_name);
Roberto Raggi's avatar
Roberto Raggi committed
688

689
690
                for (it = it->next; b && it; it = it->next) {
                    NestedNameSpecifierAST *nested_name_specifier = it->value;
Roberto Raggi's avatar
Roberto Raggi committed
691

692
693
694
                    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);
695

696
                        b = b->findType(class_or_namespace_name->name);
Roberto Raggi's avatar
Roberto Raggi committed
697
                        addType(b, class_or_namespace_name);
698
                    }
Roberto Raggi's avatar
Roberto Raggi committed
699
700
701
702
                }
            }
        }

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

    return false;
}

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

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

Roberto Raggi's avatar
Roberto Raggi committed
731
732
bool CheckSymbols::visit(MemInitializerAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
733
734
735
736
737
738
739
740
    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()){
                        checkName(ast->name, klass->members());
                        break;
                    }
Roberto Raggi's avatar
Roberto Raggi committed
741
742
743
744
                }
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
745
746
        accept(ast->expression_list);
    }
Roberto Raggi's avatar
Roberto Raggi committed
747
748
749
750

    return false;
}

751
752
bool CheckSymbols::visit(FunctionDefinitionAST *ast)
{
753
    AST *thisFunction = _astStack.takeLast();
754
    accept(ast->decl_specifier_list);
755
    _astStack.append(thisFunction);
Roberto Raggi's avatar
Roberto Raggi committed
756

Roberto Raggi's avatar
Roberto Raggi committed
757
    if (ast->declarator && ast->symbol && ! ast->symbol->isGenerated()) {
Roberto Raggi's avatar
Roberto Raggi committed
758
759
760
761
762
763
        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
764
                addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
765
            } else if (maybeVirtualMethod(fun->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
766
                addVirtualMethod(_context.lookup(fun->name(), fun->scope()), declId, fun->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
767
768
769
770
            }
        }
    }

771
772
773
774
    accept(ast->declarator);
    accept(ast->ctor_initializer);
    accept(ast->function_body);

775
776
777
778
    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
779
            addUse(u);
780
781
    }

782
    flush();
783
784
785
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
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;

804
805
806
807
808
809
810
811
812
    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
813
814
815
816
    if (tok.generated())
        return;

    unsigned line, column;
817
    getTokenStartPosition(tokenIndex, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
818
819
820
821
822
823
824
    const unsigned length = tok.length();

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

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

837
    _usages.append(use);
838
839
}

Roberto Raggi's avatar
Roberto Raggi committed
840
void CheckSymbols::addType(ClassOrNamespace *b, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
841
842
843
844
{
    if (! b)
        return;

845
846
847
848
849
    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
850
851
852
853
    if (tok.generated())
        return;

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

861
void CheckSymbols::addTypeOrStatic(const QList<LookupItem> &candidates, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
862
{
863
864
865
866
867
    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
868
869
870
    if (tok.generated())
        return;

871
872
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
Roberto Raggi's avatar
Roberto Raggi committed
873
874
875
876
877
878
        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() ||
879
                 c->isForwardClassDeclaration() || c->isTypenameArgument() || c->enclosingEnumScope() != 0) {
880
881
882
883
884

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

885
886
887
888
889
890
            Use::Kind kind = Use::Type;

            if (c->enclosingEnumScope() != 0)
                kind = Use::Static;

            const Use use(line, column, length, kind);
Roberto Raggi's avatar
Roberto Raggi committed
891
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
892
893
894
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
895
896
897
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
898
void CheckSymbols::addClassMember(const QList<LookupItem> &candidates, NameAST *ast)
899
900
901
902
903
904
905
906
907
{
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

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

908
909
910
911
912
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            continue;
        else if (! c->isDeclaration())
Roberto Raggi's avatar
Roberto Raggi committed
913
914
915
916
917
            return;
        else if (! (c->enclosingSymbol() && c->enclosingSymbol()->isClass()))
            return; // shadowed
        else if (c->isTypedef() || c->type()->isFunctionType())
            return; // shadowed
918

919
920
921
922
        unsigned line, column;
        getTokenStartPosition(startToken, &line, &column);
        const unsigned length = tok.length();

923
        const Use use(line, column, length, Use::Field);
Roberto Raggi's avatar
Roberto Raggi committed
924
        addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
925
926
927
928
        break;
    }
}

929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
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;
        if (c->scope()->isEnumScope()) {
            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
956
void CheckSymbols::addVirtualMethod(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount)
Roberto Raggi's avatar
Roberto Raggi committed
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
{
    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;
978
979
980
981
982
983
984
985
        else if (argumentCount > funTy->argumentCount()) {
            if (! funTy->isVariadic())
                continue;
        }

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

        const Use use(line, column, length, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
988
        addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
989
        break;
Roberto Raggi's avatar
Roberto Raggi committed
990
991
992
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
993
NameAST *CheckSymbols::declaratorId(DeclaratorAST *ast) const
994
{
Roberto Raggi's avatar
Roberto Raggi committed
995
996
997
998
999
1000
    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;
        }
1001
1002
    }

Roberto Raggi's avatar
Roberto Raggi committed
1003
    return 0;
1004
1005
}

Roberto Raggi's avatar
Roberto Raggi committed
1006
bool CheckSymbols::maybeType(const Name *name) const
1007
{
Roberto Raggi's avatar
Roberto Raggi committed
1008
1009
1010
1011
1012
1013
    if (name) {
        if (const Identifier *ident = name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialTypes.contains(id))
                return true;
        }
1014
1015
    }

Roberto Raggi's avatar
Roberto Raggi committed
1016
    return false;
1017
}
Roberto Raggi's avatar
Roberto Raggi committed
1018

Roberto Raggi's avatar
Roberto Raggi committed
1019
bool CheckSymbols::maybeMember(const Name *name) const
Roberto Raggi's avatar
Roberto Raggi committed
1020
{
Roberto Raggi's avatar
Roberto Raggi committed
1021
1022
1023
1024
1025
    if (name) {
        if (const Identifier *ident = name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialMembers.contains(id))
                return true;
Roberto Raggi's avatar
Roberto Raggi committed
1026
1027
1028
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
1029
    return false;
Roberto Raggi's avatar
Roberto Raggi committed
1030
1031
}

1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
bool CheckSymbols::maybeStatic(const Name *name) const
{
    if (name) {
        if (const Identifier *ident = name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialStatics.contains(id))
                return true;
        }
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1045
1046
bool CheckSymbols::maybeVirtualMethod(const Name *name) const
{
Roberto Raggi's avatar
Roberto Raggi committed
1047
1048
1049
1050
1051
1052
    if (name) {
        if (const Identifier *ident = name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialVirtualMethods.contains(id))
                return true;
        }
Roberto Raggi's avatar
Roberto Raggi committed
1053
1054
1055
1056
1057
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1058
void CheckSymbols::flush()
Roberto Raggi's avatar
Roberto Raggi committed
1059
{
1060
1061
1062
    _flushRequested = false;
    _flushLine = 0;

1063
    if (_usages.isEmpty())
Roberto Raggi's avatar
Roberto Raggi committed
1064
1065
        return;

1066
1067
    reportResults(_usages);
    _usages.clear();
Roberto Raggi's avatar
Roberto Raggi committed
1068
}