qmljscheck.h 6.05 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Christian Kamm's avatar
Christian Kamm committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Christian Kamm's avatar
Christian Kamm committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Christian Kamm's avatar
Christian Kamm committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Christian Kamm's avatar
Christian Kamm committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Christian Kamm's avatar
Christian Kamm committed
29 30 31 32

#ifndef QMLJSCHECK_H
#define QMLJSCHECK_H

33
#include <qmljs/qmljsscopebuilder.h>
34
#include <qmljs/qmljsscopechain.h>
35
#include <qmljs/qmljsstaticanalysismessage.h>
Christian Kamm's avatar
Christian Kamm committed
36 37
#include <qmljs/parser/qmljsastvisitor_p.h>

38 39 40
#include <QCoreApplication>
#include <QSet>
#include <QStack>
41

Christian Kamm's avatar
Christian Kamm committed
42 43
namespace QmlJS {

44 45
class Imports;

Christian Kamm's avatar
Christian Kamm committed
46 47
class QMLJS_EXPORT Check: protected AST::Visitor
{
48 49
    Q_DECLARE_TR_FUNCTIONS(QmlJS::Check)

50 51
    typedef QSet<QString> StringSet;

Christian Kamm's avatar
Christian Kamm committed
52
public:
53
    // prefer taking root scope chain?
54
    Check(Document::Ptr doc, const ContextPtr &context);
Eike Ziller's avatar
Eike Ziller committed
55
    ~Check();
Christian Kamm's avatar
Christian Kamm committed
56

57 58 59 60
    QList<StaticAnalysis::Message> operator()();

    void enableMessage(StaticAnalysis::Type type);
    void disableMessage(StaticAnalysis::Type type);
61

62 63 64 65 66 67
    void enableQmlDesignerChecks();
    void disableQmlDesignerChecks();

    void enableQmlDesignerUiFileChecks();
    void disableQmlDesignerUiFileChecks();

Christian Kamm's avatar
Christian Kamm committed
68
protected:
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    bool preVisit(AST::Node *ast) Q_DECL_OVERRIDE;
    void postVisit(AST::Node *ast) Q_DECL_OVERRIDE;

    bool visit(AST::UiProgram *ast) Q_DECL_OVERRIDE;
    bool visit(AST::UiObjectDefinition *ast) Q_DECL_OVERRIDE;
    bool visit(AST::UiObjectBinding *ast) Q_DECL_OVERRIDE;
    bool visit(AST::UiScriptBinding *ast) Q_DECL_OVERRIDE;
    bool visit(AST::UiArrayBinding *ast) Q_DECL_OVERRIDE;
    bool visit(AST::UiPublicMember *ast) Q_DECL_OVERRIDE;
    bool visit(AST::IdentifierExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::FieldMemberExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::FunctionDeclaration *ast) Q_DECL_OVERRIDE;
    bool visit(AST::FunctionExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::UiObjectInitializer *) Q_DECL_OVERRIDE;

    bool visit(AST::BinaryExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::Block *ast) Q_DECL_OVERRIDE;
    bool visit(AST::WithStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::VoidExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::Expression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::ExpressionStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::IfStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::ForStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::LocalForStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::WhileStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::DoWhileStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::CaseBlock *ast) Q_DECL_OVERRIDE;
    bool visit(AST::NewExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::NewMemberExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::CallExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::StatementList *ast) Q_DECL_OVERRIDE;
    bool visit(AST::ReturnStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::ThrowStatement *ast) Q_DECL_OVERRIDE;
    bool visit(AST::DeleteExpression *ast) Q_DECL_OVERRIDE;
    bool visit(AST::TypeOfExpression *ast) Q_DECL_OVERRIDE;

    void endVisit(QmlJS::AST::UiObjectInitializer *) Q_DECL_OVERRIDE;
106

Christian Kamm's avatar
Christian Kamm committed
107
private:
108 109
    void visitQmlObject(AST::Node *ast, AST::UiQualifiedId *typeId,
                        AST::UiObjectInitializer *initializer);
110
    const Value *checkScopeObjectMember(const AST::UiQualifiedId *id);
111
    void checkAssignInCondition(AST::ExpressionNode *condition);
112
    void checkCaseFallthrough(AST::StatementList *statements, AST::SourceLocation errorLoc, AST::SourceLocation nextLoc);
113
    void checkProperty(QmlJS::AST::UiQualifiedId *);
114
    void checkNewExpression(AST::ExpressionNode *node);
115
    void checkBindingRhs(AST::Statement *statement);
116
    void checkExtraParentheses(AST::ExpressionNode *expression);
Christian Kamm's avatar
Christian Kamm committed
117

118 119 120 121
    void addMessages(const QList<StaticAnalysis::Message> &messages);
    void addMessage(const StaticAnalysis::Message &message);
    void addMessage(StaticAnalysis::Type type, const AST::SourceLocation &location,
                    const QString &arg1 = QString(), const QString &arg2 = QString());
Christian Kamm's avatar
Christian Kamm committed
122

123 124 125
    void scanCommentsForAnnotations();
    void warnAboutUnnecessarySuppressions();

126
    bool isQtQuick2() const;
127
    bool isQtQuick2Ui() const;
128

129 130
    AST::Node *parent(int distance = 0);

Christian Kamm's avatar
Christian Kamm committed
131 132
    Document::Ptr _doc;

133 134
    ContextPtr _context;
    ScopeChain _scopeChain;
135
    ScopeBuilder _scopeBuilder;
Christian Kamm's avatar
Christian Kamm committed
136

137 138
    QList<StaticAnalysis::Message> _messages;
    QSet<StaticAnalysis::Type> _enabledMessages;
139

140
    QList<AST::Node *> _chain;
141
    QStack<StringSet> m_idStack;
142
    QStack<StringSet> m_propertyStack;
143
    QStack<QString> m_typeStack;
144

145 146 147 148 149 150 151 152 153 154
    class MessageTypeAndSuppression
    {
    public:
        AST::SourceLocation suppressionSource;
        StaticAnalysis::Type type;
        bool wasSuppressed;
    };

    QHash< int, QList<MessageTypeAndSuppression> > m_disabledMessageTypesByLine;

155
    bool _importsOk;
156
    bool _inStatementBinding;
157 158
    const Imports *_imports;
    bool _isQtQuick2;
Christian Kamm's avatar
Christian Kamm committed
159 160 161 162 163
};

} // namespace QmlJS

#endif // QMLJSCHECK_H