cppchecksymbols.h 7.34 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30

31 32
#ifndef CPLUSPLUSCHECKSYMBOLS_H
#define CPLUSPLUSCHECKSYMBOLS_H
33

34
#include "cpptools_global.h"
35
#include "cppsemanticinfo.h"
36
#include "semantichighlighter.h"
37

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

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

44
namespace CppTools {
45

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

Erik Verbruggen's avatar
Erik Verbruggen committed
56
    typedef TextEditor::HighlightingResult Result;
57
    typedef SemanticHighlighter::Kind Kind;
58

59
    virtual void run();
60

Erik Verbruggen's avatar
Erik Verbruggen committed
61
    typedef QFuture<Result> Future;
62 63 64 65 66 67

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

72 73
    static Future go(CPlusPlus::Document::Ptr doc,
                     const CPlusPlus::LookupContext &context,
Erik Verbruggen's avatar
Erik Verbruggen committed
74
                     const QList<Result> &macroUses);
75 76 77
    static CheckSymbols * create(CPlusPlus::Document::Ptr doc,
                                 const CPlusPlus::LookupContext &context,
                                 const QList<Result> &macroUses);
78

Erik Verbruggen's avatar
Erik Verbruggen committed
79
    static QMap<int, QVector<Result> > chunks(const QFuture<Result> &future, int from, int to)
80
    {
Erik Verbruggen's avatar
Erik Verbruggen committed
81
        QMap<int, QVector<Result> > chunks;
82 83

        for (int i = from; i < to; ++i) {
Erik Verbruggen's avatar
Erik Verbruggen committed
84
            const Result use = future.resultAt(i);
85 86
            if (use.isInvalid())
                continue;
87 88 89 90 91 92 93 94

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

        return chunks;
    }

95 96 97 98
signals:
    void codeWarningsUpdated(CPlusPlus::Document::Ptr document,
                             const QList<CPlusPlus::Document::DiagnosticMessage> selections);

99 100
protected:
    using ASTVisitor::visit;
101
    using ASTVisitor::endVisit;
102

103 104
    CheckSymbols(CPlusPlus::Document::Ptr doc,
                 const CPlusPlus::LookupContext &context,
Erik Verbruggen's avatar
Erik Verbruggen committed
105
                 const QList<Result> &otherUses);
106

107
    bool hasVirtualDestructor(CPlusPlus::Class *klass) const;
108
    bool hasVirtualDestructor(CPlusPlus::LookupScope *binding) const;
109

110
    bool warning(unsigned line, unsigned column, const QString &text, unsigned length = 0);
111
    bool warning(CPlusPlus::AST *ast, const QString &text);
112

113
    QByteArray textOf(CPlusPlus::AST *ast) const;
114

115 116 117 118
    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
119

120 121
    void checkNamespace(CPlusPlus::NameAST *name);
    void checkName(CPlusPlus::NameAST *ast, CPlusPlus::Scope *scope = 0);
122
    CPlusPlus::LookupScope *checkNestedName(CPlusPlus::QualifiedNameAST *ast);
123

Erik Verbruggen's avatar
Erik Verbruggen committed
124 125 126
    void addUse(const Result &use);
    void addUse(unsigned tokenIndex, Kind kind);
    void addUse(CPlusPlus::NameAST *name, Kind kind);
127

128
    void addType(CPlusPlus::LookupScope *b, CPlusPlus::NameAST *ast);
Roberto Raggi's avatar
Roberto Raggi committed
129

130 131 132 133 134 135
    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
136

137
    bool isTemplateClass(CPlusPlus::Symbol *s) const;
138

139 140 141
    CPlusPlus::Scope *enclosingScope() const;
    CPlusPlus::FunctionDefinitionAST *enclosingFunctionDefinition(bool skipTopOfStack = false) const;
    CPlusPlus::TemplateDeclarationAST *enclosingTemplateDeclaration() const;
142

143 144
    virtual bool preVisit(CPlusPlus::AST *);
    virtual void postVisit(CPlusPlus::AST *);
145

146 147 148 149 150 151 152
    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);
153

154
    virtual bool visit(CPlusPlus::ElaboratedTypeSpecifierAST *ast);
155

156 157 158 159
    virtual bool visit(CPlusPlus::SimpleNameAST *ast);
    virtual bool visit(CPlusPlus::DestructorNameAST *ast);
    virtual bool visit(CPlusPlus::QualifiedNameAST *ast);
    virtual bool visit(CPlusPlus::TemplateIdAST *ast);
160

161 162 163
    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
164

165 166 167 168
    virtual bool visit(CPlusPlus::GotoStatementAST *ast);
    virtual bool visit(CPlusPlus::LabeledStatementAST *ast);
    virtual bool visit(CPlusPlus::SimpleSpecifierAST *ast);
    virtual bool visit(CPlusPlus::ClassSpecifierAST *ast);
169

170 171
    virtual bool visit(CPlusPlus::MemInitializerAST *ast);
    virtual bool visit(CPlusPlus::EnumeratorAST *ast);
172

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

175
    CPlusPlus::NameAST *declaratorId(CPlusPlus::DeclaratorAST *ast) const;
176

177
    static unsigned referenceToken(CPlusPlus::NameAST *name);
178

Roberto Raggi's avatar
Roberto Raggi committed
179 180
    void flush();

181
private:
182
    bool isConstructorDeclaration(CPlusPlus::Symbol *declaration);
183

184 185 186
    CPlusPlus::Document::Ptr _doc;
    CPlusPlus::LookupContext _context;
    CPlusPlus::TypeOfExpression typeOfExpression;
187
    QString _fileName;
188
    QSet<QByteArray> _potentialTypes;
189
    QSet<QByteArray> _potentialFields;
190
    QSet<QByteArray> _potentialFunctions;
191
    QSet<QByteArray> _potentialStatics;
192
    QList<CPlusPlus::AST *> _astStack;
Erik Verbruggen's avatar
Erik Verbruggen committed
193
    QVector<Result> _usages;
194
    QList<CPlusPlus::Document::DiagnosticMessage> _diagMsgs;
195
    int _chunkSize;
196
    unsigned _lineOfLastUsage;
Erik Verbruggen's avatar
Erik Verbruggen committed
197
    QList<Result> _macroUses;
198 199
};

200
} // namespace CppTools
201

202
#endif // CPLUSPLUSCHECKSYMBOLS_H