cppchecksymbols.h 6.68 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"

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

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

42
namespace CppTools {
43

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

52 53
    typedef TextEditor::SemanticHighlighter::Result Use;
    typedef CppTools::SemanticInfo::UseKind UseKind;
Roberto Raggi's avatar
Roberto Raggi committed
54

55
    virtual void run();
56 57

    typedef QFuture<Use> Future;
58 59 60 61 62 63

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

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

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

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

        return chunks;
    }

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

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

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

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

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

104 105 106 107
    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
108

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

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

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

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

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

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

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

135 136 137 138 139 140 141
    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);
142

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

145 146 147 148
    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
149

150 151 152
    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
153

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

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

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

164
    static unsigned referenceToken(CPlusPlus::NameAST *name);
165

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

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

184
} // namespace CppTools
185

186
#endif // CPLUSPLUSCHECKSYMBOLS_H