cpplocalsymbols.cpp 9.22 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 30

#include "cpplocalsymbols.h"
31
#include "semantichighlighter.h"
32

33 34 35
#include "cppsemanticinfo.h"

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

namespace {

class FindLocalSymbols: protected ASTVisitor
{
public:
    FindLocalSymbols(Document::Ptr doc)
44
        : ASTVisitor(doc->translationUnit())
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
    { }

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

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

        if (!ast)
            return;

        if (FunctionDefinitionAST *def = ast->asFunctionDefinition()) {
            if (def->symbol) {
                accept(ast);
            }
        } else if (ObjCMethodDeclarationAST *decl = ast->asObjCMethodDeclaration()) {
            if (decl->method_prototype->symbol) {
                accept(ast);
            }
        }
    }

protected:
    using ASTVisitor::visit;
70
    using ASTVisitor::endVisit;
71

Erik Verbruggen's avatar
Erik Verbruggen committed
72 73
    typedef TextEditor::HighlightingResult HighlightingResult;

74
    void enterScope(Scope *scope)
75
    {
76 77
        _scopeStack.append(scope);

78 79
        for (unsigned i = 0; i < scope->memberCount(); ++i) {
            if (Symbol *member = scope->memberAt(i)) {
80 81
                if (member->isTypedef())
                    continue;
82
                if (!member->isGenerated() && (member->isDeclaration() || member->isArgument())) {
83
                    if (member->name() && member->name()->isNameId()) {
84
                        const Token token = tokenAt(member->sourceLocation());
85
                        unsigned line, column;
86
                        getPosition(token.utf16charsBegin(), &line, &column);
Erik Verbruggen's avatar
Erik Verbruggen committed
87
                        localUses[member].append(
88
                                    HighlightingResult(line, column, token.utf16chars(),
89
                                                       SemanticHighlighter::LocalUse));
90
                    }
91 92 93 94 95
                }
            }
        }
    }

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

120 121
        return true;
    }
122

123 124 125 126 127
    virtual bool visit(CaptureAST *ast)
    {
        return checkLocalUse(ast->identifier, ast->firstToken());
    }

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

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

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

172
    virtual void endVisit(FunctionDefinitionAST *ast)
173
    {
174 175
        if (ast->symbol)
            _scopeStack.removeLast();
176 177
    }

178 179 180 181 182 183 184 185 186 187 188 189 190
    virtual bool visit(LambdaExpressionAST *ast)
    {
        if (ast->lambda_declarator && ast->lambda_declarator->symbol)
            enterScope(ast->lambda_declarator->symbol);
        return true;
    }

    virtual void endVisit(LambdaExpressionAST *ast)
    {
        if (ast->lambda_declarator && ast->lambda_declarator->symbol)
            _scopeStack.removeLast();
    }

191
    virtual bool visit(CompoundStatementAST *ast)
192
    {
193
        if (ast->symbol)
194
            enterScope(ast->symbol);
195
        return true;
196 197
    }

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

204
    virtual bool visit(IfStatementAST *ast)
205
    {
206
        if (ast->symbol)
207
            enterScope(ast->symbol);
208
        return true;
209 210
    }

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

217
    virtual bool visit(WhileStatementAST *ast)
218
    {
219
        if (ast->symbol)
220
            enterScope(ast->symbol);
221
        return true;
222 223
    }

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

230
    virtual bool visit(ForStatementAST *ast)
231
    {
232
        if (ast->symbol)
233
            enterScope(ast->symbol);
234
        return true;
235 236
    }

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

243 244 245
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
246
            enterScope(ast->symbol);
247 248 249
        return true;
    }

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

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

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

269
    virtual bool visit(SwitchStatementAST *ast)
270
    {
271
        if (ast->symbol)
272
            enterScope(ast->symbol);
273 274
        return true;
    }
275

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

282 283 284
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
285
            enterScope(ast->symbol);
286
        return true;
287 288
    }

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

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

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

} // end of anonymous namespace


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