Skip to content

Commit

Permalink
Merge pull request #2829 from kivra-pauoli/fix/xref-for-generated-code
Browse files Browse the repository at this point in the history
Prevent XRef issue when analysis runs on generated code
  • Loading branch information
ferd authored Sep 29, 2023
2 parents 4cc854d + 77c0fca commit a16f41a
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions apps/rebar/src/rebar_prv_xref.erl
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,24 @@ find_function_source(M, F, A, Bin) ->

find_function_source_in_abstract_code(F, A, AbstractCode) ->
%% Extract the original source filename from the abstract code
[{attribute, _, file, {Source0, _}} | _] = [Attr || Attr = {attribute, _, file, _} <- AbstractCode],
Source = rebar_dir:make_relative_path(Source0, rebar_dir:get_cwd()),
%% Extract the line number for a given function def
Fn = [E || E <- AbstractCode,
safe_element(1, E) == function,
safe_element(3, E) == F,
safe_element(4, E) == A],
case Fn of
[{function, Anno, F, _, _}] -> {Source, erl_anno:line(Anno)};
%% do not crash if functions are exported, even though they
%% are not in the source.
%% parameterized modules add new/1 and instance/1 for example.
[] -> {Source, function_not_found}
case [Attr || Attr = {attribute, _, file, _} <- AbstractCode] of
[] ->
% Forms compiled from generated code don't get a 'file' attribute
% and we don't even want to analyze those since it would be
% pointless to try to change them
{module_not_found, function_not_found};
[{attribute, _, file, {Source0, _}} | _] ->
Source = rebar_dir:make_relative_path(Source0, rebar_dir:get_cwd()),
%% Extract the line number for a given function def
Fn = [E || E <- AbstractCode,
safe_element(1, E) == function,
safe_element(3, E) == F,
safe_element(4, E) == A],
case Fn of
[{function, Anno, F, _, _}] -> {Source, erl_anno:line(Anno)};
%% do not crash if functions are exported, even though they
%% are not in the source.
%% parameterized modules add new/1 and instance/1 for example.
[] -> {Source, function_not_found}
end
end.

0 comments on commit a16f41a

Please sign in to comment.