cpplocalsymbols.cpp 8.9 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
#include "cppsemanticinfo.h"

34 35 36
#include <cplusplus/AST.h>
#include <cplusplus/ASTVisitor.h>
#include <cplusplus/CoreTypes.h>
37
#include <cplusplus/CppDocument.h>
38 39 40 41
#include <cplusplus/Literals.h>
#include <cplusplus/Names.h>
#include <cplusplus/Scope.h>
#include <cplusplus/Symbols.h>
42 43

using namespace CPlusPlus;
44
using namespace CppTools;
45 46 47 48 49 50 51 52 53 54

namespace {

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

public:
    FindLocalSymbols(Document::Ptr doc)
Nikolai Kosjar's avatar
Nikolai Kosjar committed
55
        : ASTVisitor(doc->translationUnit()), _doc(doc)
56 57 58 59 60 61 62 63 64 65 66 67 68 69
    { }

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

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

        if (!ast)
            return;

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

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

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

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

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

126 127
        return true;
    }
128

129 130 131 132 133
    virtual bool visit(CaptureAST *ast)
    {
        return checkLocalUse(ast->identifier, ast->firstToken());
    }

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    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;
    }

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    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;
    }

171
    virtual bool visit(FunctionDefinitionAST *ast)
Roberto Raggi's avatar
Roberto Raggi committed
172
    {
173
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
174
            enterScope(ast->symbol);
175
        return true;
Roberto Raggi's avatar
Roberto Raggi committed
176 177
    }

178
    virtual void endVisit(FunctionDefinitionAST *ast)
179
    {
180 181
        if (ast->symbol)
            _scopeStack.removeLast();
182 183
    }

184
    virtual bool visit(CompoundStatementAST *ast)
185
    {
186
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
187
            enterScope(ast->symbol);
188
        return true;
189 190
    }

191
    virtual void endVisit(CompoundStatementAST *ast)
192
    {
193 194
        if (ast->symbol)
            _scopeStack.removeLast();
195 196
    }

197
    virtual bool visit(IfStatementAST *ast)
198
    {
199
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
200
            enterScope(ast->symbol);
201
        return true;
202 203
    }

204
    virtual void endVisit(IfStatementAST *ast)
205
    {
206 207
        if (ast->symbol)
            _scopeStack.removeLast();
208 209
    }

210
    virtual bool visit(WhileStatementAST *ast)
211
    {
212
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
213
            enterScope(ast->symbol);
214
        return true;
215 216
    }

217
    virtual void endVisit(WhileStatementAST *ast)
218
    {
219 220
        if (ast->symbol)
            _scopeStack.removeLast();
221 222
    }

223
    virtual bool visit(ForStatementAST *ast)
224
    {
225
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
226
            enterScope(ast->symbol);
227
        return true;
228 229
    }

230
    virtual void endVisit(ForStatementAST *ast)
231
    {
232 233 234
        if (ast->symbol)
            _scopeStack.removeLast();
    }
235

236 237 238
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
239
            enterScope(ast->symbol);
240 241 242
        return true;
    }

243
    virtual void endVisit(ForeachStatementAST *ast)
244
    {
245 246
        if (ast->symbol)
            _scopeStack.removeLast();
247
    }
248 249 250 251 252 253 254 255 256 257 258 259 260

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

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

262
    virtual bool visit(SwitchStatementAST *ast)
263
    {
264
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
265
            enterScope(ast->symbol);
266 267
        return true;
    }
268

269 270 271 272 273
    virtual void endVisit(SwitchStatementAST *ast)
    {
        if (ast->symbol)
            _scopeStack.removeLast();
    }
274

275 276 277
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
Roberto Raggi's avatar
Roberto Raggi committed
278
            enterScope(ast->symbol);
279
        return true;
280 281
    }

282
    virtual void endVisit(CatchClauseAST *ast)
283
    {
284 285
        if (ast->symbol)
            _scopeStack.removeLast();
286 287
    }

288
    virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
289
    {
290
        accept(ast->declaration);
291 292
        return false;
    }
293 294 295

private:
    QList<Scope *> _scopeStack;
296 297 298 299 300 301 302
};

} // end of anonymous namespace


LocalSymbols::LocalSymbols(CPlusPlus::Document::Ptr doc, CPlusPlus::DeclarationAST *ast)
{
303 304 305
    FindLocalSymbols findLocalSymbols(doc);
    findLocalSymbols(ast);
    uses = findLocalSymbols.localUses;
306
}