cpplocalsymbols.cpp 8.66 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** 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

#include "cpplocalsymbols.h"
31

32 33 34
#include "cppsemanticinfo.h"

using namespace CPlusPlus;
35
using namespace CppTools;
36 37 38 39 40 41 42 43 44 45

namespace {

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

public:
    FindLocalSymbols(Document::Ptr doc)
Nikolai Kosjar's avatar
Nikolai Kosjar committed
46
        : ASTVisitor(doc->translationUnit()), _doc(doc)
47 48 49 50 51 52 53 54 55 56 57 58 59 60
    { }

    // local and external uses.
    SemanticInfo::LocalUseMap localUses;

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

        if (!ast)
            return;

        if (FunctionDefinitionAST *def = ast->asFunctionDefinition()) {
            if (def->symbol) {
61
                _functionScope = def->symbol;
62 63 64 65
                accept(ast);
            }
        } else if (ObjCMethodDeclarationAST *decl = ast->asObjCMethodDeclaration()) {
            if (decl->method_prototype->symbol) {
66
                _functionScope = decl->method_prototype->symbol;
67 68 69 70 71 72 73
                accept(ast);
            }
        }
    }

protected:
    using ASTVisitor::visit;
74
    using ASTVisitor::endVisit;
75

76
    void enterScope(Scope *scope)
77
    {
78 79
        _scopeStack.append(scope);

80 81
        for (unsigned i = 0; i < scope->memberCount(); ++i) {
            if (Symbol *member = scope->memberAt(i)) {
82 83 84
                if (member->isTypedef())
                    continue;
                else if (! member->isGenerated() && (member->isDeclaration() || member->isArgument())) {
85 86 87 88
                    if (member->name() && member->name()->isNameId()) {
                        const Identifier *id = member->identifier();
                        unsigned line, column;
                        getTokenStartPosition(member->sourceLocation(), &line, &column);
89
                        localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::LocalUse));
90
                    }
91 92 93 94 95
                }
            }
        }
    }

96
    bool checkLocalUse(NameAST *nameAst, unsigned firstToken)
97
    {
98
        if (SimpleNameAST *simpleName = nameAst->asSimpleName()) {
99 100
            if (tokenAt(simpleName->identifier_token).generated())
                return false;
101 102
            const Identifier *id = identifier(simpleName->identifier_token);
            for (int i = _scopeStack.size() - 1; i != -1; --i) {
103
                if (Symbol *member = _scopeStack.at(i)->find(id)) {
104 105
                    if (member->isTypedef() ||
                            !(member->isDeclaration() || member->isArgument()))
106
                        continue;
107
                    else if (!member->isGenerated() && (member->sourceLocation() < firstToken || member->enclosingScope()->isFunction())) {
108 109
                        unsigned line, column;
                        getTokenStartPosition(simpleName->identifier_token, &line, &column);
110
                        localUses[member].append(SemanticInfo::Use(line, column, id->size(), SemanticInfo::LocalUse));
111 112 113
                        return false;
                    }
                }
114 115 116
            }
        }

117 118
        return true;
    }
119

120 121 122 123 124
    virtual bool visit(CaptureAST *ast)
    {
        return checkLocalUse(ast->identifier, ast->firstToken());
    }

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    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;
    }

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    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;
    }

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

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

175
    virtual bool visit(CompoundStatementAST *ast)
176
    {
177
        if (ast->symbol)
178
            enterScope(ast->symbol);
179
        return true;
180 181
    }

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

188
    virtual bool visit(IfStatementAST *ast)
189
    {
190
        if (ast->symbol)
191
            enterScope(ast->symbol);
192
        return true;
193 194
    }

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

201
    virtual bool visit(WhileStatementAST *ast)
202
    {
203
        if (ast->symbol)
204
            enterScope(ast->symbol);
205
        return true;
206 207
    }

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

214
    virtual bool visit(ForStatementAST *ast)
215
    {
216
        if (ast->symbol)
217
            enterScope(ast->symbol);
218
        return true;
219 220
    }

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

227 228 229
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
230
            enterScope(ast->symbol);
231 232 233
        return true;
    }

234
    virtual void endVisit(ForeachStatementAST *ast)
235
    {
236 237
        if (ast->symbol)
            _scopeStack.removeLast();
238
    }
239 240 241 242 243 244 245 246 247 248 249 250 251

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

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

253
    virtual bool visit(SwitchStatementAST *ast)
254
    {
255
        if (ast->symbol)
256
            enterScope(ast->symbol);
257 258
        return true;
    }
259

260 261 262 263 264
    virtual void endVisit(SwitchStatementAST *ast)
    {
        if (ast->symbol)
            _scopeStack.removeLast();
    }
265

266 267 268
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
269
            enterScope(ast->symbol);
270
        return true;
271 272
    }

273
    virtual void endVisit(CatchClauseAST *ast)
274
    {
275 276
        if (ast->symbol)
            _scopeStack.removeLast();
277 278
    }

279
    virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
280
    {
281
        accept(ast->declaration);
282 283
        return false;
    }
284 285 286

private:
    QList<Scope *> _scopeStack;
287 288 289 290 291 292 293
};

} // end of anonymous namespace


LocalSymbols::LocalSymbols(CPlusPlus::Document::Ptr doc, CPlusPlus::DeclarationAST *ast)
{
294 295 296
    FindLocalSymbols findLocalSymbols(doc);
    findLocalSymbols(ast);
    uses = findLocalSymbols.localUses;
297
}