2011-01-31 16:43:31 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
2010-04-09 15:25:00 +00:00
|
|
|
%% ex: ts=4 sw=4 et
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% rebar: Erlang Build Tools
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
|
|
|
|
%%
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
%%
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%% This module borrows heavily from http://github.com/etnt/exrefcheck project as
|
2012-08-28 17:21:05 +00:00
|
|
|
%% written by Torbjorn Tornkvist <tobbe@kreditor.se>, Daniel Luna
|
|
|
|
%% <daniel@lunas.se> and others.
|
2010-04-09 15:25:00 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
-module(rebar_xref).
|
|
|
|
|
|
|
|
-include("rebar.hrl").
|
|
|
|
|
|
|
|
-export([xref/2]).
|
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
%% for internal use only
|
|
|
|
-export([info/2]).
|
|
|
|
|
2010-04-09 15:25:00 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2011-12-12 18:08:40 +00:00
|
|
|
xref(Config, _) ->
|
2010-04-09 15:25:00 +00:00
|
|
|
%% Spin up xref
|
2010-10-10 21:24:20 +00:00
|
|
|
{ok, _} = xref:start(xref),
|
2012-08-28 17:22:57 +00:00
|
|
|
ok = xref:set_library_path(xref, code_path(Config)),
|
2011-01-28 15:08:27 +00:00
|
|
|
|
|
|
|
xref:set_default(xref, [{warnings,
|
|
|
|
rebar_config:get(Config, xref_warnings, false)},
|
2013-12-06 19:55:41 +00:00
|
|
|
{verbose, rebar_log:is_verbose(Config)}]),
|
2011-01-28 15:08:27 +00:00
|
|
|
|
2010-04-09 15:25:00 +00:00
|
|
|
{ok, _} = xref:add_directory(xref, "ebin"),
|
|
|
|
|
2010-04-23 15:27:50 +00:00
|
|
|
%% Save the code path prior to doing anything
|
|
|
|
OrigPath = code:get_path(),
|
2012-08-06 17:00:18 +00:00
|
|
|
true = code:add_path(rebar_utils:ebin_dir()),
|
2010-04-23 15:27:50 +00:00
|
|
|
|
2010-04-09 15:25:00 +00:00
|
|
|
%% Get list of xref checks we want to run
|
2013-01-30 13:06:46 +00:00
|
|
|
ConfXrefChecks = rebar_config:get(Config, xref_checks,
|
2013-06-18 09:30:32 +00:00
|
|
|
[exports_not_used,
|
|
|
|
undefined_function_calls]),
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2013-01-30 13:06:46 +00:00
|
|
|
SupportedXrefs = [undefined_function_calls, undefined_functions,
|
2013-06-18 09:30:32 +00:00
|
|
|
locals_not_used, exports_not_used,
|
|
|
|
deprecated_function_calls, deprecated_functions],
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
XrefChecks = sets:to_list(sets:intersection(
|
|
|
|
sets:from_list(SupportedXrefs),
|
2013-01-30 13:06:46 +00:00
|
|
|
sets:from_list(ConfXrefChecks))),
|
|
|
|
|
|
|
|
%% Run xref checks
|
|
|
|
XrefNoWarn = xref_checks(XrefChecks),
|
2012-04-02 15:01:39 +00:00
|
|
|
|
|
|
|
%% Run custom queries
|
|
|
|
QueryChecks = rebar_config:get(Config, xref_queries, []),
|
|
|
|
QueryNoWarn = lists:all(fun check_query/1, QueryChecks),
|
|
|
|
|
2010-04-23 15:27:50 +00:00
|
|
|
%% Restore the original code path
|
2010-10-10 20:11:13 +00:00
|
|
|
true = code:set_path(OrigPath),
|
2010-04-23 15:27:50 +00:00
|
|
|
|
2011-03-26 11:14:52 +00:00
|
|
|
%% Stop xref
|
|
|
|
stopped = xref:stop(xref),
|
|
|
|
|
2013-01-30 13:06:46 +00:00
|
|
|
case lists:member(false, [XrefNoWarn, QueryNoWarn]) of
|
2011-03-29 13:43:53 +00:00
|
|
|
true ->
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL;
|
2011-03-29 13:43:53 +00:00
|
|
|
false ->
|
2012-04-02 15:11:18 +00:00
|
|
|
ok
|
2011-03-29 13:43:53 +00:00
|
|
|
end.
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2011-12-12 18:08:40 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
info(help, xref) ->
|
|
|
|
?CONSOLE(
|
|
|
|
"Run cross reference analysis.~n"
|
|
|
|
"~n"
|
|
|
|
"Valid rebar.config options:~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n",
|
|
|
|
[
|
|
|
|
{xref_warnings, false},
|
|
|
|
{xref_checks, [undefined_function_calls, undefined_functions,
|
|
|
|
locals_not_used, exports_not_used,
|
|
|
|
deprecated_function_calls, deprecated_functions]},
|
|
|
|
{xref_queries,
|
|
|
|
[{"(xc - uc) || (xu - x - b"
|
|
|
|
" - (\"mod\":\".*foo\"/\"4\"))",[]}]}
|
|
|
|
]).
|
|
|
|
|
2013-01-30 13:06:46 +00:00
|
|
|
xref_checks(XrefChecks) ->
|
2013-06-18 09:30:32 +00:00
|
|
|
XrefWarnCount = lists:foldl(fun run_xref_check/2, 0, XrefChecks),
|
2013-01-30 13:06:46 +00:00
|
|
|
XrefWarnCount =:= 0.
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
run_xref_check(XrefCheck, Acc) ->
|
|
|
|
{ok, Results} = xref:analyze(xref, XrefCheck),
|
|
|
|
FilteredResults =filter_xref_results(XrefCheck, Results),
|
|
|
|
lists:foreach(fun(Res) ->
|
|
|
|
display_xref_result(XrefCheck, Res)
|
|
|
|
end,
|
|
|
|
FilteredResults),
|
|
|
|
Acc + length(FilteredResults).
|
|
|
|
|
2012-04-02 15:01:39 +00:00
|
|
|
check_query({Query, Value}) ->
|
|
|
|
{ok, Answer} = xref:q(xref, Query),
|
|
|
|
case Answer =:= Value of
|
|
|
|
false ->
|
|
|
|
?CONSOLE("Query ~s~n answer ~p~n did not match ~p~n",
|
|
|
|
[Query, Answer, Value]),
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2012-08-28 17:22:57 +00:00
|
|
|
code_path(Config) ->
|
2012-11-05 13:17:56 +00:00
|
|
|
%% Slight hack to ensure that sub_dirs get properly included
|
|
|
|
%% in code path for xref -- otherwise one gets a lot of undefined
|
|
|
|
%% functions, even though those functions are present as part
|
|
|
|
%% of compilation. H/t to @dluna. Long term we should tie more
|
|
|
|
%% properly into the overall compile code path if possible.
|
2012-08-28 17:22:57 +00:00
|
|
|
BaseDir = rebar_config:get_xconf(Config, base_dir),
|
|
|
|
[P || P <- code:get_path() ++
|
|
|
|
[filename:join(BaseDir, filename:join(SubDir, "ebin"))
|
|
|
|
|| SubDir <- rebar_config:get(Config, sub_dirs, [])],
|
|
|
|
filelib:is_dir(P)].
|
2010-04-09 15:25:00 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
%% Ignore behaviour functions, and explicitly marked functions
|
|
|
|
%%
|
2013-01-30 13:06:46 +00:00
|
|
|
%% Functions can be ignored by using
|
|
|
|
%% -ignore_xref([{F, A}, {M, F, A}...]).
|
|
|
|
|
|
|
|
get_xref_ignorelist(Mod, XrefCheck) ->
|
|
|
|
%% Get ignore_xref attribute and combine them in one list
|
|
|
|
Attributes =
|
|
|
|
try
|
|
|
|
Mod:module_info(attributes)
|
|
|
|
catch
|
|
|
|
_Class:_Error -> []
|
2010-04-09 15:25:00 +00:00
|
|
|
end,
|
2013-01-30 13:06:46 +00:00
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
IgnoreXref = keyall(ignore_xref, Attributes),
|
2013-01-30 13:06:46 +00:00
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
BehaviourCallbacks = get_behaviour_callbacks(XrefCheck, Attributes),
|
2013-01-30 13:06:46 +00:00
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
%% And create a flat {M,F,A} list
|
2013-01-30 13:06:46 +00:00
|
|
|
lists:foldl(
|
2013-06-18 09:30:32 +00:00
|
|
|
fun({F, A}, Acc) -> [{Mod,F,A} | Acc];
|
|
|
|
({M, F, A}, Acc) -> [{M,F,A} | Acc]
|
|
|
|
end, [], lists:flatten([IgnoreXref, BehaviourCallbacks])).
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2012-09-13 20:07:18 +00:00
|
|
|
keyall(Key, List) ->
|
|
|
|
lists:flatmap(fun({K, L}) when Key =:= K -> L; (_) -> [] end, List).
|
2010-04-09 15:25:00 +00:00
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
get_behaviour_callbacks(exports_not_used, Attributes) ->
|
|
|
|
[B:behaviour_info(callbacks) || B <- keyall(behaviour, Attributes)];
|
|
|
|
get_behaviour_callbacks(_XrefCheck, _Attributes) ->
|
|
|
|
[].
|
|
|
|
|
|
|
|
parse_xref_result({_, MFAt}) -> MFAt;
|
|
|
|
parse_xref_result(MFAt) -> MFAt.
|
2013-01-30 13:06:46 +00:00
|
|
|
|
|
|
|
filter_xref_results(XrefCheck, XrefResults) ->
|
2013-06-18 09:30:32 +00:00
|
|
|
SearchModules = lists:usort(
|
|
|
|
lists:map(
|
|
|
|
fun({Mt,_Ft,_At}) -> Mt;
|
|
|
|
({{Ms,_Fs,_As},{_Mt,_Ft,_At}}) -> Ms;
|
|
|
|
(_) -> undefined
|
|
|
|
end, XrefResults)),
|
|
|
|
|
|
|
|
Ignores = lists:flatmap(fun(Module) ->
|
|
|
|
get_xref_ignorelist(Module, XrefCheck)
|
|
|
|
end, SearchModules),
|
2013-01-30 13:06:46 +00:00
|
|
|
|
|
|
|
[Result || Result <- XrefResults,
|
2013-06-18 09:30:32 +00:00
|
|
|
not lists:member(parse_xref_result(Result), Ignores)].
|
2013-01-30 13:06:46 +00:00
|
|
|
|
2013-06-18 09:30:32 +00:00
|
|
|
display_xref_result(Type, XrefResult) ->
|
2013-01-30 13:06:46 +00:00
|
|
|
{ Source, SMFA, TMFA } = case XrefResult of
|
2013-06-18 09:30:32 +00:00
|
|
|
{MFASource, MFATarget} ->
|
|
|
|
{format_mfa_source(MFASource),
|
|
|
|
format_mfa(MFASource),
|
|
|
|
format_mfa(MFATarget)};
|
|
|
|
MFATarget ->
|
|
|
|
{format_mfa_source(MFATarget),
|
|
|
|
format_mfa(MFATarget),
|
|
|
|
undefined}
|
|
|
|
end,
|
2013-01-30 13:06:46 +00:00
|
|
|
case Type of
|
|
|
|
undefined_function_calls ->
|
|
|
|
?CONSOLE("~sWarning: ~s calls undefined function ~s (Xref)\n",
|
2013-06-18 09:30:32 +00:00
|
|
|
[Source, SMFA, TMFA]);
|
2013-01-30 13:06:46 +00:00
|
|
|
undefined_functions ->
|
|
|
|
?CONSOLE("~sWarning: ~s is undefined function (Xref)\n",
|
2013-06-18 09:30:32 +00:00
|
|
|
[Source, SMFA]);
|
2013-01-30 13:06:46 +00:00
|
|
|
locals_not_used ->
|
|
|
|
?CONSOLE("~sWarning: ~s is unused local function (Xref)\n",
|
2013-06-18 09:30:32 +00:00
|
|
|
[Source, SMFA]);
|
2013-01-30 13:06:46 +00:00
|
|
|
exports_not_used ->
|
|
|
|
?CONSOLE("~sWarning: ~s is unused export (Xref)\n",
|
2013-06-18 09:30:32 +00:00
|
|
|
[Source, SMFA]);
|
2013-01-30 13:06:46 +00:00
|
|
|
deprecated_function_calls ->
|
|
|
|
?CONSOLE("~sWarning: ~s calls deprecated function ~s (Xref)\n",
|
2013-06-18 09:30:32 +00:00
|
|
|
[Source, SMFA, TMFA]);
|
2013-01-30 13:06:46 +00:00
|
|
|
deprecated_functions ->
|
|
|
|
?CONSOLE("~sWarning: ~s is deprecated function (Xref)\n",
|
2013-06-18 09:30:32 +00:00
|
|
|
[Source, SMFA]);
|
2013-01-30 13:06:46 +00:00
|
|
|
Other ->
|
|
|
|
?CONSOLE("~sWarning: ~s - ~s xref check: ~s (Xref)\n",
|
2013-06-18 09:30:32 +00:00
|
|
|
[Source, SMFA, TMFA, Other])
|
2013-01-30 13:06:46 +00:00
|
|
|
end.
|
2010-04-09 15:25:00 +00:00
|
|
|
|
|
|
|
format_mfa({M, F, A}) ->
|
|
|
|
?FMT("~s:~s/~w", [M, F, A]).
|
|
|
|
|
2013-01-30 13:06:46 +00:00
|
|
|
format_mfa_source(MFA) ->
|
|
|
|
case find_mfa_source(MFA) of
|
|
|
|
{module_not_found, function_not_found} -> "";
|
|
|
|
{Source, function_not_found} -> ?FMT("~s: ", [Source]);
|
|
|
|
{Source, Line} -> ?FMT("~s:~w: ", [Source, Line])
|
|
|
|
end.
|
2010-04-09 15:25:00 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
%% Extract an element from a tuple, or undefined if N > tuple size
|
2011-01-09 10:54:40 +00:00
|
|
|
%%
|
2010-04-09 15:25:00 +00:00
|
|
|
safe_element(N, Tuple) ->
|
|
|
|
case catch(element(N, Tuple)) of
|
|
|
|
{'EXIT', {badarg, _}} ->
|
|
|
|
undefined;
|
|
|
|
Value ->
|
|
|
|
Value
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Given a MFA, find the file and LOC where it's defined. Note that
|
|
|
|
%% xref doesn't work if there is no abstract_code, so we can avoid
|
|
|
|
%% being too paranoid here.
|
|
|
|
%%
|
|
|
|
find_mfa_source({M, F, A}) ->
|
2013-01-30 13:06:46 +00:00
|
|
|
case code:get_object_code(M) of
|
|
|
|
error -> {module_not_found, function_not_found};
|
|
|
|
{M, Bin, _} -> find_function_source(M,F,A,Bin)
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_function_source(M, F, A, Bin) ->
|
2011-01-28 15:08:27 +00:00
|
|
|
AbstractCode = beam_lib:chunks(Bin, [abstract_code]),
|
|
|
|
{ok, {M, [{abstract_code, {raw_abstract_v1, Code}}]}} = AbstractCode,
|
2011-01-09 10:54:40 +00:00
|
|
|
%% Extract the original source filename from the abstract code
|
|
|
|
[{attribute, 1, file, {Source, _}} | _] = Code,
|
|
|
|
%% Extract the line number for a given function def
|
2011-09-22 13:01:22 +00:00
|
|
|
Fn = [E || E <- Code,
|
|
|
|
safe_element(1, E) == function,
|
|
|
|
safe_element(3, E) == F,
|
|
|
|
safe_element(4, E) == A],
|
|
|
|
case Fn of
|
|
|
|
[{function, Line, F, _, _}] -> {Source, Line};
|
|
|
|
%% 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.
|