cppchecksymbols.h 5.98 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
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 40
#include <texteditor/semantichighlighter.h>

41
#include <ASTVisitor.h>
42 43 44
#include <QSet>
#include <QFuture>
#include <QtConcurrentRun>
45 46 47

namespace CPlusPlus {

Roberto Raggi's avatar
Roberto Raggi committed
48
class CheckSymbols:
49
        protected ASTVisitor,
50
        public QRunnable,
51
        public QFutureInterface<TextEditor::SemanticHighlighter::Result>
52 53
{
public:
Roberto Raggi's avatar
Roberto Raggi committed
54
    virtual ~CheckSymbols();
55

56 57
    typedef TextEditor::SemanticHighlighter::Result Use;
    typedef CppTools::SemanticInfo::UseKind UseKind;
Roberto Raggi's avatar
Roberto Raggi committed
58

59
    virtual void run();
60 61

    typedef QFuture<Use> Future;
62 63 64 65 66 67

    Future start()
    {
        this->setRunnable(this);
        this->reportStarted();
        Future future = this->future();
68
        QThreadPool::globalInstance()->start(this, QThread::LowestPriority);
69 70 71
        return future;
    }

72
    static Future go(Document::Ptr doc, const LookupContext &context, const QList<Use> &macroUses);
Roberto Raggi's avatar
Roberto Raggi committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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;
    }

90 91
protected:
    using ASTVisitor::visit;
92
    using ASTVisitor::endVisit;
93

94
    CheckSymbols(Document::Ptr doc, const LookupContext &context, const QList<Use> &macroUses);
95

Roberto Raggi's avatar
Roberto Raggi committed
96 97 98
    bool hasVirtualDestructor(Class *klass) const;
    bool hasVirtualDestructor(ClassOrNamespace *binding) const;

99 100
    bool warning(unsigned line, unsigned column, const QString &text, unsigned length = 0);
    bool warning(AST *ast, const QString &text);
101

Roberto Raggi's avatar
Roberto Raggi committed
102 103
    QByteArray textOf(AST *ast) const;

Roberto Raggi's avatar
Roberto Raggi committed
104
    bool maybeType(const Name *name) const;
105
    bool maybeField(const Name *name) const;
106
    bool maybeStatic(const Name *name) const;
107
    bool maybeFunction(const Name *name) const;
Roberto Raggi's avatar
Roberto Raggi committed
108

109
    void checkNamespace(NameAST *name);
110 111
    void checkName(NameAST *ast, Scope *scope = 0);
    ClassOrNamespace *checkNestedName(QualifiedNameAST *ast);
112

Roberto Raggi's avatar
Roberto Raggi committed
113
    void addUse(const Use &use);
114 115
    void addUse(unsigned tokenIndex, UseKind kind);
    void addUse(NameAST *name, UseKind kind);
Roberto Raggi's avatar
Roberto Raggi committed
116

Roberto Raggi's avatar
Roberto Raggi committed
117 118
    void addType(ClassOrNamespace *b, NameAST *ast);

119 120 121
    bool maybeAddTypeOrStatic(const QList<LookupItem> &candidates, NameAST *ast);
    bool maybeAddField(const QList<LookupItem> &candidates, NameAST *ast);
    bool maybeAddFunction(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount);
Roberto Raggi's avatar
Roberto Raggi committed
122

Roberto Raggi's avatar
Roberto Raggi committed
123 124
    bool isTemplateClass(Symbol *s) const;

Roberto Raggi's avatar
Roberto Raggi committed
125
    Scope *enclosingScope() const;
126
    FunctionDefinitionAST *enclosingFunctionDefinition(bool skipTopOfStack = false) const;
Roberto Raggi's avatar
Roberto Raggi committed
127
    TemplateDeclarationAST *enclosingTemplateDeclaration() const;
128

129
    virtual bool preVisit(AST *);
Roberto Raggi's avatar
Roberto Raggi committed
130
    virtual void postVisit(AST *);
131

Roberto Raggi's avatar
Roberto Raggi committed
132
    virtual bool visit(NamespaceAST *);
133 134
    virtual bool visit(UsingDirectiveAST *);
    virtual bool visit(SimpleDeclarationAST *);
135 136 137 138
    virtual bool visit(TypenameTypeParameterAST *ast);
    virtual bool visit(TemplateTypeParameterAST *ast);
    virtual bool visit(FunctionDefinitionAST *ast);
    virtual bool visit(ParameterDeclarationAST *ast);
139

140
    virtual bool visit(ElaboratedTypeSpecifierAST *ast);
141

Roberto Raggi's avatar
Roberto Raggi committed
142 143 144 145 146
    virtual bool visit(SimpleNameAST *ast);
    virtual bool visit(DestructorNameAST *ast);
    virtual bool visit(QualifiedNameAST *ast);
    virtual bool visit(TemplateIdAST *ast);

147
    virtual bool visit(MemberAccessAST *ast);
Roberto Raggi's avatar
Roberto Raggi committed
148
    virtual bool visit(CallAST *ast);
149
    virtual bool visit(NewExpressionAST *ast);
Roberto Raggi's avatar
Roberto Raggi committed
150

151 152
    virtual bool visit(GotoStatementAST *ast);
    virtual bool visit(LabeledStatementAST *ast);
153 154
    virtual bool visit(SimpleSpecifierAST *ast);
    virtual bool visit(ClassSpecifierAST *ast);
155

156 157 158
    virtual bool visit(MemInitializerAST *ast);
    virtual bool visit(EnumeratorAST *ast);

Roberto Raggi's avatar
Roberto Raggi committed
159 160
    NameAST *declaratorId(DeclaratorAST *ast) const;

161 162
    static unsigned referenceToken(NameAST *name);

Roberto Raggi's avatar
Roberto Raggi committed
163 164
    void flush();

165
private:
166
    Document::Ptr _doc;
167
    LookupContext _context;
Roberto Raggi's avatar
Roberto Raggi committed
168
    TypeOfExpression typeOfExpression;
169
    QString _fileName;
Roberto Raggi's avatar
Roberto Raggi committed
170
    QSet<QByteArray> _potentialTypes;
171
    QSet<QByteArray> _potentialFields;
172
    QSet<QByteArray> _potentialFunctions;
173
    QSet<QByteArray> _potentialStatics;
Roberto Raggi's avatar
Roberto Raggi committed
174
    QList<AST *> _astStack;
175
    QVector<Use> _usages;
176
    unsigned _lineOfLastUsage;
177
    QList<Use> _macroUses;
178 179
};

180
} // namespace CPlusPlus
181

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