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

30 31
#ifndef CPLUSPLUSCHECKSYMBOLS_H
#define CPLUSPLUSCHECKSYMBOLS_H
32

33
#include "cpptools_global.h"
34 35
#include "cppsemanticinfo.h"

36 37 38
#include <texteditor/semantichighlighter.h>

#include <cplusplus/ASTVisitor.h>
39 40
#include <cplusplus/CppDocument.h>
#include <cplusplus/LookupContext.h>
Roberto Raggi's avatar
Roberto Raggi committed
41 42
#include <cplusplus/TypeOfExpression.h>

43 44 45
#include <QSet>
#include <QFuture>
#include <QtConcurrentRun>
46 47 48

namespace CPlusPlus {

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

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

60
    virtual void run();
61 62

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

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

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

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

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

        return chunks;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

141
    virtual bool visit(ElaboratedTypeSpecifierAST *ast);
142

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

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

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

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

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

162 163
    static unsigned referenceToken(NameAST *name);

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

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

182
} // namespace CPlusPlus
183

184
#endif // CPLUSPLUSCHECKSYMBOLS_H