2010-02-16 10:36:09 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-02-16 10:36:09 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-02-16 10:36:09 +01:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-02-16 10:36:09 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
**
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-02-16 10:36:09 +01:00
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "qmljscheck.h"
|
|
|
|
#include "qmljsbind.h"
|
2011-07-01 13:51:53 +02:00
|
|
|
#include "qmljscontext.h"
|
2010-02-16 11:53:21 +01:00
|
|
|
#include "qmljsevaluate.h"
|
2011-10-07 14:04:06 +02:00
|
|
|
#include "qmljsutils.h"
|
2010-02-16 10:36:09 +01:00
|
|
|
#include "parser/qmljsast_p.h"
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
#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;
|
2011-09-28 15:16:00 +02:00
|
|
|
using namespace QmlJS::StaticAnalysis;
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-02-23 17:02:50 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AssignmentCheck : public ValueVisitor
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 15:16:00 +02:00
|
|
|
Message operator()(
|
2010-11-23 14:30:23 +01:00
|
|
|
const Document::Ptr &document,
|
2010-02-23 17:02:50 +01:00
|
|
|
const SourceLocation &location,
|
2011-08-08 12:47:49 +02:00
|
|
|
const Value *lhsValue,
|
|
|
|
const Value *rhsValue,
|
2011-09-01 15:04:21 +02:00
|
|
|
Node *ast)
|
2010-02-23 17:02:50 +01:00
|
|
|
{
|
2010-11-23 14:30:23 +01:00
|
|
|
_doc = document;
|
2010-02-23 17:02:50 +01:00
|
|
|
_rhsValue = rhsValue;
|
2011-09-28 15:16:00 +02:00
|
|
|
_location = location;
|
2011-09-01 15:04:21 +02:00
|
|
|
if (ExpressionStatement *expStmt = cast<ExpressionStatement *>(ast))
|
|
|
|
_ast = expStmt->expression;
|
|
|
|
else
|
|
|
|
_ast = ast->expressionCast();
|
2010-02-23 17:02:50 +01:00
|
|
|
|
|
|
|
if (lhsValue)
|
|
|
|
lhsValue->accept(this);
|
|
|
|
|
|
|
|
return _message;
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
void setMessage(Type type)
|
|
|
|
{
|
|
|
|
_message = Message(type, _location);
|
|
|
|
}
|
|
|
|
|
2010-05-19 12:23:55 +02:00
|
|
|
virtual void visit(const NumberValue *value)
|
2010-02-23 17:02:50 +01:00
|
|
|
{
|
2011-10-10 10:55:37 +02:00
|
|
|
if (const QmlEnumValue *enumValue = value_cast<QmlEnumValue>(value)) {
|
2010-05-19 12:23:55 +02:00
|
|
|
if (StringLiteral *stringLiteral = cast<StringLiteral *>(_ast)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
const QString valueName = stringLiteral->value.toString();
|
2010-05-19 12:23:55 +02:00
|
|
|
|
|
|
|
if (!enumValue->keys().contains(valueName)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrInvalidEnumValue);
|
2010-05-19 12:23:55 +02:00
|
|
|
}
|
2010-11-29 08:59:54 +01:00
|
|
|
} else if (! _rhsValue->asStringValue() && ! _rhsValue->asNumberValue()
|
2011-10-10 12:53:28 +02:00
|
|
|
&& ! _rhsValue->asUnknownValue()) {
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrEnumValueMustBeStringOrNumber);
|
2010-05-19 12:23:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
2011-09-01 15:04:21 +02:00
|
|
|
if (cast<TrueLiteral *>(_ast)
|
|
|
|
|| cast<FalseLiteral *>(_ast)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrNumberValueExpected);
|
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))) {
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrBooleanValueExpected);
|
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))
|
2011-09-01 15:04:21 +02:00
|
|
|
|| cast<TrueLiteral *>(_ast)
|
|
|
|
|| cast<FalseLiteral *>(_ast)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrStringValueExpected);
|
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)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
QUrl url(literal->value.toString());
|
2010-11-23 14:30:23 +01:00
|
|
|
if (!url.isValid() && !url.isEmpty()) {
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrInvalidUrl);
|
2010-11-23 14:30:23 +01:00
|
|
|
} else {
|
|
|
|
QString fileName = url.toLocalFile();
|
|
|
|
if (!fileName.isEmpty()) {
|
2011-02-08 14:38:48 +01:00
|
|
|
if (QFileInfo(fileName).isRelative()) {
|
2010-11-23 14:30:23 +01:00
|
|
|
fileName.prepend(QDir::separator());
|
|
|
|
fileName.prepend(_doc->path());
|
|
|
|
}
|
2011-03-01 14:33:40 +01:00
|
|
|
if (!QFileInfo(fileName).exists()) {
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(WarnFileOrDirectoryDoesNotExist);
|
2011-03-01 14:33:40 +01:00
|
|
|
}
|
2010-11-23 14:30:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-23 17:02:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(const ColorValue *)
|
|
|
|
{
|
|
|
|
if (StringLiteral *stringLiteral = cast<StringLiteral *>(_ast)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!toQColor(stringLiteral->value.toString()).isValid())
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrInvalidColor);
|
2010-02-23 17:02:50 +01:00
|
|
|
} else {
|
|
|
|
visit((StringValue *)0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(const AnchorLineValue *)
|
|
|
|
{
|
2011-10-10 12:53:28 +02:00
|
|
|
if (! (_rhsValue->asAnchorLineValue() || _rhsValue->asUnknownValue()))
|
2011-09-28 15:16:00 +02:00
|
|
|
setMessage(ErrAnchorLineExpected);
|
2010-02-23 17:02:50 +01:00
|
|
|
}
|
|
|
|
|
2010-11-23 14:30:23 +01:00
|
|
|
Document::Ptr _doc;
|
2011-09-28 15:16:00 +02:00
|
|
|
Message _message;
|
|
|
|
SourceLocation _location;
|
2010-02-23 17:02:50 +01:00
|
|
|
const Value *_rhsValue;
|
|
|
|
ExpressionNode *_ast;
|
|
|
|
};
|
|
|
|
|
2011-09-07 07:21:38 +02:00
|
|
|
class ReachesEndCheck : protected Visitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(Node *node)
|
|
|
|
{
|
|
|
|
_labels.clear();
|
|
|
|
_labelledBreaks.clear();
|
|
|
|
return check(node) == ReachesEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Sorted by how much code will be reachable from that state, i.e.
|
|
|
|
// ReachesEnd is guaranteed to reach more code than Break and so on.
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
ReachesEnd = 0,
|
|
|
|
Break = 1,
|
|
|
|
Continue = 2,
|
|
|
|
ReturnOrThrow = 3
|
|
|
|
};
|
|
|
|
State _state;
|
2011-09-16 10:35:48 +02:00
|
|
|
QHash<QString, Node *> _labels;
|
2011-09-07 07:21:38 +02:00
|
|
|
QSet<Node *> _labelledBreaks;
|
|
|
|
|
|
|
|
virtual void onUnreachable(Node *)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual State check(Node *node)
|
|
|
|
{
|
|
|
|
_state = ReachesEnd;
|
|
|
|
Node::accept(node, this);
|
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool preVisit(Node *ast)
|
|
|
|
{
|
|
|
|
if (ast->expressionCast())
|
|
|
|
return false;
|
|
|
|
if (_state == ReachesEnd)
|
|
|
|
return true;
|
|
|
|
if (Statement *stmt = ast->statementCast())
|
|
|
|
onUnreachable(stmt);
|
|
|
|
if (FunctionSourceElement *fun = cast<FunctionSourceElement *>(ast))
|
|
|
|
onUnreachable(fun->declaration);
|
|
|
|
if (StatementSourceElement *stmt = cast<StatementSourceElement *>(ast))
|
|
|
|
onUnreachable(stmt->statement);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit(LabelledStatement *ast)
|
|
|
|
{
|
|
|
|
// get the target statement
|
|
|
|
Statement *end = ast->statement;
|
|
|
|
forever {
|
|
|
|
if (LabelledStatement *label = cast<LabelledStatement *>(end))
|
|
|
|
end = label->statement;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!ast->label.isEmpty())
|
|
|
|
_labels[ast->label.toString()] = end;
|
2011-09-07 07:21:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit(BreakStatement *ast)
|
|
|
|
{
|
|
|
|
_state = Break;
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!ast->label.isEmpty()) {
|
2011-09-19 15:28:05 +02:00
|
|
|
if (Node *target = _labels.value(ast->label.toString())) {
|
2011-09-07 07:21:38 +02:00
|
|
|
_labelledBreaks.insert(target);
|
2011-09-19 15:28:05 +02:00
|
|
|
_state = ReturnOrThrow; // unwind until label is hit
|
|
|
|
}
|
2011-09-07 07:21:38 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// labelled continues don't change control flow...
|
|
|
|
virtual bool visit(ContinueStatement *) { _state = Continue; return false; }
|
|
|
|
|
|
|
|
virtual bool visit(ReturnStatement *) { _state = ReturnOrThrow; return false; }
|
|
|
|
virtual bool visit(ThrowStatement *) { _state = ReturnOrThrow; return false; }
|
|
|
|
|
|
|
|
virtual bool visit(IfStatement *ast)
|
|
|
|
{
|
|
|
|
State ok = check(ast->ok);
|
|
|
|
State ko = check(ast->ko);
|
|
|
|
_state = qMin(ok, ko);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleClause(StatementList *statements, State *result, bool *fallthrough)
|
|
|
|
{
|
|
|
|
State clauseResult = check(statements);
|
|
|
|
if (clauseResult == ReachesEnd) {
|
|
|
|
*fallthrough = true;
|
|
|
|
} else {
|
|
|
|
*fallthrough = false;
|
|
|
|
*result = qMin(*result, clauseResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit(SwitchStatement *ast)
|
|
|
|
{
|
|
|
|
if (!ast->block)
|
|
|
|
return false;
|
|
|
|
State result = ReturnOrThrow;
|
|
|
|
bool lastWasFallthrough = false;
|
|
|
|
|
|
|
|
for (CaseClauses *it = ast->block->clauses; it; it = it->next) {
|
|
|
|
if (it->clause)
|
|
|
|
handleClause(it->clause->statements, &result, &lastWasFallthrough);
|
|
|
|
}
|
|
|
|
if (ast->block->defaultClause)
|
|
|
|
handleClause(ast->block->defaultClause->statements, &result, &lastWasFallthrough);
|
|
|
|
for (CaseClauses *it = ast->block->moreClauses; it; it = it->next) {
|
|
|
|
if (it->clause)
|
|
|
|
handleClause(it->clause->statements, &result, &lastWasFallthrough);
|
|
|
|
}
|
|
|
|
|
2011-09-19 15:28:05 +02:00
|
|
|
if (lastWasFallthrough || !ast->block->defaultClause)
|
2011-09-07 07:21:38 +02:00
|
|
|
result = ReachesEnd;
|
|
|
|
if (result == Break || _labelledBreaks.contains(ast))
|
|
|
|
result = ReachesEnd;
|
|
|
|
_state = result;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit(TryStatement *ast)
|
|
|
|
{
|
|
|
|
State tryBody = check(ast->statement);
|
2011-09-16 15:32:47 +02:00
|
|
|
State catchBody = ReturnOrThrow;
|
|
|
|
if (ast->catchExpression)
|
|
|
|
catchBody = check(ast->catchExpression->statement);
|
|
|
|
State finallyBody = ReachesEnd;
|
|
|
|
if (ast->finallyExpression)
|
|
|
|
finallyBody = check(ast->finallyExpression->statement);
|
2011-09-07 07:21:38 +02:00
|
|
|
|
|
|
|
_state = qMax(qMin(tryBody, catchBody), finallyBody);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-19 15:28:05 +02:00
|
|
|
bool preconditionLoopStatement(Node *, Statement *body)
|
2011-09-07 07:21:38 +02:00
|
|
|
{
|
|
|
|
check(body);
|
2011-09-19 15:28:05 +02:00
|
|
|
_state = ReachesEnd; // condition could be false...
|
2011-09-07 07:21:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-19 15:28:05 +02:00
|
|
|
virtual bool visit(WhileStatement *ast) { return preconditionLoopStatement(ast, ast->statement); }
|
|
|
|
virtual bool visit(ForStatement *ast) { return preconditionLoopStatement(ast, ast->statement); }
|
|
|
|
virtual bool visit(ForEachStatement *ast) { return preconditionLoopStatement(ast, ast->statement); }
|
|
|
|
virtual bool visit(LocalForStatement *ast) { return preconditionLoopStatement(ast, ast->statement); }
|
|
|
|
virtual bool visit(LocalForEachStatement *ast) { return preconditionLoopStatement(ast, ast->statement); }
|
|
|
|
|
|
|
|
virtual bool visit(DoWhileStatement *ast)
|
|
|
|
{
|
|
|
|
check(ast->statement);
|
|
|
|
// not necessarily an infinite loop due to labelled breaks
|
|
|
|
if (_state == Continue)
|
|
|
|
_state = ReturnOrThrow;
|
|
|
|
if (_state == Break || _labelledBreaks.contains(ast))
|
|
|
|
_state = ReachesEnd;
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-07 07:21:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MarkUnreachableCode : protected ReachesEndCheck
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> _messages;
|
2011-09-07 07:21:38 +02:00
|
|
|
bool _emittedWarning;
|
|
|
|
|
|
|
|
public:
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> operator()(Node *ast)
|
2011-09-07 07:21:38 +02:00
|
|
|
{
|
|
|
|
_messages.clear();
|
|
|
|
check(ast);
|
|
|
|
return _messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual State check(Node *node)
|
|
|
|
{
|
|
|
|
bool oldwarning = _emittedWarning;
|
|
|
|
_emittedWarning = false;
|
|
|
|
State s = ReachesEndCheck::check(node);
|
|
|
|
_emittedWarning = oldwarning;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onUnreachable(Node *node)
|
|
|
|
{
|
|
|
|
if (_emittedWarning)
|
|
|
|
return;
|
|
|
|
_emittedWarning = true;
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
Message message(WarnUnreachable, SourceLocation());
|
2011-09-07 07:21:38 +02:00
|
|
|
if (Statement *statement = node->statementCast())
|
2011-09-28 15:16:00 +02:00
|
|
|
message.location = locationFromRange(statement->firstSourceLocation(), statement->lastSourceLocation());
|
2011-09-07 07:21:38 +02:00
|
|
|
else if (ExpressionNode *expr = node->expressionCast())
|
2011-09-28 15:16:00 +02:00
|
|
|
message.location = locationFromRange(expr->firstSourceLocation(), expr->lastSourceLocation());
|
|
|
|
if (message.isValid())
|
2011-09-07 07:21:38 +02:00
|
|
|
_messages += message;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-06 13:59:50 +02:00
|
|
|
class DeclarationsCheck : protected Visitor
|
2010-11-25 13:38:15 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> operator()(FunctionExpression *function)
|
2010-11-25 13:38:15 +01:00
|
|
|
{
|
2010-12-06 10:03:53 +01:00
|
|
|
clear();
|
2010-11-25 13:38:15 +01:00
|
|
|
for (FormalParameterList *plist = function->formals; plist; plist = plist->next) {
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!plist->name.isEmpty())
|
|
|
|
_formalParameterNames += plist->name.toString();
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Node::accept(function->body, this);
|
|
|
|
return _messages;
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> operator()(Node *node)
|
2010-12-06 10:03:53 +01:00
|
|
|
{
|
|
|
|
clear();
|
2011-09-07 07:21:38 +02:00
|
|
|
Node::accept(node, this);
|
2010-12-06 10:03:53 +01:00
|
|
|
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)
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast->name.isEmpty())
|
2010-11-25 13:38:15 +01:00
|
|
|
return false;
|
2011-09-13 09:57:24 +02:00
|
|
|
const QString &name = ast->name.toString();
|
2010-11-25 13:38:15 +01:00
|
|
|
if (!_declaredFunctions.contains(name) && !_declaredVariables.contains(name))
|
|
|
|
_possiblyUndeclaredUses[name].append(ast->identifierToken);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(VariableStatement *ast)
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
if (_seenNonDeclarationStatement) {
|
|
|
|
addMessage(HintDeclarationsShouldBeAtStartOfFunction, ast->declarationKindToken);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(VariableDeclaration *ast)
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast->name.isEmpty())
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
2011-09-13 09:57:24 +02:00
|
|
|
const QString &name = ast->name.toString();
|
2010-11-25 13:38:15 +01:00
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
if (_formalParameterNames.contains(name)) {
|
|
|
|
addMessage(WarnAlreadyFormalParameter, ast->identifierToken, name);
|
|
|
|
} else if (_declaredFunctions.contains(name)) {
|
|
|
|
addMessage(WarnAlreadyFunction, ast->identifierToken, name);
|
|
|
|
} else if (_declaredVariables.contains(name)) {
|
|
|
|
addMessage(WarnDuplicateDeclaration, ast->identifierToken, name);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_possiblyUndeclaredUses.contains(name)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
foreach (const SourceLocation &loc, _possiblyUndeclaredUses.value(name)) {
|
|
|
|
addMessage(WarnVarUsedBeforeDeclaration, loc, name);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
_possiblyUndeclaredUses.remove(name);
|
|
|
|
}
|
|
|
|
_declaredVariables[name] = ast;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(FunctionDeclaration *ast)
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
if (_seenNonDeclarationStatement) {
|
|
|
|
addMessage(HintDeclarationsShouldBeAtStartOfFunction, ast->functionToken);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return visit(static_cast<FunctionExpression *>(ast));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(FunctionExpression *ast)
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast->name.isEmpty())
|
2010-11-25 13:38:15 +01:00
|
|
|
return false;
|
2011-09-13 09:57:24 +02:00
|
|
|
const QString &name = ast->name.toString();
|
2010-11-25 13:38:15 +01:00
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
if (_formalParameterNames.contains(name)) {
|
|
|
|
addMessage(WarnAlreadyFormalParameter, ast->identifierToken, name);
|
|
|
|
} else if (_declaredVariables.contains(name)) {
|
|
|
|
addMessage(WarnAlreadyVar, ast->identifierToken, name);
|
|
|
|
} else if (_declaredFunctions.contains(name)) {
|
|
|
|
addMessage(WarnDuplicateDeclaration, ast->identifierToken, name);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FunctionDeclaration *decl = cast<FunctionDeclaration *>(ast)) {
|
|
|
|
if (_possiblyUndeclaredUses.contains(name)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
foreach (const SourceLocation &loc, _possiblyUndeclaredUses.value(name)) {
|
|
|
|
addMessage(WarnFunctionUsedBeforeDeclaration, loc, name);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
_possiblyUndeclaredUses.remove(name);
|
|
|
|
}
|
|
|
|
_declaredFunctions[name] = decl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-09-28 15:16:00 +02:00
|
|
|
void addMessage(Type type, const SourceLocation &loc, const QString &arg1 = QString())
|
2010-11-25 13:38:15 +01:00
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
_messages.append(Message(type, loc, arg1));
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> _messages;
|
2010-11-25 13:38:15 +01:00
|
|
|
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
|
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
Check::Check(Document::Ptr doc, const ContextPtr &context)
|
2010-02-16 10:36:09 +01:00
|
|
|
: _doc(doc)
|
2011-07-13 15:04:27 +02:00
|
|
|
, _context(context)
|
|
|
|
, _scopeChain(doc, _context)
|
2011-07-12 14:55:27 +02:00
|
|
|
, _scopeBuilder(&_scopeChain)
|
2010-11-24 15:12:11 +01:00
|
|
|
, _lastValue(0)
|
2011-09-29 11:48:13 +02:00
|
|
|
, _importsOk(false)
|
2011-12-02 10:41:13 +01:00
|
|
|
, _inStatementBinding(false)
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
2011-09-29 11:48:13 +02:00
|
|
|
const Imports *imports = context->imports(doc.data());
|
|
|
|
if (imports && !imports->importFailed())
|
|
|
|
_importsOk = true;
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
_enabledMessages = Message::allMessageTypes().toSet();
|
|
|
|
disableMessage(HintAnonymousFunctionSpacing);
|
|
|
|
disableMessage(HintDeclareVarsInOneLine);
|
|
|
|
disableMessage(HintDeclarationsShouldBeAtStartOfFunction);
|
2011-09-30 10:07:37 +02:00
|
|
|
disableMessage(HintBinaryOperatorSpacing);
|
2011-09-30 12:11:58 +02:00
|
|
|
disableMessage(HintOneStatementPerLine);
|
2011-10-04 09:49:30 +02:00
|
|
|
disableMessage(HintExtraParentheses);
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Check::~Check()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> Check::operator()()
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
|
|
|
_messages.clear();
|
2011-10-20 09:45:29 +02:00
|
|
|
scanCommentsForAnnotations();
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
Node::accept(_doc->ast(), this);
|
2011-10-20 09:45:29 +02:00
|
|
|
warnAboutUnnecessarySuppressions();
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
return _messages;
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
void Check::enableMessage(Type type)
|
|
|
|
{
|
|
|
|
_enabledMessages.insert(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::disableMessage(Type type)
|
|
|
|
{
|
|
|
|
_enabledMessages.remove(type);
|
|
|
|
}
|
|
|
|
|
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 *)
|
|
|
|
{
|
2010-12-13 16:46:29 +01:00
|
|
|
m_propertyStack.push(StringSet());
|
|
|
|
UiObjectDefinition *objectDefinition = cast<UiObjectDefinition *>(parent());
|
2011-09-13 09:57:24 +02:00
|
|
|
if (objectDefinition && objectDefinition->qualifiedTypeNameId->name == "Component")
|
2010-12-13 16:46:29 +01:00
|
|
|
m_idStack.push(StringSet());
|
|
|
|
UiObjectBinding *objectBinding = cast<UiObjectBinding *>(parent());
|
2011-09-13 09:57:24 +02:00
|
|
|
if (objectBinding && objectBinding->qualifiedTypeNameId->name == "Component")
|
2010-12-13 16:46:29 +01:00
|
|
|
m_idStack.push(StringSet());
|
|
|
|
if (m_idStack.isEmpty())
|
|
|
|
m_idStack.push(StringSet());
|
|
|
|
return true;
|
2010-12-07 17:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Check::endVisit(UiObjectInitializer *)
|
|
|
|
{
|
|
|
|
m_propertyStack.pop();
|
2010-12-13 16:46:29 +01:00
|
|
|
UiObjectDefinition *objectDenition = cast<UiObjectDefinition *>(parent());
|
2011-09-13 09:57:24 +02:00
|
|
|
if (objectDenition && objectDenition->qualifiedTypeNameId->name == "Component")
|
2010-12-13 16:46:29 +01:00
|
|
|
m_idStack.pop();
|
|
|
|
UiObjectBinding *objectBinding = cast<UiObjectBinding *>(parent());
|
2011-09-13 09:57:24 +02:00
|
|
|
if (objectBinding && objectBinding->qualifiedTypeNameId->name == "Component")
|
2010-12-13 16:46:29 +01:00
|
|
|
m_idStack.pop();
|
2010-12-07 17:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Check::checkProperty(UiQualifiedId *qualifiedId)
|
|
|
|
{
|
2011-10-07 14:04:06 +02:00
|
|
|
const QString id = toString(qualifiedId);
|
2010-12-07 17:31:53 +01:00
|
|
|
if (id.at(0).isLower()) {
|
|
|
|
if (m_propertyStack.top().contains(id)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrPropertiesCanOnlyHaveOneBinding, fullLocationForQualifiedId(qualifiedId));
|
2010-12-07 17:31:53 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2011-06-23 10:25:43 +02:00
|
|
|
// Don't do type checks if it's a grouped property binding.
|
|
|
|
// For instance: anchors { ... }
|
|
|
|
if (_doc->bind()->isGroupedPropertyBinding(ast)) {
|
2010-02-17 09:29:13 +01:00
|
|
|
checkScopeObjectMember(typeId);
|
|
|
|
// ### don't give up!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-20 13:19:16 +02:00
|
|
|
bool typeError = false;
|
2011-09-29 11:48:13 +02:00
|
|
|
if (_importsOk) {
|
|
|
|
const SourceLocation typeErrorLocation = fullLocationForQualifiedId(typeId);
|
|
|
|
const ObjectValue *prototype = _context->lookupType(_doc.data(), typeId);
|
|
|
|
if (!prototype) {
|
2011-05-20 13:19:16 +02:00
|
|
|
typeError = true;
|
2011-09-29 11:48:13 +02:00
|
|
|
addMessage(ErrUnknownComponent, typeErrorLocation);
|
|
|
|
} else {
|
|
|
|
PrototypeIterator iter(prototype, _context);
|
|
|
|
QList<const ObjectValue *> prototypes = iter.all();
|
|
|
|
if (iter.error() != PrototypeIterator::NoError)
|
|
|
|
typeError = true;
|
|
|
|
const ObjectValue *lastPrototype = prototypes.last();
|
|
|
|
if (iter.error() == PrototypeIterator::ReferenceResolutionError) {
|
|
|
|
if (const QmlPrototypeReference *ref =
|
2011-10-10 10:55:37 +02:00
|
|
|
value_cast<QmlPrototypeReference>(lastPrototype->prototype())) {
|
2011-09-29 11:48:13 +02:00
|
|
|
addMessage(ErrCouldNotResolvePrototypeOf, typeErrorLocation,
|
|
|
|
toString(ref->qmlTypeName()), lastPrototype->className());
|
|
|
|
} else {
|
|
|
|
addMessage(ErrCouldNotResolvePrototype, typeErrorLocation,
|
|
|
|
lastPrototype->className());
|
|
|
|
}
|
|
|
|
} else if (iter.error() == PrototypeIterator::CycleError) {
|
|
|
|
addMessage(ErrPrototypeCycle, typeErrorLocation,
|
2011-09-28 15:16:00 +02:00
|
|
|
lastPrototype->className());
|
2011-05-20 13:19:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_scopeBuilder.push(ast);
|
|
|
|
|
|
|
|
if (typeError) {
|
2010-02-19 15:55:11 +01:00
|
|
|
// suppress subsequent errors about scope object lookup by clearing
|
|
|
|
// the scope object list
|
|
|
|
// ### todo: better way?
|
2011-07-12 14:55:27 +02:00
|
|
|
_scopeChain.setQmlScopeObjects(QList<const ObjectValue *>());
|
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
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast->qualifiedId->name == QLatin1String("id") && ! ast->qualifiedId->next) {
|
2010-02-23 14:55:38 +01:00
|
|
|
if (! ast->statement)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const SourceLocation loc = locationFromRange(ast->statement->firstSourceLocation(),
|
|
|
|
ast->statement->lastSourceLocation());
|
|
|
|
|
|
|
|
ExpressionStatement *expStmt = cast<ExpressionStatement *>(ast->statement);
|
|
|
|
if (!expStmt) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrIdExpected, loc);
|
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)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
id = idExp->name.toString();
|
2010-03-04 16:40:47 +01:00
|
|
|
} else if (StringLiteral *strExp = cast<StringLiteral *>(expStmt->expression)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
id = strExp->value.toString();
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrInvalidId, loc);
|
2010-03-04 16:40:47 +01:00
|
|
|
} else {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrIdExpected, loc);
|
2010-02-23 14:55:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
if (id.isEmpty() || (!id.at(0).isLower() && id.at(0) != '_')) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrInvalidId, loc);
|
2010-02-23 14:55:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-12-07 17:31:53 +01:00
|
|
|
|
2010-12-13 16:46:29 +01:00
|
|
|
if (m_idStack.top().contains(id)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrDuplicateId, loc);
|
2010-12-07 17:31:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-12-13 16:46:29 +01:00
|
|
|
m_idStack.top().insert(id);
|
2010-02-23 14:55:38 +01:00
|
|
|
}
|
|
|
|
|
2010-12-07 17:31:53 +01:00
|
|
|
checkProperty(ast->qualifiedId);
|
|
|
|
|
2011-09-01 15:04:21 +02:00
|
|
|
if (!ast->statement)
|
|
|
|
return false;
|
|
|
|
|
2010-02-19 15:10:39 +01:00
|
|
|
const Value *lhsValue = checkScopeObjectMember(ast->qualifiedId);
|
|
|
|
if (lhsValue) {
|
2011-09-01 15:04:21 +02:00
|
|
|
Evaluate evaluator(&_scopeChain);
|
|
|
|
const Value *rhsValue = evaluator(ast->statement);
|
2010-02-19 15:10:39 +01:00
|
|
|
|
2011-09-01 15:04:21 +02:00
|
|
|
const SourceLocation loc = locationFromRange(ast->statement->firstSourceLocation(),
|
|
|
|
ast->statement->lastSourceLocation());
|
|
|
|
AssignmentCheck assignmentCheck;
|
2011-09-28 15:16:00 +02:00
|
|
|
Message message = assignmentCheck(_doc, loc, lhsValue, rhsValue, ast->statement);
|
|
|
|
if (message.isValid())
|
|
|
|
addMessage(message);
|
2010-02-19 15:10:39 +01:00
|
|
|
}
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2011-09-07 07:21:38 +02:00
|
|
|
checkBindingRhs(ast->statement);
|
2010-12-06 10:03:53 +01:00
|
|
|
|
2011-09-07 07:21:38 +02:00
|
|
|
Node::accept(ast->qualifiedId, this);
|
|
|
|
_scopeBuilder.push(ast);
|
2011-12-02 10:41:13 +01:00
|
|
|
_inStatementBinding = true;
|
2011-09-07 07:21:38 +02:00
|
|
|
Node::accept(ast->statement, this);
|
2011-12-02 10:41:13 +01:00
|
|
|
_inStatementBinding = false;
|
2011-09-07 07:21:38 +02:00
|
|
|
_scopeBuilder.pop();
|
|
|
|
|
|
|
|
return false;
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-05 15:37:14 +02:00
|
|
|
bool Check::visit(UiPublicMember *ast)
|
|
|
|
{
|
2011-10-19 14:09:15 +02:00
|
|
|
if (ast->type == UiPublicMember::Property) {
|
|
|
|
// check if the member type is valid
|
|
|
|
if (!ast->memberType.isEmpty()) {
|
|
|
|
const QString &name = ast->memberType.toString();
|
|
|
|
if (!name.isEmpty() && name.at(0).isLower()) {
|
|
|
|
if (!isValidBuiltinPropertyType(name))
|
|
|
|
addMessage(ErrInvalidPropertyType, ast->typeToken, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// warn about dubious use of var/variant
|
|
|
|
if (name == QLatin1String("variant") || name == QLatin1String("var")) {
|
|
|
|
Evaluate evaluator(&_scopeChain);
|
|
|
|
const Value *init = evaluator(ast->statement);
|
|
|
|
QString preferedType;
|
|
|
|
if (init->asNumberValue())
|
|
|
|
preferedType = tr("'int' or 'real'");
|
2011-11-21 12:37:02 +01:00
|
|
|
else if (init->asStringValue())
|
2011-10-19 14:09:15 +02:00
|
|
|
preferedType = QLatin1String("'string'");
|
2011-11-21 12:37:02 +01:00
|
|
|
else if (init->asBooleanValue())
|
2011-10-19 14:09:15 +02:00
|
|
|
preferedType = QLatin1String("'bool'");
|
2011-11-21 12:37:02 +01:00
|
|
|
else if (init->asColorValue())
|
|
|
|
preferedType = QLatin1String("'color'");
|
|
|
|
else if (init == _context->valueOwner()->qmlPointObject())
|
|
|
|
preferedType = QLatin1String("'point'");
|
|
|
|
else if (init == _context->valueOwner()->qmlRectObject())
|
|
|
|
preferedType = QLatin1String("'rect'");
|
|
|
|
else if (init == _context->valueOwner()->qmlSizeObject())
|
|
|
|
preferedType = QLatin1String("'size'");
|
|
|
|
else if (init == _context->valueOwner()->qmlVector3DObject())
|
|
|
|
preferedType = QLatin1String("'vector3d'");
|
|
|
|
|
2011-10-19 14:09:15 +02:00
|
|
|
if (!preferedType.isEmpty())
|
|
|
|
addMessage(HintPreferNonVarPropertyType, ast->typeToken, preferedType);
|
|
|
|
}
|
2011-09-05 15:37:14 +02:00
|
|
|
}
|
2011-09-07 07:21:38 +02:00
|
|
|
|
2011-10-19 14:09:15 +02:00
|
|
|
checkBindingRhs(ast->statement);
|
2011-09-07 07:21:38 +02:00
|
|
|
|
2011-10-19 14:09:15 +02:00
|
|
|
_scopeBuilder.push(ast);
|
2011-12-02 10:41:13 +01:00
|
|
|
_inStatementBinding = true;
|
2011-10-19 14:09:15 +02:00
|
|
|
Node::accept(ast->statement, this);
|
2011-12-02 10:41:13 +01:00
|
|
|
_inStatementBinding = false;
|
2011-10-19 14:09:15 +02:00
|
|
|
Node::accept(ast->binding, this);
|
|
|
|
_scopeBuilder.pop();
|
|
|
|
}
|
2011-09-07 07:21:38 +02:00
|
|
|
|
|
|
|
return false;
|
2011-09-05 15:37:14 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
bool Check::visit(IdentifierExpression *)
|
2010-11-24 15:12:11 +01:00
|
|
|
{
|
|
|
|
// currently disabled: too many false negatives
|
|
|
|
return true;
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
// _lastValue = 0;
|
|
|
|
// if (!ast->name.isEmpty()) {
|
|
|
|
// Evaluate evaluator(&_scopeChain);
|
|
|
|
// _lastValue = evaluator.reference(ast);
|
|
|
|
// if (!_lastValue)
|
|
|
|
// addMessage(ErrUnknownIdentifier, ast->identifierToken);
|
2011-10-10 10:55:37 +02:00
|
|
|
// if (const Reference *ref = value_cast<Reference>(_lastValue)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
// _lastValue = _context->lookupReference(ref);
|
|
|
|
// if (!_lastValue)
|
|
|
|
// error(ast->identifierToken, tr("could not resolve"));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false;
|
2010-11-24 15:12:11 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
bool Check::visit(FieldMemberExpression *)
|
2010-11-24 15:12:11 +01:00
|
|
|
{
|
|
|
|
// currently disabled: too many false negatives
|
|
|
|
return true;
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
// 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.isEmpty()) {
|
|
|
|
// _lastValue = 0;
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// _lastValue = obj->lookupMember(ast->name.toString(), _context);
|
|
|
|
// if (!_lastValue)
|
|
|
|
// error(ast->identifierToken, tr("unknown member"));
|
|
|
|
// return false;
|
2010-11-24 15:12:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(FunctionDeclaration *ast)
|
|
|
|
{
|
|
|
|
return visit(static_cast<FunctionExpression *>(ast));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(FunctionExpression *ast)
|
|
|
|
{
|
2011-09-30 09:50:08 +02:00
|
|
|
if (ast->name.isEmpty()) {
|
|
|
|
SourceLocation locfunc = ast->functionToken;
|
|
|
|
SourceLocation loclparen = ast->lparenToken;
|
|
|
|
if (locfunc.isValid() && loclparen.isValid()
|
|
|
|
&& (locfunc.startLine != loclparen.startLine
|
|
|
|
|| locfunc.end() + 1 != loclparen.begin())) {
|
|
|
|
addMessage(HintAnonymousFunctionSpacing, locationFromRange(locfunc, loclparen));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-06 13:59:50 +02:00
|
|
|
DeclarationsCheck bodyCheck;
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessages(bodyCheck(ast));
|
|
|
|
|
|
|
|
MarkUnreachableCode unreachableCheck;
|
|
|
|
addMessages(unreachableCheck(ast->body));
|
2010-11-25 13:38:15 +01:00
|
|
|
|
2010-11-24 15:12:11 +01:00
|
|
|
Node::accept(ast->formals, this);
|
2011-12-02 10:41:13 +01:00
|
|
|
|
|
|
|
const bool wasInStatementBinding = _inStatementBinding;
|
|
|
|
_inStatementBinding = false;
|
2010-11-24 15:12:11 +01:00
|
|
|
_scopeBuilder.push(ast);
|
|
|
|
Node::accept(ast->body, this);
|
|
|
|
_scopeBuilder.pop();
|
2011-12-02 10:41:13 +01:00
|
|
|
_inStatementBinding = wasInStatementBinding;
|
|
|
|
|
2010-11-24 15:12:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-12 10:44:11 +02:00
|
|
|
static bool shouldAvoidNonStrictEqualityCheck(const Value *lhs, const Value *rhs)
|
2010-11-25 13:38:15 +01:00
|
|
|
{
|
2011-10-10 12:53:28 +02:00
|
|
|
if (lhs->asUnknownValue() || rhs->asUnknownValue())
|
|
|
|
return true; // may coerce or not
|
2011-09-12 10:44:11 +02:00
|
|
|
|
|
|
|
if (lhs->asStringValue() && rhs->asNumberValue())
|
|
|
|
return true; // coerces string to number
|
|
|
|
|
|
|
|
if (lhs->asObjectValue() && rhs->asNumberValue())
|
|
|
|
return true; // coerces object to primitive
|
|
|
|
|
|
|
|
if (lhs->asObjectValue() && rhs->asStringValue())
|
|
|
|
return true; // coerces object to primitive
|
|
|
|
|
2011-10-10 12:53:28 +02:00
|
|
|
if (lhs->asBooleanValue() && (!rhs->asBooleanValue()
|
|
|
|
&& !rhs->asUndefinedValue()))
|
2011-09-12 10:44:11 +02:00
|
|
|
return true; // coerces bool to number
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(BinaryExpression *ast)
|
|
|
|
{
|
2011-09-30 10:07:37 +02:00
|
|
|
const QString source = _doc->source();
|
|
|
|
|
|
|
|
// check spacing
|
|
|
|
SourceLocation op = ast->operatorToken;
|
|
|
|
if ((op.begin() > 0 && !source.at(op.begin() - 1).isSpace())
|
|
|
|
|| (int(op.end()) < source.size() && !source.at(op.end()).isSpace())) {
|
|
|
|
addMessage(HintBinaryOperatorSpacing, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check ==, !=
|
2010-11-25 13:38:15 +01:00
|
|
|
if (ast->op == QSOperator::Equal || ast->op == QSOperator::NotEqual) {
|
2011-09-28 15:16:00 +02:00
|
|
|
Evaluate eval(&_scopeChain);
|
2011-09-30 10:07:37 +02:00
|
|
|
const Value *lhsValue = eval(ast->left);
|
|
|
|
const Value *rhsValue = eval(ast->right);
|
|
|
|
if (shouldAvoidNonStrictEqualityCheck(lhsValue, rhsValue)
|
|
|
|
|| shouldAvoidNonStrictEqualityCheck(rhsValue, lhsValue)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(MaybeWarnEqualityTypeCoercion, ast->operatorToken);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-30 10:39:00 +02:00
|
|
|
|
|
|
|
// check odd + ++ combinations
|
|
|
|
const QLatin1Char newline('\n');
|
|
|
|
if (ast->op == QSOperator::Add || ast->op == QSOperator::Sub) {
|
|
|
|
QChar match;
|
|
|
|
Type msg;
|
|
|
|
if (ast->op == QSOperator::Add) {
|
|
|
|
match = QLatin1Char('+');
|
|
|
|
msg = WarnConfusingPluses;
|
|
|
|
} else {
|
|
|
|
QTC_CHECK(ast->op == QSOperator::Sub);
|
|
|
|
match = QLatin1Char('-');
|
|
|
|
msg = WarnConfusingMinuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (int(op.end()) + 1 < source.size()) {
|
|
|
|
const QChar next = source.at(op.end());
|
|
|
|
if (next.isSpace() && next != newline
|
|
|
|
&& source.at(op.end() + 1) == match)
|
|
|
|
addMessage(msg, SourceLocation(op.begin(), 3, op.startLine, op.startColumn));
|
|
|
|
}
|
|
|
|
if (op.begin() >= 2) {
|
|
|
|
const QChar prev = source.at(op.begin() - 1);
|
|
|
|
if (prev.isSpace() && prev != newline
|
|
|
|
&& source.at(op.begin() - 2) == match)
|
|
|
|
addMessage(msg, SourceLocation(op.begin() - 2, 3, op.startLine, op.startColumn - 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(Block *ast)
|
|
|
|
{
|
|
|
|
if (Node *p = parent()) {
|
2011-09-28 15:16:00 +02:00
|
|
|
if (!cast<UiScriptBinding *>(p)
|
2011-09-07 11:01:13 +02:00
|
|
|
&& !cast<UiPublicMember *>(p)
|
2010-11-25 13:38:15 +01:00
|
|
|
&& !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)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnBlock, ast->lbraceToken);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
2011-09-07 11:01:13 +02:00
|
|
|
if (!ast->statements
|
2011-10-28 10:17:44 +02:00
|
|
|
&& cast<UiPublicMember *>(p)
|
2011-10-13 14:19:50 +02:00
|
|
|
&& ast->lbraceToken.startLine == ast->rbraceToken.startLine) {
|
|
|
|
addMessage(WarnUnintentinalEmptyBlock, locationFromRange(ast->firstSourceLocation(), ast->lastSourceLocation()));
|
2011-09-07 11:01:13 +02:00
|
|
|
}
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(WithStatement *ast)
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnWith, ast->withToken);
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(VoidExpression *ast)
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnVoid, ast->voidToken);
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(Expression *ast)
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
if (ast->left && ast->right) {
|
2010-12-06 09:54:10 +01:00
|
|
|
Node *p = parent();
|
|
|
|
if (!cast<ForStatement *>(p)
|
|
|
|
&& !cast<LocalForStatement *>(p)) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnComma, ast->commaToken);
|
2010-12-06 09:54:10 +01:00
|
|
|
}
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(ExpressionStatement *ast)
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
if (ast->expression) {
|
2010-11-25 13:38:15 +01:00
|
|
|
bool ok = cast<CallExpression *>(ast->expression)
|
|
|
|
|| cast<DeleteExpression *>(ast->expression)
|
|
|
|
|| cast<PreDecrementExpression *>(ast->expression)
|
|
|
|
|| cast<PreIncrementExpression *>(ast->expression)
|
|
|
|
|| cast<PostIncrementExpression *>(ast->expression)
|
2011-04-21 09:43:24 +02:00
|
|
|
|| cast<PostDecrementExpression *>(ast->expression)
|
|
|
|
|| cast<FunctionExpression *>(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) {
|
2011-12-02 10:41:13 +01:00
|
|
|
ok = _inStatementBinding;
|
2010-11-29 12:21:02 +01:00
|
|
|
}
|
2010-11-25 13:38:15 +01:00
|
|
|
|
|
|
|
if (!ok) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnConfusingExpressionStatement,
|
|
|
|
locationFromRange(ast->firstSourceLocation(), ast->lastSourceLocation()));
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-28 14:58:01 +01:00
|
|
|
bool Check::visit(CaseBlock *ast)
|
2010-11-25 13:38:15 +01:00
|
|
|
{
|
2011-11-28 14:58:01 +01:00
|
|
|
QList< QPair<SourceLocation, StatementList *> > clauses;
|
|
|
|
for (CaseClauses *it = ast->clauses; it; it = it->next)
|
|
|
|
clauses += qMakePair(it->clause->caseToken, it->clause->statements);
|
|
|
|
if (ast->defaultClause)
|
|
|
|
clauses += qMakePair(ast->defaultClause->defaultToken, ast->defaultClause->statements);
|
|
|
|
for (CaseClauses *it = ast->moreClauses; it; it = it->next)
|
|
|
|
clauses += qMakePair(it->clause->caseToken, it->clause->statements);
|
|
|
|
|
2011-11-29 10:19:04 +01:00
|
|
|
// check all but the last clause for fallthrough
|
|
|
|
for (int i = 0; i < clauses.size() - 1; ++i) {
|
|
|
|
const SourceLocation nextToken = clauses[i + 1].first;
|
2011-11-28 14:58:01 +01:00
|
|
|
checkCaseFallthrough(clauses[i].second, clauses[i].first, nextToken);
|
|
|
|
}
|
2010-11-25 13:38:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-06 12:59:30 +02:00
|
|
|
static QString functionName(ExpressionNode *ast, SourceLocation *location)
|
|
|
|
{
|
|
|
|
if (IdentifierExpression *id = cast<IdentifierExpression *>(ast)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!id->name.isEmpty()) {
|
2011-09-06 12:59:30 +02:00
|
|
|
*location = id->identifierToken;
|
2011-09-13 09:57:24 +02:00
|
|
|
return id->name.toString();
|
2011-09-06 12:59:30 +02:00
|
|
|
}
|
|
|
|
} else if (FieldMemberExpression *fme = cast<FieldMemberExpression *>(ast)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!fme->name.isEmpty()) {
|
2011-09-06 12:59:30 +02:00
|
|
|
*location = fme->identifierToken;
|
2011-09-13 09:57:24 +02:00
|
|
|
return fme->name.toString();
|
2011-09-06 12:59:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::checkNewExpression(ExpressionNode *ast)
|
|
|
|
{
|
|
|
|
SourceLocation location;
|
|
|
|
const QString name = functionName(ast, &location);
|
|
|
|
if (name.isEmpty())
|
|
|
|
return;
|
|
|
|
if (!name.at(0).isUpper()) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnNewWithLowercaseFunction, location);
|
2011-09-06 12:59:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-07 07:21:38 +02:00
|
|
|
void Check::checkBindingRhs(Statement *statement)
|
|
|
|
{
|
|
|
|
if (!statement)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DeclarationsCheck bodyCheck;
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessages(bodyCheck(statement));
|
|
|
|
|
|
|
|
MarkUnreachableCode unreachableCheck;
|
|
|
|
addMessages(unreachableCheck(statement));
|
|
|
|
}
|
|
|
|
|
2011-10-04 09:49:30 +02:00
|
|
|
void Check::checkExtraParentheses(ExpressionNode *expression)
|
|
|
|
{
|
|
|
|
if (NestedExpression *nested = cast<NestedExpression *>(expression)) {
|
|
|
|
addMessage(HintExtraParentheses, nested->lparenToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
void Check::addMessages(const QList<Message> &messages)
|
|
|
|
{
|
|
|
|
foreach (const Message &msg, messages)
|
|
|
|
addMessage(msg);
|
|
|
|
}
|
|
|
|
|
2011-10-19 14:27:40 +02:00
|
|
|
static bool hasOnlySpaces(const QString &s)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < s.size(); ++i)
|
|
|
|
if (!s.at(i).isSpace())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
void Check::addMessage(const Message &message)
|
|
|
|
{
|
2011-10-19 14:27:40 +02:00
|
|
|
if (message.isValid() && _enabledMessages.contains(message.type)) {
|
2011-10-20 09:45:29 +02:00
|
|
|
if (m_disabledMessageTypesByLine.contains(message.location.startLine)) {
|
|
|
|
QList<MessageTypeAndSuppression> &disabledMessages = m_disabledMessageTypesByLine[message.location.startLine];
|
|
|
|
for (int i = 0; i < disabledMessages.size(); ++i) {
|
|
|
|
if (disabledMessages[i].type == message.type) {
|
|
|
|
disabledMessages[i].wasSuppressed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_messages += message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::addMessage(Type type, const SourceLocation &location, const QString &arg1, const QString &arg2)
|
|
|
|
{
|
|
|
|
addMessage(Message(type, location, arg1, arg2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::scanCommentsForAnnotations()
|
|
|
|
{
|
|
|
|
m_disabledMessageTypesByLine.clear();
|
2011-10-28 12:27:10 +02:00
|
|
|
const QRegExp disableCommentPattern(Message::suppressionPattern());
|
2011-10-28 15:56:56 +02:00
|
|
|
|
2011-10-20 09:45:29 +02:00
|
|
|
foreach (const SourceLocation &commentLoc, _doc->engine()->comments()) {
|
|
|
|
const QString &comment = _doc->source().mid(commentLoc.begin(), commentLoc.length);
|
2011-10-28 15:56:56 +02:00
|
|
|
|
|
|
|
// enable all checks annotation
|
|
|
|
if (comment.contains(QLatin1String("@enable-all-checks"))) {
|
|
|
|
_enabledMessages = Message::allMessageTypes().toSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
// find all disable annotations
|
2011-10-20 09:45:29 +02:00
|
|
|
int lastOffset = -1;
|
|
|
|
QList<MessageTypeAndSuppression> disabledMessageTypes;
|
|
|
|
forever {
|
|
|
|
lastOffset = disableCommentPattern.indexIn(comment, lastOffset + 1);
|
|
|
|
if (lastOffset == -1)
|
2011-10-19 14:27:40 +02:00
|
|
|
break;
|
2011-10-20 09:45:29 +02:00
|
|
|
MessageTypeAndSuppression entry;
|
|
|
|
entry.type = static_cast<StaticAnalysis::Type>(disableCommentPattern.cap(1).toInt());
|
|
|
|
entry.wasSuppressed = false;
|
|
|
|
entry.suppressionSource = SourceLocation(commentLoc.offset + lastOffset,
|
|
|
|
disableCommentPattern.matchedLength(),
|
|
|
|
commentLoc.startLine,
|
|
|
|
commentLoc.startColumn + lastOffset);
|
|
|
|
disabledMessageTypes += entry;
|
|
|
|
}
|
|
|
|
if (!disabledMessageTypes.isEmpty()) {
|
|
|
|
int appliesToLine = commentLoc.startLine;
|
2011-10-19 14:27:40 +02:00
|
|
|
|
2011-10-20 09:45:29 +02:00
|
|
|
// if the comment is preceded by spaces only, it applies to the next line
|
2011-10-19 14:27:40 +02:00
|
|
|
// note: startColumn is 1-based and *after* the starting // or /*
|
2011-11-07 13:16:37 +01:00
|
|
|
if (commentLoc.startColumn >= 3) {
|
2011-10-19 14:27:40 +02:00
|
|
|
const QString &beforeComment = _doc->source().mid(commentLoc.begin() - commentLoc.startColumn + 1,
|
|
|
|
commentLoc.startColumn - 3);
|
2011-10-20 09:45:29 +02:00
|
|
|
if (hasOnlySpaces(beforeComment))
|
|
|
|
++appliesToLine;
|
2011-10-19 14:27:40 +02:00
|
|
|
}
|
|
|
|
|
2011-10-20 09:45:29 +02:00
|
|
|
m_disabledMessageTypesByLine[appliesToLine] += disabledMessageTypes;
|
2011-10-19 14:27:40 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-28 15:16:00 +02:00
|
|
|
}
|
|
|
|
|
2011-10-20 09:45:29 +02:00
|
|
|
void Check::warnAboutUnnecessarySuppressions()
|
2011-09-28 15:16:00 +02:00
|
|
|
{
|
2011-10-20 09:45:29 +02:00
|
|
|
QHashIterator< int, QList<MessageTypeAndSuppression> > it(m_disabledMessageTypesByLine);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
foreach (const MessageTypeAndSuppression &entry, it.value()) {
|
|
|
|
if (!entry.wasSuppressed)
|
|
|
|
addMessage(WarnUnnecessaryMessageSuppression, entry.suppressionSource);
|
|
|
|
}
|
|
|
|
}
|
2011-09-07 07:21:38 +02:00
|
|
|
}
|
|
|
|
|
2011-09-06 12:59:30 +02:00
|
|
|
bool Check::visit(NewExpression *ast)
|
|
|
|
{
|
|
|
|
checkNewExpression(ast->expression);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(NewMemberExpression *ast)
|
|
|
|
{
|
|
|
|
checkNewExpression(ast->base);
|
2011-09-30 11:11:01 +02:00
|
|
|
|
|
|
|
// check for Number, Boolean, etc constructor usage
|
|
|
|
if (IdentifierExpression *idExp = cast<IdentifierExpression *>(ast->base)) {
|
|
|
|
const QStringRef name = idExp->name;
|
|
|
|
if (name == QLatin1String("Number")) {
|
|
|
|
addMessage(WarnNumberConstructor, idExp->identifierToken);
|
|
|
|
} else if (name == QLatin1String("Boolean")) {
|
|
|
|
addMessage(WarnBooleanConstructor, idExp->identifierToken);
|
|
|
|
} else if (name == QLatin1String("String")) {
|
|
|
|
addMessage(WarnStringConstructor, idExp->identifierToken);
|
|
|
|
} else if (name == QLatin1String("Object")) {
|
|
|
|
addMessage(WarnObjectConstructor, idExp->identifierToken);
|
|
|
|
} else if (name == QLatin1String("Array")) {
|
|
|
|
bool ok = false;
|
|
|
|
if (ast->arguments && ast->arguments->expression && !ast->arguments->next) {
|
|
|
|
Evaluate evaluate(&_scopeChain);
|
|
|
|
const Value *arg = evaluate(ast->arguments->expression);
|
2011-10-10 12:53:28 +02:00
|
|
|
if (arg->asNumberValue() || arg->asUnknownValue())
|
2011-09-30 11:11:01 +02:00
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
addMessage(WarnArrayConstructor, idExp->identifierToken);
|
|
|
|
} else if (name == QLatin1String("Function")) {
|
|
|
|
addMessage(WarnFunctionConstructor, idExp->identifierToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-06 12:59:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(CallExpression *ast)
|
|
|
|
{
|
|
|
|
// check for capitalized function name being called
|
2011-09-28 15:16:00 +02:00
|
|
|
SourceLocation location;
|
|
|
|
const QString name = functionName(ast->base, &location);
|
2011-10-05 13:51:10 +02:00
|
|
|
if (!name.isEmpty() && name.at(0).isUpper()
|
|
|
|
&& name != QLatin1String("String")
|
|
|
|
&& name != QLatin1String("Boolean")
|
|
|
|
&& name != QLatin1String("Date")
|
|
|
|
&& name != QLatin1String("Number")
|
|
|
|
&& name != QLatin1String("Object")) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnExpectedNewWithUppercaseFunction, location);
|
2011-09-06 12:59:30 +02:00
|
|
|
}
|
2011-09-30 11:15:15 +02:00
|
|
|
if (cast<IdentifierExpression *>(ast->base) && name == QLatin1String("eval"))
|
|
|
|
addMessage(WarnEval, location);
|
2011-09-06 12:59:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-30 12:11:58 +02:00
|
|
|
bool Check::visit(StatementList *ast)
|
|
|
|
{
|
|
|
|
SourceLocation warnStart;
|
|
|
|
SourceLocation warnEnd;
|
|
|
|
unsigned currentLine = 0;
|
|
|
|
for (StatementList *it = ast; it; it = it->next) {
|
|
|
|
if (!it->statement)
|
|
|
|
continue;
|
|
|
|
const SourceLocation itLoc = it->statement->firstSourceLocation();
|
|
|
|
if (itLoc.startLine != currentLine) { // first statement on a line
|
|
|
|
if (warnStart.isValid())
|
|
|
|
addMessage(HintOneStatementPerLine, locationFromRange(warnStart, warnEnd));
|
|
|
|
warnStart = SourceLocation();
|
|
|
|
currentLine = itLoc.startLine;
|
|
|
|
} else { // other statements on the same line
|
|
|
|
if (!warnStart.isValid())
|
|
|
|
warnStart = itLoc;
|
|
|
|
warnEnd = it->statement->lastSourceLocation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (warnStart.isValid())
|
|
|
|
addMessage(HintOneStatementPerLine, locationFromRange(warnStart, warnEnd));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-04 09:49:30 +02:00
|
|
|
bool Check::visit(ReturnStatement *ast)
|
|
|
|
{
|
|
|
|
checkExtraParentheses(ast->expression);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(ThrowStatement *ast)
|
|
|
|
{
|
|
|
|
checkExtraParentheses(ast->expression);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(DeleteExpression *ast)
|
|
|
|
{
|
|
|
|
checkExtraParentheses(ast->expression);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(TypeOfExpression *ast)
|
|
|
|
{
|
|
|
|
checkExtraParentheses(ast->expression);
|
|
|
|
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
|
|
|
{
|
2011-09-29 11:48:13 +02:00
|
|
|
if (!_importsOk)
|
|
|
|
return 0;
|
|
|
|
|
2011-07-12 14:55:27 +02:00
|
|
|
QList<const ObjectValue *> scopeObjects = _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
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
if (id->name.isEmpty()) // possible after error recovery
|
2010-04-06 11:44:55 +02:00
|
|
|
return 0;
|
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
QString propertyName = id->name.toString();
|
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;
|
2011-07-12 14:55:27 +02:00
|
|
|
if (const ObjectValue *qmlTypes = _scopeChain.qmlTypes())
|
2010-08-30 13:31:50 +02:00
|
|
|
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) {
|
2011-07-13 15:04:27 +02:00
|
|
|
value = scopeObjects[i]->lookupMember(propertyName, _context);
|
2010-02-19 10:14:34 +01:00
|
|
|
if (value)
|
|
|
|
break;
|
|
|
|
}
|
2010-02-16 10:36:09 +01:00
|
|
|
if (!value) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrInvalidPropertyName, id->identifierToken, propertyName);
|
2011-05-04 11:12:45 +02:00
|
|
|
return 0;
|
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
|
|
|
|
2011-05-04 11:12:45 +02:00
|
|
|
// resolve references
|
|
|
|
if (const Reference *ref = value->asReference())
|
2011-07-13 15:04:27 +02:00
|
|
|
value = _context->lookupReference(ref);
|
2011-05-04 11:12:45 +02:00
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
// member lookup
|
|
|
|
const UiQualifiedId *idPart = id;
|
|
|
|
while (idPart->next) {
|
2011-10-10 10:55:37 +02:00
|
|
|
const ObjectValue *objectValue = value_cast<ObjectValue>(value);
|
2010-02-18 14:21:53 +01:00
|
|
|
if (! objectValue) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrDoesNotHaveMembers, idPart->identifierToken, propertyName);
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-18 14:21:53 +01:00
|
|
|
}
|
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
if (idPart->next->name.isEmpty()) {
|
2010-02-24 17:14:14 +01:00
|
|
|
// 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;
|
2011-09-13 09:57:24 +02:00
|
|
|
propertyName = idPart->name.toString();
|
2010-02-18 14:21:53 +01:00
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
value = objectValue->lookupMember(propertyName, _context);
|
2010-02-18 14:21:53 +01:00
|
|
|
if (! value) {
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(ErrInvalidMember, idPart->identifierToken, 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)
|
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
if (BinaryExpression *binary = cast<BinaryExpression *>(condition)) {
|
|
|
|
if (binary->op == QSOperator::Assign)
|
|
|
|
addMessage(WarnAssignmentInCondition, binary->operatorToken);
|
2010-11-25 13:38:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-28 14:58:01 +01:00
|
|
|
void Check::checkCaseFallthrough(StatementList *statements, SourceLocation errorLoc, SourceLocation nextLoc)
|
2010-11-25 13:38:15 +01:00
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
if (!statements)
|
2010-11-25 13:38:15 +01:00
|
|
|
return;
|
|
|
|
|
2011-09-07 07:21:38 +02:00
|
|
|
ReachesEndCheck check;
|
|
|
|
if (check(statements)) {
|
2011-11-28 14:58:01 +01:00
|
|
|
// check for fallthrough comments
|
|
|
|
if (nextLoc.isValid()) {
|
|
|
|
quint32 afterLastStatement = 0;
|
|
|
|
for (StatementList *it = statements; it; it = it->next) {
|
|
|
|
if (!it->next)
|
|
|
|
afterLastStatement = it->statement->lastSourceLocation().end();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const SourceLocation &comment, _doc->engine()->comments()) {
|
|
|
|
if (comment.begin() < afterLastStatement
|
|
|
|
|| comment.end() > nextLoc.begin())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const QString &commentText = _doc->source().mid(comment.begin(), comment.length);
|
|
|
|
if (commentText.contains(QLatin1String("fall through"))
|
|
|
|
|| commentText.contains(QLatin1String("fall-through"))
|
|
|
|
|| commentText.contains(QLatin1String("fallthrough"))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
addMessage(WarnCaseWithoutFlowControl, errorLoc);
|
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);
|
|
|
|
}
|