cpplocalsymbols.cpp 8.77 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@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
29
** Nokia at qt-info@nokia.com.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
**
**************************************************************************/

#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;
46
using namespace CppTools;
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

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) {
74
                _functionScope = def->symbol;
75 76 77 78
                accept(ast);
            }
        } else if (ObjCMethodDeclarationAST *decl = ast->asObjCMethodDeclaration()) {
            if (decl->method_prototype->symbol) {
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);

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
    bool checkLocalUse(NameAST *nameAst, unsigned firstToken)
110
    {
111
        if (SimpleNameAST *simpleName = nameAst->asSimpleName()) {
112 113
            const Identifier *id = identifier(simpleName->identifier_token);
            for (int i = _scopeStack.size() - 1; i != -1; --i) {
114
                if (Symbol *member = _scopeStack.at(i)->find(id)) {
115 116
                    if (member->isTypedef() ||
                            !(member->isDeclaration() || member->isArgument()))
117
                        continue;
118
                    else if (!member->isGenerated() && (member->sourceLocation() < firstToken || member->enclosingScope()->isFunction())) {
119 120
                        unsigned line, column;
                        getTokenStartPosition(simpleName->identifier_token, &line, &column);
121
                        localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::LocalUse));
122 123 124
                        return false;
                    }
                }
125 126 127
            }
        }

128 129
        return true;
    }
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    virtual bool visit(IdExpressionAST *ast)
    {
        return checkLocalUse(ast->name, ast->firstToken());
    }

    virtual bool visit(SizeofExpressionAST *ast)
    {
        if (ast->expression && ast->expression->asTypeId()) {
            TypeIdAST *typeId = ast->expression->asTypeId();
            if (!typeId->declarator && typeId->type_specifier_list && !typeId->type_specifier_list->next) {
                if (NamedTypeSpecifierAST *namedTypeSpec = typeId->type_specifier_list->value->asNamedTypeSpecifier()) {
                    if (checkLocalUse(namedTypeSpec->name, namedTypeSpec->firstToken()))
                        return false;
                }
            }
        }

        return true;
    }

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    virtual bool visit(CastExpressionAST *ast)
    {
        if (ast->expression && ast->expression->asUnaryExpression()) {
            TypeIdAST *typeId = ast->type_id->asTypeId();
            if (typeId && !typeId->declarator && typeId->type_specifier_list && !typeId->type_specifier_list->next) {
                if (NamedTypeSpecifierAST *namedTypeSpec = typeId->type_specifier_list->value->asNamedTypeSpecifier()) {
                    if (checkLocalUse(namedTypeSpec->name, namedTypeSpec->firstToken())) {
                        accept(ast->expression);
                        return false;
                    }
                }
            }
        }

        return true;
    }

168
    virtual bool visit(QtMemberDeclarationAST *ast)
169
    {
170 171 172 173
        if (tokenKind(ast->q_token) == T_Q_D)
            hasD = true;
        else
            hasQ = true;
174

175
        return true;
176 177
    }

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

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

191
    virtual bool visit(CompoundStatementAST *ast)
192
    {
193
        if (ast->symbol)
194
            enterScope(ast->symbol);
195
        return true;
196 197
    }

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

204
    virtual bool visit(IfStatementAST *ast)
205
    {
206
        if (ast->symbol)
207
            enterScope(ast->symbol);
208
        return true;
209 210
    }

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

217
    virtual bool visit(WhileStatementAST *ast)
218
    {
219
        if (ast->symbol)
220
            enterScope(ast->symbol);
221
        return true;
222 223
    }

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

230
    virtual bool visit(ForStatementAST *ast)
231
    {
232
        if (ast->symbol)
233
            enterScope(ast->symbol);
234
        return true;
235 236
    }

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

243 244 245
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
246
            enterScope(ast->symbol);
247 248 249
        return true;
    }

250
    virtual void endVisit(ForeachStatementAST *ast)
251
    {
252 253
        if (ast->symbol)
            _scopeStack.removeLast();
254
    }
255 256 257 258 259 260 261 262 263 264 265 266 267

    virtual bool visit(RangeBasedForStatementAST *ast)
    {
        if (ast->symbol)
            enterScope(ast->symbol);
        return true;
    }

    virtual void endVisit(RangeBasedForStatementAST *ast)
    {
        if (ast->symbol)
            _scopeStack.removeLast();
    }
268

269
    virtual bool visit(SwitchStatementAST *ast)
270
    {
271
        if (ast->symbol)
272
            enterScope(ast->symbol);
273 274
        return true;
    }
275

276 277 278 279 280
    virtual void endVisit(SwitchStatementAST *ast)
    {
        if (ast->symbol)
            _scopeStack.removeLast();
    }
281

282 283 284
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
285
            enterScope(ast->symbol);
286
        return true;
287 288
    }

289
    virtual void endVisit(CatchClauseAST *ast)
290
    {
291 292
        if (ast->symbol)
            _scopeStack.removeLast();
293 294
    }

295
    virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
296
    {
297
        accept(ast->declaration);
298 299
        return false;
    }
300 301 302

private:
    QList<Scope *> _scopeStack;
303 304 305 306 307 308 309
};

} // end of anonymous namespace


LocalSymbols::LocalSymbols(CPlusPlus::Document::Ptr doc, CPlusPlus::DeclarationAST *ast)
{
310 311 312 313 314
    FindLocalSymbols findLocalSymbols(doc);
    findLocalSymbols(ast);
    hasD = findLocalSymbols.hasD;
    hasQ = findLocalSymbols.hasQ;
    uses = findLocalSymbols.localUses;
315
}