summaryrefslogtreecommitdiff
path: root/lib/dialyzer/src/typer.erl
blob: 6467ae00939a273744d35eac8352c9dbd3f50318 (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
%% -*- erlang-indent-level: 2 -*-
%%
%% Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

%%-----------------------------------------------------------------------
%% File        : typer.erl
%% Author(s)   : The first version of typer was written by Bingwen He
%%               with guidance from Kostis Sagonas and Tobias Lindahl.
%%               Since June 2008 typer is maintained by Kostis Sagonas.
%%               On 2022, Brujo, Pablo, and Mackenzie from NextRoll
%%               started working on the rebar3 plugin for typer and,
%%               with that in mind, split typer and typer_core apart.
%% Description : An Erlang/OTP application that shows type information
%%               for Erlang modules to the user.  Additionally, it can
%%               annotate the code of files with such type information.
%%-----------------------------------------------------------------------

-module(typer).

-export([start/0]).

%%-----------------------------------------------------------------------

-define(SHOW, show).
-define(SHOW_EXPORTED, show_exported).
-define(ANNOTATE, annotate).
-define(ANNOTATE_IN_PLACE, annotate_in_place).
-define(ANNOTATE_INC_FILES, annotate_inc_files).

%%-----------------------------------------------------------------------

-spec start() -> no_return().

start() ->
  _ = io:setopts(standard_error, [{encoding,unicode}]),
  _ = io:setopts([{encoding,unicode}]),
  ok = typer_core:run(process_cl_args()),
  erlang:halt(0).

%%--------------------------------------------------------------------
%% Processing of command-line options and arguments.
%%--------------------------------------------------------------------

-spec process_cl_args() -> typer_core:opts().

process_cl_args() ->
  ArgList = init:get_plain_arguments(),
  %% io:format("Args is ~tp\n", [ArgList]),
  Opts = analyze_args(ArgList, #{}),
  %% if the mode has not been set, set it to the default mode (show)
  case Opts of
    #{mode := _} -> Opts;
    Opts -> Opts#{mode => ?SHOW}
  end.

analyze_args([], Opts) ->
  Opts;
analyze_args(ArgList, Opts) ->
  {Result, Rest} = cl(ArgList),
  NewOpts = analyze_result(Result, Opts),
  analyze_args(Rest, NewOpts).

cl(["-h"|_])     -> help_message();
cl(["--help"|_]) -> help_message();
cl(["-v"|_])        -> version_message();
cl(["--version"|_]) -> version_message();
cl(["--edoc"|Opts]) -> {edoc, Opts};
cl(["--show"|Opts]) -> {{mode, ?SHOW}, Opts};
cl(["--show_exported"|Opts]) -> {{mode, ?SHOW_EXPORTED}, Opts};
cl(["--show-exported"|Opts]) -> {{mode, ?SHOW_EXPORTED}, Opts};
cl(["--show_success_typings"|Opts]) -> {show_succ, Opts};
cl(["--show-success-typings"|Opts]) -> {show_succ, Opts};
cl(["--annotate"|Opts]) -> {{mode, ?ANNOTATE}, Opts};
cl(["--annotate-inc-files"|Opts]) -> {{mode, ?ANNOTATE_INC_FILES}, Opts};
cl(["--annotate-in-place"|Opts]) -> {{mode, ?ANNOTATE_IN_PLACE}, Opts};
cl(["--no_spec"|Opts]) -> {no_spec, Opts};
cl(["--plt",Plt|Opts]) -> {{plt, Plt}, Opts};
cl(["-D"|_Opts]) -> fatal_error("no variable name specified after -D");
cl(["-D"++Def|Opts]) ->
  DefPair = process_def_list(re:split(Def, "=", [{return, list}, unicode])),
  {{def, DefPair}, Opts};
cl(["-I",Dir|Opts]) -> {{inc, Dir}, Opts};
cl(["-I"|_Opts]) -> fatal_error("no include directory specified after -I");
cl(["-I"++Dir|Opts]) -> {{inc, Dir}, Opts};
cl(["-T"|Opts]) ->
  {Files, RestOpts} = collect_args(Opts),
  case Files of
    [] -> fatal_error("no file or directory specified after -T");
    [_|_] -> {{trusted, Files}, RestOpts}
  end;
cl(["-r"|Opts]) ->
  {Files, RestOpts} = collect_args(Opts),
  {{files_r, Files}, RestOpts};
cl(["-pa",Dir|Opts]) -> {{pa,Dir}, Opts};
cl(["-pz",Dir|Opts]) -> {{pz,Dir}, Opts};
cl(["-"++H|_]) -> fatal_error("unknown option -"++H);
cl(Opts) ->
  {Files, RestOpts} = collect_args(Opts),
  {{files, Files}, RestOpts}.

-spec collect_args([string()]) -> {[string()], [string()]}.

collect_args(List) ->
    collect_args_1(List, []).

collect_args_1(["-"++_|_] = L, Acc) ->
    {lists:reverse(Acc), L};
collect_args_1([Arg|T], Acc) ->
    collect_args_1(T, [Arg|Acc]);
collect_args_1([], Acc) ->
    {lists:reverse(Acc), []}.

process_def_list(L) ->
  case L of
    [Name, Value] ->
      {ok, Tokens, _} = erl_scan:string(Value ++ "."),
      {ok, ErlValue} = erl_parse:parse_term(Tokens),
      {list_to_atom(Name), ErlValue};
    [Name] ->
      {list_to_atom(Name), true}
  end.

%% Get information about files that the user trusts and wants to analyze
analyze_result({files, Val}, Opts) ->
  append_in_map(files, Val, Opts);
analyze_result({files_r, Val}, Opts) ->
  append_in_map(files_r, Val, Opts);
analyze_result({trusted, Val}, Opts) ->
  append_in_map(trusted, Val, Opts);
analyze_result(edoc, Opts) ->
  Opts#{edoc => true};
%% Get useful information for actual analysis
analyze_result({mode, Mode}, #{mode := OldMode}) ->
  mode_error(OldMode, Mode);
analyze_result({mode, Mode}, Opts) ->
  Opts#{mode => Mode};
analyze_result({def, Val}, Opts) ->
  append_in_map(macros, [Val], Opts);
analyze_result({inc, Val}, Opts) ->
  append_in_map(includes, [Val], Opts);
analyze_result({plt, Plt}, Opts) ->
  Opts#{plt => Plt};
analyze_result(show_succ, Opts) ->
  Opts#{show_succ => true};
analyze_result(no_spec, Opts) ->
  Opts#{no_spec => true};
analyze_result({pa, Dir}, Opts) ->
  true = code:add_patha(Dir),
  Opts;
analyze_result({pz, Dir}, Opts) ->
  true = code:add_pathz(Dir),
  Opts.

append_in_map(Key, List, Map) ->
  maps:update_with(Key, fun(L) -> L ++ List end, List, Map).

%%--------------------------------------------------------------------
%% Utilities for error reporting.
%%--------------------------------------------------------------------

-spec fatal_error(string()) -> no_return().

fatal_error(Slogan) ->
  msg(io_lib:format("typer: ~ts\n", [Slogan])),
  erlang:halt(1).

-spec mode_error(typer_core:mode(), typer_core:mode()) -> no_return().

mode_error(OldMode, NewMode) ->
  Msg = io_lib:format("Mode was previously set to '~s'; "
		      "cannot set it to '~s' now",
		      [OldMode, NewMode]),
  fatal_error(Msg).

-spec msg(string()) -> 'ok'.

msg(Msg) ->
  io:format(standard_error, "~ts", [Msg]).

%%--------------------------------------------------------------------
%% Version and help messages.
%%--------------------------------------------------------------------

-spec version_message() -> no_return().

version_message() ->
  io:format("TypEr version "++?VSN++"\n"),
  erlang:halt(0).

-spec help_message() -> no_return().

help_message() ->
  S = <<" Usage: typer [--help] [--version] [--plt PLT] [--edoc]
              [--show | --show-exported | --annotate | --annotate-inc-files | --annotate-in-place]
              [-Ddefine]* [-I include_dir]* [-pa dir]* [-pz dir]*
              [-T application]* [-r] file*

 Options:
   -r dir*
       search directories recursively for .erl files below them
   --show
       Prints type specifications for all functions on stdout.
       (this is the default behaviour; this option is not really needed)
   --show-exported (or --show_exported)
       Same as --show, but prints specifications for exported functions only
       Specs are displayed sorted alphabetically on the function's name
   --annotate
       Annotates the specified files with type specifications and writes
       the resulting files into a new typer_ann folder.
   --annotate-inc-files
       Same as --annotate but annotates all -include() files as well as
       all .erl files
   --annotate-in-place
       Annotate directly on the source code files, instead of dumping the
       annotated files in a different directory
   --edoc
       Prints type information as Edoc @spec comments, not as type specs
   --plt PLT
       Use the specified dialyzer PLT file rather than the default one
       (Incremental and non-incremental PLT files are supported)
   -T file*
       The specified file(s) already contain type specifications and these
       are to be trusted in order to print specs for the rest of the files
       (Multiple files or dirs, separated by spaces, can be specified.)
   -Dname (or -Dname=value)
       pass the defined name(s) to TypEr
       (The syntax of defines is the same as that used by \"erlc\".)
   -I include_dir
       pass the include_dir to TypEr
       (The syntax of includes is the same as that used by \"erlc\".)
   -pa dir
   -pz dir
       Set code path options to TypEr
       (This is useful for files that use parse transforms.)
   --version (or -v)
       prints the Typer version and exits
   --help (or -h)
       prints this message and exits

 Note:
   * denotes that multiple occurrences of these options are possible.
">>,
  io:put_chars(S),
  erlang:halt(0).