summaryrefslogtreecommitdiff
path: root/check_xref
blob: ff38ebd0cfb64826476f63980d8509fb79f8af82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env escript
%% -*- erlang -*-
-mode(compile).

%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License
%% at http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2010-2014 GoPivotal, Inc.  All rights reserved.
%%

main(["-h"]) ->
    io:format("usage: check_xref PluginDirectory (options)~n"
              "options:~n"
              "      -q - quiet mode (only prints errors)~n"
              "      -X - disables all filters~n");
main([PluginsDir|Argv]) ->
    put({?MODULE, quiet}, lists:member("-q", Argv)),
    put({?MODULE, no_filters}, lists:member("-X", Argv)),

    {ok, Cwd} = file:get_cwd(),
    code:add_pathz(filename:join(Cwd, "ebin")),
    LibDir = filename:join(Cwd, "lib"),
    case filelib:is_dir(LibDir) of
        false -> ok;
        true  -> os:cmd("rm -rf " ++ LibDir)
    end,
    Rc = try
             check(Cwd, PluginsDir, LibDir, checks())
         catch
             _:Err ->
                 io:format(user, "failed: ~p~n", [Err]),
                 1
         end,
    shutdown(Rc, LibDir).

shutdown(Rc, LibDir) ->
    os:cmd("rm -rf " ++ LibDir),
    erlang:halt(Rc).

check(Cwd, PluginsDir, LibDir, Checks) ->
    {ok, Plugins} = file:list_dir(PluginsDir),
    ok = file:make_dir(LibDir),
    put({?MODULE, third_party}, []),
    [begin
        Source = filename:join(PluginsDir, Plugin),
        Target = filename:join(LibDir, Plugin),
        IsExternal = external_dependency(Plugin),
        AppN = case IsExternal of
                   true  -> filename:join(LibDir, unmangle_name(Plugin));
                   false -> filename:join(
                              LibDir, filename:basename(Plugin, ".ez"))
               end,

        report(info, "mkdir -p ~s~n", [Target]),
        filelib:ensure_dir(Target),

        report(info, "cp ~s ~s~n", [Source, Target]),
        {ok, _} = file:copy(Source, Target),

        report(info, "unzip -d ~s ~s~n", [LibDir, Target]),
        {ok, _} = zip:unzip(Target, [{cwd, LibDir}]),

        UnpackDir = filename:join(LibDir, filename:basename(Target, ".ez")),
        report(info, "mv ~s ~s~n", [UnpackDir, AppN]),
        ok = file:rename(UnpackDir, AppN),

        code:add_patha(filename:join(AppN, "ebin")),
        case IsExternal of
            true -> App = list_to_atom(hd(string:tokens(filename:basename(AppN),
                                                        "-"))),
                    report(info, "loading ~p~n", [App]),
                    application:load(App),
                    store_third_party(App);
            _    -> ok
        end
     end || Plugin <- Plugins,
            lists:suffix(".ez", Plugin)],

    RabbitAppEbin = filename:join([LibDir, "rabbit", "ebin"]),
    filelib:ensure_dir(filename:join(RabbitAppEbin, "foo")),
    {ok, Beams} = file:list_dir("ebin"),
    [{ok, _} = file:copy(filename:join("ebin", Beam),
                         filename:join(RabbitAppEbin, Beam)) || Beam <- Beams],
    xref:start(?MODULE),
    xref:set_default(?MODULE, [{verbose, false}, {warnings, false}]),
    xref:set_library_path(?MODULE, code:get_path()),
    xref:add_release(?MODULE, Cwd, {name, rabbit}),
    store_unresolved_calls(),
    Results = lists:flatten([perform_analysis(Q) || Q <- Checks]),
    report(Results).

%%
%% Analysis
%%

perform_analysis({Query, Description, Severity}) ->
    perform_analysis({Query, Description, Severity, fun(_) -> false end});
perform_analysis({Query, Description, Severity, Filter}) ->
    report_progress("Checking whether any code ~s "
                    "(~s)~n", [Description, Query]),
    case analyse(Query) of
        {ok, Analysis} ->
            [filter(Result, Filter) ||
                Result <- process_analysis(Query, Description,
                                           Severity, Analysis)];
        {error, Module, Reason} ->
            {analysis_error, {Module, Reason}}
    end.

partition(Results) ->
    lists:partition(fun({{_, L}, _}) -> L =:= error end, Results).

analyse(Query) when is_atom(Query) ->
    xref:analyse(?MODULE, Query, [{verbose, false}]);
analyse(Query) when is_list(Query) ->
    xref:q(?MODULE, Query).

process_analysis(Query, Tag, Severity, Analysis) when is_atom(Query) ->
    [{{Tag, Severity}, MFA} || MFA <- Analysis];
process_analysis(Query, Tag, Severity, Analysis) when is_list(Query) ->
    [{{Tag, Severity}, Result} || Result <- Analysis].

checks() ->
   [{"(XXL)(Lin) ((XC - UC) || (XU - X - B))",
     "has call to undefined function(s)",
     error, filters()},
    {"(Lin) (L - LU)",
     "has unused local function(s)",
     error, filters()},
    {"(E | \"(rabbit|amqp).*\":_/_ || \"gen_server2?\":call/2)",
     "has 5 sec timeout in",
     error, filters()},
    {"(Lin) (LU * (X - XU))",
     "has exported function(s) only used locally",
     warning, filters()},
    {"(Lin) (DF * (XU + LU))", "used deprecated function(s)",
     warning, filters()}].
%%    {"(Lin) (X - XU)", "possibly unused export",
%%     warning, fun filter_unused/1}].

%%
%% noise filters (can be disabled with -X) - strip uninteresting analyses
%%

filter(Result, Filter) ->
    case Filter(Result) of
        false -> Result;
        true  -> []  %% NB: this gets flattened out later on....
    end.

filters() ->
    case get({?MODULE, no_filters}) of
        true  -> fun(_) -> false end;
        _     -> filter_chain([fun is_unresolved_call/1, fun is_callback/1,
                               fun is_unused/1, fun is_irrelevant/1])
    end.

filter_chain(FnChain) ->
    fun(AnalysisResult) ->
        Result = cleanup(AnalysisResult),
        lists:foldl(fun(F, false) -> F(Result);
                       (_F, true) -> true
                    end, false, FnChain)
    end.

cleanup({{_, _},{{{{_,_,_}=MFA1,_},{{_,_,_}=MFA2,_}},_}}) -> {MFA1, MFA2};
cleanup({{_, _},{{{_,_,_}=MFA1,_},{{_,_,_}=MFA2,_}}})     -> {MFA1, MFA2};
cleanup({{_, _},{{_,_,_}=MFA1,{_,_,_}=MFA2},_})           -> {MFA1, MFA2};
cleanup({{_, _},{{_,_,_}=MFA1,{_,_,_}=MFA2}})             -> {MFA1, MFA2};
cleanup({{_, _}, {_,_,_}=MFA})                            -> MFA;
cleanup({{_, _}, {{_,_,_}=MFA,_}})                        -> MFA;
cleanup({{_,_,_}=MFA, {_,_,_}})                           -> MFA;
cleanup({{_,_,_}=MFA, {_,_,_},_})                         -> MFA;
cleanup(Other)                                            -> Other.

is_irrelevant({{M,_,_}, {_,_,_}}) ->
    is_irrelevant(M);
is_irrelevant({M,_,_}) ->
    is_irrelevant(M);
is_irrelevant(Mod) when is_atom(Mod) ->
    lists:member(Mod, get({?MODULE, third_party})).

is_unused({{_,_,_}=MFA, {_,_,_}}) ->
    is_unused(MFA);
is_unused({M,_F,_A}) ->
    lists:suffix("_tests", atom_to_list(M));
is_unused(_) ->
    false.

is_unresolved_call({_, F, A}) ->
    UC = get({?MODULE, unresolved_calls}),
    sets:is_element({'$M_EXPR', F, A}, UC);
is_unresolved_call(_) ->
    false.

%% TODO: cache this....
is_callback({M,_,_}=MFA) ->
    Attributes = M:module_info(attributes),
    Behaviours = proplists:append_values(behaviour, Attributes),
    {_, Callbacks} = lists:foldl(fun acc_behaviours/2, {M, []}, Behaviours),
    lists:member(MFA, Callbacks);
is_callback(_) ->
    false.

acc_behaviours(B, {M, CB}=Acc) ->
    case catch(B:behaviour_info(callbacks)) of
        [{_,_} | _] = Callbacks ->
            {M, CB ++ [{M, F, A} || {F,A} <- Callbacks]};
        _ ->
            Acc
    end.

%%
%% reporting/output
%%

report(Results) ->
    [report_failures(F) || F <- Results],
    {Errors, Warnings} = partition(Results),
    report(info, "Completed: ~p errors, ~p warnings~n",
                 [length(Errors), length(Warnings)]),
    case length(Errors) > 0 of
        true  -> 1;
        false -> 0
    end.

report_failures({analysis_error, {Mod, Reason}}) ->
    report(error, "~s:0 Analysis Error: ~p~n", [source_file(Mod), Reason]);
report_failures({{Tag, Level}, {{{{M,_,_},L},{{M2,F2,A2},_}},_}}) ->
    report(Level, "~s:~w ~s ~p:~p/~p~n",
           [source_file(M), L, Tag, M2, F2, A2]);
report_failures({{Tag, Level}, {{M,F,A},L}}) ->
    report(Level, "~s:~w ~s ~p:~p/~p~n", [source_file(M), L, Tag, M, F, A]);
report_failures({{Tag, Level}, {M,F,A}}) ->
    report(Level, "~s:unknown ~s ~p:~p/~p~n", [source_file(M), Tag, M, F, A]);
report_failures(Term) ->
    report(error, "Ignoring ~p~n", [Term]),
    ok.

report_progress(Fmt, Args) ->
    report(info, Fmt, Args).

report(Level, Fmt, Args) ->
    case {get({?MODULE, quiet}), Level} of
        {true,  error} -> do_report(lookup_prefix(Level), Fmt, Args);
        {false, _}     -> do_report(lookup_prefix(Level), Fmt, Args);
        _              -> ok
    end.

do_report(Prefix, Fmt, Args) ->
    io:format(Prefix ++ Fmt, Args).

lookup_prefix(error)   -> "ERROR: ";
lookup_prefix(warning) -> "WARNING: ";
lookup_prefix(info)    -> "INFO: ".

source_file(M) ->
    proplists:get_value(source, M:module_info(compile)).

%%
%% setup/code-path/file-system ops
%%

store_third_party(App) ->
    {ok, AppConfig} = application:get_all_key(App),
    AppModules = proplists:get_value(modules, AppConfig),
    put({?MODULE, third_party}, AppModules ++ get({?MODULE, third_party})).

%% TODO: this ought not to be maintained in such a fashion
external_dependency(Path) ->
    lists:any(fun(P) -> lists:prefix(P, Path) end,
              ["mochiweb", "webmachine", "rfc4627", "eldap"]).

unmangle_name(Path) ->
    [Name, Vsn | _] = re:split(Path, "-", [{return, list}]),
    string:join([Name, Vsn], "-").

store_unresolved_calls() ->
    {ok, UCFull} = analyse("UC"),
    UC = [MFA || {_, {_,_,_} = MFA} <- UCFull],
    put({?MODULE, unresolved_calls}, sets:from_list(UC)).