cpplocalsymbols.cpp 7.03 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
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
**
**************************************************************************/

#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
74
                _functionScope = def->symbol;
75 76 77 78
                accept(ast);
            }
        } else if (ObjCMethodDeclarationAST *decl = ast->asObjCMethodDeclaration()) {
            if (decl->method_prototype->symbol) {
Roberto Raggi's avatar
Roberto Raggi committed
79
                _functionScope = decl->method_prototype->symbol;
80 81 82 83 84 85 86
                accept(ast);
            }
        }
    }

protected:
    using ASTVisitor::visit;
87
    using ASTVisitor::endVisit;
88

89
    void enterScope(Scope *scope)
90
    {
91 92
        _scopeStack.append(scope);

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

109
    virtual bool visit(IdExpressionAST *ast)
110
    {
111 112 113
        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
114
                if (Symbol *member = _scopeStack.at(i)->find(id)) {
115 116
                    if (member->isTypedef())
                        continue;
117
                    else if (!member->isGenerated() && (member->sourceLocation() < ast->firstToken() || member->enclosingScope()->isFunction())) {
118 119
                        unsigned line, column;
                        getTokenStartPosition(simpleName->identifier_token, &line, &column);
120
                        localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::LocalUse));
121 122 123
                        return false;
                    }
                }
124 125 126
            }
        }

127 128
        return true;
    }
129

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

137
        return true;
138 139
    }

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

147
    virtual void endVisit(FunctionDefinitionAST *ast)
148
    {
149 150
        if (ast->symbol)
            _scopeStack.removeLast();
151 152
    }

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

160
    virtual void endVisit(CompoundStatementAST *ast)
161
    {
162 163
        if (ast->symbol)
            _scopeStack.removeLast();
164 165
    }

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

173
    virtual void endVisit(IfStatementAST *ast)
174
    {
175 176
        if (ast->symbol)
            _scopeStack.removeLast();
177 178
    }

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

186
    virtual void endVisit(WhileStatementAST *ast)
187
    {
188 189
        if (ast->symbol)
            _scopeStack.removeLast();
190 191
    }

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

199
    virtual void endVisit(ForStatementAST *ast)
200
    {
201 202 203
        if (ast->symbol)
            _scopeStack.removeLast();
    }
204

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

212
    virtual void endVisit(ForeachStatementAST *ast)
213
    {
214 215
        if (ast->symbol)
            _scopeStack.removeLast();
216 217
    }

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

225 226 227 228 229
    virtual void endVisit(SwitchStatementAST *ast)
    {
        if (ast->symbol)
            _scopeStack.removeLast();
    }
230

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

238
    virtual void endVisit(CatchClauseAST *ast)
239
    {
240 241
        if (ast->symbol)
            _scopeStack.removeLast();
242 243
    }

244
    virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
245
    {
246
        accept(ast->declaration);
247 248
        return false;
    }
249 250 251

private:
    QList<Scope *> _scopeStack;
252 253 254 255 256 257 258
};

} // end of anonymous namespace


LocalSymbols::LocalSymbols(CPlusPlus::Document::Ptr doc, CPlusPlus::DeclarationAST *ast)
{
259 260 261 262 263
    FindLocalSymbols findLocalSymbols(doc);
    findLocalSymbols(ast);
    hasD = findLocalSymbols.hasD;
    hasQ = findLocalSymbols.hasQ;
    uses = findLocalSymbols.localUses;
264
}