-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathlexical_tracker.ex
More file actions
318 lines (255 loc) · 8.17 KB
/
lexical_tracker.ex
File metadata and controls
318 lines (255 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2021 The Elixir Team
# SPDX-FileCopyrightText: 2012 Plataformatec
# This is an Elixir module responsible for tracking references
# to modules, remote dispatches, and the usage of
# aliases/imports/requires in the Elixir scope.
#
# Note that since this is required for bootstrap, we can't use
# any of the `GenServer.Behaviour` conveniences.
defmodule Kernel.LexicalTracker do
@moduledoc false
@timeout :infinity
@behaviour :gen_server
@warn_key 0
@doc """
Returns all references in this lexical scope.
"""
def references(pid) do
:gen_server.call(pid, :references, @timeout)
end
@doc """
Invoked during module expansion to annotate a require
must be warned if unused.
"""
def warn_require(pid, meta, module, alias) do
:gen_server.cast(pid, {:warn_require, module, meta, alias})
module
end
@doc """
Invoked during module expansion to annotate an alias
must be warned if unused.
"""
def warn_alias(pid, meta, alias, module) do
:gen_server.cast(pid, {:warn_alias, alias, meta})
module
end
@doc """
Invoked during module expansion to annotate an import
must be warned if unused.
"""
def warn_import(pid, module) do
:gen_server.cast(pid, {:warn_import, module})
module
end
# Internal API
# Starts the tracker and returns its PID.
@doc false
def start_link() do
:gen_server.start_link(__MODULE__, :ok, [])
end
@doc false
def stop(pid) do
:gen_server.call(pid, :stop)
end
@doc false
def add_export(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:add_export, module})
end
@doc false
def add_require(pid, module, meta) when is_atom(module) do
:gen_server.cast(pid, {:add_require, module, meta})
end
@doc false
def add_import(pid, module, fas, meta, warn) when is_atom(module) do
:gen_server.cast(pid, {:add_import, module, fas, meta, warn})
end
@doc false
def remote_dispatch(pid, module, mode) when is_atom(module) do
:gen_server.cast(pid, {:remote_dispatch, module, mode})
end
@doc false
def import_dispatch(pid, module, fa, mode) when is_atom(module) do
:gen_server.cast(pid, {:import_dispatch, module, fa, mode})
end
@doc false
def alias_dispatch(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:alias_dispatch, module})
end
@doc false
def import_quoted(pid, module, function, arities) when is_atom(module) do
:gen_server.cast(pid, {:import_quoted, module, function, arities})
end
@doc false
def add_compile_env(pid, app, path, return) do
:gen_server.cast(pid, {:compile_env, app, path, return})
end
@doc false
def set_file(pid, file) do
:gen_server.cast(pid, {:set_file, file})
end
@doc false
def reset_file(pid) do
:gen_server.cast(pid, :reset_file)
end
@doc false
def write_cache(pid, value) do
key = :erlang.unique_integer()
:gen_server.cast(pid, {:write_cache, key, value})
key
end
@doc false
def read_cache(pid, key) do
:gen_server.call(pid, {:read_cache, key}, @timeout)
end
@doc false
def collect_unused_imports(pid) do
:gen_server.call(pid, :unused_imports, @timeout)
end
@doc false
def collect_unused_aliases(pid) do
:gen_server.call(pid, :unused_aliases, @timeout)
end
@doc false
def collect_unused_requires(pid) do
:gen_server.call(pid, :unused_requires, @timeout)
end
# Callbacks
def init(:ok) do
state = %{
aliases: %{},
imports: %{},
requires: %{},
references: %{},
exports: %{},
cache: %{},
compile_env: :ordsets.new(),
file: nil
}
{:ok, state}
end
@doc false
def handle_call(:unused_aliases, _from, state) do
aliases = for {alias, meta} when is_list(meta) <- state.aliases, do: {alias, meta}
{:reply, Enum.sort(aliases), state}
end
def handle_call(:unused_imports, _from, state) do
imports =
for {module, %{@warn_key => _} = map} <- state.imports do
{module, Map.delete(map, @warn_key)}
end
{:reply, Enum.sort(imports), state}
end
def handle_call(:unused_requires, _from, state) do
%{references: references, aliases: aliases} = state
unused_requires =
for {module, {meta, alias}} <- state.requires,
Map.get(references, module) != :compile do
{module, meta, alias, Map.get(aliases, alias) == :used}
end
{:reply, Enum.sort(unused_requires), state}
end
def handle_call(:references, _from, state) do
{compile, runtime} = partition(Map.to_list(state.references), [], [])
{:reply, {compile, Map.keys(state.exports), runtime, state.compile_env}, state}
end
def handle_call({:read_cache, key}, _from, %{cache: cache} = state) do
{:reply, Map.get(cache, key), state}
end
def handle_call(:stop, _from, state) do
{:stop, :normal, :ok, state}
end
def handle_cast({:write_cache, key, value}, %{cache: cache} = state) do
{:noreply, %{state | cache: Map.put(cache, key, value)}}
end
def handle_cast({:remote_dispatch, module, mode}, state) do
references = add_reference(state.references, module, mode)
{:noreply, %{state | references: references}}
end
def handle_cast({:import_dispatch, module, {function, arity}, mode}, state) do
%{imports: imports, references: references} = state
imports =
case imports do
%{^module => modules_and_fas} ->
modules_and_fas
|> Map.delete(module)
|> Map.delete({function, arity})
|> then(&Map.put(imports, module, &1))
%{} ->
imports
end
references = add_reference(references, module, mode)
{:noreply, %{state | imports: imports, references: references}}
end
def handle_cast({:alias_dispatch, module}, state) do
{:noreply, put_in(state.aliases[module], :used)}
end
def handle_cast({:import_quoted, module, function, arities}, state) do
%{imports: imports} = state
imports =
case imports do
%{^module => modules_and_fas} ->
arities
|> Enum.reduce(modules_and_fas, &Map.delete(&2, {function, &1}))
|> Map.delete(module)
|> then(&Map.put(imports, module, &1))
%{} ->
imports
end
{:noreply, %{state | imports: imports}}
end
def handle_cast({:set_file, file}, state) do
{:noreply, %{state | file: file}}
end
def handle_cast(:reset_file, state) do
{:noreply, %{state | file: nil}}
end
def handle_cast({:compile_env, app, path, return}, state) do
{:noreply, update_in(state.compile_env, &:ordsets.add_element({app, path, return}, &1))}
end
def handle_cast({:add_export, module}, state) do
{:noreply, put_in(state.exports[module], true)}
end
def handle_cast({:add_import, module, fas, meta, warn}, state) do
keys = if warn, do: [@warn_key, module | fas], else: [module | fas]
{:noreply, put_in(state.imports[module], Map.from_keys(keys, meta))}
end
def handle_cast({:warn_alias, alias, meta}, %{aliases: aliases} = state) do
case aliases do
%{^alias => :used} -> {:noreply, state}
%{} -> {:noreply, %{state | aliases: Map.put(aliases, alias, meta)}}
end
end
def handle_cast({:warn_import, module}, state) do
{:noreply, put_in(state.imports[module][@warn_key], true)}
end
def handle_cast({:warn_require, module, meta, alias}, state) do
{:noreply, put_in(state.requires[module], {meta, alias})}
end
@doc false
def handle_info(_msg, state) do
{:noreply, state}
end
@doc false
def terminate(_reason, _state) do
:ok
end
@doc false
def code_change(_old, state, _extra) do
{:ok, state}
end
defp partition([{remote, :compile} | t], compile, runtime),
do: partition(t, [remote | compile], runtime)
defp partition([{remote, :runtime} | t], compile, runtime),
do: partition(t, compile, [remote | runtime])
defp partition([], compile, runtime), do: {compile, runtime}
# Callbacks helpers
defp add_reference(references, module, :compile) when is_atom(module),
do: Map.put(references, module, :compile)
defp add_reference(references, module, :runtime) when is_atom(module) do
case references do
%{^module => _} -> references
%{} -> Map.put(references, module, :runtime)
end
end
end