summaryrefslogtreecommitdiff
path: root/src/plugins/cppeditor/cppchecksymbols.h
blob: 044dc76f4ca910faabd1eb03d4bfad45b5b497ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include "cppeditor_global.h"
#include "cppsemanticinfo.h"
#include "semantichighlighter.h"

#include <cplusplus/TypeOfExpression.h>

#include <QFuture>
#include <QSet>
#include <QThreadPool>

namespace CppEditor {

class CPPEDITOR_EXPORT CheckSymbols:
        public QObject,
        protected CPlusPlus::ASTVisitor,
        public QRunnable,
        public QFutureInterface<TextEditor::HighlightingResult>
{
    Q_OBJECT
public:
    ~CheckSymbols() override;

    using Result = TextEditor::HighlightingResult;
    using Kind = SemanticHighlighter::Kind;

    void run() override;

    using Future = QFuture<Result>;

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

    static Future go(CPlusPlus::Document::Ptr doc,
                     const CPlusPlus::LookupContext &context,
                     const QList<Result> &macroUses);
    static CheckSymbols * create(CPlusPlus::Document::Ptr doc,
                                 const CPlusPlus::LookupContext &context,
                                 const QList<Result> &macroUses);

    static QMap<int, QVector<Result> > chunks(const QFuture<Result> &future, int from, int to)
    {
        QMap<int, QVector<Result> > chunks;

        for (int i = from; i < to; ++i) {
            const Result use = future.resultAt(i);
            if (use.isInvalid())
                continue;

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

        return chunks;
    }

signals:
    void codeWarningsUpdated(CPlusPlus::Document::Ptr document,
                             const QList<CPlusPlus::Document::DiagnosticMessage> &selections);

protected:
    using ASTVisitor::visit;
    using ASTVisitor::endVisit;

    enum FunctionKind
    {
        FunctionDeclaration,
        FunctionCall
    };

    CheckSymbols(CPlusPlus::Document::Ptr doc,
                 const CPlusPlus::LookupContext &context,
                 const QList<Result> &otherUses);

    bool hasVirtualDestructor(CPlusPlus::Class *klass) const;
    bool hasVirtualDestructor(CPlusPlus::ClassOrNamespace *binding) const;

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

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

    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;

    void checkNamespace(CPlusPlus::NameAST *name);
    void checkName(CPlusPlus::NameAST *ast, CPlusPlus::Scope *scope = nullptr);
    CPlusPlus::ClassOrNamespace *checkNestedName(CPlusPlus::QualifiedNameAST *ast);

    void addUse(const Result &use);
    void addUse(unsigned tokenIndex, Kind kind);
    void addUse(CPlusPlus::NameAST *name, Kind kind);

    void addType(CPlusPlus::ClassOrNamespace *b, CPlusPlus::NameAST *ast);

    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, int argumentCount,
                          FunctionKind functionKind);

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

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

    bool preVisit(CPlusPlus::AST *) override;
    void postVisit(CPlusPlus::AST *) override;

    bool visit(CPlusPlus::NamespaceAST *) override;
    bool visit(CPlusPlus::UsingDirectiveAST *) override;
    bool visit(CPlusPlus::SimpleDeclarationAST *) override;
    bool visit(CPlusPlus::TypenameTypeParameterAST *ast) override;
    bool visit(CPlusPlus::TemplateTypeParameterAST *ast) override;
    bool visit(CPlusPlus::FunctionDefinitionAST *ast) override;
    bool visit(CPlusPlus::ParameterDeclarationAST *ast) override;

    bool visit(CPlusPlus::ElaboratedTypeSpecifierAST *ast) override;

    bool visit(CPlusPlus::ObjCProtocolDeclarationAST *ast) override;
    bool visit(CPlusPlus::ObjCProtocolForwardDeclarationAST *ast) override;
    bool visit(CPlusPlus::ObjCClassDeclarationAST *ast) override;
    bool visit(CPlusPlus::ObjCClassForwardDeclarationAST *ast) override;
    bool visit(CPlusPlus::ObjCProtocolRefsAST *ast) override;

    bool visit(CPlusPlus::SimpleNameAST *ast) override;
    bool visit(CPlusPlus::DestructorNameAST *ast) override;
    bool visit(CPlusPlus::QualifiedNameAST *ast) override;
    bool visit(CPlusPlus::TemplateIdAST *ast) override;

    bool visit(CPlusPlus::MemberAccessAST *ast) override;
    bool visit(CPlusPlus::CallAST *ast) override;
    bool visit(CPlusPlus::ObjCSelectorArgumentAST *ast) override;
    bool visit(CPlusPlus::NewExpressionAST *ast) override;

    bool visit(CPlusPlus::GotoStatementAST *ast) override;
    bool visit(CPlusPlus::LabeledStatementAST *ast) override;
    bool visit(CPlusPlus::SimpleSpecifierAST *ast) override;
    bool visit(CPlusPlus::ClassSpecifierAST *ast) override;

    bool visit(CPlusPlus::MemInitializerAST *ast) override;
    bool visit(CPlusPlus::EnumeratorAST *ast) override;

    bool visit(CPlusPlus::DotDesignatorAST *ast) override;

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

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

    void flush();

private:
    bool isConstructorDeclaration(CPlusPlus::Symbol *declaration);

    CPlusPlus::Document::Ptr _doc;
    CPlusPlus::LookupContext _context;
    CPlusPlus::TypeOfExpression typeOfExpression;
    QString _fileName;
    QSet<QByteArray> _potentialTypes;
    QSet<QByteArray> _potentialFields;
    QSet<QByteArray> _potentialFunctions;
    QSet<QByteArray> _potentialStatics;
    QList<CPlusPlus::AST *> _astStack;
    QVector<Result> _usages;
    QList<CPlusPlus::Document::DiagnosticMessage> _diagMsgs;
    int _chunkSize;
    int _lineOfLastUsage;
    QList<Result> _macroUses;
};

} // namespace CppEditor