src: update most tools to new constructors
[nit.git] / src / frontend / simple_misc_analysis.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Simple vavious processing on a AST
18 # The analysis warns on :
19 # * superfluous parentheses
20 # * nested "once" expressions
21 # * use of "while true" instead of "loop"
22 module simple_misc_analysis
23
24 import phase
25
26 redef class ToolContext
27 var simple_misc_analysis_phase: Phase = new SimpleMiscAnalysisPhase(self, null)
28 end
29
30 private class SimpleMiscAnalysisPhase
31 super Phase
32 redef fun process_nmodule(nmodule) do nmodule.do_simple_misc_analysis(toolcontext)
33 end
34
35 redef class AModule
36 # Visit the module to detect easy warnings that does not need the metamodel or the importation
37 # Warnings are displayed on the toolcontext
38 fun do_simple_misc_analysis(toolcontext: ToolContext)
39 do
40 var v = new SimpleMiscVisitor(toolcontext)
41 v.enter_visit(self)
42 end
43 end
44
45 private class SimpleMiscVisitor
46 super Visitor
47 redef fun visit(n)
48 do
49 n.accept_simple_misc(self)
50 end
51
52 # Number of nested once
53 var once_count: Int = 0
54
55 var toolcontext: ToolContext
56
57 fun warning(node: ANode, tag, msg: String)
58 do
59 toolcontext.warning(node.hot_location, tag, msg)
60 end
61 end
62
63
64 ###############################################################################
65
66 redef class ANode
67 private fun accept_simple_misc(v: SimpleMiscVisitor)
68 do
69 visit_all(v)
70 after_simple_misc(v)
71 end
72 private fun after_simple_misc(v: SimpleMiscVisitor) do end
73 end
74
75 redef class ASignature
76 redef fun after_simple_misc(v)
77 do
78 if self.n_opar != null and self.n_params.is_empty then
79 v.warning(self, "parentheses", "Warning: superfluous parentheses.")
80 end
81 end
82 end
83
84 redef class AExpr
85 # Warn in case of superfluous parentheses
86 private fun warn_parentheses(v: SimpleMiscVisitor) do end
87 end
88
89 redef class AParExpr
90 redef fun warn_parentheses(v)
91 do
92 v.warning(self, "parentheses", "Warning: superfluous parentheses.")
93 end
94 end
95
96 redef class AParExprs
97 redef fun after_simple_misc(v)
98 do
99 if n_exprs.is_empty then
100 v.warning(self, "parentheses", "Warning: superfluous parentheses.")
101 end
102 end
103 end
104
105 redef class AReturnExpr
106 redef fun after_simple_misc(v)
107 do
108 var e = n_expr
109 if e != null then
110 e.warn_parentheses(v)
111 end
112 end
113 end
114
115 redef class AEscapeExpr
116 redef fun after_simple_misc(v)
117 do
118 var e = n_expr
119 if e != null then
120 e.warn_parentheses(v)
121 end
122 end
123 end
124
125 redef class AWhileExpr
126 redef fun after_simple_misc(v)
127 do
128 if n_expr isa ATrueExpr then
129 v.warning(self, "loop", "Warning: use `loop` instead of `while true do`.")
130 else
131 n_expr.warn_parentheses(v)
132 end
133 end
134 end
135
136 redef class AForExpr
137 redef fun after_simple_misc(v)
138 do
139 n_expr.warn_parentheses(v)
140 end
141 end
142
143 redef class AIfExpr
144 redef fun after_simple_misc(v)
145 do
146 n_expr.warn_parentheses(v)
147 end
148 end
149
150 redef class AIfexprExpr
151 redef fun after_simple_misc(v)
152 do
153 n_expr.warn_parentheses(v)
154 end
155 end
156
157 redef class AOnceExpr
158 redef fun accept_simple_misc(v)
159 do
160 if v.once_count > 0 then
161 v.warning(self, "nested-once", "Warning: useless once in a once expression.")
162 end
163 v.once_count = v.once_count + 1
164
165 super
166
167 v.once_count = v.once_count - 1
168 end
169 end