cppchecksymbols.h 4.91 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
31
#ifndef CPLUSPLUS_CHECKSYMBOLS_H
#define CPLUSPLUS_CHECKSYMBOLS_H
32

33
34
#include "cppsemanticinfo.h"

35
36
#include <cplusplus/CppDocument.h>
#include <cplusplus/LookupContext.h>
Roberto Raggi's avatar
Roberto Raggi committed
37
38
#include <cplusplus/TypeOfExpression.h>

39
#include <ASTVisitor.h>
Roberto Raggi's avatar
Roberto Raggi committed
40
#include <QtCore/QSet>
41
42
#include <QtCore/QFuture>
#include <QtCore/QtConcurrentRun>
43
44
45

namespace CPlusPlus {

Roberto Raggi's avatar
Roberto Raggi committed
46
class CheckSymbols:
47
        protected ASTVisitor,
48
49
        public QRunnable,
        public QFutureInterface<CppEditor::Internal::SemanticInfo::Use>
50
51
{
public:
Roberto Raggi's avatar
Roberto Raggi committed
52
    virtual ~CheckSymbols();
53

54
    typedef CppEditor::Internal::SemanticInfo::Use Use;
Roberto Raggi's avatar
Roberto Raggi committed
55

56
    virtual void run();
57
58

    typedef QFuture<Use> Future;
59
60
61
62
63
64
65
66
67
68

    Future start()
    {
        this->setRunnable(this);
        this->reportStarted();
        Future future = this->future();
        QThreadPool::globalInstance()->start(this, QThread::IdlePriority);
        return future;
    }

69
    static Future go(Document::Ptr doc, const LookupContext &context);
Roberto Raggi's avatar
Roberto Raggi committed
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    static QMap<int, QVector<Use> > chunks(const QFuture<Use> &future, int from, int to)
    {
        QMap<int, QVector<Use> > chunks;

        for (int i = from; i < to; ++i) {
            const Use use = future.resultAt(i);
            if (! use.line)
                continue; // skip it, it's an invalid use.

            const int blockNumber = use.line - 1;
            chunks[blockNumber].append(use);
        }

        return chunks;
    }

87
88
protected:
    using ASTVisitor::visit;
89
    using ASTVisitor::endVisit;
90

Roberto Raggi's avatar
Roberto Raggi committed
91
    CheckSymbols(Document::Ptr doc, const LookupContext &context);
92

93
94
    bool warning(unsigned line, unsigned column, const QString &text, unsigned length = 0);
    bool warning(AST *ast, const QString &text);
95

Roberto Raggi's avatar
Roberto Raggi committed
96
97
    QByteArray textOf(AST *ast) const;

Roberto Raggi's avatar
Roberto Raggi committed
98
99
100
101
    bool maybeType(const Name *name) const;
    bool maybeMember(const Name *name) const;
    bool maybeVirtualMethod(const Name *name) const;

Roberto Raggi's avatar
Roberto Raggi committed
102
    void checkName(NameAST *ast, Scope *scope = 0);
103
    void checkNamespace(NameAST *name);
104

Roberto Raggi's avatar
Roberto Raggi committed
105
106
    void addUse(const Use &use);
    void addUse(NameAST *name, Use::Kind kind);
Roberto Raggi's avatar
Roberto Raggi committed
107

Roberto Raggi's avatar
Roberto Raggi committed
108
109
110
111
112
113
114
115
116
    void addType(ClassOrNamespace *b, NameAST *ast);
    void addType(const QList<LookupItem> &candidates, NameAST *ast);

    void addClassMember(const QList<LookupItem> &candidates, NameAST *ast);
    void addVirtualMethod(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount);

    Scope *enclosingScope() const;
    FunctionDefinitionAST *enclosingFunctionDefinition() const;
    TemplateDeclarationAST *enclosingTemplateDeclaration() const;
117

118
    virtual bool preVisit(AST *);
Roberto Raggi's avatar
Roberto Raggi committed
119
    virtual void postVisit(AST *);
120

Roberto Raggi's avatar
Roberto Raggi committed
121
    virtual bool visit(NamespaceAST *);
122
123
124
    virtual bool visit(UsingDirectiveAST *);
    virtual bool visit(SimpleDeclarationAST *);
    virtual bool visit(NamedTypeSpecifierAST *);
125

Roberto Raggi's avatar
Roberto Raggi committed
126
127
128
129
130
    virtual bool visit(SimpleNameAST *ast);
    virtual bool visit(DestructorNameAST *ast);
    virtual bool visit(QualifiedNameAST *ast);
    virtual bool visit(TemplateIdAST *ast);

131
132
133
    virtual bool visit(TypenameTypeParameterAST *ast);
    virtual bool visit(TemplateTypeParameterAST *ast);

134
135
    virtual bool visit(FunctionDefinitionAST *ast);
    virtual bool visit(MemberAccessAST *ast);
Roberto Raggi's avatar
Roberto Raggi committed
136
    virtual bool visit(CallAST *ast);
137

Roberto Raggi's avatar
Roberto Raggi committed
138
139
    virtual bool visit(MemInitializerAST *ast);

Roberto Raggi's avatar
Roberto Raggi committed
140
141
    NameAST *declaratorId(DeclaratorAST *ast) const;

Roberto Raggi's avatar
Roberto Raggi committed
142
143
    void flush();

144
private:
145
    Document::Ptr _doc;
146
    LookupContext _context;
Roberto Raggi's avatar
Roberto Raggi committed
147
    TypeOfExpression typeOfExpression;
148
149
    QString _fileName;
    QList<Document::DiagnosticMessage> _diagnosticMessages;
Roberto Raggi's avatar
Roberto Raggi committed
150
    QSet<QByteArray> _potentialTypes;
151
    QSet<QByteArray> _potentialMembers;
Roberto Raggi's avatar
Roberto Raggi committed
152
    QSet<QByteArray> _potentialVirtualMethods;
Roberto Raggi's avatar
Roberto Raggi committed
153
    QList<AST *> _astStack;
154
    QVector<Use> _usages;
155
156
    bool _flushRequested;
    unsigned _flushLine;
157
158
159
160
};

} // end of namespace CPlusPlus

Roberto Raggi's avatar
Roberto Raggi committed
161
#endif // CPLUSPLUS_CHECKSYMBOLS_H