-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_erlang.erl
executable file
·178 lines (160 loc) · 5.77 KB
/
check_erlang.erl
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
#!/usr/bin/env escript
% Dialyzer enhanced flymake checker for Erlang
% Copyright (c) 2011, bkil.hu
% This program is free software and can be distributed under the terms of
% the GNU General Public License v2,
% see COPYING for detailed licensing terms.
%-module(check_erlang).
-export([main/1]).
-compile([export_all]).
main([FileName]) ->
io:format("CURR DIR: ~p~n", [os:cmd("pwd")]),
correct_modname(FileName),
Result =
compile:file(
FileName,
[strong_validation, report,
{warn_format,2}, warn_export_all,
warn_export_vars, warn_shadow_vars,
warn_obsolete_guard, warn_unused_import, bin_opt_info, verbose,
{i, filename:join("..","include")},
{i, filename:join("..","..")}] ++ find_comp_includes()),
case Result of
error ->
Result;
_ ->
case dia(FileName) of
C when C==0; C==2 ->
halt(0);
_ ->
halt(0)
end
end.
correct_modname(FileName)->
NewMod = filename:rootname(filename:basename(FileName)),
{ok,Binary} = file:read_file(FileName),
New = re:replace(Binary,
"^ *-module *\\([^)]*\\) *\. *$",
"-module("++NewMod++").",
[multiline]),
ok = file:write_file(FileName,New).
dia(FileName)->
PLTName = ".dialyzer_prj.plt",
Var = case os:type() of
{win32,_} -> "USERPROFILE"; %% @todo test
{unix,_} -> "DIA_DIR";
vxworks -> "HOME" %% @todo find out
end,
OTP_PLT = filename:join(os:getenv(Var),PLTName),
% # takes a few hours:
% nice dialyzer --build_plt \
% --output_plt ~/.dialyzer_otp.plt \
% -r /usr/lib/erlang/lib/*/ebin
% # takes much less time, but still considerable:
% nice dialyzer --add_to_plt \
% --plt ~/.dialyzer_otp.plt \
% --output_plt ~/.dialyzer_prj.plt \
% -r /my_project/lib/*/ebin
% # then you periodically (daily or once in a while) run:
% nice dialyzer --check_plt \
% --plt ~/.dialyzer_prj.plt
dia_plt(FileName,OTP_PLT).
exec_timeout() ->
100000.
max_line_length()->
255.
dia_plt(FileName,OTP_PLT)->
Args = find_includes() ++
["--plt", OTP_PLT, "--quiet", "--no_check_plt", "--src",
"-I", filename:join("..","include"),
"-I", filename:join("..",".."),
"-Wunmatched_returns", "-Werror_handling", "-Wspecdiffs",
"-c", FileName],
os_cmd("dialyzer",Args,print_line_filename(FileName)).
find_includes() ->
case os:getenv("EPROJ_DIR") of
false -> ProjDir = ".";
ProjDir -> ok end,
Dirs = split(os:cmd("find " ++ ProjDir ++ " -iname deps"), $\n),
Out = lists:flatten([["-I", Dir, " "] || Dir <- Dirs]),
io:format("Got includes2: ~p~n", [Out]),
[Out].
find_comp_includes() ->
case os:getenv("EPROJ_DIR") of
false -> ProjDir = ".";
ProjDir -> ok end,
%Dirs = split(os:cmd("find " ++ ProjDir ++ " -iname include && find -iname deps"), $\n),
io:format("EPROJ IS ~p~n", [ProjDir]),
Dirs = split(os:cmd("find " ++ ProjDir ++ " -iname deps"), $\n),
Out = lists:flatten([{i, Dir} || Dir <- Dirs]),
io:format("Got includes: ~p~n", [Out]),
Out.
split(Str, Sym) ->
NewStr = string:strip(Str, both, $\n),
lists:foldl(fun (FSym, Acc) ->
check_with(FSym, Sym, Acc) end, [], NewStr).
check_with(Sym, Sym, Acc) -> [[] | Acc];
check_with(FSym, _, []) -> [[FSym]];
check_with(FSym, _, [Head|Rest]) -> [Head ++[FSym]|Rest].
print_line_filename(FileName)->
fun("")->
ok;
(OldLine)->
case splitaround(fun(C)->C/=$:end,4,OldLine) of
[_,":",Idx,":",[$ |Msg]] ->
io:format("~s~n",[[FileName,$:,Idx,": Warning: ",Msg]]);
_ ->
io:format("~s~n",[OldLine])
end
end.
splitaround(F,N,L) when is_function(F,1), is_integer(N), N>=0, is_list(L) ->
splitaround_(F,fun(X)->not F(X)end,N,L).
splitaround_(_,_,0,L) ->
[L];
splitaround_(F,NF,N,L) ->
{A,B} = lists:splitwith(F,L),
[A | splitaround_(NF,F,N-1,B)].
os_cmd(Cmd,Args,F)-> % os:cmd/1 is buggy in most versions of Erlang
RelExec = os:find_executable(Cmd), % spawn failed on w1n without this
GrepArgs = Args ++ [" | grep -v lager"],
CmdLine = string:join([RelExec|GrepArgs]," "),
Opts = [stderr_to_stdout, exit_status, in, hide,
stream, {line,max_line_length()}],
Port = open_port({spawn,CmdLine},Opts),
%R13+: ({spawn_executable,Exec},[{args,Args}|Opts])
output_data(Port,F,"").
output_data(Port,F,Old) ->
receive
{Port,{data,{Eol,New}}} ->
Aggr = [New|Old],
case Eol of
eol ->
F(lists:flatten(lists:reverse(Aggr))),
output_data(Port,F,[]);
noeol ->
output_data(Port,F,Aggr)
end;
{Port,{exit_status,Status}} ->
Status
after
exec_timeout() ->
io:format("error: timout!~n"),
127
end.
%% it would be this simple if option check_plt was available here:
newdia_plt(FileName,OTP_PLT)->
Warnings =
dialyzer:run(
[{analysis_type,succ_typings},
{check_plt,false}, %UNSUPPORTED!!!
{warnings,[error_handling, unmatched_returns, specdiffs]},
{init_plt,OTP_PLT},
{include_dirs,[filename:join("..","include"),
filename:join("..","..")]},
{from,src_code},
{files,[FileName]}]),
[begin
Line = dialyzer:format_warning(Warning),
NoEOL = string:strip(Line,right,$\n),
(print_line_filename(FileName))(NoEOL)
end || Warning <- Warnings ].