cpplocalsymbols.cpp 6.97 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "cpplocalsymbols.h"
#include "cppsemanticinfo.h"

#include <cplusplus/CppDocument.h>
#include <ASTVisitor.h>
#include <AST.h>
#include <Scope.h>
#include <Symbols.h>
#include <CoreTypes.h>
#include <Names.h>
#include <Literals.h>

using namespace CPlusPlus;
using namespace CppEditor::Internal;

namespace {

class FindLocalSymbols: protected ASTVisitor
{
    Scope *_functionScope;
    Document::Ptr _doc;

public:
    FindLocalSymbols(Document::Ptr doc)
        : ASTVisitor(doc->translationUnit()), _doc(doc), hasD(false), hasQ(false)
    { }

    // local and external uses.
    SemanticInfo::LocalUseMap localUses;
    bool hasD;
    bool hasQ;

    void operator()(DeclarationAST *ast)
    {
        localUses.clear();

        if (!ast)
            return;

        if (FunctionDefinitionAST *def = ast->asFunctionDefinition()) {
            if (def->symbol) {
Roberto Raggi's avatar
Roberto Raggi committed
71
                _functionScope = def->symbol;
72
73
74
75
                accept(ast);
            }
        } else if (ObjCMethodDeclarationAST *decl = ast->asObjCMethodDeclaration()) {
            if (decl->method_prototype->symbol) {
Roberto Raggi's avatar
Roberto Raggi committed
76
                _functionScope = decl->method_prototype->symbol;
77
78
79
80
81
82
83
                accept(ast);
            }
        }
    }

protected:
    using ASTVisitor::visit;
84
    using ASTVisitor::endVisit;
85

86
    void enterScope(Scope *scope)
87
    {
88
89
        _scopeStack.append(scope);

Roberto Raggi's avatar
Roberto Raggi committed
90
91
        for (unsigned i = 0; i < scope->memberCount(); ++i) {
            if (Symbol *member = scope->memberAt(i)) {
92
93
94
                if (member->isTypedef())
                    continue;
                else if (! member->isGenerated() && (member->isDeclaration() || member->isArgument())) {
95
96
97
98
99
100
                    if (member->name() && member->name()->isNameId()) {
                        const Identifier *id = member->identifier();
                        unsigned line, column;
                        getTokenStartPosition(member->sourceLocation(), &line, &column);
                        localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::Use::Local));
                    }
101
102
103
104
105
                }
            }
        }
    }

106
    virtual bool visit(IdExpressionAST *ast)
107
    {
108
109
110
        if (SimpleNameAST *simpleName = ast->name->asSimpleName()) {
            const Identifier *id = identifier(simpleName->identifier_token);
            for (int i = _scopeStack.size() - 1; i != -1; --i) {
Roberto Raggi's avatar
Roberto Raggi committed
111
                if (Symbol *member = _scopeStack.at(i)->find(id)) {
112
113
114
                    if (member->isTypedef())
                        continue;
                    else if (!member->isGenerated() && (member->sourceLocation() < ast->firstToken() || member->scope()->isFunction())) {
115
116
117
118
119
120
                        unsigned line, column;
                        getTokenStartPosition(simpleName->identifier_token, &line, &column);
                        localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::Use::Local));
                        return false;
                    }
                }
121
122
123
            }
        }

124
125
        return true;
    }
126

127
    virtual bool visit(QtMemberDeclarationAST *ast)
128
    {
129
130
131
132
        if (tokenKind(ast->q_token) == T_Q_D)
            hasD = true;
        else
            hasQ = true;
133

134
        return true;
135
136
    }

137
    virtual bool visit(FunctionDefinitionAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
138
    {
139
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
140
            enterScope(ast->symbol);
141
        return true;
Roberto Raggi's avatar
Roberto Raggi committed
142
143
    }

144
    virtual void endVisit(FunctionDefinitionAST *ast)
145
    {
146
147
        if (ast->symbol)
            _scopeStack.removeLast();
148
149
    }

150
    virtual bool visit(CompoundStatementAST *ast)
151
    {
152
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
153
            enterScope(ast->symbol);
154
        return true;
155
156
    }

157
    virtual void endVisit(CompoundStatementAST *ast)
158
    {
159
160
        if (ast->symbol)
            _scopeStack.removeLast();
161
162
    }

163
    virtual bool visit(IfStatementAST *ast)
164
    {
165
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
166
            enterScope(ast->symbol);
167
        return true;
168
169
    }

170
    virtual void endVisit(IfStatementAST *ast)
171
    {
172
173
        if (ast->symbol)
            _scopeStack.removeLast();
174
175
    }

176
    virtual bool visit(WhileStatementAST *ast)
177
    {
178
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
179
            enterScope(ast->symbol);
180
        return true;
181
182
    }

183
    virtual void endVisit(WhileStatementAST *ast)
184
    {
185
186
        if (ast->symbol)
            _scopeStack.removeLast();
187
188
    }

189
    virtual bool visit(ForStatementAST *ast)
190
    {
191
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
192
            enterScope(ast->symbol);
193
        return true;
194
195
    }

196
    virtual void endVisit(ForStatementAST *ast)
197
    {
198
199
200
        if (ast->symbol)
            _scopeStack.removeLast();
    }
201

202
203
204
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
205
            enterScope(ast->symbol);
206
207
208
        return true;
    }

209
    virtual void endVisit(ForeachStatementAST *ast)
210
    {
211
212
        if (ast->symbol)
            _scopeStack.removeLast();
213
214
    }

215
    virtual bool visit(SwitchStatementAST *ast)
216
    {
217
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
218
            enterScope(ast->symbol);
219
220
        return true;
    }
221

222
223
224
225
226
    virtual void endVisit(SwitchStatementAST *ast)
    {
        if (ast->symbol)
            _scopeStack.removeLast();
    }
227

228
229
230
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
231
            enterScope(ast->symbol);
232
        return true;
233
234
    }

235
    virtual void endVisit(CatchClauseAST *ast)
236
    {
237
238
        if (ast->symbol)
            _scopeStack.removeLast();
239
240
    }

241
    virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
242
    {
243
        accept(ast->declaration);
244
245
        return false;
    }
246
247
248

private:
    QList<Scope *> _scopeStack;
249
250
251
252
253
254
255
256
257
258
259
260
261
};

} // end of anonymous namespace


LocalSymbols::LocalSymbols(CPlusPlus::Document::Ptr doc, CPlusPlus::DeclarationAST *ast)
{
    FindLocalSymbols FindLocalSymbols(doc);
    FindLocalSymbols(ast);
    hasD = FindLocalSymbols.hasD;
    hasQ = FindLocalSymbols.hasQ;
    uses = FindLocalSymbols.localUses;
}