Skip to content

Commit 74772e6

Browse files
committed
Fill generated Erlang data-flow source labels
1 parent e234c80 commit 74772e6

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

lib/reach/visualize.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,16 @@ defmodule Reach.Visualize do
278278
module = function_module(function)
279279
file = node.source_span[:file] || source_file(function)
280280
line = node.source_span[:start_line]
281+
node_label = ir_node_label(node)
281282

282283
%{
283284
id: to_string(node.id),
284285
function_id: if(function_id, do: to_string(function_id)),
285-
label: "#{function_prefix}L#{line}: #{ir_node_label(node)}",
286+
label: "#{function_prefix}L#{line}: #{node_label}",
286287
module: if(module, do: display_module(module)),
287288
file: file,
288289
start_line: line,
289-
source_html: Source.highlight_line(file, line)
290+
source_html: Source.highlight_line(file, line, node_label)
290291
}
291292
end
292293
end

lib/reach/visualize/source.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ defmodule Reach.Visualize.Source do
6969

7070
def highlight_line(_, _), do: nil
7171

72+
def highlight_line(file, line, fallback) when is_binary(fallback) do
73+
case highlight_line(file, line) do
74+
source when is_binary(source) ->
75+
if String.trim(source) == "", do: highlight_fallback(fallback, file), else: source
76+
77+
_missing_source ->
78+
highlight_fallback(fallback, file)
79+
end
80+
end
81+
82+
def highlight_line(file, line, _fallback), do: highlight_line(file, line)
83+
7284
def highlight_lines(file, from, to) when is_binary(file) do
7385
case cached_file_lines(file) do
7486
nil ->
@@ -168,6 +180,10 @@ defmodule Reach.Visualize.Source do
168180
def source_file?(nil), do: false
169181
def source_file?(file), do: Path.extname(file) in @source_extensions
170182

183+
defp highlight_fallback(fallback, file) do
184+
highlight_source(fallback, lang_for_file(file)) || escape_html(fallback)
185+
end
186+
171187
defp escape_html(source) do
172188
source
173189
|> String.replace("&", "&")

test/reach/visualize_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,32 @@ defmodule Reach.VisualizeTest do
188188
assert Enum.all?(data_nodes, &(is_binary(&1.function_id) and &1.function_id != ""))
189189
refute Enum.any?(data_nodes, &(&1.label =~ "export(...)" or &1.label =~ "spec(...)"))
190190
end
191+
192+
test "data flow falls back to labels for generated Erlang source locations" do
193+
dir =
194+
Path.join(
195+
System.tmp_dir!(),
196+
"reach-generated-erlang-data-flow-#{System.unique_integer([:positive])}"
197+
)
198+
199+
path = Path.join(dir, "generated_parser.erl")
200+
File.mkdir_p!(dir)
201+
202+
File.write!(path, """
203+
-file("grammar.yrl", 42).
204+
-module(generated_parser).
205+
-export([normalize/1]).
206+
normalize(Input) -> Normalized = Input, Normalized.
207+
""")
208+
209+
on_exit(fn -> File.rm_rf!(dir) end)
210+
211+
project = Reach.Project.from_sources([path], plugins: [])
212+
data_nodes = Reach.Visualize.to_graph_json(project).data_flow.functions
213+
214+
assert data_nodes != []
215+
assert Enum.all?(data_nodes, &(is_binary(&1.source_html) and &1.source_html != ""))
216+
end
191217
end
192218

193219
describe "to_json/2" do

0 commit comments

Comments
 (0)