cppchecksymbols.cpp 26.5 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
    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;
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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()) {
121
122
            addType(q->base());
            addType(q->name());
Roberto Raggi's avatar
Roberto Raggi committed
123
124
125
126
127
128
129

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

        }
    }

130
131
132
133
134
135
136
137
138
139
140
141
    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
142
143
144
145
146
147
148
149
150
151
152
153
    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
154
155
156
157
158
159
160
161
    // 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
162
163
164
        if (symbol->isVirtual())
            addVirtualMethod(symbol->name());

165
166
167
168
169
170
        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
171
172
173
        return true;
    }

Roberto Raggi's avatar
Roberto Raggi committed
174
    virtual bool visit(Block *)
Roberto Raggi's avatar
Roberto Raggi committed
175
176
177
178
179
180
181
182
183
184
185
186
    {
        return true;
    }

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

    virtual bool visit(Declaration *symbol)
    {
Roberto Raggi's avatar
Roberto Raggi committed
187
188
189
190
191
        if (Function *funTy = symbol->type()->asFunctionType()) {
            if (funTy->isVirtual())
                addVirtualMethod(symbol->name());
        }

Roberto Raggi's avatar
Roberto Raggi committed
192
193
        if (symbol->isTypedef())
            addType(symbol->name());
194
195
        else if (! symbol->type()->isFunctionType() && symbol->enclosingSymbol()->isClass())
            addMember(symbol->name());
Roberto Raggi's avatar
Roberto Raggi committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

        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)
    {
220
221
222
223
224
225
        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
226
227
228
229
230
231
        addType(symbol->name());
        return true;
    }

    virtual bool visit(ForwardClassDeclaration *symbol)
    {
232
233
234
235
236
237
        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
238
239
240
241
242
243
244
245
        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
246
    virtual bool visit(ObjCMethod *) { return true; }
Roberto Raggi's avatar
Roberto Raggi committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

    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
275
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context)
276
277
{
    Q_ASSERT(doc);
278

Roberto Raggi's avatar
Roberto Raggi committed
279
    return (new CheckSymbols(doc, context))->start();
280
281
}

Roberto Raggi's avatar
Roberto Raggi committed
282
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context)
283
    : ASTVisitor(doc->translationUnit()), _doc(doc), _context(context)
284
{
285
    CollectTypes collectTypes(doc, context.snapshot());
Roberto Raggi's avatar
Roberto Raggi committed
286
287

    _fileName = doc->fileName();
Roberto Raggi's avatar
Roberto Raggi committed
288
    _potentialTypes = collectTypes.types();
289
    _potentialMembers = collectTypes.members();
Roberto Raggi's avatar
Roberto Raggi committed
290
    _potentialVirtualMethods = collectTypes.virtualMethods();
291
292
    _flushRequested = false;
    _flushLine = 0;
Roberto Raggi's avatar
Roberto Raggi committed
293
294

    typeOfExpression.init(_doc, _context.snapshot(), _context.bindings());
295
296
}

Roberto Raggi's avatar
Roberto Raggi committed
297
CheckSymbols::~CheckSymbols()
298
{ }
299

Roberto Raggi's avatar
Roberto Raggi committed
300
void CheckSymbols::run()
301
{
302
    _diagnosticMessages.clear();
303

304
305
306
307
308
    if (! isCanceled()) {
        if (_doc->translationUnit()) {
            accept(_doc->translationUnit()->ast());
            flush();
        }
Roberto Raggi's avatar
Roberto Raggi committed
309
    }
310
311

    reportFinished();
312
313
}

Roberto Raggi's avatar
Roberto Raggi committed
314
bool CheckSymbols::warning(unsigned line, unsigned column, const QString &text, unsigned length)
315
{
316
317
    Document::DiagnosticMessage m(Document::DiagnosticMessage::Warning, _fileName, line, column, text, length);
    _diagnosticMessages.append(m);
318
319
320
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
321
bool CheckSymbols::warning(AST *ast, const QString &text)
322
{
323
324
    const Token &firstToken = tokenAt(ast->firstToken());
    const Token &lastToken = tokenAt(ast->lastToken() - 1);
325

326
327
328
    const unsigned length = lastToken.end() - firstToken.begin();
    unsigned line = 1, column = 1;
    getTokenStartPosition(ast->firstToken(), &line, &column);
329

330
331
    warning(line, column, text, length);
    return false;
332
333
}

Roberto Raggi's avatar
Roberto Raggi committed
334
335
336
337
338
339
340
341
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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();

395
396
397
398
399
400
401
402
        } 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
403
404
405
406
407
408
409
        }
    }

    return _doc->globalSymbols();
}

bool CheckSymbols::preVisit(AST *ast)
410
{
Roberto Raggi's avatar
Roberto Raggi committed
411
412
    _astStack.append(ast);

413
    if (isCanceled())
414
415
416
417
418
        return false;

    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
419
420
421
422
423
void CheckSymbols::postVisit(AST *)
{
    _astStack.takeLast();
}

Roberto Raggi's avatar
Roberto Raggi committed
424
bool CheckSymbols::visit(NamespaceAST *ast)
425
{
Roberto Raggi's avatar
Roberto Raggi committed
426
427
428
429
430
431
    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
432
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
433
434
435
436
        }
    }

    return true;
437
438
}

Roberto Raggi's avatar
Roberto Raggi committed
439
bool CheckSymbols::visit(UsingDirectiveAST *)
440
441
442
443
{
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
444
445
446
447
448
449
450
451
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
452
                        addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
453
                    } else if (maybeVirtualMethod(decl->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
454
                        addVirtualMethod(_context.lookup(decl->name(), decl->scope()), declId, funTy->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
455
456
457
458
459
460
                    }
                }
            }
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
461
462
    return true;
}
463

Roberto Raggi's avatar
Roberto Raggi committed
464
bool CheckSymbols::visit(NamedTypeSpecifierAST *)
Roberto Raggi's avatar
Roberto Raggi committed
465
{
Roberto Raggi's avatar
Roberto Raggi committed
466
    return true;
467
}
468

469
470
471
bool CheckSymbols::visit(MemberAccessAST *ast)
{
    accept(ast->base_expression);
Roberto Raggi's avatar
Roberto Raggi committed
472
473
474
475
476
477
478
479
480
481
482
    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
483
484
                const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(), TypeOfExpression::Preprocess);
                addClassMember(candidates, ast->member_name);
Roberto Raggi's avatar
Roberto Raggi committed
485
486
487
488
            }
        }
    }

489
490
491
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
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
507
508
                    const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(),
                                                                          TypeOfExpression::Preprocess);
509
510
511
512
513

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

Roberto Raggi's avatar
Roberto Raggi committed
514
                    addVirtualMethod(candidates, memberName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
515
516
517
518
519
                }
            }
        } else if (IdExpressionAST *idExpr = ast->base_expression->asIdExpression()) {
            if (const Name *name = idExpr->name->name) {
                if (maybeVirtualMethod(name)) {
520
521
522
523
                    NameAST *exprName = idExpr->name;
                    if (QualifiedNameAST *q = exprName->asQualifiedName())
                        exprName = q->unqualified_name;

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

Roberto Raggi's avatar
Roberto Raggi committed
527
                    addVirtualMethod(candidates, exprName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
                }
            }
        }

        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
546
void CheckSymbols::checkNamespace(NameAST *name)
547
{
548
549
    if (! name)
        return;
550

551
552
    unsigned line, column;
    getTokenStartPosition(name->firstToken(), &line, &column);
553

Roberto Raggi's avatar
Roberto Raggi committed
554
    if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope())) {
555
556
557
        foreach (Symbol *s, b->symbols()) {
            if (s->isNamespace())
                return;
558
559
560
        }
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
565
void CheckSymbols::checkName(NameAST *ast, Scope *scope)
Roberto Raggi's avatar
Roberto Raggi committed
566
{
567
    if (ast && ast->name) {
Roberto Raggi's avatar
Roberto Raggi committed
568
        if (! scope)
Roberto Raggi's avatar
Roberto Raggi committed
569
570
571
572
573
574
575
576
            scope = enclosingScope();

        if (maybeType(ast->name)) {
            const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
            addType(candidates, ast);
        } else if (maybeMember(ast->name)) {
            const QList<LookupItem> candidates = _context.lookup(ast->name, scope);
            addClassMember(candidates, ast);
Roberto Raggi's avatar
Roberto Raggi committed
577
578
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
579
}
Roberto Raggi's avatar
Roberto Raggi committed
580

Roberto Raggi's avatar
Roberto Raggi committed
581
bool CheckSymbols::visit(SimpleNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
582
583
{
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
584
585
586
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
587
bool CheckSymbols::visit(TemplateIdAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
588
{
Roberto Raggi's avatar
Roberto Raggi committed
589
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
590
591
592
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
593
bool CheckSymbols::visit(DestructorNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
594
{
595
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
596
597
598
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
599
bool CheckSymbols::visit(QualifiedNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
600
601
602
603
604
{
    if (ast->name) {
        ClassOrNamespace *b = 0;
        if (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list) {
            NestedNameSpecifierAST *nested_name_specifier = it->value;
605
            if (NameAST *class_or_namespace_name = nested_name_specifier->class_or_namespace_name) { // ### remove shadowing
Roberto Raggi's avatar
Roberto Raggi committed
606

607
608
609
                if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                    accept(template_id->template_argument_list);

610
                const Name *name = class_or_namespace_name->name;
Roberto Raggi's avatar
Roberto Raggi committed
611
612
                b = _context.lookupType(name, enclosingScope());
                addType(b, class_or_namespace_name);
Roberto Raggi's avatar
Roberto Raggi committed
613

614
615
                for (it = it->next; b && it; it = it->next) {
                    NestedNameSpecifierAST *nested_name_specifier = it->value;
Roberto Raggi's avatar
Roberto Raggi committed
616

617
618
619
                    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);
620

621
                        b = b->findType(class_or_namespace_name->name);
Roberto Raggi's avatar
Roberto Raggi committed
622
                        addType(b, class_or_namespace_name);
623
                    }
Roberto Raggi's avatar
Roberto Raggi committed
624
625
626
627
                }
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
628
629
630
        if (b && ast->unqualified_name) {
            addType(b->find(ast->unqualified_name->name), ast->unqualified_name);
        }
Roberto Raggi's avatar
Roberto Raggi committed
631
632
633
634
635
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
636
bool CheckSymbols::visit(TypenameTypeParameterAST *ast)
637
{
Roberto Raggi's avatar
Roberto Raggi committed
638
639
640
    addUse(ast->name, Use::Type);
    accept(ast->type_id);
    return false;
641
642
}

Roberto Raggi's avatar
Roberto Raggi committed
643
bool CheckSymbols::visit(TemplateTypeParameterAST *ast)
644
{
Roberto Raggi's avatar
Roberto Raggi committed
645
646
647
648
    accept(ast->template_parameter_list);
    addUse(ast->name, Use::Type);
    accept(ast->type_id);
    return false;
649
650
}

Roberto Raggi's avatar
Roberto Raggi committed
651
652
bool CheckSymbols::visit(MemInitializerAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
653
654
655
656
657
658
659
660
    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
661
662
663
664
                }
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
665
666
        accept(ast->expression_list);
    }
Roberto Raggi's avatar
Roberto Raggi committed
667
668
669
670

    return false;
}

671
672
bool CheckSymbols::visit(FunctionDefinitionAST *ast)
{
673
    AST *thisFunction = _astStack.takeLast();
674
    accept(ast->decl_specifier_list);
675
    _astStack.append(thisFunction);
Roberto Raggi's avatar
Roberto Raggi committed
676

Roberto Raggi's avatar
Roberto Raggi committed
677
    if (ast->declarator && ast->symbol && ! ast->symbol->isGenerated()) {
Roberto Raggi's avatar
Roberto Raggi committed
678
679
680
681
682
683
        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
684
                addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
685
            } else if (maybeVirtualMethod(fun->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
686
                addVirtualMethod(_context.lookup(fun->name(), fun->scope()), declId, fun->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
687
688
689
690
            }
        }
    }

691
692
693
694
    accept(ast->declarator);
    accept(ast->ctor_initializer);
    accept(ast->function_body);

695
696
697
698
    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
699
            addUse(u);
700
701
    }

702
    flush();
703
704
705
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
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;

    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, kind);
    addUse(use);
}

void CheckSymbols::addUse(const Use &use)
737
{
Roberto Raggi's avatar
Roberto Raggi committed
738
    if (! enclosingFunctionDefinition()) {
739
        if (_usages.size() >= 50) {
740
741
742
743
744
745
            if (_flushRequested && use.line != _flushLine)
                flush();
            else if (! _flushRequested) {
                _flushRequested = true;
                _flushLine = use.line;
            }
746
747
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
748

749
    _usages.append(use);
750
751
}

Roberto Raggi's avatar
Roberto Raggi committed
752
void CheckSymbols::addType(ClassOrNamespace *b, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
753
754
755
756
{
    if (! b)
        return;

757
758
759
760
761
    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
762
763
764
765
    if (tok.generated())
        return;

    unsigned line, column;
766
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
767
    const unsigned length = tok.length();
Roberto Raggi's avatar
Roberto Raggi committed
768
769
    const Use use(line, column, length, Use::Type);
    addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
770
771
772
    //qDebug() << "added use" << oo(ast->name) << line << column << length;
}

Roberto Raggi's avatar
Roberto Raggi committed
773
void CheckSymbols::addType(const QList<LookupItem> &candidates, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
774
{
775
776
777
778
779
    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
780
781
782
    if (tok.generated())
        return;

783
784
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
Roberto Raggi's avatar
Roberto Raggi committed
785
786
787
788
789
790
791
        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()) {
792
793
794
795
796

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

Roberto Raggi's avatar
Roberto Raggi committed
797
798
            const Use use(line, column, length, Use::Type);
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
799
800
801
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
802
803
804
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
805
void CheckSymbols::addClassMember(const QList<LookupItem> &candidates, NameAST *ast)
806
807
808
809
810
811
812
813
814
{
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

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

815
816
817
818
819
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            continue;
        else if (! c->isDeclaration())
Roberto Raggi's avatar
Roberto Raggi committed
820
821
822
823
824
            return;
        else if (! (c->enclosingSymbol() && c->enclosingSymbol()->isClass()))
            return; // shadowed
        else if (c->isTypedef() || c->type()->isFunctionType())
            return; // shadowed
825

826
827
828
829
        unsigned line, column;
        getTokenStartPosition(startToken, &line, &column);
        const unsigned length = tok.length();

830
        const Use use(line, column, length, Use::Field);
Roberto Raggi's avatar
Roberto Raggi committed
831
        addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
832
833
834
835
        break;
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
836
void CheckSymbols::addVirtualMethod(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount)
Roberto Raggi's avatar
Roberto Raggi committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
{
    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;
858
859
860
861
862
863
864
865
        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
866
867

        const Use use(line, column, length, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
868
        addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
869
        break;
Roberto Raggi's avatar
Roberto Raggi committed
870
871
872
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
873
NameAST *CheckSymbols::declaratorId(DeclaratorAST *ast) const
874
{
Roberto Raggi's avatar
Roberto Raggi committed
875
876
877
878
879
880
    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;
        }
881
882
    }

Roberto Raggi's avatar
Roberto Raggi committed
883
    return 0;
884
885
}

Roberto Raggi's avatar
Roberto Raggi committed
886
bool CheckSymbols::maybeType(const Name *name) const
887
{
Roberto Raggi's avatar
Roberto Raggi committed
888
889
890
891
892
893
    if (name) {
        if (const Identifier *ident = name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialTypes.contains(id))
                return true;
        }
894
895
    }

Roberto Raggi's avatar
Roberto Raggi committed
896
    return false;
897
}
Roberto Raggi's avatar
Roberto Raggi committed
898

Roberto Raggi's avatar
Roberto Raggi committed
899
bool CheckSymbols::maybeMember(const Name *name) const
Roberto Raggi's avatar
Roberto Raggi committed
900
{
Roberto Raggi's avatar
Roberto Raggi committed
901
902
903
904
905
    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
906
907
908
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
909
    return false;
Roberto Raggi's avatar
Roberto Raggi committed
910
911
912
913
}

bool CheckSymbols::maybeVirtualMethod(const Name *name) const
{
Roberto Raggi's avatar
Roberto Raggi committed
914
915
916
917
918
919
    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
920
921
922
923
924
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
925
void CheckSymbols::flush()
Roberto Raggi's avatar
Roberto Raggi committed
926
{
927
928
929
    _flushRequested = false;
    _flushLine = 0;

930
    if (_usages.isEmpty())
Roberto Raggi's avatar
Roberto Raggi committed
931
932
        return;

933
934
    reportResults(_usages);
    _usages.clear();
Roberto Raggi's avatar
Roberto Raggi committed
935
}