OSDN Git Service

8df87ee0f94bb1be3fd9d605fac27572975d01a7
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljs / qmljscheck.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef QMLJSCHECK_H
35 #define QMLJSCHECK_H
36
37 #include <qmljs/qmljsdocument.h>
38 #include <qmljs/qmljsinterpreter.h>
39 #include <qmljs/qmljsscopebuilder.h>
40 #include <qmljs/parser/qmljsastvisitor_p.h>
41
42 #include <QtCore/QCoreApplication>
43 #include <QtCore/QSet>
44 #include <QtCore/QStack>
45 #include <QtGui/QColor>
46
47 namespace QmlJS {
48
49 class QMLJS_EXPORT Check: protected AST::Visitor
50 {
51     Q_DECLARE_TR_FUNCTIONS(QmlJS::Check)
52
53     typedef QSet<QString> StringSet;
54
55 public:
56     Check(Document::Ptr doc, const Snapshot &snapshot, const Interpreter::Context *linkedContextNoScope);
57     virtual ~Check();
58
59     QList<DiagnosticMessage> operator()();
60
61     enum Option {
62         WarnDangerousNonStrictEqualityChecks = 1 << 0,
63         WarnAllNonStrictEqualityChecks       = 1 << 1,
64         WarnBlocks                           = 1 << 2,
65         WarnWith                             = 1 << 3,
66         WarnVoid                             = 1 << 4,
67         WarnCommaExpression                  = 1 << 5,
68         WarnExpressionStatement              = 1 << 6,
69         WarnAssignInCondition                = 1 << 7,
70         WarnUseBeforeDeclaration             = 1 << 8,
71         WarnDuplicateDeclaration             = 1 << 9,
72         WarnDeclarationsNotStartOfFunction   = 1 << 10,
73         WarnCaseWithoutFlowControlEnd        = 1 << 11,
74         ErrCheckTypeErrors                   = 1 << 12
75     };
76     Q_DECLARE_FLAGS(Options, Option)
77
78     const Options options() const
79     { return _options; }
80
81     void setOptions(Options options)
82     { _options = options; }
83
84 protected:
85     virtual bool preVisit(AST::Node *ast);
86     virtual void postVisit(AST::Node *ast);
87
88     virtual bool visit(AST::UiProgram *ast);
89     virtual bool visit(AST::UiObjectDefinition *ast);
90     virtual bool visit(AST::UiObjectBinding *ast);
91     virtual bool visit(AST::UiScriptBinding *ast);
92     virtual bool visit(AST::UiArrayBinding *ast);
93     virtual bool visit(AST::IdentifierExpression *ast);
94     virtual bool visit(AST::FieldMemberExpression *ast);
95     virtual bool visit(AST::FunctionDeclaration *ast);
96     virtual bool visit(AST::FunctionExpression *ast);
97     virtual bool visit(AST::UiObjectInitializer *);
98
99     virtual bool visit(AST::BinaryExpression *ast);
100     virtual bool visit(AST::Block *ast);
101     virtual bool visit(AST::WithStatement *ast);
102     virtual bool visit(AST::VoidExpression *ast);
103     virtual bool visit(AST::Expression *ast);
104     virtual bool visit(AST::ExpressionStatement *ast);
105     virtual bool visit(AST::IfStatement *ast);
106     virtual bool visit(AST::ForStatement *ast);
107     virtual bool visit(AST::LocalForStatement *ast);
108     virtual bool visit(AST::WhileStatement *ast);
109     virtual bool visit(AST::DoWhileStatement *ast);
110     virtual bool visit(AST::CaseClause *ast);
111     virtual bool visit(AST::DefaultClause *ast);
112
113     virtual void endVisit(QmlJS::AST::UiObjectInitializer *);
114
115 private:
116     void visitQmlObject(AST::Node *ast, AST::UiQualifiedId *typeId,
117                         AST::UiObjectInitializer *initializer);
118     const Interpreter::Value *checkScopeObjectMember(const AST::UiQualifiedId *id);
119     void checkAssignInCondition(AST::ExpressionNode *condition);
120     void checkEndsWithControlFlow(AST::StatementList *statements, AST::SourceLocation errorLoc);
121     void checkProperty(QmlJS::AST::UiQualifiedId *);
122
123     void warning(const AST::SourceLocation &loc, const QString &message);
124     void error(const AST::SourceLocation &loc, const QString &message);
125
126     AST::Node *parent(int distance = 0);
127
128     Document::Ptr _doc;
129     Snapshot _snapshot;
130
131     Interpreter::Context _context;
132     ScopeBuilder _scopeBuilder;
133
134     QList<DiagnosticMessage> _messages;
135
136     Options _options;
137
138     const Interpreter::Value *_lastValue;
139     QList<AST::Node *> _chain;
140     QStack<StringSet> m_idStack;
141     QStack<StringSet> m_propertyStack;
142 };
143
144 QMLJS_EXPORT QColor toQColor(const QString &qmlColorString);
145
146 QMLJS_EXPORT AST::SourceLocation locationFromRange(const AST::SourceLocation &start,
147                                                    const AST::SourceLocation &end);
148
149 QMLJS_EXPORT AST::SourceLocation fullLocationForQualifiedId(AST::UiQualifiedId *);
150
151 QMLJS_EXPORT DiagnosticMessage errorMessage(const AST::SourceLocation &loc,
152                                             const QString &message);
153
154 template <class T>
155 DiagnosticMessage errorMessage(const T *node, const QString &message)
156 {
157     return DiagnosticMessage(DiagnosticMessage::Error,
158                              locationFromRange(node->firstSourceLocation(),
159                                                node->lastSourceLocation()),
160                              message);
161 }
162
163 } // namespace QmlJS
164
165 #endif // QMLJSCHECK_H