cpplocalsymbols.cpp 9.02 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 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
                    if (member->name() && member->name()->isNameId()) {
89
                        const Token token = tokenAt(member->sourceLocation());
90
                        unsigned line, column;
91
                        getPosition(token.utf16charsBegin(), &line, &column);
Erik Verbruggen's avatar
Erik Verbruggen committed
92
                        localUses[member].append(
93
                                    HighlightingResult(line, column, token.utf16chars(),
Erik Verbruggen's avatar
Erik Verbruggen committed
94
                                                       CppHighlightingSupport::LocalUse));
95
                    }
96 97 98 99 100
                }
            }
        }
    }

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

125 126
        return true;
    }
127

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

} // end of anonymous namespace


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