55# ## Implementation
66#
77# The implementation uses the digraph module to track
8- # all dependencies. The graph starts with three main
9- # vertices:
8+ # all dependencies. The graph starts with one main vertice:
109#
1110# * `:local` - points to local functions
12- # * `:import` - points to imported modules
13- # * `:warn` - points to imported modules that should be warned
14- # * `:remote` - points to remote modules
1511#
16- # Besides those, we have can the following vertices:
12+ # We also have can the following vertices:
1713#
1814# * `Module` - a module that was invoked via an import or remotely
1915# * `{ name, arity }` - a local function/arity pair
2420# as described below:
2521#
2622# * `Module`
27- # * in neighbours: `:import`, `:remote`, `:warn`,
28- # `{ :import, name, arity }` and `{ :remote, name arity }`
29- # * out neighbours: `:warn`
23+ # * in neighbours: `{ :import, name, arity }` and `{ :remote, name arity }`
3024#
3125# * `{ name, arity }`
3226# * in neighbours: `:local`, `{ name, arity }`
@@ -92,15 +86,6 @@ defmodule Module.DispatchTracker do
9286 :digraph . out_neighbours ( d , { name , arity } ) |> only_tuples
9387 end
9488
95- @ doc """
96- Returns all the modules which were imported.
97- """
98- @ spec imports ( ref ) :: [ module ]
99- def imports ( ref ) do
100- d = :gen_server . call ( to_pid ( ref ) , :digraph , @ timeout )
101- :digraph . out_neighbours ( d , :import )
102- end
103-
10489 @ doc """
10590 Returns all imported modules that had the given
10691 `{ name, arity }` invoked.
@@ -111,24 +96,6 @@ defmodule Module.DispatchTracker do
11196 :digraph . out_neighbours ( d , { :import , name , arity } )
11297 end
11398
114- @ doc """
115- Returns all the aliases defined in the given module.
116- """
117- @ spec aliases ( ref ) :: [ module ]
118- def aliases ( ref ) do
119- d = :gen_server . call ( to_pid ( ref ) , :digraph , @ timeout )
120- :digraph . out_neighbours ( d , :alias )
121- end
122-
123- @ doc """
124- Returns all the modules which were remotely dispatched to.
125- """
126- @ spec remotes ( ref ) :: [ module ]
127- def remotes ( ref ) do
128- d = :gen_server . call ( to_pid ( ref ) , :digraph , @ timeout )
129- :digraph . out_neighbours ( d , :remote )
130- end
131-
13299 @ doc """
133100 Returns all modules that had the given `{ name, arity }`
134101 invoked remotely.
@@ -206,12 +173,6 @@ defmodule Module.DispatchTracker do
206173 :gen_server . cast ( pid , { :add_local , from , to } )
207174 end
208175
209- # Adds an alias.
210- @ doc false
211- def add_alias ( pid , module ) when is_atom ( module ) do
212- :gen_server . cast ( pid , { :add_alias , module } )
213- end
214-
215176 # Adds a remote dispatch to the given target.
216177 @ doc false
217178 def add_remote ( pid , function , module , target ) when is_atom ( module ) and is_tuple ( target ) do
@@ -224,40 +185,14 @@ defmodule Module.DispatchTracker do
224185 :gen_server . cast ( pid , { :add_external , :import , function , module , target } )
225186 end
226187
227- # Associates a module with a warn. This adds the given
228- # module and associates it with the `:import` vertex
229- # permanently, even if warn is false.
230- @ doc false
231- def add_warnable ( pid , module , warn , line ) when is_atom ( module ) and is_boolean ( warn ) do
232- :gen_server . cast ( pid , { :add_warnable , module , warn , line } )
233- end
234-
235- # Collect all unused imports where warn has been set to true.
236- def collect_unused_imports ( pid ) do
237- d = :gen_server . call ( pid , :digraph , @ timeout )
238- warnable = :digraph . out_neighbours ( d , :warn )
239-
240- lc mod inlist warnable , not has_imports? ( d , mod ) , line = get_warn_line ( d , mod ) do
241- { mod , line }
242- end
243- end
244-
245- defp get_warn_line ( d , mod ) do
246- [ edge ] = :digraph . out_edges ( d , mod )
247- { ^ edge , ^ mod , :warn , line } = :digraph . edge ( d , edge )
248- line
249- end
250-
251- defp has_imports? ( d , mod ) do
252- Enum . any? ( :digraph . in_neighbours ( d , mod ) , & match? ( { :import , _ , _ } , & 1 ) )
253- end
254-
255188 # Yanks a local node. Returns its in and out vertices in a tuple.
256189 @ doc false
257190 def yank ( pid , local ) do
258191 :gen_server . call ( to_pid ( pid ) , { :yank , local } , @ timeout )
259192 end
260193
194+ # Reattach a previously yanked node
195+ @ doc false
261196 def reattach ( pid , kind , tuple , neighbours ) do
262197 pid = to_pid ( pid )
263198 add_definition ( pid , kind , tuple )
@@ -322,10 +257,6 @@ defmodule Module.DispatchTracker do
322257 def init ( [ ] ) do
323258 d = :digraph . new ( [ :protected ] )
324259 :digraph . add_vertex ( d , :local )
325- :digraph . add_vertex ( d , :alias )
326- :digraph . add_vertex ( d , :import )
327- :digraph . add_vertex ( d , :remote )
328- :digraph . add_vertex ( d , :warn )
329260 { :ok , d }
330261 end
331262
@@ -353,30 +284,11 @@ defmodule Module.DispatchTracker do
353284 { :noreply , d }
354285 end
355286
356- def handle_cast ( { :add_alias , module } , d ) do
357- :digraph . add_vertex ( d , module )
358- replace_edge! ( d , :alias , module )
359- { :noreply , d }
360- end
361-
362287 def handle_cast ( { :add_external , kind , function , module , { name , arity } } , d ) do
363288 handle_import_or_remote ( d , kind , function , module , name , arity )
364289 { :noreply , d }
365290 end
366291
367- def handle_cast ( { :add_warnable , module , warn , line } , d ) do
368- :digraph . add_vertex ( d , module )
369- replace_edge! ( d , :import , module )
370-
371- if warn do
372- :digraph . add_edge ( d , :warn , module , line )
373- :digraph . add_edge ( d , module , :warn , line )
374- else
375- :digraph . del_path ( d , :warn , module )
376- end
377- { :noreply , d }
378- end
379-
380292 def handle_cast ( { :add_definition , kind , tuple } , d ) do
381293 handle_add_definition ( d , kind , tuple )
382294 { :noreply , d }
@@ -414,7 +326,6 @@ defmodule Module.DispatchTracker do
414326
415327 defp handle_import_or_remote ( d , kind , function , module , name , arity ) do
416328 :digraph . add_vertex ( d , module )
417- replace_edge! ( d , kind , module )
418329
419330 tuple = { kind , name , arity }
420331 :digraph . add_vertex ( d , tuple )
0 commit comments