cppchecksymbols.h 5.63 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30 31 32
**
**************************************************************************/

Roberto Raggi's avatar
Roberto Raggi committed
33 34
#ifndef CPLUSPLUS_CHECKSYMBOLS_H
#define CPLUSPLUS_CHECKSYMBOLS_H
35

36 37
#include "cppsemanticinfo.h"

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

42 43
#include <texteditor/semantichighlighter.h>

44
#include <ASTVisitor.h>
Roberto Raggi's avatar
Roberto Raggi committed
45
#include <QtCore/QSet>
46 47
#include <QtCore/QFuture>
#include <QtCore/QtConcurrentRun>
48 49 50

namespace CPlusPlus {

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

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

62
    virtual void run();
63 64

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

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

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

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    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;
    }

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

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

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
107 108
    bool maybeType(const Name *name) const;
    bool maybeMember(const Name *name) const;
109
    bool maybeStatic(const Name *name) const;
Roberto Raggi's avatar
Roberto Raggi committed
110 111
    bool maybeVirtualMethod(const Name *name) const;

Roberto Raggi's avatar
Roberto Raggi committed
112
    void checkName(NameAST *ast, Scope *scope = 0);
113
    void checkNamespace(NameAST *name);
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
    void addTypeOrStatic(const QList<LookupItem> &candidates, NameAST *ast);
    void addStatic(const QList<LookupItem> &candidates, NameAST *ast);
Roberto Raggi's avatar
Roberto Raggi committed
123 124 125
    void addClassMember(const QList<LookupItem> &candidates, NameAST *ast);
    void addVirtualMethod(const QList<LookupItem> &candidates, NameAST *ast, unsigned argumentCount);

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
135
    virtual bool visit(NamespaceAST *);
136 137 138
    virtual bool visit(UsingDirectiveAST *);
    virtual bool visit(SimpleDeclarationAST *);
    virtual bool visit(NamedTypeSpecifierAST *);
139
    virtual bool visit(ElaboratedTypeSpecifierAST *ast);
140

141 142
    virtual bool visit(EnumeratorAST *);

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 149 150
    virtual bool visit(TypenameTypeParameterAST *ast);
    virtual bool visit(TemplateTypeParameterAST *ast);

151 152
    virtual bool visit(FunctionDefinitionAST *ast);
    virtual bool visit(MemberAccessAST *ast);
Roberto Raggi's avatar
Roberto Raggi committed
153
    virtual bool visit(CallAST *ast);
154

Roberto Raggi's avatar
Roberto Raggi committed
155 156
    virtual bool visit(MemInitializerAST *ast);

157 158 159
    virtual bool visit(GotoStatementAST *ast);
    virtual bool visit(LabeledStatementAST *ast);

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

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

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

179
} // namespace CPlusPlus
180

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