cppchecksymbols.h 6.02 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** 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 "cpptools_global.h"

35 36
#include "cppsemanticinfo.h"

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

41 42
#include <texteditor/semantichighlighter.h>

43
#include <ASTVisitor.h>
44 45 46
#include <QSet>
#include <QFuture>
#include <QtConcurrentRun>
47 48 49

namespace CPlusPlus {

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

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

61
    virtual void run();
62 63

    typedef QFuture<Use> Future;
64 65 66 67 68 69

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

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

76 77 78 79 80 81
    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);
82 83
            if (use.isInvalid())
                continue;
84 85 86 87 88 89 90 91

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

        return chunks;
    }

92 93
protected:
    using ASTVisitor::visit;
94
    using ASTVisitor::endVisit;
95

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
104 105
    QByteArray textOf(AST *ast) const;

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

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

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

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

121 122 123
    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
124

Roberto Raggi's avatar
Roberto Raggi committed
125 126
    bool isTemplateClass(Symbol *s) const;

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

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

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

142
    virtual bool visit(ElaboratedTypeSpecifierAST *ast);
143

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

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

153 154
    virtual bool visit(GotoStatementAST *ast);
    virtual bool visit(LabeledStatementAST *ast);
155 156
    virtual bool visit(SimpleSpecifierAST *ast);
    virtual bool visit(ClassSpecifierAST *ast);
157

158 159 160
    virtual bool visit(MemInitializerAST *ast);
    virtual bool visit(EnumeratorAST *ast);

Roberto Raggi's avatar
Roberto Raggi committed
161 162
    NameAST *declaratorId(DeclaratorAST *ast) const;

163 164
    static unsigned referenceToken(NameAST *name);

Roberto Raggi's avatar
Roberto Raggi committed
165 166
    void flush();

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

183
} // namespace CPlusPlus
184

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