1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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 package simple_misc_analysis
28 import modelbuilder
#FIXME useless
30 redef class ToolContext
31 var simple_misc_analysis_phase
: Phase = new SimpleMiscAnalysisPhase(self, null)
34 private class SimpleMiscAnalysisPhase
36 redef fun process_nmodule
(nmodule
) do nmodule
.do_simple_misc_analysis
(toolcontext
)
40 # Visit the module to detect easy warnings that does not need the metamodel or the importation
41 # Warnings are displayed on the toolcontext
42 fun do_simple_misc_analysis
(toolcontext
: ToolContext)
44 var v
= new SimpleMiscVisitor(toolcontext
)
49 private class SimpleMiscVisitor
53 n
.accept_simple_misc
(self)
56 # Number of nested once
57 var once_count
: Int = 0
59 var toolcontext
: ToolContext
61 fun warning
(node
: ANode, msg
: String)
63 toolcontext
.warning
(node
.hot_location
, msg
)
66 init(toolcontext
: ToolContext)
68 self.toolcontext
= toolcontext
73 ###############################################################################
76 private fun accept_simple_misc
(v
: SimpleMiscVisitor)
81 private fun after_simple_misc
(v
: SimpleMiscVisitor) do end
84 redef class ASignature
85 redef fun after_simple_misc
(v
)
87 if self.n_opar
!= null and self.n_params
.is_empty
then
88 v
.warning
(self, "Warning: superfluous parentheses.")
94 # Warn in case of superfluous parentheses
95 private fun warn_parentheses
(v
: SimpleMiscVisitor) do end
99 redef fun warn_parentheses
(v
)
101 v
.warning
(self, "Warning: superfluous parentheses.")
105 redef class AParExprs
106 redef fun after_simple_misc
(v
)
108 if n_exprs
.is_empty
then
109 v
.warning
(self, "Warning: superfluous parentheses.")
114 redef class AReturnExpr
115 redef fun after_simple_misc
(v
)
119 e
.warn_parentheses
(v
)
124 redef class AContinueExpr
125 redef fun after_simple_misc
(v
)
129 e
.warn_parentheses
(v
)
134 redef class ABreakExpr
135 redef fun after_simple_misc
(v
)
139 e
.warn_parentheses
(v
)
144 redef class AWhileExpr
145 redef fun after_simple_misc
(v
)
147 if n_expr
isa ATrueExpr then
148 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
150 n_expr
.warn_parentheses
(v
)
156 redef fun after_simple_misc
(v
)
158 n_expr
.warn_parentheses
(v
)
163 redef fun after_simple_misc
(v
)
165 n_expr
.warn_parentheses
(v
)
169 redef class AIfexprExpr
170 redef fun after_simple_misc
(v
)
172 n_expr
.warn_parentheses
(v
)
176 redef class AOnceExpr
177 redef fun accept_simple_misc
(v
)
179 if v
.once_count
> 0 then
180 v
.warning
(self, "Useless once in a once expression.")
182 v
.once_count
= v
.once_count
+ 1
186 v
.once_count
= v
.once_count
- 1