cpplocalsymbols.cpp 8.94 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25 26

#include "cpplocalsymbols.h"
27
#include "semantichighlighter.h"
28

29 30 31
#include "cppsemanticinfo.h"

using namespace CPlusPlus;
32
using namespace CppTools;
33 34 35 36 37 38 39

namespace {

class FindLocalSymbols: protected ASTVisitor
{
public:
    FindLocalSymbols(Document::Ptr doc)
40
        : ASTVisitor(doc->translationUnit())
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    { }

    // 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;
66
    using ASTVisitor::endVisit;
67

68 69
    typedef TextEditor::HighlightingResult HighlightingResult;

70
    void enterScope(Scope *scope)
71
    {
72 73
        _scopeStack.append(scope);

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

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

116 117
        return true;
    }
118

119 120 121 122 123
    virtual bool visit(CaptureAST *ast)
    {
        return checkLocalUse(ast->identifier, ast->firstToken());
    }

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    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;
    }

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    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;
    }

161
    virtual bool visit(FunctionDefinitionAST *ast)
162
    {
163
        if (ast->symbol)
164
            enterScope(ast->symbol);
165
        return true;
166 167
    }

168
    virtual void endVisit(FunctionDefinitionAST *ast)
169
    {
170 171
        if (ast->symbol)
            _scopeStack.removeLast();
172 173
    }

174 175 176 177 178 179 180 181 182 183 184 185 186
    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();
    }

187
    virtual bool visit(CompoundStatementAST *ast)
188
    {
189
        if (ast->symbol)
190
            enterScope(ast->symbol);
191
        return true;
192 193
    }

194
    virtual void endVisit(CompoundStatementAST *ast)
195
    {
196 197
        if (ast->symbol)
            _scopeStack.removeLast();
198 199
    }

200
    virtual bool visit(IfStatementAST *ast)
201
    {
202
        if (ast->symbol)
203
            enterScope(ast->symbol);
204
        return true;
205 206
    }

207
    virtual void endVisit(IfStatementAST *ast)
208
    {
209 210
        if (ast->symbol)
            _scopeStack.removeLast();
211 212
    }

213
    virtual bool visit(WhileStatementAST *ast)
214
    {
215
        if (ast->symbol)
216
            enterScope(ast->symbol);
217
        return true;
218 219
    }

220
    virtual void endVisit(WhileStatementAST *ast)
221
    {
222 223
        if (ast->symbol)
            _scopeStack.removeLast();
224 225
    }

226
    virtual bool visit(ForStatementAST *ast)
227
    {
228
        if (ast->symbol)
229
            enterScope(ast->symbol);
230
        return true;
231 232
    }

233
    virtual void endVisit(ForStatementAST *ast)
234
    {
235 236 237
        if (ast->symbol)
            _scopeStack.removeLast();
    }
238

239 240 241
    virtual bool visit(ForeachStatementAST *ast)
    {
        if (ast->symbol)
242
            enterScope(ast->symbol);
243 244 245
        return true;
    }

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

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

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

265
    virtual bool visit(SwitchStatementAST *ast)
266
    {
267
        if (ast->symbol)
268
            enterScope(ast->symbol);
269 270
        return true;
    }
271

272 273 274 275 276
    virtual void endVisit(SwitchStatementAST *ast)
    {
        if (ast->symbol)
            _scopeStack.removeLast();
    }
277

278 279 280
    virtual bool visit(CatchClauseAST *ast)
    {
        if (ast->symbol)
281
            enterScope(ast->symbol);
282
        return true;
283 284
    }

285
    virtual void endVisit(CatchClauseAST *ast)
286
    {
287 288
        if (ast->symbol)
            _scopeStack.removeLast();
289 290
    }

291
    virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
292
    {
293
        accept(ast->declaration);
294 295
        return false;
    }
296 297 298

private:
    QList<Scope *> _scopeStack;
299 300 301 302 303
};

} // end of anonymous namespace


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