Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent XRef issue when analysis runs on generated code #2829

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.