cppchecksymbols.h 6.83 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 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
#include "cppsemanticinfo.h"
35
#include "semantichighlighter.h"
36

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

39 40 41
#include <QSet>
#include <QFuture>
#include <QtConcurrentRun>
42

43
namespace CppTools {
44

45
class CPPTOOLS_EXPORT CheckSymbols:
46
        protected CPlusPlus::ASTVisitor,
47
        public QRunnable,
Erik Verbruggen's avatar
Erik Verbruggen committed
48
        public QFutureInterface<TextEditor::HighlightingResult>
49 50
{
public:
Roberto Raggi's avatar
Roberto Raggi committed
51
    virtual ~CheckSymbols();
52

Erik Verbruggen's avatar
Erik Verbruggen committed
53
    typedef TextEditor::HighlightingResult Result;
54
    typedef SemanticHighlighter::Kind Kind;
Roberto Raggi's avatar
Roberto Raggi committed
55

56
    virtual void run();
57

Erik Verbruggen's avatar
Erik Verbruggen committed
58
    typedef QFuture<Result> Future;
59 60 61 62 63 64

    Future start()
    {
        this->setRunnable(this);
        this->reportStarted();
        Future future = this->future();
65
        QThreadPool::globalInstance()->start(this, QThread::LowestPriority);
66 67 68
        return future;
    }

69 70
    static Future go(CPlusPlus::Document::Ptr doc,
                     const CPlusPlus::LookupContext &context,
Erik Verbruggen's avatar
Erik Verbruggen committed
71
                     const QList<Result> &macroUses);
Roberto Raggi's avatar
Roberto Raggi committed
72

Erik Verbruggen's avatar
Erik Verbruggen committed
73
    static QMap<int, QVector<Result> > chunks(const QFuture<Result> &future, int from, int to)
74
    {
Erik Verbruggen's avatar
Erik Verbruggen committed
75
        QMap<int, QVector<Result> > chunks;
76 77

        for (int i = from; i < to; ++i) {
Erik Verbruggen's avatar
Erik Verbruggen committed
78
            const Result use = future.resultAt(i);
79 80
            if (use.isInvalid())
                continue;
81 82 83 84 85 86 87 88

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

        return chunks;
    }

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

93 94
    CheckSymbols(CPlusPlus::Document::Ptr doc,
                 const CPlusPlus::LookupContext &context,
Erik Verbruggen's avatar
Erik Verbruggen committed
95
                 const QList<Result> &otherUses);
96

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

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

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

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

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

Erik Verbruggen's avatar
Erik Verbruggen committed
114 115 116
    void addUse(const Result &use);
    void addUse(unsigned tokenIndex, Kind kind);
    void addUse(CPlusPlus::NameAST *name, Kind kind);
Roberto Raggi's avatar
Roberto Raggi committed
117

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

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

127
    bool isTemplateClass(CPlusPlus::Symbol *s) const;
Roberto Raggi's avatar
Roberto Raggi committed
128

129 130 131
    CPlusPlus::Scope *enclosingScope() const;
    CPlusPlus::FunctionDefinitionAST *enclosingFunctionDefinition(bool skipTopOfStack = false) const;
    CPlusPlus::TemplateDeclarationAST *enclosingTemplateDeclaration() const;
132

133 134
    virtual bool preVisit(CPlusPlus::AST *);
    virtual void postVisit(CPlusPlus::AST *);
135

136 137 138 139 140 141 142
    virtual bool visit(CPlusPlus::NamespaceAST *);
    virtual bool visit(CPlusPlus::UsingDirectiveAST *);
    virtual bool visit(CPlusPlus::SimpleDeclarationAST *);
    virtual bool visit(CPlusPlus::TypenameTypeParameterAST *ast);
    virtual bool visit(CPlusPlus::TemplateTypeParameterAST *ast);
    virtual bool visit(CPlusPlus::FunctionDefinitionAST *ast);
    virtual bool visit(CPlusPlus::ParameterDeclarationAST *ast);
143

144
    virtual bool visit(CPlusPlus::ElaboratedTypeSpecifierAST *ast);
145

146 147 148 149
    virtual bool visit(CPlusPlus::SimpleNameAST *ast);
    virtual bool visit(CPlusPlus::DestructorNameAST *ast);
    virtual bool visit(CPlusPlus::QualifiedNameAST *ast);
    virtual bool visit(CPlusPlus::TemplateIdAST *ast);
Roberto Raggi's avatar
Roberto Raggi committed
150

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

155 156 157 158
    virtual bool visit(CPlusPlus::GotoStatementAST *ast);
    virtual bool visit(CPlusPlus::LabeledStatementAST *ast);
    virtual bool visit(CPlusPlus::SimpleSpecifierAST *ast);
    virtual bool visit(CPlusPlus::ClassSpecifierAST *ast);
159

160 161
    virtual bool visit(CPlusPlus::MemInitializerAST *ast);
    virtual bool visit(CPlusPlus::EnumeratorAST *ast);
162

Orgad Shaneh's avatar
Orgad Shaneh committed
163
    virtual bool visit(CPlusPlus::DotDesignatorAST *ast);
164

165
    CPlusPlus::NameAST *declaratorId(CPlusPlus::DeclaratorAST *ast) const;
Roberto Raggi's avatar
Roberto Raggi committed
166

167
    static unsigned referenceToken(CPlusPlus::NameAST *name);
168

Roberto Raggi's avatar
Roberto Raggi committed
169 170
    void flush();

171
private:
172
    bool isConstructorDeclaration(CPlusPlus::Symbol *declaration);
173

174 175 176
    CPlusPlus::Document::Ptr _doc;
    CPlusPlus::LookupContext _context;
    CPlusPlus::TypeOfExpression typeOfExpression;
177
    QString _fileName;
Roberto Raggi's avatar
Roberto Raggi committed
178
    QSet<QByteArray> _potentialTypes;
179
    QSet<QByteArray> _potentialFields;
180
    QSet<QByteArray> _potentialFunctions;
181
    QSet<QByteArray> _potentialStatics;
182
    QList<CPlusPlus::AST *> _astStack;
Erik Verbruggen's avatar
Erik Verbruggen committed
183
    QVector<Result> _usages;
184
    int _chunkSize;
185
    unsigned _lineOfLastUsage;
Erik Verbruggen's avatar
Erik Verbruggen committed
186
    QList<Result> _macroUses;
187 188
};

189
} // namespace CppTools
190

191
#endif // CPLUSPLUSCHECKSYMBOLS_H