cpplocalsymbols.cpp 9.03 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42

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

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
                    if (member->name() && member->name()->isNameId()) {
                        const Identifier *id = member->identifier();
                        unsigned line, column;
                        getTokenStartPosition(member->sourceLocation(), &line, &column);
99
                        localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::LocalUse));
100
                    }
101 102 103 104 105
                }
            }
        }
    }

106
    bool checkLocalUse(NameAST *nameAst, unsigned firstToken)
107
    {
108
        if (SimpleNameAST *simpleName = nameAst->asSimpleName()) {
109 110
            if (tokenAt(simpleName->identifier_token).generated())
                return false;
111 112
            const Identifier *id = identifier(simpleName->identifier_token);
            for (int i = _scopeStack.size() - 1; i != -1; --i) {
Roberto Raggi's avatar
Roberto Raggi committed
113
                if (Symbol *member = _scopeStack.at(i)->find(id)) {
114 115
                    if (member->isTypedef() ||
                            !(member->isDeclaration() || member->isArgument()))
116
                        continue;
117
                    else if (!member->isGenerated() && (member->sourceLocation() < 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    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;
    }

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    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;
    }

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

174
        return true;
175 176
    }

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

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

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

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

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

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

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

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

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

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

242 243 244
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
245
            enterScope(ast->symbol);
246 247 248
        return true;
    }

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

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

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

268
    virtual bool visit(SwitchStatementAST *ast)
269
    {
270
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
271
            enterScope(ast->symbol);
272 273
        return true;
    }
274

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

281 282 283
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
284
            enterScope(ast->symbol);
285
        return true;
286 287
    }

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

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

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

} // end of anonymous namespace


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