Skip to content

Commit 3751b5a

Browse files
committed
Show correct source of errors and warnings
1 parent 592046d commit 3751b5a

11 files changed

Lines changed: 98 additions & 49 deletions

File tree

src/issues_spec.hrl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-ifndef(issues_spec_hrl).
22
-define(issues_spec_hrl, included).
33

4-
-type warning() :: {warning, Line::pos_integer(), Description::string()}.
5-
-type error() :: {error, Description::string()} |
6-
{error, Line::pos_integer(), Description::string()}.
4+
-type warning() :: {warning, Filename::file:filename(), Line::pos_integer(), Description::string()}.
5+
-type error() :: {error, Filename::file:filename(), Description::string()} |
6+
{error, Filename::file:filename(), Line::pos_integer(), Description::string()}.
77

88
-type issue() :: warning() | error().
99

src/syntaxerl.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ check_syntax(FileName, BaseFileName, Debug) ->
7171
syntaxerl_logger:debug(Debug, "Selected handler: ~p", [Handler]),
7272
case Handler:check_syntax(FileName, BaseFileName, Debug) of
7373
{ok, Issues} ->
74-
syntaxerl_utils:print_issues(FileName, Issues),
74+
syntaxerl_utils:print_issues(BaseFileName, FileName, Issues),
7575
halt(?EXIT_SUCCESS);
7676
{error, Issues} ->
77-
syntaxerl_utils:print_issues(FileName, Issues),
77+
syntaxerl_utils:print_issues(BaseFileName, FileName, Issues),
7878
halt(?EXIT_FAILURE)
7979
end.
8080

src/syntaxerl_escript.erl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ check_syntax(FileName, BaseFileName, Debug) ->
4444
{ok, _ModuleName} ->
4545
{ok, []};
4646
{ok, _ModuleName, Warnings} ->
47-
io:format("Warnings: ~p~n", [Warnings]),
47+
Warnings0 = fix_file_names(NewFileName, FileName, Warnings),
4848
{ok, syntaxerl_format:format_warnings(
49-
?MODULE, fix_line_numbers(Warnings))};
49+
?MODULE, fix_line_numbers(Warnings0))};
5050
{error, Errors, Warnings} ->
51+
Errors0 = fix_file_names(NewFileName, FileName, Errors),
52+
Warnings0 = fix_file_names(NewFileName, FileName, Warnings),
5153
case syntaxerl_format:format_errors(
52-
?MODULE, fix_line_numbers(Errors)) of
54+
?MODULE, fix_line_numbers(Errors0)) of
5355
[] ->
5456
{ok, syntaxerl_format:format_warnings(
55-
?MODULE, fix_line_numbers(Warnings))};
57+
?MODULE, fix_line_numbers(Warnings0))};
5658
Errors2 ->
5759
{error, Errors2 ++ syntaxerl_format:format_warnings(
58-
?MODULE, fix_line_numbers(Warnings))}
60+
?MODULE, fix_line_numbers(Warnings0))}
5961
end
6062
end;
6163
{error, Reason} ->
@@ -73,6 +75,14 @@ output_warning(_) -> true.
7375
%% Internal
7476
%% ===================================================================
7577

78+
fix_file_names(TmpFileName, FileName, ErrorList) ->
79+
[fix_file_name(TmpFileName, FileName, Error) || Error <- ErrorList].
80+
81+
fix_file_name(TmpFileName, FileName, {TmpFileName, ErrorList}) ->
82+
{FileName, ErrorList};
83+
fix_file_name(_TmpFileName, _FileName, {FileName, ErrorList}) ->
84+
{FileName, ErrorList}.
85+
7686
fix_line_numbers(ErrorList) ->
7787
ErrorList0 = skip_expected_errors(ErrorList),
7888
[{F, [{fix_line_number(L), M, E} || {L, M, E} <- Es]}

src/syntaxerl_format.erl

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,31 @@
1212
%% API
1313
%% ===================================================================
1414

15-
-spec format_errors(module(), error_list()) ->
16-
[{error, integer(), string()}].
17-
format_errors(_Handler, []) ->
18-
[];
19-
format_errors(Handler, [{_FileName, Errors} | _]) ->
20-
[format_error(E) || E <- Errors, Handler:output_error(E)].
21-
22-
-spec format_warnings(module(), warning_list()) ->
23-
[{warning, integer(), string()}].
24-
format_warnings(_Handler, []) ->
25-
[];
26-
format_warnings(Handler, [{_FileName, Warnings} | _]) ->
27-
[format_warning(W) || W <- Warnings, Handler:output_warning(W)].
15+
-spec format_errors(module(), error_list()) -> [error()].
16+
format_errors(Handler, List) ->
17+
lists:flatmap(fun ({FileName, Errors}) ->
18+
[format_error(FileName, E) || E <- Errors, Handler:output_error(E)]
19+
end, List).
20+
21+
-spec format_warnings(module(), warning_list()) -> [warning()].
22+
format_warnings(Handler, List) ->
23+
lists:flatmap(fun ({FileName, Warnings}) ->
24+
[format_warning(FileName, W) || W <- Warnings, Handler:output_warning(W)]
25+
end, List).
2826

2927
%% ===================================================================
3028
%% Internal
3129
%% ===================================================================
3230

33-
format_error({Line, _Module, _Term} = Error) ->
31+
format_error(FileName, {Line, _Module, _Term} = Error) ->
3432
Description = error_description(Error),
3533
FixedLine = fix_line_number(Line),
36-
{error, FixedLine, Description}.
34+
{error, FileName, FixedLine, Description}.
3735

38-
format_warning({Line, _Module, _Term} = Error) ->
36+
format_warning(FileName, {Line, _Module, _Term} = Error) ->
3937
Description = error_description(Error),
4038
FixedLine = fix_line_number(Line),
41-
{warning, FixedLine, Description}.
39+
{warning, FileName, FixedLine, Description}.
4240

4341
-spec error_description({integer(), module(), term()}) -> string().
4442
error_description({_Line, Module, Error}) ->

src/syntaxerl_hrl.erl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,27 @@ check_syntax(FileName, BaseFileName, Debug) ->
4343
{ok, _ModuleName} ->
4444
{ok, []};
4545
{ok, _ModuleName, Warnings} ->
46+
Warnings0 = fix_file_names(NewFileName, FileName, Warnings),
4647
{ok, syntaxerl_format:format_warnings(
47-
?MODULE, fix_line_numbers(Warnings))};
48+
?MODULE, fix_line_numbers(Warnings0))};
4849
{error, Errors, Warnings} ->
50+
Errors0 = fix_file_names(NewFileName, FileName, Errors),
51+
Warnings0 = fix_file_names(NewFileName, FileName, Warnings),
4952
case syntaxerl_format:format_errors(
50-
?MODULE, fix_line_numbers(Errors)) of
53+
?MODULE, fix_line_numbers(Errors0)) of
5154
[] ->
5255
{ok, syntaxerl_format:format_warnings(
53-
?MODULE, fix_line_numbers(Warnings))};
56+
?MODULE, fix_line_numbers(Warnings0))};
5457
Errors2 ->
5558
{error, Errors2 ++ syntaxerl_format:format_warnings(
56-
?MODULE, fix_line_numbers(Warnings))}
59+
?MODULE, fix_line_numbers(Warnings0))}
5760
end
5861
end;
5962
{error, Reason} ->
60-
{error, [{error, file:format_error(Reason)}]}
63+
{error, [{FileName, file:format_error(Reason)}]}
6164
end;
6265
{error, Reason} ->
63-
{error, [{error, file:format_error(Reason)}]}
66+
{error, [{FileName, file:format_error(Reason)}]}
6467
end.
6568

6669
%% skip errors that might occur in pure header files.
@@ -76,6 +79,15 @@ output_warning(_) -> true.
7679
%% Internal
7780
%% ===================================================================
7881

82+
fix_file_names(TmpFileName, FileName, ErrorList) ->
83+
[fix_file_name(TmpFileName, FileName, Error) || Error <- ErrorList].
84+
85+
fix_file_name(TmpFileName, FileName, {TmpFileName, ErrorList}) ->
86+
{FileName, ErrorList};
87+
fix_file_name(_TmpFileName, _FileName, {FileName, ErrorList}) ->
88+
{FileName, ErrorList}.
89+
90+
7991
fix_line_numbers(ErrorList) ->
8092
[{F, [{fix_line_number(L), M, E} || {L, M, E} <- Es]}
8193
|| {F, Es} <- ErrorList].

src/syntaxerl_script.erl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
-export([
77
check_syntax/3,
88
output_error/1,
9-
output_warning/1
9+
output_warning/1,
10+
format_error/2
1011
]).
1112

1213
-include("check_syntax_spec.hrl").
@@ -20,9 +21,25 @@ check_syntax(FileName, _BaseFileName, _Debug) ->
2021
{ok, _} ->
2122
{ok, []};
2223
{error, Error} ->
23-
{error, [{error, file:format_error(Error)}]}
24+
%% unfortunately the `file:consult' returns only the first error.
25+
format_error(FileName, Error)
2426
end.
2527

2628
output_error(_) -> true.
2729

2830
output_warning(_) -> true.
31+
32+
%% ===================================================================
33+
%% Internal
34+
%% ===================================================================
35+
36+
-spec format_error(file:filename(), Error) -> {ok, term()} | {error, error()}
37+
when Error :: file:posix() | badarg | terminated | system_limit |
38+
{Line :: integer(), Mod :: module(), Term :: term()}.
39+
format_error(FileName, {Line, Mod, Term}) ->
40+
ErrorStrPrefix = iolist_to_binary(io_lib:format("~p: ", [Line])),
41+
ErrorStr = iolist_to_binary(file:format_error({Line, Mod, Term})),
42+
[<<>>, ErrorStr0] = binary:split(ErrorStr, ErrorStrPrefix),
43+
{error, [{error, FileName, Line, ErrorStr0}]};
44+
format_error(FileName, Error) ->
45+
{error, [{error, FileName, 1, file:format_error(Error)}]}.

src/syntaxerl_terms.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
%% ===================================================================
1717

1818
check_syntax(FileName, _BaseFileName, _Debug) ->
19-
case file:eval(FileName) of
20-
ok ->
19+
case file:consult(FileName) of
20+
{ok, _} ->
2121
{ok, []};
2222
{error, Error} ->
23-
%% unfortunately the `file:eval' returns only the first error.
24-
{error, [{error, file:format_error(Error)}]}
23+
%% unfortunately the `file:consult' returns only the first error.
24+
syntaxerl_script:format_error(FileName, Error)
2525
end.
2626

2727
output_error(_) -> true.

src/syntaxerl_utils.erl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
-export([
55
incls_deps_opts/1,
6-
print_issues/2,
6+
print_issues/3,
77
consult_file/1
88
]).
99

@@ -134,18 +134,16 @@ deps_opts(BaseDir, OtpStdDirs, ErlcStdOpts, Profile) ->
134134
UniqErlcOpts = uniq(ErlcStdOpts ++ ErlcOpts),
135135
{UniqDepsDirs, UniqErlcOpts}.
136136

137-
-spec print_issues(FileName::file:filename(), Issues::[issue()]) -> ok.
138-
print_issues(_FileName, []) ->
139-
ok;
140-
print_issues(FileName, [Issue | Issues]) ->
141-
print_issue(FileName, Issue),
142-
print_issues(FileName, Issues).
137+
-spec print_issues(FileName::file:filename(), BaseFileName::file:filename(), Issues::[issue()]) -> ok.
138+
print_issues(OrigFileName, BaseFileName, List) ->
139+
List0 = fix_file_names(OrigFileName, BaseFileName, List),
140+
lists:foreach(fun print_issue/1, List0).
143141

144-
print_issue(FileName, {warning, Line, Description}) ->
142+
print_issue({warning, FileName, Line, Description}) ->
145143
io:format("~s:~p: warning: ~s~n", [FileName, Line, Description]);
146-
print_issue(FileName, {error, Description}) ->
144+
print_issue({error, FileName, Description}) ->
147145
io:format("~s:~s~n", [FileName, Description]);
148-
print_issue(FileName, {error, Line, Description}) ->
146+
print_issue({error, FileName, Line, Description}) ->
149147
io:format("~s:~p: ~s~n", [FileName, Line, Description]).
150148

151149
-spec consult_file(file:filename()) -> {ok, term()} | {error, error()}.
@@ -443,6 +441,14 @@ try_consult(File) ->
443441
syntaxerl_logger:debug(true, "Failed to read config file ~s: ~p~n", [File, Reason])
444442
end.
445443

444+
fix_file_names(FileName, BaseFileName, ErrorList) ->
445+
[fix_file_name(FileName, BaseFileName, Error) || Error <- ErrorList].
446+
447+
fix_file_name(FileName, BaseFileName, {Type, FileName, Line, Description}) ->
448+
{Type, BaseFileName, Line, Description};
449+
fix_file_name(_TmpFileName, _FileName, Issue) ->
450+
Issue.
451+
446452
bs(Vars) ->
447453
lists:foldl(fun({K,V}, Bs) ->
448454
erl_eval:add_binding(K, V, Bs)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-define(macros).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-module(include).
2+
3+
-include("bad.hrl").

0 commit comments

Comments
 (0)