cppchecksymbols.cpp 26 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
**
**************************************************************************/

Roberto Raggi's avatar
Roberto Raggi committed
30
#include "cppchecksymbols.h"
31
32
#include "cpplocalsymbols.h"

33
#include <cplusplus/Overview.h>
34
35
36
37
38
39
40

#include <Names.h>
#include <Literals.h>
#include <Symbols.h>
#include <TranslationUnit.h>
#include <Scope.h>
#include <AST.h>
Roberto Raggi's avatar
Roberto Raggi committed
41
#include <SymbolVisitor.h>
42

43
44
45
46
47
#include <QtCore/QCoreApplication>
#include <QtCore/QThreadPool>
#include <QtCore/QDebug>

#include <qtconcurrent/runextensions.h>
48

49
using namespace CPlusPlus;
50
using namespace CppEditor::Internal;
51

Roberto Raggi's avatar
Roberto Raggi committed
52
53
namespace {

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

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

        }
    }

    return _doc->globalSymbols();
}

bool CheckSymbols::preVisit(AST *ast)
402
{
Roberto Raggi's avatar
Roberto Raggi committed
403
404
    _astStack.append(ast);

405
    if (isCanceled())
406
407
408
409
410
        return false;

    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
411
412
413
414
415
void CheckSymbols::postVisit(AST *)
{
    _astStack.takeLast();
}

Roberto Raggi's avatar
Roberto Raggi committed
416
bool CheckSymbols::visit(NamespaceAST *ast)
417
{
Roberto Raggi's avatar
Roberto Raggi committed
418
419
420
421
422
423
    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
424
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
425
426
427
428
        }
    }

    return true;
429
430
}

Roberto Raggi's avatar
Roberto Raggi committed
431
bool CheckSymbols::visit(UsingDirectiveAST *)
432
433
434
435
{
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
436
437
438
439
440
441
442
443
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
444
                        addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
445
                    } else if (maybeVirtualMethod(decl->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
446
                        addVirtualMethod(_context.lookup(decl->name(), decl->scope()), declId, funTy->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
447
448
449
450
451
452
                    }
                }
            }
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
453
454
    return true;
}
455

Roberto Raggi's avatar
Roberto Raggi committed
456
bool CheckSymbols::visit(NamedTypeSpecifierAST *)
Roberto Raggi's avatar
Roberto Raggi committed
457
{
Roberto Raggi's avatar
Roberto Raggi committed
458
    return true;
459
}
460

461
462
463
bool CheckSymbols::visit(MemberAccessAST *ast)
{
    accept(ast->base_expression);
Roberto Raggi's avatar
Roberto Raggi committed
464
465
466
467
468
469
470
471
472
473
474
    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
475
476
                const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(), TypeOfExpression::Preprocess);
                addClassMember(candidates, ast->member_name);
Roberto Raggi's avatar
Roberto Raggi committed
477
478
479
480
            }
        }
    }

481
482
483
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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
499
500
                    const QList<LookupItem> candidates = typeOfExpression(expression, enclosingScope(),
                                                                          TypeOfExpression::Preprocess);
501
502
503
504
505

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

Roberto Raggi's avatar
Roberto Raggi committed
506
                    addVirtualMethod(candidates, memberName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
507
508
509
510
511
                }
            }
        } else if (IdExpressionAST *idExpr = ast->base_expression->asIdExpression()) {
            if (const Name *name = idExpr->name->name) {
                if (maybeVirtualMethod(name)) {
512
513
514
515
                    NameAST *exprName = idExpr->name;
                    if (QualifiedNameAST *q = exprName->asQualifiedName())
                        exprName = q->unqualified_name;

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

Roberto Raggi's avatar
Roberto Raggi committed
519
                    addVirtualMethod(candidates, exprName, argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
                }
            }
        }

        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
538
void CheckSymbols::checkNamespace(NameAST *name)
539
{
540
541
    if (! name)
        return;
542

543
544
    unsigned line, column;
    getTokenStartPosition(name->firstToken(), &line, &column);
545

Roberto Raggi's avatar
Roberto Raggi committed
546
    if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope())) {
547
548
549
        foreach (Symbol *s, b->symbols()) {
            if (s->isNamespace())
                return;
550
551
552
        }
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
557
void CheckSymbols::checkName(NameAST *ast, Scope *scope)
Roberto Raggi's avatar
Roberto Raggi committed
558
{
559
    if (ast && ast->name) {
Roberto Raggi's avatar
Roberto Raggi committed
560
        if (! scope)
Roberto Raggi's avatar
Roberto Raggi committed
561
562
563
564
565
566
567
568
            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
569
570
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
571
}
Roberto Raggi's avatar
Roberto Raggi committed
572

Roberto Raggi's avatar
Roberto Raggi committed
573
bool CheckSymbols::visit(SimpleNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
574
575
{
    checkName(ast);
Roberto Raggi's avatar
Roberto Raggi committed
576
577
578
    return true;
}

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

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

Roberto Raggi's avatar
Roberto Raggi committed
591
bool CheckSymbols::visit(QualifiedNameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
592
593
594
595
596
{
    if (ast->name) {
        ClassOrNamespace *b = 0;
        if (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list) {
            NestedNameSpecifierAST *nested_name_specifier = it->value;
597
            if (NameAST *class_or_namespace_name = nested_name_specifier->class_or_namespace_name) { // ### remove shadowing
Roberto Raggi's avatar
Roberto Raggi committed
598

599
600
601
                if (TemplateIdAST *template_id = class_or_namespace_name->asTemplateId())
                    accept(template_id->template_argument_list);

602
                const Name *name = class_or_namespace_name->name;
Roberto Raggi's avatar
Roberto Raggi committed
603
604
                b = _context.lookupType(name, enclosingScope());
                addType(b, class_or_namespace_name);
Roberto Raggi's avatar
Roberto Raggi committed
605

606
607
                for (it = it->next; b && it; it = it->next) {
                    NestedNameSpecifierAST *nested_name_specifier = it->value;
Roberto Raggi's avatar
Roberto Raggi committed
608

609
610
611
                    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);
612

613
                        b = b->findType(class_or_namespace_name->name);
Roberto Raggi's avatar
Roberto Raggi committed
614
                        addType(b, class_or_namespace_name);
615
                    }
Roberto Raggi's avatar
Roberto Raggi committed
616
617
618
619
                }
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
620
621
622
        if (b && ast->unqualified_name) {
            addType(b->find(ast->unqualified_name->name), ast->unqualified_name);
        }
Roberto Raggi's avatar
Roberto Raggi committed
623
624
625
626
627
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
628
bool CheckSymbols::visit(TypenameTypeParameterAST *ast)
629
{
Roberto Raggi's avatar
Roberto Raggi committed
630
631
632
    addUse(ast->name, Use::Type);
    accept(ast->type_id);
    return false;
633
634
}

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

Roberto Raggi's avatar
Roberto Raggi committed
643
644
bool CheckSymbols::visit(MemInitializerAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
645
646
647
648
649
650
651
652
    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
653
654
655
656
                }
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
657
658
        accept(ast->expression_list);
    }
Roberto Raggi's avatar
Roberto Raggi committed
659
660
661
662

    return false;
}

663
664
bool CheckSymbols::visit(FunctionDefinitionAST *ast)
{
665
    AST *thisFunction = _astStack.takeLast();
666
    accept(ast->decl_specifier_list);
667
    _astStack.append(thisFunction);
Roberto Raggi's avatar
Roberto Raggi committed
668

Roberto Raggi's avatar
Roberto Raggi committed
669
    if (ast->declarator && ast->symbol && ! ast->symbol->isGenerated()) {
Roberto Raggi's avatar
Roberto Raggi committed
670
671
672
673
674
675
        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
676
                addUse(declId, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
677
            } else if (maybeVirtualMethod(fun->name())) {
Roberto Raggi's avatar
Roberto Raggi committed
678
                addVirtualMethod(_context.lookup(fun->name(), fun->scope()), declId, fun->argumentCount());
Roberto Raggi's avatar
Roberto Raggi committed
679
680
681
682
            }
        }
    }

683
684
685
686
    accept(ast->declarator);
    accept(ast->ctor_initializer);
    accept(ast->function_body);

687
688
689
690
    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
691
            addUse(u);
692
693
    }

694
    flush();
695
696
697
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
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)
729
{
Roberto Raggi's avatar
Roberto Raggi committed
730
    if (! enclosingFunctionDefinition()) {
731
        if (_usages.size() >= 50) {
732
733
734
735
736
737
            if (_flushRequested && use.line != _flushLine)
                flush();
            else if (! _flushRequested) {
                _flushRequested = true;
                _flushLine = use.line;
            }
738
739
        }
    }
Roberto Raggi's avatar
Roberto Raggi committed
740

741
    _usages.append(use);
742
743
}

Roberto Raggi's avatar
Roberto Raggi committed
744
void CheckSymbols::addType(ClassOrNamespace *b, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
745
746
747
748
{
    if (! b)
        return;

749
750
751
752
753
    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
754
755
756
757
    if (tok.generated())
        return;

    unsigned line, column;
758
    getTokenStartPosition(startToken, &line, &column);
Roberto Raggi's avatar
Roberto Raggi committed
759
    const unsigned length = tok.length();
Roberto Raggi's avatar
Roberto Raggi committed
760
761
    const Use use(line, column, length, Use::Type);
    addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
762
763
764
    //qDebug() << "added use" << oo(ast->name) << line << column << length;
}

Roberto Raggi's avatar
Roberto Raggi committed
765
void CheckSymbols::addType(const QList<LookupItem> &candidates, NameAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
766
{
767
768
769
770
771
    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
772
773
774
775
    if (tok.generated())
        return;

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

779
780
    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
Roberto Raggi's avatar
Roberto Raggi committed
781
782
783
784
785
786
787
        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()) {
Roberto Raggi's avatar
Roberto Raggi committed
788
789
            const Use use(line, column, length, Use::Type);
            addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
790
791
792
            //qDebug() << "added use" << oo(ast->name) << line << column << length;
            break;
        }
793
794
795
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
796
void CheckSymbols::addClassMember(const QList<LookupItem> &candidates, NameAST *ast)
797
798
799
800
801
802
803
804
805
806
807
808
809
{
    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();

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

        const Use use(line, column, length, Use::Field);
Roberto Raggi's avatar
Roberto Raggi committed
822
        addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
823
824
825
826
        break;
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
827
void CheckSymbols::addVirtualMethod(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount)
Roberto Raggi's avatar
Roberto Raggi committed
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
{
    unsigned startToken = ast->firstToken();
    if (DestructorNameAST *dtor = ast->asDestructorName())
        startToken = dtor->identifier_token;

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

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

    foreach (const LookupItem &r, candidates) {
        Symbol *c = r.declaration();
        if (! c)
            continue;

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

        const Use use(line, column, length, Use::VirtualMethod);
Roberto Raggi's avatar
Roberto Raggi committed
855
        addUse(use);
Roberto Raggi's avatar
Roberto Raggi committed
856
        break;
Roberto Raggi's avatar
Roberto Raggi committed
857
858
859
    }
}

Roberto Raggi's avatar
Roberto Raggi committed
860
NameAST *CheckSymbols::declaratorId(DeclaratorAST *ast) const
861
{
Roberto Raggi's avatar
Roberto Raggi committed
862
863
864
865
866
867
    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;
        }
868
869
    }

Roberto Raggi's avatar
Roberto Raggi committed
870
    return 0;
871
872
}

Roberto Raggi's avatar
Roberto Raggi committed
873
bool CheckSymbols::maybeType(const Name *name) const
874
{
Roberto Raggi's avatar
Roberto Raggi committed
875
876
877
878
879
880
    if (name) {
        if (const Identifier *ident = name->identifier()) {
            const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size());
            if (_potentialTypes.contains(id))
                return true;
        }
881
882
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
886
bool CheckSymbols::maybeMember(const Name *name) const
Roberto Raggi's avatar
Roberto Raggi committed
887
{
Roberto Raggi's avatar
Roberto Raggi committed
888
889
890
891
892
    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
893
894
895
        }
    }

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

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

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
912
void CheckSymbols::flush()
Roberto Raggi's avatar
Roberto Raggi committed
913
{
914
915
916
    _flushRequested = false;
    _flushLine = 0;

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

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