cpplocalsymbols.cpp 8.97 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

Erik Verbruggen's avatar
Erik Verbruggen committed
30
#include "cpphighlightingsupport.h"
31
#include "cpplocalsymbols.h"
32

33 34 35
#include "cppsemanticinfo.h"

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

namespace {

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

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

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

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

        if (!ast)
            return;

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

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

Erik Verbruggen's avatar
Erik Verbruggen committed
77 78
    typedef TextEditor::HighlightingResult HighlightingResult;

79
    void enterScope(Scope *scope)
80
    {
81 82
        _scopeStack.append(scope);

83 84
        for (unsigned i = 0; i < scope->memberCount(); ++i) {
            if (Symbol *member = scope->memberAt(i)) {
85 86
                if (member->isTypedef())
                    continue;
87
                if (!member->isGenerated() && (member->isDeclaration() || member->isArgument())) {
88 89 90 91
                    if (member->name() && member->name()->isNameId()) {
                        const Identifier *id = member->identifier();
                        unsigned line, column;
                        getTokenStartPosition(member->sourceLocation(), &line, &column);
Erik Verbruggen's avatar
Erik Verbruggen committed
92 93 94
                        localUses[member].append(
                                    HighlightingResult(line, column, id->size(),
                                                       CppHighlightingSupport::LocalUse));
95
                    }
96 97 98 99 100
                }
            }
        }
    }

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

124 125
        return true;
    }
126

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

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

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

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

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

182
    virtual bool visit(CompoundStatementAST *ast)
183
    {
184
        if (ast->symbol)
185
            enterScope(ast->symbol);
186
        return true;
187 188
    }

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

195
    virtual bool visit(IfStatementAST *ast)
196
    {
197
        if (ast->symbol)
198
            enterScope(ast->symbol);
199
        return true;
200 201
    }

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

208
    virtual bool visit(WhileStatementAST *ast)
209
    {
210
        if (ast->symbol)
211
            enterScope(ast->symbol);
212
        return true;
213 214
    }

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

221
    virtual bool visit(ForStatementAST *ast)
222
    {
223
        if (ast->symbol)
224
            enterScope(ast->symbol);
225
        return true;
226 227
    }

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

234 235 236
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
237
            enterScope(ast->symbol);
238 239 240
        return true;
    }

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

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

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

260
    virtual bool visit(SwitchStatementAST *ast)
261
    {
262
        if (ast->symbol)
263
            enterScope(ast->symbol);
264 265
        return true;
    }
266

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

273 274 275
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
276
            enterScope(ast->symbol);
277
        return true;
278 279
    }

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

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

private:
    QList<Scope *> _scopeStack;
294 295 296 297 298 299 300
};

} // end of anonymous namespace


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