summaryrefslogtreecommitdiff
path: root/src/rebar_prv_dialyzer.erl
blob: 5328d1be5bc482ed1384bce437397c5bc610b2b3 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et

-module(rebar_prv_dialyzer).

-behaviour(provider).

-export([init/1,
         do/1,
         format_error/1]).

-include("rebar.hrl").

-define(PROVIDER, dialyzer).
-define(DEPS, [compile]).

%% ===================================================================
%% Public API
%% ===================================================================

-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
    Opts = [{update_plt, $u, "update-plt", boolean, "Enable updating the PLT. Default: true"},
            {succ_typings, $s, "succ-typings", boolean, "Enable success typing analysis. Default: true"}],
    State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
                                                               {module, ?MODULE},
                                                               {bare, false},
                                                               {deps, ?DEPS},
                                                               {example, "rebar dialyzer"},
                                                               {short_desc, short_desc()},
                                                               {desc, desc()},
                                                               {opts, Opts}])),
    {ok, State1}.

desc() ->
    short_desc() ++ "\n"
    "\n"
    "This command will build, and keep up-to-date, a suitable PLT and will use "
    "it to carry out success typing analysis on the current project.\n"
    "\n"
    "The following (optional) configurations can be added to a rebar.config:\n"
    "`dialyzer_warnings` - a list of dialyzer warnings\n"
    "`dialyzer_plt` - the PLT file to use\n"
    "`dialyzer_plt_apps` - a list of applications to include in the PLT file*\n"
    "`dialyzer_plt_warnings` - display warnings when updating a PLT file "
    "(boolean)\n"
    "`dialyzer_base_plt` - the base PLT file to use**\n"
    "`dialyzer_base_plt_dir` - the base PLT directory**\n"
    "`dialyzer_base_plt_apps` - a list of applications to include in the base "
    "PLT file**\n"
    "\n"
    "*The applications in `dialyzer_base_plt_apps` and any `applications` and "
    "`included_applications` listed in their .app files will be added to the "
    "list.\n"
    "**The base PLT is a PLT containing the core OTP applications often "
    "required for a project's PLT. One base PLT is created per OTP version and "
    "stored in `dialyzer_base_plt_dir` (defaults to $HOME/.rebar3/). A base "
    "PLT is used to create a project's initial PLT.".

short_desc() ->
    "Run the Dialyzer analyzer on the project.".

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
    ?INFO("Dialyzer starting, this may take a while...", []),
    Plt = get_plt_location(State),
    Apps = rebar_state:project_apps(State),

    try
        do(State, Plt, Apps)
    catch
        throw:{dialyzer_error, Error} ->
            {error, {?MODULE, {error_processing_apps, Error}}};
        throw:{dialyzer_warnings, Warnings} ->
            {error, {?MODULE, {dialyzer_warnings, Warnings}}}
    end.

-spec format_error(any()) -> iolist().
format_error({error_processing_apps, Error}) ->
    io_lib:format("Error in dialyzing apps: ~s", [Error]);
format_error({dialyzer_warnings, Warnings}) ->
    io_lib:format("Warnings occured running dialyzer: ~b", [Warnings]);
format_error(Reason) ->
    io_lib:format("~p", [Reason]).

%% Internal functions

get_plt_location(State) ->
    BuildDir = rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR),
    DefaultPlt = filename:join([BuildDir, default_plt()]),
    rebar_state:get(State, dialyzer_plt, DefaultPlt).

default_plt() ->
    ".rebar3.otp-" ++ otp_version() ++ ".plt".

otp_version() ->
    Release = erlang:system_info(otp_release),
    try otp_version(Release) of
        Vsn ->
            Vsn
    catch
        error:_ ->
            Release
    end.

otp_version(Release) ->
    File = filename:join([code:root_dir(), "releases", Release, "OTP_VERSION"]),
    {ok, Contents} = file:read_file(File),
    [Vsn] = binary:split(Contents, [<<$\n>>], [global, trim]),
    [_ | _] = unicode:characters_to_list(Vsn).


do(State, Plt, Apps) ->
    {PltWarnings, State1} = update_proj_plt(State, Plt, Apps),
    {Warnings, State2} = succ_typings(State1, Plt, Apps),
    case PltWarnings + Warnings of
        0 ->
            {ok, State2};
        TotalWarnings ->
            throw({dialyzer_warnings, TotalWarnings})
    end.

update_proj_plt(State, Plt, Apps) ->
    {Args, _} = rebar_state:command_parsed_args(State),
    case proplists:get_value(update_plt, Args) of
        false ->
            {0, State};
        _ ->
            do_update_proj_plt(State, Plt, Apps)
    end.

do_update_proj_plt(State, Plt, Apps) ->
    ?INFO("Updating plt...", []),
    Files = get_plt_files(State, Apps),
    case read_plt(State, Plt) of
        {ok, OldFiles} ->
            check_plt(State, Plt, OldFiles, Files);
        {error, no_such_file} ->
            build_proj_plt(State, Plt, Files)
    end.

get_plt_files(State, Apps) ->
    BasePltApps = rebar_state:get(State, dialyzer_base_plt_apps,
                                  default_plt_apps()),
    PltApps = rebar_state:get(State, dialyzer_plt_apps, []),
    DepApps = lists:flatmap(fun rebar_app_info:applications/1, Apps),
    get_plt_files(BasePltApps ++ PltApps ++ DepApps, Apps, [], []).

default_plt_apps() ->
    [erts,
     crypto,
     kernel,
     stdlib].

get_plt_files([], _, _, Files) ->
    Files;
get_plt_files([AppName | DepApps], Apps, PltApps, Files) ->
    case lists:member(AppName, PltApps) orelse app_member(AppName, Apps) of
        true ->
            get_plt_files(DepApps, Apps, PltApps, Files);
        false ->
            {DepApps2, Files2} = app_name_to_info(AppName),
            DepApps3 = DepApps2 ++ DepApps,
            Files3 = Files2 ++ Files,
            get_plt_files(DepApps3, Apps, [AppName | PltApps], Files3)
    end.

app_member(AppName, Apps) ->
    case rebar_app_utils:find(ec_cnv:to_binary(AppName), Apps) of
        {ok, _App} ->
            true;
        error ->
            false
    end.

app_name_to_info(AppName) ->
    case app_name_to_ebin(AppName) of
        {error, _} ->
            ?CONSOLE("Unknown application ~s", [AppName]),
            {[], []};
        EbinDir ->
            ebin_to_info(EbinDir, AppName)
    end.

app_name_to_ebin(AppName) ->
    case code:lib_dir(AppName, ebin) of
        {error, bad_name} ->
            search_ebin(AppName);
        EbinDir ->
            check_ebin(EbinDir, AppName)
    end.

check_ebin(EbinDir, AppName) ->
    case filelib:is_dir(EbinDir) of
        true ->
            EbinDir;
        false ->
            search_ebin(AppName)
    end.

search_ebin(AppName) ->
    case code:where_is_file(atom_to_list(AppName) ++ ".app") of
        non_existing ->
            {error, bad_name};
        AppFile ->
            filename:dirname(AppFile)
    end.

ebin_to_info(EbinDir, AppName) ->
    AppFile = filename:join(EbinDir, atom_to_list(AppName) ++ ".app"),
    case file:consult(AppFile) of
        {ok, [{application, AppName, AppDetails}]} ->
            DepApps = proplists:get_value(applications, AppDetails, []),
            IncApps = proplists:get_value(included_applications, AppDetails,
                                          []),
            Modules = proplists:get_value(modules, AppDetails, []),
            Files = modules_to_files(Modules, EbinDir),
            {IncApps ++ DepApps, Files};
        _ ->
            Error = io_lib:format("Could not parse ~p", [AppFile]),
            throw({dialyzer_error, Error})
    end.

modules_to_files(Modules, EbinDir) ->
    Ext = code:objfile_extension(),
    Mod2File = fun(Module) -> module_to_file(Module, EbinDir, Ext) end,
    rebar_utils:filtermap(Mod2File, Modules).

module_to_file(Module, EbinDir, Ext) ->
    File = filename:join(EbinDir, atom_to_list(Module) ++ Ext),
    case filelib:is_file(File) of
        true ->
            {true, File};
        false ->
            ?CONSOLE("Unknown module ~s", [Module]),
            false
    end.

read_plt(_State, Plt) ->
    case dialyzer:plt_info(Plt) of
        {ok, Info} ->
            Files = proplists:get_value(files, Info, []),
            {ok, Files};
        {error, no_such_file} = Error ->
            Error;
        {error, read_error} ->
            Error = io_lib:format("Could not read the PLT file ~p", [Plt]),
            throw({dialyzer_error, Error})
    end.

check_plt(State, Plt, OldList, FilesList) ->
    Old = sets:from_list(OldList),
    Files = sets:from_list(FilesList),
    Remove = sets:subtract(Old, Files),
    {RemWarnings, State1} = remove_plt(State, Plt, sets:to_list(Remove)),
    Check = sets:intersection(Files, Old),
    {CheckWarnings, State2} = check_plt(State1, Plt, sets:to_list(Check)),
    Add = sets:subtract(Files, Old),
    {AddWarnings, State3} = add_plt(State2, Plt, sets:to_list(Add)),
    ?DEBUG("~p", [[RemWarnings, CheckWarnings, AddWarnings]]),
    {RemWarnings + CheckWarnings + AddWarnings, State3}.

remove_plt(State, _Plt, []) ->
    {0, State};
remove_plt(State, Plt, Files) ->
    ?INFO("Removing ~b files from ~p...", [length(Files), Plt]),
    run_plt(State, Plt, plt_remove, Files).

check_plt(State, _Plt, []) ->
    {o, State};
check_plt(State, Plt, Files) ->
    ?INFO("Checking ~b files in ~p...", [length(Files), Plt]),
    run_plt(State, Plt, plt_check, Files).

add_plt(State, _Plt, []) ->
    {0, State};
add_plt(State, Plt, Files) ->
    ?INFO("Adding ~b files to ~p...", [length(Files), Plt]),
    run_plt(State, Plt, plt_add, Files).

run_plt(State, Plt, Analysis, Files) ->
    GetWarnings = rebar_state:get(State, dialyzer_plt_warnings, false),
    Opts = [{analysis_type, Analysis},
            {get_warnings, GetWarnings},
            {init_plt, Plt},
            {from, byte_code},
            {files, Files}],
    run_dialyzer(State, Opts).

build_proj_plt(State, Plt, Files) ->
    BasePlt = get_base_plt_location(State),
    BaseFiles = get_base_plt_files(State),
    {BaseWarnings, State1} = update_base_plt(State, BasePlt, BaseFiles),
    ?INFO("Copying ~p to ~p...", [BasePlt, Plt]),
    _ = filelib:ensure_dir(Plt),
    case file:copy(BasePlt, Plt) of
        {ok, _} ->
            {CheckWarnings, State2} = check_plt(State1, Plt, BaseFiles, Files),
            {BaseWarnings + CheckWarnings, State2};
        {error, Reason} ->
            Error = io_lib:format("Could not copy PLT from ~p to ~p: ~p",
                                  [BasePlt, Plt, file:format_error(Reason)]),
            throw({dialyzer_error, Error})
    end.

get_base_plt_location(State) ->
    Home = rebar_dir:home_dir(),
    GlobalConfigDir = filename:join(Home, ?CONFIG_DIR),
    BaseDir = rebar_state:get(State, dialyzer_base_plt_dir, GlobalConfigDir),
    BasePlt = rebar_state:get(State, dialyzer_base_plt, default_plt()),
    filename:join(BaseDir, BasePlt).

get_base_plt_files(State) ->
    BasePltApps = rebar_state:get(State, dialyzer_base_plt_apps,
                                  default_plt_apps()),
    app_names_to_files(BasePltApps).

app_names_to_files(AppNames) ->
    ToFiles = fun(AppName) ->
                      {_, Files} = app_name_to_info(AppName),
                      Files
              end,
    lists:flatmap(ToFiles, AppNames).

update_base_plt(State, BasePlt, BaseFiles) ->
    ?INFO("Updating base plt...", []),
    case read_plt(State, BasePlt) of
        {ok, OldBaseFiles} ->
            check_plt(State, BasePlt, OldBaseFiles, BaseFiles);
        {error, no_such_file} ->
            _ = filelib:ensure_dir(BasePlt),
            build_plt(State, BasePlt, BaseFiles)
    end.

build_plt(State, Plt, Files) ->
    ?INFO("Adding ~b files to ~p...", [length(Files), Plt]),
    GetWarnings = rebar_state:get(State, dialyzer_plt_warnings, false),
    Opts = [{analysis_type, plt_build},
            {get_warnings, GetWarnings},
            {output_plt, Plt},
            {files, Files}],
    run_dialyzer(State, Opts).

succ_typings(State, Plt, Apps) ->
    {Args, _} = rebar_state:command_parsed_args(State),
    case proplists:get_value(succ_typings, Args) of
        false ->
            {ok, State};
        _ ->
            do_succ_typings(State, Plt, Apps)
    end.

do_succ_typings(State, Plt, Apps) ->
    ?INFO("Doing success typing analysis...", []),
    Files = apps_to_files(Apps),
    ?INFO("Analyzing ~b files with ~p...", [length(Files), Plt]),
    Opts = [{analysis_type, succ_typings},
            {get_warnings, true},
            {from, byte_code},
            {files, Files},
            {init_plt, Plt}],
    run_dialyzer(State, Opts).

apps_to_files(Apps) ->
    lists:flatmap(fun app_to_files/1, Apps).

app_to_files(App) ->
    AppName = ec_cnv:to_atom(rebar_app_info:name(App)),
    {_, Files} = app_name_to_info(AppName),
    Files.

run_dialyzer(State, Opts) ->
    WarningsList = rebar_state:get(State, dialyzer_warnings, default_warnings()),
    Opts2 = [{warnings, WarningsList} | Opts],
    {Unknowns, Warnings} = format_warnings(dialyzer:run(Opts2)),
    _ = [?CONSOLE("~s", [Unknown]) || Unknown <- Unknowns],
    _ = [?CONSOLE("~s", [Warning]) || Warning <- Warnings],
    {length(Warnings), State}.

format_warnings(Warnings) ->
    format_warnings(Warnings, [], []).

format_warnings([Warning | Rest], Unknowns, Warnings) ->
    case dialyzer:format_warning(Warning) of
        ":0: " ++ Unknown ->
            format_warnings(Rest, [strip(Unknown) | Unknowns], Warnings);
        Warning2 ->
            format_warnings(Rest, Unknowns, [strip(Warning2) | Warnings])
    end;
format_warnings([], Unknowns, Warnings) ->
    {Unknowns, Warnings}.

strip(Warning) ->
    string:strip(Warning, right, $\n).

default_warnings() ->
    [error_handling,
     unmatched_returns,
     underspecs].