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 module simple_misc_analysis
28 redef class ToolContext
29 var simple_misc_analysis_phase
: Phase = new SimpleMiscAnalysisPhase(self, null)
32 private class SimpleMiscAnalysisPhase
34 redef fun process_nmodule
(nmodule
) do nmodule
.do_simple_misc_analysis
(toolcontext
)
38 # Visit the module to detect easy warnings that does not need the metamodel or the importation
39 # Warnings are displayed on the toolcontext
40 fun do_simple_misc_analysis
(toolcontext
: ToolContext)
42 var v
= new SimpleMiscVisitor(toolcontext
)
47 private class SimpleMiscVisitor
51 n
.accept_simple_misc
(self)
54 # Number of nested once
55 var once_count
: Int = 0
57 var toolcontext
: ToolContext
59 fun warning
(node
: ANode, msg
: String)
61 toolcontext
.warning
(node
.hot_location
, msg
)
64 init(toolcontext
: ToolContext)
66 self.toolcontext
= toolcontext
71 ###############################################################################
74 private fun accept_simple_misc
(v
: SimpleMiscVisitor)
79 private fun after_simple_misc
(v
: SimpleMiscVisitor) do end
82 redef class ASignature
83 redef fun after_simple_misc
(v
)
85 if self.n_opar
!= null and self.n_params
.is_empty
then
86 v
.warning
(self, "Warning: superfluous parentheses.")
92 # Warn in case of superfluous parentheses
93 private fun warn_parentheses
(v
: SimpleMiscVisitor) do end
97 redef fun warn_parentheses
(v
)
99 v
.warning
(self, "Warning: superfluous parentheses.")
103 redef class AParExprs
104 redef fun after_simple_misc
(v
)
106 if n_exprs
.is_empty
then
107 v
.warning
(self, "Warning: superfluous parentheses.")
112 redef class AReturnExpr
113 redef fun after_simple_misc
(v
)
117 e
.warn_parentheses
(v
)
122 redef class AContinueExpr
123 redef fun after_simple_misc
(v
)
127 e
.warn_parentheses
(v
)
132 redef class ABreakExpr
133 redef fun after_simple_misc
(v
)
137 e
.warn_parentheses
(v
)
142 redef class AWhileExpr
143 redef fun after_simple_misc
(v
)
145 if n_expr
isa ATrueExpr then
146 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
148 n_expr
.warn_parentheses
(v
)
154 redef fun after_simple_misc
(v
)
156 n_expr
.warn_parentheses
(v
)
161 redef fun after_simple_misc
(v
)
163 n_expr
.warn_parentheses
(v
)
167 redef class AIfexprExpr
168 redef fun after_simple_misc
(v
)
170 n_expr
.warn_parentheses
(v
)
174 redef class AOnceExpr
175 redef fun accept_simple_misc
(v
)
177 if v
.once_count
> 0 then
178 v
.warning
(self, "Useless once in a once expression.")
180 v
.once_count
= v
.once_count
+ 1
184 v
.once_count
= v
.once_count
- 1