src: improve messages (and sometime location) of errors and warnings
[nit.git] / src / semantize / local_var_init.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 # Verify that local variables are initialized before their usage
18 # Require that the scope and the flow analysis are already performed
19 module local_var_init
20
21 import flow
22
23 redef class ToolContext
24 # Run `APropdef::do_local_var_init` on each propdef
25 var local_var_init_phase: Phase = new LocalVarInitPhase(self, [flow_phase])
26 end
27
28 private class LocalVarInitPhase
29 super Phase
30
31 redef fun process_npropdef(npropdef) do npropdef.do_local_var_init(toolcontext)
32 end
33
34 redef class APropdef
35 # Entry point of the whole local variable initialization verifier
36 fun do_local_var_init(toolcontext: ToolContext)
37 do
38 var v = new LocalVarInitVisitor(toolcontext)
39 v.enter_visit(self)
40 end
41 end
42
43 private class LocalVarInitVisitor
44 super Visitor
45
46 var toolcontext: ToolContext
47
48 # Local variables that are possibly unset (ie local variable without an initial value)
49 var maybe_unset_vars: Set[Variable] = new HashSet[Variable]
50
51 fun mark_is_unset(node: AExpr, variable: nullable Variable)
52 do
53 assert variable != null
54 self.maybe_unset_vars.add(variable)
55 end
56
57 fun mark_is_set(node: AExpr, variable: nullable Variable)
58 do
59 assert variable != null
60 if not maybe_unset_vars.has(variable) then return
61
62 var flow = node.after_flow_context.as(not null)
63 flow.set_vars.add(variable)
64 end
65
66 fun check_is_set(node: AExpr, variable: nullable Variable)
67 do
68 assert variable != null
69 if not maybe_unset_vars.has(variable) then return
70
71 var flow = node.after_flow_context.as(not null)
72 if not flow.is_variable_set(variable) then
73 self.toolcontext.error(node.hot_location, "Error: possibly unset variable `{variable}`.")
74 # Remove the variable to avoid repeating errors
75 self.maybe_unset_vars.remove(variable)
76 end
77 end
78
79 redef fun visit(n)
80 do
81 n.accept_local_var_visitor(self)
82 end
83 end
84
85 redef class FlowContext
86 private var set_vars: Set[Variable] = new HashSet[Variable]
87
88 private fun is_variable_set(variable: Variable): Bool
89 do
90 if self.set_vars.has(variable) then return true
91 var previous = self.previous
92 if previous.length == 0 then return false
93 if previous.length == 1 then return previous.first.is_variable_set(variable)
94 for p in self.previous do
95 if not p.is_variable_set(variable) then
96 return false
97 end
98 end
99 # Cache the result
100 self.set_vars.add(variable)
101 return true
102 end
103 end
104
105 redef class ANode
106 private fun accept_local_var_visitor(v: LocalVarInitVisitor) do self.visit_all(v)
107 end
108
109 redef class AVardeclExpr
110 redef fun accept_local_var_visitor(v)
111 do
112 super
113 # The variable is unset only if there is no initial value.
114
115 # Note: loops in initial value are not a problem
116 # Example:
117 #
118 # var foo = foo + 1 #-> Error during typing: "self.foo" unknown
119 #
120 # var foo
121 # foo = foo + 1 #-> Error here because 'foo' is possibly unset
122 if self.n_expr == null then
123 v.mark_is_unset(self, self.variable)
124 end
125 end
126 end
127
128 redef class AVarExpr
129 redef fun accept_local_var_visitor(v)
130 do
131 super
132 v.check_is_set(self, self.variable)
133 end
134 end
135
136 redef class AVarAssignExpr
137 redef fun accept_local_var_visitor(v)
138 do
139 super
140 v.mark_is_set(self, self.variable)
141 end
142 end
143
144 redef class AVarReassignExpr
145 redef fun accept_local_var_visitor(v)
146 do
147 super
148 v.check_is_set(self, self.variable)
149 end
150 end