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
26 redef class ToolContext
27 # Execute `AModule::do_simple_misc_analysis` on each module.
28 var simple_misc_analysis_phase
: Phase = new SimpleMiscAnalysisPhase(self, null)
31 private class SimpleMiscAnalysisPhase
33 redef fun process_nmodule
(nmodule
) do nmodule
.do_simple_misc_analysis
(toolcontext
)
37 # Visit the module to detect easy warnings that does not need the metamodel or the importation
38 # Warnings are displayed on the toolcontext
39 fun do_simple_misc_analysis
(toolcontext
: ToolContext)
41 var v
= new SimpleMiscVisitor(toolcontext
)
44 var t
= location
.file
.first_token
46 t
.accept_simple_misc_token
(v
)
52 private class SimpleMiscVisitor
56 n
.accept_simple_misc
(self)
59 # Number of nested once
60 var once_count
: Int = 0
62 var toolcontext
: ToolContext
64 fun warning
(node
: ANode, tag
, msg
: String)
66 toolcontext
.warning
(node
.hot_location
, tag
, msg
)
71 ###############################################################################
74 private fun accept_simple_misc
(v
: SimpleMiscVisitor)
79 private fun after_simple_misc
(v
: SimpleMiscVisitor) do end
83 private fun accept_simple_misc_token
(v
: SimpleMiscVisitor)
88 redef class ASignature
89 redef fun after_simple_misc
(v
)
91 if self.n_opar
!= null and self.n_params
.is_empty
then
92 v
.warning
(self, "parentheses", "Warning: superfluous parentheses.")
98 # Warn in case of superfluous parentheses
99 private fun warn_parentheses
(v
: SimpleMiscVisitor) do end
103 redef fun warn_parentheses
(v
)
105 v
.warning
(self, "parentheses", "Warning: superfluous parentheses.")
109 redef class AParExprs
110 redef fun after_simple_misc
(v
)
112 if n_exprs
.is_empty
then
113 v
.warning
(self, "parentheses", "Warning: superfluous parentheses.")
118 redef class AReturnExpr
119 redef fun after_simple_misc
(v
)
123 e
.warn_parentheses
(v
)
128 redef class AEscapeExpr
129 redef fun after_simple_misc
(v
)
133 e
.warn_parentheses
(v
)
138 redef class AWhileExpr
139 redef fun after_simple_misc
(v
)
141 if n_expr
isa ATrueExpr then
142 v
.warning
(self, "loop", "Warning: use `loop` instead of `while true do`.")
144 n_expr
.warn_parentheses
(v
)
150 redef fun after_simple_misc
(v
)
152 n_expr
.warn_parentheses
(v
)
157 redef fun after_simple_misc
(v
)
159 n_expr
.warn_parentheses
(v
)
163 redef class AIfexprExpr
164 redef fun after_simple_misc
(v
)
166 n_expr
.warn_parentheses
(v
)
170 redef class AOnceExpr
171 redef fun accept_simple_misc
(v
)
173 if v
.once_count
> 0 then
174 v
.warning
(self, "nested-once", "Warning: useless once in a once expression.")
176 v
.once_count
= v
.once_count
+ 1
180 v
.once_count
= v
.once_count
- 1
185 redef fun accept_simple_misc_token
(v
)
200 v
.warning
(self, "semi", "Warning: superfluous `;`.")