2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-07-15 16:03:48 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-07-15 16:03:48 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-07-15 16:03:48 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-07-15 16:03:48 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-07-15 16:03:48 +02:00
|
|
|
|
|
|
|
|
#include "cpplocalsymbols.h"
|
2014-08-19 15:59:29 +02:00
|
|
|
#include "semantichighlighter.h"
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2010-07-15 16:03:48 +02:00
|
|
|
#include "cppsemanticinfo.h"
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
2012-02-07 15:09:08 +01:00
|
|
|
using namespace CppTools;
|
2010-07-15 16:03:48 +02:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class FindLocalSymbols: protected ASTVisitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2019-01-14 01:40:53 +01:00
|
|
|
explicit FindLocalSymbols(Document::Ptr doc)
|
2014-06-26 16:09:09 -04:00
|
|
|
: ASTVisitor(doc->translationUnit())
|
2010-07-15 16:03:48 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
// 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;
|
2010-08-06 11:29:41 +02:00
|
|
|
using ASTVisitor::endVisit;
|
2010-07-15 16:03:48 +02:00
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
using HighlightingResult = TextEditor::HighlightingResult;
|
2013-04-16 16:48:10 +02:00
|
|
|
|
2010-08-06 11:29:41 +02:00
|
|
|
void enterScope(Scope *scope)
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
_scopeStack.append(scope);
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
for (int i = 0; i < scope->memberCount(); ++i) {
|
2010-08-11 12:26:02 +02:00
|
|
|
if (Symbol *member = scope->memberAt(i)) {
|
2010-08-12 12:40:43 +02:00
|
|
|
if (member->isTypedef())
|
|
|
|
|
continue;
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!member->isGenerated() && (member->isDeclaration() || member->isArgument())) {
|
2010-08-06 11:29:41 +02:00
|
|
|
if (member->name() && member->name()->isNameId()) {
|
2014-05-05 11:43:24 -04:00
|
|
|
const Token token = tokenAt(member->sourceLocation());
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2014-05-05 11:43:24 -04:00
|
|
|
getPosition(token.utf16charsBegin(), &line, &column);
|
2013-04-16 16:48:10 +02:00
|
|
|
localUses[member].append(
|
2014-05-05 11:43:24 -04:00
|
|
|
HighlightingResult(line, column, token.utf16chars(),
|
2014-08-19 15:59:29 +02:00
|
|
|
SemanticHighlighter::LocalUse));
|
2010-08-06 11:29:41 +02:00
|
|
|
}
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
bool checkLocalUse(NameAST *nameAst, int firstToken)
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2011-10-26 11:51:02 +02:00
|
|
|
if (SimpleNameAST *simpleName = nameAst->asSimpleName()) {
|
2014-05-05 11:43:24 -04:00
|
|
|
const Token token = tokenAt(simpleName->identifier_token);
|
|
|
|
|
if (token.generated())
|
2012-03-16 15:31:41 +01:00
|
|
|
return false;
|
2010-08-06 11:29:41 +02:00
|
|
|
const Identifier *id = identifier(simpleName->identifier_token);
|
|
|
|
|
for (int i = _scopeStack.size() - 1; i != -1; --i) {
|
2010-08-11 12:26:02 +02:00
|
|
|
if (Symbol *member = _scopeStack.at(i)->find(id)) {
|
2013-07-17 00:01:45 +03:00
|
|
|
if (member->isTypedef() || !(member->isDeclaration() || member->isArgument()))
|
2010-08-12 12:40:43 +02:00
|
|
|
continue;
|
2013-07-17 00:01:45 +03:00
|
|
|
if (!member->isGenerated() && (member->sourceLocation() < firstToken
|
|
|
|
|
|| member->enclosingScope()->isFunction())) {
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2010-08-06 11:29:41 +02:00
|
|
|
getTokenStartPosition(simpleName->identifier_token, &line, &column);
|
2013-04-16 16:48:10 +02:00
|
|
|
localUses[member].append(
|
2014-05-05 11:43:24 -04:00
|
|
|
HighlightingResult(line, column, token.utf16chars(),
|
2014-08-19 15:59:29 +02:00
|
|
|
SemanticHighlighter::LocalUse));
|
2010-08-06 11:29:41 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2010-07-15 16:03:48 +02:00
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(CaptureAST *ast) override
|
2012-10-25 16:22:42 +02:00
|
|
|
{
|
|
|
|
|
return checkLocalUse(ast->identifier, ast->firstToken());
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(IdExpressionAST *ast) override
|
2011-10-26 11:51:02 +02:00
|
|
|
{
|
|
|
|
|
return checkLocalUse(ast->name, ast->firstToken());
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(SizeofExpressionAST *ast) override
|
2011-10-26 11:51:02 +02:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(CastExpressionAST *ast) override
|
2011-10-26 14:35:52 +02:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(FunctionDefinitionAST *ast) override
|
2010-08-03 13:01:24 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
2010-08-03 13:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(FunctionDefinitionAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(LambdaExpressionAST *ast) override
|
2014-06-26 15:07:31 -04:00
|
|
|
{
|
|
|
|
|
if (ast->lambda_declarator && ast->lambda_declarator->symbol)
|
|
|
|
|
enterScope(ast->lambda_declarator->symbol);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(LambdaExpressionAST *ast) override
|
2014-06-26 15:07:31 -04:00
|
|
|
{
|
|
|
|
|
if (ast->lambda_declarator && ast->lambda_declarator->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(CompoundStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(CompoundStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(IfStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(IfStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(WhileStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(WhileStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(ForStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(ForStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
|
|
|
|
}
|
2010-07-15 16:03:48 +02:00
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(ForeachStatementAST *ast) override
|
2010-08-06 11:29:41 +02:00
|
|
|
{
|
|
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-07-15 16:03:48 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(ForeachStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
2012-02-19 16:33:25 +04:00
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(RangeBasedForStatementAST *ast) override
|
2012-02-19 16:33:25 +04:00
|
|
|
{
|
|
|
|
|
if (ast->symbol)
|
|
|
|
|
enterScope(ast->symbol);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(RangeBasedForStatementAST *ast) override
|
2012-02-19 16:33:25 +04:00
|
|
|
{
|
|
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
|
|
|
|
}
|
2010-07-15 16:03:48 +02:00
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(SwitchStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2010-07-15 16:03:48 +02:00
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(SwitchStatementAST *ast) override
|
2010-08-06 11:29:41 +02:00
|
|
|
{
|
|
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
|
|
|
|
}
|
2010-07-15 16:03:48 +02:00
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(CatchClauseAST *ast) override
|
2010-08-06 11:29:41 +02:00
|
|
|
{
|
|
|
|
|
if (ast->symbol)
|
2010-08-11 12:26:02 +02:00
|
|
|
enterScope(ast->symbol);
|
2010-08-06 11:29:41 +02:00
|
|
|
return true;
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void endVisit(CatchClauseAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
if (ast->symbol)
|
|
|
|
|
_scopeStack.removeLast();
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(ExpressionOrDeclarationStatementAST *ast) override
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-06 11:29:41 +02:00
|
|
|
accept(ast->declaration);
|
2010-07-15 16:03:48 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-08-06 11:29:41 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QList<Scope *> _scopeStack;
|
2010-07-15 16:03:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
LocalSymbols::LocalSymbols(Document::Ptr doc, DeclarationAST *ast)
|
2010-07-15 16:03:48 +02:00
|
|
|
{
|
2010-08-12 12:45:46 +02:00
|
|
|
FindLocalSymbols findLocalSymbols(doc);
|
|
|
|
|
findLocalSymbols(ast);
|
|
|
|
|
uses = findLocalSymbols.localUses;
|
2010-07-15 16:03:48 +02:00
|
|
|
}
|