cpplocalsymbols.cpp 9.28 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
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** 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
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31

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

34 35 36
#include "cppsemanticinfo.h"

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

namespace {

class FindLocalSymbols: protected ASTVisitor
{
public:
    FindLocalSymbols(Document::Ptr doc)
45
        : ASTVisitor(doc->translationUnit())
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    { }

    // 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;
71
    using ASTVisitor::endVisit;
72

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

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

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

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

121 122
        return true;
    }
123

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

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

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

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

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

179 180 181 182 183 184 185 186 187 188 189 190 191
    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();
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

} // end of anonymous namespace


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