2010-02-16 10:36:09 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2010-02-16 10:36:09 +01:00
|
|
|
**
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
**
|
|
|
|
** Commercial Usage
|
|
|
|
**
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "qmljscheck.h"
|
|
|
|
#include "qmljsbind.h"
|
|
|
|
#include "qmljsinterpreter.h"
|
2010-02-16 11:53:21 +01:00
|
|
|
#include "qmljsevaluate.h"
|
2010-02-16 10:36:09 +01:00
|
|
|
#include "parser/qmljsast_p.h"
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
2010-11-23 14:30:23 +01:00
|
|
|
#include <QtCore/QDir>
|
2010-02-23 14:36:38 +01:00
|
|
|
#include <QtGui/QColor>
|
2010-02-23 12:36:12 +01:00
|
|
|
#include <QtGui/QApplication>
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
using namespace QmlJS::Interpreter;
|
|
|
|
|
2010-07-29 11:20:06 +02:00
|
|
|
QColor QmlJS::toQColor(const QString &qmlColorString)
|
|
|
|
{
|
|
|
|
QColor color;
|
|
|
|
if (qmlColorString.size() == 9 && qmlColorString.at(0) == QLatin1Char('#')) {
|
|
|
|
bool ok;
|
|
|
|
const int alpha = qmlColorString.mid(1, 2).toInt(&ok, 16);
|
|
|
|
if (ok) {
|
|
|
|
QString name(qmlColorString.at(0));
|
|
|
|
name.append(qmlColorString.right(6));
|
|
|
|
if (QColor::isValidColor(name)) {
|
|
|
|
color.setNamedColor(name);
|
|
|
|
color.setAlpha(alpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (QColor::isValidColor(qmlColorString))
|
|
|
|
color.setNamedColor(qmlColorString);
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
2010-02-23 17:02:50 +01:00
|
|
|
|
2010-09-16 15:29:37 +02:00
|
|
|
SourceLocation QmlJS::locationFromRange(const SourceLocation &start,
|
|
|
|
const SourceLocation &end)
|
|
|
|
{
|
|
|
|
return SourceLocation(start.offset,
|
|
|
|
end.end() - start.begin(),
|
|
|
|
start.startLine,
|
|
|
|
start.startColumn);
|
|
|
|
}
|
|
|
|
|
2010-12-07 17:31:53 +01:00
|
|
|
SourceLocation QmlJS::fullLocationForQualifiedId(AST::UiQualifiedId *qualifiedId)
|
|
|
|
{
|
|
|
|
SourceLocation start = qualifiedId->identifierToken;
|
|
|
|
SourceLocation end = qualifiedId->identifierToken;
|
|
|
|
|
|
|
|
for (UiQualifiedId *iter = qualifiedId; iter; iter = iter->next) {
|
|
|
|
if (iter->name)
|
|
|
|
end = iter->identifierToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
return locationFromRange(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-16 15:29:37 +02:00
|
|
|
DiagnosticMessage QmlJS::errorMessage(const AST::SourceLocation &loc, const QString &message)
|
|
|
|
{
|
|
|
|
return DiagnosticMessage(DiagnosticMessage::Error, loc, message);
|
|
|
|
}
|
|
|
|
|
2010-02-23 17:02:50 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AssignmentCheck : public ValueVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DiagnosticMessage operator()(
|
2010-11-23 14:30:23 +01:00
|
|
|
const Document::Ptr &document,
|
2010-02-23 17:02:50 +01:00
|
|
|
const SourceLocation &location,
|
|
|
|
const Interpreter::Value *lhsValue,
|
|
|
|
const Interpreter::Value *rhsValue,
|
|
|
|
ExpressionNode *ast)
|
|
|
|
{
|
2010-11-23 14:30:23 +01:00
|
|
|
_doc = document;
|
2010-02-23 17:02:50 +01:00
|
|
|
_message = DiagnosticMessage(DiagnosticMessage::Error, location, QString());
|
|
|
|
_rhsValue = rhsValue;
|
|
|
|
_ast = ast;
|
|
|
|
|
|
|
|
if (lhsValue)
|
|
|
|
lhsValue->accept(this);
|
|
|
|
|
|
|
|
return _message;
|
|
|
|
}
|
|
|
|
|
2010-05-19 12:23:55 +02:00
|
|
|
virtual void visit(const NumberValue *value)
|
2010-02-23 17:02:50 +01:00
|
|
|
{
|
2010-05-19 12:23:55 +02:00
|
|
|
if (const QmlEnumValue *enumValue = dynamic_cast<const QmlEnumValue *>(value)) {
|
|
|
|
if (StringLiteral *stringLiteral = cast<StringLiteral *>(_ast)) {
|
|
|
|
const QString valueName = stringLiteral->value->asString();
|
|
|
|
|
|
|
|
if (!enumValue->keys().contains(valueName)) {
|
2010-05-19 13:32:11 +02:00
|
|
|
_message.message = Check::tr("unknown value for enum");
|
2010-05-19 12:23:55 +02:00
|
|
|
}
|
2010-11-29 08:59:54 +01:00
|
|
|
} else if (! _rhsValue->asStringValue() && ! _rhsValue->asNumberValue()
|
|
|
|
&& ! _rhsValue->asUndefinedValue()) {
|
2010-05-19 13:32:11 +02:00
|
|
|
_message.message = Check::tr("enum value is not a string or number");
|
2010-05-19 12:23:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (/*cast<StringLiteral *>(_ast)
|
2010-02-23 17:02:50 +01:00
|
|
|
||*/ _ast->kind == Node::Kind_TrueLiteral
|
2010-05-19 12:23:55 +02:00
|
|
|
|| _ast->kind == Node::Kind_FalseLiteral) {
|
2010-05-19 13:32:11 +02:00
|
|
|
_message.message = Check::tr("numerical value expected");
|
2010-05-19 12:23:55 +02:00
|
|
|
}
|
2010-02-23 17:02:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(const BooleanValue *)
|
|
|
|
{
|
|
|
|
UnaryMinusExpression *unaryMinus = cast<UnaryMinusExpression *>(_ast);
|
|
|
|
|
|
|
|
if (cast<StringLiteral *>(_ast)
|
|
|
|
|| cast<NumericLiteral *>(_ast)
|
|
|
|
|| (unaryMinus && cast<NumericLiteral *>(unaryMinus->expression))) {
|
2010-05-19 13:32:11 +02:00
|
|
|
_message.message = Check::tr("boolean value expected");
|
2010-02-23 17:02:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-23 14:30:23 +01:00
|
|
|
virtual void visit(const StringValue *value)
|
2010-02-23 17:02:50 +01:00
|
|
|
{
|
|
|
|
UnaryMinusExpression *unaryMinus = cast<UnaryMinusExpression *>(_ast);
|
|
|
|
|
|
|
|
if (cast<NumericLiteral *>(_ast)
|
|
|
|
|| (unaryMinus && cast<NumericLiteral *>(unaryMinus->expression))
|
|
|
|
|| _ast->kind == Node::Kind_TrueLiteral
|
|
|
|
|| _ast->kind == Node::Kind_FalseLiteral) {
|
2010-05-19 13:32:11 +02:00
|
|
|
_message.message = Check::tr("string value expected");
|
2010-02-23 17:02:50 +01:00
|
|
|
}
|
2010-11-23 14:30:23 +01:00
|
|
|
|
|
|
|
if (value && value->asUrlValue()) {
|
|
|
|
if (StringLiteral *literal = cast<StringLiteral *>(_ast)) {
|
|
|
|
QUrl url(literal->value->asString());
|
|
|
|
if (!url.isValid() && !url.isEmpty()) {
|
|
|
|
_message.message = Check::tr("not a valid url");
|
|
|
|
} else {
|
|
|
|
QString fileName = url.toLocalFile();
|
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
if (url.isRelative()) {
|
|
|
|
fileName.prepend(QDir::separator());
|
|
|
|
fileName.prepend(_doc->path());
|
|
|
|
}
|
|
|
|
if (!QFileInfo(fileName).exists())
|
|
|
|
_message.message = Check::tr("file or directory does not exist");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-23 17:02:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(const ColorValue *)
|
|
|
|
{
|
|
|
|
if (StringLiteral *stringLiteral = cast<StringLiteral *>(_ast)) {
|
2010-07-29 11:20:06 +02:00
|
|
|
if (!toQColor(stringLiteral->value->asString()).isValid())
|
2010-05-19 13:32:11 +02:00
|
|
|
_message.message = Check::tr("not a valid color");
|
2010-02-23 17:02:50 +01:00
|
|
|
} else {
|
|
|
|
visit((StringValue *)0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(const AnchorLineValue *)
|
|
|
|
{
|
|
|
|
if (! (_rhsValue->asAnchorLineValue() || _rhsValue->asUndefinedValue()))
|
2010-05-19 13:32:11 +02:00
|
|
|
_message.message = Check::tr("expected anchor line");
|
2010-02-23 17:02:50 +01:00
|
|
|
}
|
|
|
|
|
2010-11-23 14:30:23 +01:00
|
|
|
Document::Ptr _doc;
|
2010-02-23 17:02:50 +01:00
|
|
|
DiagnosticMessage _message;
|
|
|
|
const Value *_rhsValue;
|
|
|
|
ExpressionNode *_ast;
|
|
|
|
};
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
class FunctionBodyCheck : protected Visitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QList<DiagnosticMessage> operator()(FunctionExpression *function, Check::Options options)
|
|
|
|
{
|
2010-12-06 10:03:53 +01:00
|
|
|
clear();
|
2010-11-25 13:38:15 +01:00
|
|
|
_options = options;
|
|
|
|
for (FormalParameterList *plist = function->formals; plist; plist = plist->next) {
|
|
|
|
if (plist->name)
|
|
|
|
_formalParameterNames += plist->name->asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::accept(function->body, this);
|
|
|
|
return _messages;
|
|
|
|
}
|
|
|
|
|
2010-12-06 10:03:53 +01:00
|
|
|
QList<DiagnosticMessage> operator()(StatementList *statements, Check::Options options)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_options = options;
|
|
|
|
Node::accept(statements, this);
|
|
|
|
return _messages;
|
|
|
|
}
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
protected:
|
2010-12-06 10:03:53 +01:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
_messages.clear();
|
|
|
|
_declaredFunctions.clear();
|
|
|
|
_declaredVariables.clear();
|
|
|
|
_possiblyUndeclaredUses.clear();
|
|
|
|
_seenNonDeclarationStatement = false;
|
|
|
|
_formalParameterNames.clear();
|
|
|
|
}
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
void postVisit(Node *ast)
|
|
|
|
{
|
|
|
|
if (!_seenNonDeclarationStatement && ast->statementCast()
|
|
|
|
&& !cast<VariableStatement *>(ast)) {
|
|
|
|
_seenNonDeclarationStatement = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(IdentifierExpression *ast)
|
|
|
|
{
|
|
|
|
if (!ast->name)
|
|
|
|
return false;
|
|
|
|
const QString name = ast->name->asString();
|
|
|
|
if (!_declaredFunctions.contains(name) && !_declaredVariables.contains(name))
|
|
|
|
_possiblyUndeclaredUses[name].append(ast->identifierToken);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(VariableStatement *ast)
|
|
|
|
{
|
|
|
|
if (_options & Check::WarnDeclarationsNotStartOfFunction && _seenNonDeclarationStatement) {
|
|
|
|
warning(ast->declarationKindToken, Check::tr("declarations should be at the start of a function"));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(VariableDeclaration *ast)
|
|
|
|
{
|
|
|
|
if (!ast->name)
|
|
|
|
return true;
|
|
|
|
const QString name = ast->name->asString();
|
|
|
|
|
|
|
|
if (_formalParameterNames.contains(name)) {
|
|
|
|
if (_options & Check::WarnDuplicateDeclaration)
|
|
|
|
warning(ast->identifierToken, Check::tr("already a formal parameter"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_declaredFunctions.contains(name)) {
|
|
|
|
if (_options & Check::WarnDuplicateDeclaration)
|
|
|
|
warning(ast->identifierToken, Check::tr("already declared as function"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_declaredVariables.contains(name)) {
|
|
|
|
if (_options & Check::WarnDuplicateDeclaration)
|
|
|
|
warning(ast->identifierToken, Check::tr("duplicate declaration"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_possiblyUndeclaredUses.contains(name)) {
|
|
|
|
if (_options & Check::WarnUseBeforeDeclaration) {
|
|
|
|
foreach (const SourceLocation &loc, _possiblyUndeclaredUses.value(name)) {
|
|
|
|
warning(loc, Check::tr("variable is used before being declared"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_possiblyUndeclaredUses.remove(name);
|
|
|
|
}
|
|
|
|
_declaredVariables[name] = ast;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(FunctionDeclaration *ast)
|
|
|
|
{
|
|
|
|
if (_options & Check::WarnDeclarationsNotStartOfFunction &&_seenNonDeclarationStatement) {
|
|
|
|
warning(ast->functionToken, Check::tr("declarations should be at the start of a function"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return visit(static_cast<FunctionExpression *>(ast));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(FunctionExpression *ast)
|
|
|
|
{
|
|
|
|
if (!ast->name)
|
|
|
|
return false;
|
|
|
|
const QString name = ast->name->asString();
|
|
|
|
|
|
|
|
if (_formalParameterNames.contains(name)) {
|
|
|
|
if (_options & Check::WarnDuplicateDeclaration)
|
|
|
|
warning(ast->identifierToken, Check::tr("already a formal parameter"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_declaredVariables.contains(name)) {
|
|
|
|
if (_options & Check::WarnDuplicateDeclaration)
|
|
|
|
warning(ast->identifierToken, Check::tr("already declared as var"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_declaredFunctions.contains(name)) {
|
|
|
|
if (_options & Check::WarnDuplicateDeclaration)
|
|
|
|
warning(ast->identifierToken, Check::tr("duplicate declaration"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FunctionDeclaration *decl = cast<FunctionDeclaration *>(ast)) {
|
|
|
|
if (_possiblyUndeclaredUses.contains(name)) {
|
|
|
|
if (_options & Check::WarnUseBeforeDeclaration) {
|
|
|
|
foreach (const SourceLocation &loc, _possiblyUndeclaredUses.value(name)) {
|
|
|
|
warning(loc, Check::tr("function is used before being declared"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_possiblyUndeclaredUses.remove(name);
|
|
|
|
}
|
|
|
|
_declaredFunctions[name] = decl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void warning(const SourceLocation &loc, const QString &message)
|
|
|
|
{
|
|
|
|
_messages.append(DiagnosticMessage(DiagnosticMessage::Warning, loc, message));
|
|
|
|
}
|
|
|
|
|
|
|
|
Check::Options _options;
|
|
|
|
QList<DiagnosticMessage> _messages;
|
|
|
|
QStringList _formalParameterNames;
|
|
|
|
QHash<QString, VariableDeclaration *> _declaredVariables;
|
|
|
|
QHash<QString, FunctionDeclaration *> _declaredFunctions;
|
|
|
|
QHash<QString, QList<SourceLocation> > _possiblyUndeclaredUses;
|
|
|
|
bool _seenNonDeclarationStatement;
|
|
|
|
};
|
|
|
|
|
2010-02-23 17:02:50 +01:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
2010-08-31 10:39:07 +02:00
|
|
|
Check::Check(Document::Ptr doc, const Snapshot &snapshot, const Context *linkedContextNoScope)
|
2010-02-16 10:36:09 +01:00
|
|
|
: _doc(doc)
|
|
|
|
, _snapshot(snapshot)
|
2010-08-31 10:39:07 +02:00
|
|
|
, _context(*linkedContextNoScope)
|
2010-09-24 14:05:34 +02:00
|
|
|
, _scopeBuilder(&_context, doc, snapshot)
|
2010-08-26 10:50:00 +02:00
|
|
|
, _ignoreTypeErrors(false)
|
2010-11-25 13:38:15 +01:00
|
|
|
, _options(WarnDangerousNonStrictEqualityChecks | WarnBlocks | WarnWith
|
|
|
|
| WarnVoid | WarnCommaExpression | WarnExpressionStatement
|
|
|
|
| WarnAssignInCondition | WarnUseBeforeDeclaration | WarnDuplicateDeclaration
|
|
|
|
| WarnCaseWithoutFlowControlEnd)
|
2010-11-24 15:12:11 +01:00
|
|
|
, _lastValue(0)
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Check::~Check()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<DiagnosticMessage> Check::operator()()
|
|
|
|
{
|
|
|
|
_messages.clear();
|
|
|
|
Node::accept(_doc->ast(), this);
|
|
|
|
return _messages;
|
|
|
|
}
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
bool Check::preVisit(Node *ast)
|
|
|
|
{
|
|
|
|
_chain.append(ast);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::postVisit(Node *)
|
|
|
|
{
|
|
|
|
_chain.removeLast();
|
|
|
|
}
|
|
|
|
|
2010-02-19 12:25:26 +01:00
|
|
|
bool Check::visit(UiProgram *)
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-07 17:31:53 +01:00
|
|
|
bool Check::visit(UiObjectInitializer *)
|
|
|
|
{
|
|
|
|
m_propertyStack.push(StringSet());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::endVisit(UiObjectInitializer *)
|
|
|
|
{
|
|
|
|
m_propertyStack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::checkProperty(UiQualifiedId *qualifiedId)
|
|
|
|
{
|
|
|
|
const QString id = Bind::toString(qualifiedId);
|
|
|
|
if (id.at(0).isLower()) {
|
|
|
|
if (m_propertyStack.top().contains(id)) {
|
|
|
|
error(fullLocationForQualifiedId(qualifiedId),
|
|
|
|
Check::tr("properties can only be assigned once"));
|
|
|
|
}
|
|
|
|
m_propertyStack.top().insert(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
bool Check::visit(UiObjectDefinition *ast)
|
|
|
|
{
|
2010-02-16 13:28:43 +01:00
|
|
|
visitQmlObject(ast, ast->qualifiedTypeNameId, ast->initializer);
|
2010-02-16 10:36:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(UiObjectBinding *ast)
|
|
|
|
{
|
|
|
|
checkScopeObjectMember(ast->qualifiedId);
|
2010-12-13 15:08:32 +01:00
|
|
|
if (!ast->hasOnToken)
|
|
|
|
checkProperty(ast->qualifiedId);
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-02-16 13:28:43 +01:00
|
|
|
visitQmlObject(ast, ast->qualifiedTypeNameId, ast->initializer);
|
2010-02-16 11:53:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-16 13:28:43 +01:00
|
|
|
void Check::visitQmlObject(Node *ast, UiQualifiedId *typeId,
|
|
|
|
UiObjectInitializer *initializer)
|
2010-02-16 11:53:21 +01:00
|
|
|
{
|
2010-02-17 09:29:13 +01:00
|
|
|
// If the 'typeId' starts with a lower-case letter, it doesn't define
|
|
|
|
// a new object instance. For instance: anchors { ... }
|
|
|
|
if (typeId->name->asString().at(0).isLower() && ! typeId->next) {
|
|
|
|
checkScopeObjectMember(typeId);
|
|
|
|
// ### don't give up!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-19 15:55:11 +01:00
|
|
|
_scopeBuilder.push(ast);
|
2010-02-18 14:24:46 +01:00
|
|
|
|
2010-02-16 13:28:43 +01:00
|
|
|
if (! _context.lookupType(_doc.data(), typeId)) {
|
2010-04-07 10:43:09 +02:00
|
|
|
if (! _ignoreTypeErrors)
|
2010-04-28 10:32:45 +02:00
|
|
|
error(typeId->identifierToken,
|
2010-05-19 13:32:11 +02:00
|
|
|
Check::tr("unknown type"));
|
2010-02-19 15:55:11 +01:00
|
|
|
// suppress subsequent errors about scope object lookup by clearing
|
|
|
|
// the scope object list
|
|
|
|
// ### todo: better way?
|
|
|
|
_context.scopeChain().qmlScopeObjects.clear();
|
|
|
|
_context.scopeChain().update();
|
2010-02-16 13:28:43 +01:00
|
|
|
}
|
|
|
|
|
2010-02-16 11:53:21 +01:00
|
|
|
Node::accept(initializer, this);
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-02-19 15:55:11 +01:00
|
|
|
_scopeBuilder.pop();
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(UiScriptBinding *ast)
|
|
|
|
{
|
2010-02-23 14:55:38 +01:00
|
|
|
// special case for id property
|
|
|
|
if (ast->qualifiedId->name->asString() == QLatin1String("id") && ! ast->qualifiedId->next) {
|
|
|
|
if (! ast->statement)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const SourceLocation loc = locationFromRange(ast->statement->firstSourceLocation(),
|
|
|
|
ast->statement->lastSourceLocation());
|
|
|
|
|
|
|
|
ExpressionStatement *expStmt = cast<ExpressionStatement *>(ast->statement);
|
|
|
|
if (!expStmt) {
|
2010-05-19 13:32:11 +02:00
|
|
|
error(loc, Check::tr("expected id"));
|
2010-02-23 14:55:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-04 16:40:47 +01:00
|
|
|
QString id;
|
|
|
|
if (IdentifierExpression *idExp = cast<IdentifierExpression *>(expStmt->expression)) {
|
|
|
|
id = idExp->name->asString();
|
|
|
|
} else if (StringLiteral *strExp = cast<StringLiteral *>(expStmt->expression)) {
|
|
|
|
id = strExp->value->asString();
|
2010-05-19 13:32:11 +02:00
|
|
|
warning(loc, Check::tr("using string literals for ids is discouraged"));
|
2010-03-04 16:40:47 +01:00
|
|
|
} else {
|
2010-05-19 13:32:11 +02:00
|
|
|
error(loc, Check::tr("expected id"));
|
2010-02-23 14:55:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-07 17:31:53 +01:00
|
|
|
if (id.isEmpty() || (!id[0].isLower() && id[0] != '_')) {
|
|
|
|
error(loc, Check::tr("ids must be lower case or start with underscore"));
|
2010-02-23 14:55:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-12-07 17:31:53 +01:00
|
|
|
|
|
|
|
if (m_ids.contains(id)) {
|
|
|
|
error(loc, Check::tr("ids must be unique"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_ids.insert(id);
|
2010-02-23 14:55:38 +01:00
|
|
|
}
|
|
|
|
|
2010-12-07 17:31:53 +01:00
|
|
|
checkProperty(ast->qualifiedId);
|
|
|
|
|
2010-02-19 15:10:39 +01:00
|
|
|
const Value *lhsValue = checkScopeObjectMember(ast->qualifiedId);
|
|
|
|
if (lhsValue) {
|
|
|
|
// ### Fix the evaluator to accept statements!
|
|
|
|
if (ExpressionStatement *expStmt = cast<ExpressionStatement *>(ast->statement)) {
|
2010-02-23 12:36:12 +01:00
|
|
|
ExpressionNode *expr = expStmt->expression;
|
2010-02-23 14:36:38 +01:00
|
|
|
|
|
|
|
Evaluate evaluator(&_context);
|
|
|
|
const Value *rhsValue = evaluator(expr);
|
|
|
|
|
|
|
|
const SourceLocation loc = locationFromRange(expStmt->firstSourceLocation(),
|
|
|
|
expStmt->lastSourceLocation());
|
2010-02-23 17:02:50 +01:00
|
|
|
AssignmentCheck assignmentCheck;
|
2010-11-23 14:30:23 +01:00
|
|
|
DiagnosticMessage message = assignmentCheck(_doc, loc, lhsValue, rhsValue, expr);
|
2010-02-23 17:02:50 +01:00
|
|
|
if (! message.message.isEmpty())
|
|
|
|
_messages += message;
|
2010-02-19 15:10:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-12-06 10:03:53 +01:00
|
|
|
if (Block *block = cast<Block *>(ast->statement)) {
|
|
|
|
FunctionBodyCheck bodyCheck;
|
|
|
|
_messages.append(bodyCheck(block->statements, _options));
|
|
|
|
}
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(UiArrayBinding *ast)
|
|
|
|
{
|
|
|
|
checkScopeObjectMember(ast->qualifiedId);
|
2010-12-07 17:31:53 +01:00
|
|
|
checkProperty(ast->qualifiedId);
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-24 15:12:11 +01:00
|
|
|
bool Check::visit(IdentifierExpression *ast)
|
|
|
|
{
|
|
|
|
// currently disabled: too many false negatives
|
|
|
|
return true;
|
|
|
|
|
|
|
|
_lastValue = 0;
|
|
|
|
if (ast->name) {
|
|
|
|
Evaluate evaluator(&_context);
|
|
|
|
_lastValue = evaluator.reference(ast);
|
|
|
|
if (!_lastValue)
|
|
|
|
error(ast->identifierToken, tr("unknown identifier"));
|
|
|
|
if (const Reference *ref = value_cast<const Reference *>(_lastValue)) {
|
|
|
|
_lastValue = _context.lookupReference(ref);
|
|
|
|
if (!_lastValue)
|
|
|
|
error(ast->identifierToken, tr("could not resolve"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(FieldMemberExpression *ast)
|
|
|
|
{
|
|
|
|
// currently disabled: too many false negatives
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Node::accept(ast->base, this);
|
|
|
|
if (!_lastValue)
|
|
|
|
return false;
|
|
|
|
const ObjectValue *obj = _lastValue->asObjectValue();
|
|
|
|
if (!obj) {
|
|
|
|
error(locationFromRange(ast->base->firstSourceLocation(), ast->base->lastSourceLocation()),
|
|
|
|
tr("does not have members"));
|
|
|
|
}
|
|
|
|
if (!obj || !ast->name) {
|
|
|
|
_lastValue = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_lastValue = obj->lookupMember(ast->name->asString(), &_context);
|
|
|
|
if (!_lastValue)
|
|
|
|
error(ast->identifierToken, tr("unknown member"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(FunctionDeclaration *ast)
|
|
|
|
{
|
|
|
|
return visit(static_cast<FunctionExpression *>(ast));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(FunctionExpression *ast)
|
|
|
|
{
|
2010-11-25 13:38:15 +01:00
|
|
|
FunctionBodyCheck bodyCheck;
|
|
|
|
_messages.append(bodyCheck(ast, _options));
|
|
|
|
|
2010-11-24 15:12:11 +01:00
|
|
|
Node::accept(ast->formals, this);
|
|
|
|
_scopeBuilder.push(ast);
|
|
|
|
Node::accept(ast->body, this);
|
|
|
|
_scopeBuilder.pop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-29 11:20:07 +01:00
|
|
|
static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp, const Value *other)
|
2010-11-25 13:38:15 +01:00
|
|
|
{
|
|
|
|
if (NumericLiteral *literal = cast<NumericLiteral *>(exp)) {
|
2010-11-29 11:20:07 +01:00
|
|
|
if (literal->value == 0 && !other->asNumberValue())
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
2010-11-29 11:52:30 +01:00
|
|
|
} else if ((cast<TrueLiteral *>(exp) || cast<FalseLiteral *>(exp)) && !other->asBooleanValue()) {
|
|
|
|
return true;
|
|
|
|
} else if (cast<NullExpression *>(exp)) {
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
|
|
|
} else if (IdentifierExpression *ident = cast<IdentifierExpression *>(exp)) {
|
|
|
|
if (ident->name && ident->name->asString() == QLatin1String("undefined"))
|
|
|
|
return true;
|
|
|
|
} else if (StringLiteral *literal = cast<StringLiteral *>(exp)) {
|
2010-11-29 11:52:30 +01:00
|
|
|
if ((!literal->value || literal->value->asString().isEmpty()) && !other->asStringValue())
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(BinaryExpression *ast)
|
|
|
|
{
|
|
|
|
if (ast->op == QSOperator::Equal || ast->op == QSOperator::NotEqual) {
|
2010-11-29 11:20:07 +01:00
|
|
|
bool warn = _options & WarnAllNonStrictEqualityChecks;
|
|
|
|
if (!warn && _options & WarnDangerousNonStrictEqualityChecks) {
|
|
|
|
Evaluate eval(&_context);
|
|
|
|
const Value *lhs = eval(ast->left);
|
|
|
|
const Value *rhs = eval(ast->right);
|
|
|
|
warn = shouldAvoidNonStrictEqualityCheck(ast->left, rhs)
|
|
|
|
|| shouldAvoidNonStrictEqualityCheck(ast->right, lhs);
|
|
|
|
}
|
|
|
|
if (warn) {
|
2010-11-25 13:38:15 +01:00
|
|
|
warning(ast->operatorToken, tr("== and != perform type coercion, use === or !== instead to avoid"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(Block *ast)
|
|
|
|
{
|
|
|
|
if (Node *p = parent()) {
|
|
|
|
if (_options & WarnBlocks
|
|
|
|
&& !cast<UiScriptBinding *>(p)
|
|
|
|
&& !cast<TryStatement *>(p)
|
|
|
|
&& !cast<Catch *>(p)
|
|
|
|
&& !cast<Finally *>(p)
|
|
|
|
&& !cast<ForStatement *>(p)
|
|
|
|
&& !cast<ForEachStatement *>(p)
|
2010-12-06 09:54:10 +01:00
|
|
|
&& !cast<LocalForStatement *>(p)
|
|
|
|
&& !cast<LocalForEachStatement *>(p)
|
2010-11-25 13:38:15 +01:00
|
|
|
&& !cast<DoWhileStatement *>(p)
|
|
|
|
&& !cast<WhileStatement *>(p)
|
|
|
|
&& !cast<IfStatement *>(p)
|
|
|
|
&& !cast<SwitchStatement *>(p)
|
|
|
|
&& !cast<WithStatement *>(p)) {
|
|
|
|
warning(ast->lbraceToken, tr("blocks do not introduce a new scope, avoid"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(WithStatement *ast)
|
|
|
|
{
|
|
|
|
if (_options & WarnWith)
|
|
|
|
warning(ast->withToken, tr("use of the with statement is not recommended, use a var instead"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(VoidExpression *ast)
|
|
|
|
{
|
|
|
|
if (_options & WarnVoid)
|
|
|
|
warning(ast->voidToken, tr("use of void is usually confusing and not recommended"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(Expression *ast)
|
|
|
|
{
|
|
|
|
if (_options & WarnCommaExpression && ast->left && ast->right) {
|
2010-12-06 09:54:10 +01:00
|
|
|
Node *p = parent();
|
|
|
|
if (!cast<ForStatement *>(p)
|
|
|
|
&& !cast<LocalForStatement *>(p)) {
|
2010-11-25 13:38:15 +01:00
|
|
|
warning(ast->commaToken, tr("avoid comma expressions"));
|
2010-12-06 09:54:10 +01:00
|
|
|
}
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(ExpressionStatement *ast)
|
|
|
|
{
|
|
|
|
if (_options & WarnExpressionStatement && ast->expression) {
|
|
|
|
bool ok = cast<CallExpression *>(ast->expression)
|
|
|
|
|| cast<DeleteExpression *>(ast->expression)
|
|
|
|
|| cast<PreDecrementExpression *>(ast->expression)
|
|
|
|
|| cast<PreIncrementExpression *>(ast->expression)
|
|
|
|
|| cast<PostIncrementExpression *>(ast->expression)
|
2010-11-29 12:21:02 +01:00
|
|
|
|| cast<PostDecrementExpression *>(ast->expression);
|
2010-11-25 13:38:15 +01:00
|
|
|
if (BinaryExpression *binary = cast<BinaryExpression *>(ast->expression)) {
|
|
|
|
switch (binary->op) {
|
|
|
|
case QSOperator::Assign:
|
|
|
|
case QSOperator::InplaceAdd:
|
|
|
|
case QSOperator::InplaceAnd:
|
|
|
|
case QSOperator::InplaceDiv:
|
|
|
|
case QSOperator::InplaceLeftShift:
|
|
|
|
case QSOperator::InplaceRightShift:
|
|
|
|
case QSOperator::InplaceMod:
|
|
|
|
case QSOperator::InplaceMul:
|
|
|
|
case QSOperator::InplaceOr:
|
|
|
|
case QSOperator::InplaceSub:
|
|
|
|
case QSOperator::InplaceURightShift:
|
|
|
|
case QSOperator::InplaceXor:
|
|
|
|
ok = true;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2010-11-29 12:21:02 +01:00
|
|
|
if (!ok) {
|
|
|
|
for (int i = 0; Node *p = parent(i); ++i) {
|
|
|
|
if (UiScriptBinding *binding = cast<UiScriptBinding *>(p)) {
|
|
|
|
if (!cast<Block *>(binding->statement)) {
|
|
|
|
ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-25 13:38:15 +01:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
warning(locationFromRange(ast->firstSourceLocation(), ast->lastSourceLocation()),
|
|
|
|
tr("expression statements should be assignments, calls or delete expressions only"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(IfStatement *ast)
|
|
|
|
{
|
|
|
|
if (ast->expression)
|
|
|
|
checkAssignInCondition(ast->expression);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(ForStatement *ast)
|
|
|
|
{
|
|
|
|
if (ast->condition)
|
|
|
|
checkAssignInCondition(ast->condition);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-06 09:54:10 +01:00
|
|
|
bool Check::visit(LocalForStatement *ast)
|
|
|
|
{
|
|
|
|
if (ast->condition)
|
|
|
|
checkAssignInCondition(ast->condition);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
bool Check::visit(WhileStatement *ast)
|
|
|
|
{
|
|
|
|
if (ast->expression)
|
|
|
|
checkAssignInCondition(ast->expression);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(DoWhileStatement *ast)
|
|
|
|
{
|
|
|
|
if (ast->expression)
|
|
|
|
checkAssignInCondition(ast->expression);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(CaseClause *ast)
|
|
|
|
{
|
|
|
|
checkEndsWithControlFlow(ast->statements, ast->caseToken);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(DefaultClause *ast)
|
|
|
|
{
|
|
|
|
checkEndsWithControlFlow(ast->statements, ast->defaultToken);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-06 11:44:55 +02:00
|
|
|
/// When something is changed here, also change ReadingContext::lookupProperty in
|
|
|
|
/// texttomodelmerger.cpp
|
|
|
|
/// ### Maybe put this into the context as a helper method.
|
2010-02-19 15:10:39 +01:00
|
|
|
const Value *Check::checkScopeObjectMember(const UiQualifiedId *id)
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
2010-02-19 10:14:34 +01:00
|
|
|
QList<const ObjectValue *> scopeObjects = _context.scopeChain().qmlScopeObjects;
|
2010-02-19 15:55:11 +01:00
|
|
|
if (scopeObjects.isEmpty())
|
|
|
|
return 0;
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
if (! id)
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0; // ### error?
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-04-06 11:44:55 +02:00
|
|
|
if (! id->name) // possible after error recovery
|
|
|
|
return 0;
|
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
QString propertyName = id->name->asString();
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
if (propertyName == QLatin1String("id") && ! id->next)
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0; // ### should probably be a special value
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
// attached properties
|
2010-02-18 15:01:26 +01:00
|
|
|
bool isAttachedProperty = false;
|
|
|
|
if (! propertyName.isEmpty() && propertyName[0].isUpper()) {
|
|
|
|
isAttachedProperty = true;
|
2010-08-30 13:31:50 +02:00
|
|
|
if (const ObjectValue *qmlTypes = _context.scopeChain().qmlTypes)
|
|
|
|
scopeObjects += qmlTypes;
|
2010-02-18 15:01:26 +01:00
|
|
|
}
|
2010-02-18 10:42:15 +01:00
|
|
|
|
2010-02-19 10:14:34 +01:00
|
|
|
if (scopeObjects.isEmpty())
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
// global lookup for first part of id
|
2010-02-19 10:14:34 +01:00
|
|
|
const Value *value = 0;
|
|
|
|
for (int i = scopeObjects.size() - 1; i >= 0; --i) {
|
|
|
|
value = scopeObjects[i]->lookupMember(propertyName, &_context);
|
|
|
|
if (value)
|
|
|
|
break;
|
|
|
|
}
|
2010-02-16 10:36:09 +01:00
|
|
|
if (!value) {
|
|
|
|
error(id->identifierToken,
|
2010-05-19 13:32:11 +02:00
|
|
|
Check::tr("'%1' is not a valid property name").arg(propertyName));
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
2010-02-18 15:01:26 +01:00
|
|
|
// can't look up members for attached properties
|
|
|
|
if (isAttachedProperty)
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-18 15:01:26 +01:00
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
// member lookup
|
|
|
|
const UiQualifiedId *idPart = id;
|
|
|
|
while (idPart->next) {
|
|
|
|
const ObjectValue *objectValue = value_cast<const ObjectValue *>(value);
|
|
|
|
if (! objectValue) {
|
|
|
|
error(idPart->identifierToken,
|
2010-05-19 13:32:11 +02:00
|
|
|
Check::tr("'%1' does not have members").arg(propertyName));
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-18 14:21:53 +01:00
|
|
|
}
|
|
|
|
|
2010-02-24 17:14:14 +01:00
|
|
|
if (! idPart->next->name) {
|
|
|
|
// somebody typed "id." and error recovery still gave us a valid tree,
|
|
|
|
// so just bail out here.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
idPart = idPart->next;
|
|
|
|
propertyName = idPart->name->asString();
|
|
|
|
|
|
|
|
value = objectValue->lookupMember(propertyName, &_context);
|
|
|
|
if (! value) {
|
|
|
|
error(idPart->identifierToken,
|
2010-05-19 13:32:11 +02:00
|
|
|
Check::tr("'%1' is not a member of '%2'").arg(
|
2010-04-28 10:32:45 +02:00
|
|
|
propertyName, objectValue->className()));
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-18 14:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-19 15:10:39 +01:00
|
|
|
|
|
|
|
return value;
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
void Check::checkAssignInCondition(AST::ExpressionNode *condition)
|
|
|
|
{
|
|
|
|
if (_options & WarnAssignInCondition) {
|
|
|
|
if (BinaryExpression *binary = cast<BinaryExpression *>(condition)) {
|
|
|
|
if (binary->op == QSOperator::Assign)
|
|
|
|
warning(binary->operatorToken, tr("avoid assignments in conditions"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::checkEndsWithControlFlow(StatementList *statements, SourceLocation errorLoc)
|
|
|
|
{
|
|
|
|
// full flow analysis would be neat
|
|
|
|
if (!statements || !(_options & WarnCaseWithoutFlowControlEnd))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Statement *lastStatement = 0;
|
|
|
|
for (StatementList *slist = statements; slist; slist = slist->next)
|
|
|
|
lastStatement = slist->statement;
|
|
|
|
|
|
|
|
if (!cast<ReturnStatement *>(lastStatement)
|
|
|
|
&& !cast<ThrowStatement *>(lastStatement)
|
|
|
|
&& !cast<BreakStatement *>(lastStatement)
|
|
|
|
&& !cast<ContinueStatement *>(lastStatement)) {
|
|
|
|
warning(errorLoc, tr("case does not end with return, break, continue or throw"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
void Check::error(const AST::SourceLocation &loc, const QString &message)
|
|
|
|
{
|
|
|
|
_messages.append(DiagnosticMessage(DiagnosticMessage::Error, loc, message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::warning(const AST::SourceLocation &loc, const QString &message)
|
|
|
|
{
|
|
|
|
_messages.append(DiagnosticMessage(DiagnosticMessage::Warning, loc, message));
|
|
|
|
}
|
2010-11-25 13:38:15 +01:00
|
|
|
|
|
|
|
Node *Check::parent(int distance)
|
|
|
|
{
|
|
|
|
const int index = _chain.size() - 2 - distance;
|
|
|
|
if (index < 0)
|
|
|
|
return 0;
|
|
|
|
return _chain.at(index);
|
|
|
|
}
|