Files
qt-creator/src/plugins/cpptools/cpplocalsymbols.cpp

301 lines
8.9 KiB
C++
Raw Normal View History

/****************************************************************************
2010-07-15 16:03:48 +02:00
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2010-07-15 16:03:48 +02:00
**
** This file is part of Qt Creator.
2010-07-15 16:03:48 +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
** 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.
2010-07-15 16:03:48 +02:00
**
** GNU Lesser General Public License Usage
** 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
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2010-07-15 16:03:48 +02:00
#include "cpphighlightingsupport.h"
2010-07-15 16:03:48 +02:00
#include "cpplocalsymbols.h"
2010-07-15 16:03:48 +02:00
#include "cppsemanticinfo.h"
using namespace CPlusPlus;
using namespace CppTools;
2010-07-15 16:03:48 +02:00
namespace {
class FindLocalSymbols: protected ASTVisitor
{
public:
FindLocalSymbols(Document::Ptr doc)
: 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;
using ASTVisitor::endVisit;
2010-07-15 16:03:48 +02:00
typedef TextEditor::HighlightingResult HighlightingResult;
void enterScope(Scope *scope)
2010-07-15 16:03:48 +02:00
{
_scopeStack.append(scope);
2010-08-11 12:26:02 +02:00
for (unsigned i = 0; i < scope->memberCount(); ++i) {
if (Symbol *member = scope->memberAt(i)) {
2010-08-12 12:40:43 +02:00
if (member->isTypedef())
continue;
if (!member->isGenerated() && (member->isDeclaration() || member->isArgument())) {
if (member->name() && member->name()->isNameId()) {
const Token token = tokenAt(member->sourceLocation());
unsigned line, column;
getPosition(token.utf16charsBegin(), &line, &column);
localUses[member].append(
HighlightingResult(line, column, token.utf16chars(),
CppHighlightingSupport::LocalUse));
}
2010-07-15 16:03:48 +02:00
}
}
}
}
bool checkLocalUse(NameAST *nameAst, unsigned firstToken)
2010-07-15 16:03:48 +02:00
{
if (SimpleNameAST *simpleName = nameAst->asSimpleName()) {
const Token token = tokenAt(simpleName->identifier_token);
if (token.generated())
return false;
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)) {
if (member->isTypedef() || !(member->isDeclaration() || member->isArgument()))
2010-08-12 12:40:43 +02:00
continue;
if (!member->isGenerated() && (member->sourceLocation() < firstToken
|| member->enclosingScope()->isFunction())) {
unsigned line, column;
getTokenStartPosition(simpleName->identifier_token, &line, &column);
localUses[member].append(
HighlightingResult(line, column, token.utf16chars(),
CppHighlightingSupport::LocalUse));
return false;
}
}
2010-07-15 16:03:48 +02:00
}
}
return true;
}
2010-07-15 16:03:48 +02:00
virtual bool visit(CaptureAST *ast)
{
return checkLocalUse(ast->identifier, ast->firstToken());
}
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;
}
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;
}
virtual bool visit(FunctionDefinitionAST *ast)
2010-08-03 13:01:24 +02:00
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
return true;
2010-08-03 13:01:24 +02:00
}
virtual void endVisit(FunctionDefinitionAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
_scopeStack.removeLast();
2010-07-15 16:03:48 +02:00
}
virtual bool visit(CompoundStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
return true;
2010-07-15 16:03:48 +02:00
}
virtual void endVisit(CompoundStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
_scopeStack.removeLast();
2010-07-15 16:03:48 +02:00
}
virtual bool visit(IfStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
return true;
2010-07-15 16:03:48 +02:00
}
virtual void endVisit(IfStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
_scopeStack.removeLast();
2010-07-15 16:03:48 +02:00
}
virtual bool visit(WhileStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
return true;
2010-07-15 16:03:48 +02:00
}
virtual void endVisit(WhileStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
_scopeStack.removeLast();
2010-07-15 16:03:48 +02:00
}
virtual bool visit(ForStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
return true;
2010-07-15 16:03:48 +02:00
}
virtual void endVisit(ForStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
_scopeStack.removeLast();
}
2010-07-15 16:03:48 +02:00
virtual bool visit(ForeachStatementAST *ast)
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
2010-07-15 16:03:48 +02:00
return true;
}
virtual void endVisit(ForeachStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
_scopeStack.removeLast();
2010-07-15 16:03:48 +02:00
}
virtual bool visit(RangeBasedForStatementAST *ast)
{
if (ast->symbol)
enterScope(ast->symbol);
return true;
}
virtual void endVisit(RangeBasedForStatementAST *ast)
{
if (ast->symbol)
_scopeStack.removeLast();
}
2010-07-15 16:03:48 +02:00
virtual bool visit(SwitchStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
return true;
}
2010-07-15 16:03:48 +02:00
virtual void endVisit(SwitchStatementAST *ast)
{
if (ast->symbol)
_scopeStack.removeLast();
}
2010-07-15 16:03:48 +02:00
virtual bool visit(CatchClauseAST *ast)
{
if (ast->symbol)
2010-08-11 12:26:02 +02:00
enterScope(ast->symbol);
return true;
2010-07-15 16:03:48 +02:00
}
virtual void endVisit(CatchClauseAST *ast)
2010-07-15 16:03:48 +02:00
{
if (ast->symbol)
_scopeStack.removeLast();
2010-07-15 16:03:48 +02:00
}
virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
2010-07-15 16:03:48 +02:00
{
accept(ast->declaration);
2010-07-15 16:03:48 +02:00
return false;
}
private:
QList<Scope *> _scopeStack;
2010-07-15 16:03:48 +02:00
};
} // end of anonymous namespace
LocalSymbols::LocalSymbols(CPlusPlus::Document::Ptr doc, CPlusPlus::DeclarationAST *ast)
{
FindLocalSymbols findLocalSymbols(doc);
findLocalSymbols(ast);
uses = findLocalSymbols.localUses;
2010-07-15 16:03:48 +02:00
}