aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ic/src/ic_error.erl
blob: 790e1f05394b1c0f562019da58f6ead1851b3061 (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1998-2016. All Rights Reserved.
%% 
%% 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.
%% 
%% %CopyrightEnd%
%%
%%

-module(ic_error).

-include_lib("ic/src/ic.hrl").
-include_lib("ic/src/ic_debug.hrl").

%%-----------------------------------------------------------------
%% External exports
%%-----------------------------------------------------------------
-export([error/2, 
	 fatal_error/2, 
	 init_errors/1, 
	 return/1, 
	 warn/2, 
	 get_error_count/1]).

%%-----------------------------------------------------------------
%% Internal exports
%%-----------------------------------------------------------------
-export([]).

%%-----------------------------------------------------------------
%% External functions
%%-----------------------------------------------------------------

%%--------------------------------------------------------------------
%%
%% Error and warning utilities.
%%
%% Note that errors are somewhat brutal and that warnings are kept in
%% a list for the user to extract at a later stage. The handling of
%% warnings is entirely up to the user while handling of errors is
%% never left to the user.
%%
%%--------------------------------------------------------------------

return(G) ->
    case ic_options:get_opt(G, silent2) of
	true ->
	    case get_error_count(G) of
		0 -> {ok, get_list(G, warn_list)};
		_X -> {error, get_list(G, warn_list), get_list(G, error_list)}
	    end;
	false ->
	    case get_error_count(G) of
		0 -> ok;
		X -> print_error(G, {error, g, ic_genobj:idlfile(G), {error_count, X}}),
		     error
	    end
    end.


get_list(G, ListName) ->
    ?lookup(G#genobj.options, ListName).


%% Public function for reporting an error
error(G, Err) ->
    Error = {error, g, ic_genobj:idlfile(G), Err},
    case insert_in_list(G, Error, error_list) of
	new ->
	    print_error(G, Error),
	    MaxErrs = ic_options:get_opt(G, maxerrs),
	    case incr_counter(G, error_count) of
		X when X >= MaxErrs ->
		    fatal_error(G, {error_count_exceeded, X});
		_ -> Error
	    end;
	old -> 
	    Error
    end.

%% Public function for reporting an error. NOTE: also stops execution
fatal_error(G, Err) ->
    Error = {error, g, ic_genobj:idlfile(G), Err},
    insert_in_list(G, Error, error_list),
    incr_counter(G, error_count),
    print_error(G, Error),
    throw(Error).


%% Public function for reporting a warning
warn(G, Warn) ->
    Warning = {warn, g, ic_genobj:idlfile(G), Warn},
    case insert_in_list(G, Warning, warn_list) of
	new ->
	    print_warn(G, Warning),
	    MaxErrs = ic_options:get_opt(G, maxwarns),
	    case incr_counter(G, warn_count) of
		X when X >= MaxErrs ->
		    fatal_error(G, {warn_count_exceeded, X});
		_ -> ok
	    end;
	old -> ok
end.


%% Initialisation of all counters and lists associated with errors and
%% warnings.
init_errors(G) ->
    reset_counter(G, error_count),
    reset_counter(G, warn_count),
    reset_list(G, error_list),
    reset_list(G, warn_list),
    ok.



%%--------------------------------------------------------------------
%% Counter and list (warn and error) handling
%%

incr_counter(G, Counter) ->
    Num = ?lookup(G#genobj.options, Counter) + 1,
    ?insert(G#genobj.options, Counter, Num),
    Num.

reset_counter(G, Counter) ->
    ?insert(G#genobj.options, Counter, 0).

get_error_count(G) ->
    ?lookup(G#genobj.options, error_count).

reset_list(G, ListName) ->
    ?insert(G#genobj.options, ListName, []).

insert_in_list(G, Item, ListName) ->
    List = ?lookup(G#genobj.options, ListName),
    case lists:member(Item, List) of
	true -> old;
	false ->
	    ?insert(G#genobj.options, ListName, [Item| List]),
	    new
    end.


%%--------------------------------------------------------------------
%% 
%% Nice printouts of errors and warnings
%%


%% Errors

print_error(G, Error) ->
    case {ic_options:get_opt(G, silent), ic_options:get_opt(G, silent2)} of
	{true, _} -> ok;
	{_, true} -> ok;
	_ -> format_error(Error)
    end,
    error.

format_error({error, _, File, {parse_error, Line, Args}}) ->
    Fmt = lists:foldl(fun(_, Acc) -> [$~, $s | Acc] end, [], Args),
    display(File, Line, Fmt, Args);
format_error({error, _, File, {error_count, X}}) ->
    display(File, "~p errors found", [X]);
format_error({error, _, File, {error_count_exceeded, X}}) ->
    display(File, "too many errors found (~p)", [X]);
format_error({error, _, File, {warn_count_exceeded, X}}) ->
    display(File, "too many warnings found (~p)", [X]);
format_error({error, _, File, {inherit_name_collision, 
			       {Orig, Item}, {Base, NewItem}}}) ->
    display(File, ic_forms:get_line(Item), "~s collides with ~s", 
	    [pp([ic_forms:get_id2(Item) | Orig]), pp([ic_forms:get_id2(NewItem) | Base])]);
format_error({error, _, File, {unsupported_op, {'~', Line}}}) ->
    display(File, Line, "unsupported unary operation ~~", []);
format_error({error, _, File, {multiply_defined, X}}) ->
    display(File, ic_forms:get_line(X), "multiple defined identifier ~p", [ic_forms:get_id2(X)]);
format_error({error, _, File, {illegal_spelling, X}}) ->
    display(File, ic_forms:get_line(X), 
%	    "illegal spelling of identifier ~s (capitalisation?)", 
	    "identifier ~p multiply declared - differs in case only",
	    [ic_forms:get_id2(X)]);
format_error({error, _, File, {illegal_enumerant_value, X}}) ->
    display(File, ic_forms:get_line(X), 
	    "Enumerant ~s's value collide by name with other type", 
	    [ic_forms:get_id2(X)]);
format_error({error, _, File, {illegal_forward, X}}) ->
    display(File, ic_forms:get_line(X), 
	    "cannot inherit from forwarded interface ~s", [ic_forms:get_id2(X)]);
format_error({error, _, File, {illegal_const_t, X, Type}}) ->
    display(File, ic_forms:get_line(X), 
	    "Illegal constant type ~s of ~s", [pp(Type), ic_forms:get_id2(X)]);
format_error({error, _, File, {multiple_cases, X}}) ->
    display(File, ic_forms:get_line(X), "multiple case values ~s", [pp(X)]);
format_error({error, _, File, {symtab_not_found, X}}) ->
    display(File, ic_forms:get_line(X), "undeclared identifier ~s", [ic_forms:get_id2(X)]);
format_error({error, _, File, {preproc, Lines}}) ->
    display(File, "preprocessor error: ~s", [hd(Lines)]);
format_error({error, _, File, {ic_pp_error, Lines}}) ->
    display(File, "preprocessor error: ~s", [Lines]);
format_error({error, _, File, {illegal_float, Line}}) ->
    display(File, Line, "illegal floating point number", []);
format_error({error, _, File, {bad_type_combination, E, V1, V2}}) ->
    display(File, ic_forms:get_line(E), "incompatible types, ~p and ~p", [V1, V2]);
format_error({error, _, File, {bad_oneway_type, X, _TK}}) ->
    display(File, ic_forms:get_line(X), "oneway operations must be declared void", []);
format_error({error, _, File, {inout_spec_for_c, X, Arg}}) ->
    display(File, ic_forms:get_line(X), "inout parameter ~s specified in native c mode",
	    [Arg]);
format_error({error, _, File, {sequence_not_defined, X, Arg}}) ->
    display(File, ic_forms:get_line(X), "sequence ~s not defined", [Arg]);
format_error({error, _, File, {illegal_typecode_for_c, Arg}}) ->
    display(File, not_specified, "illegal typecode ~s used in native c mode",
	    [Arg]);
format_error({error, _, File, {name_not_found, N}}) ->
    display(File, not_specified, "name ~s not found", [N]);
format_error({error, _, File, {illegal_typecode_for_c, Arg, N}}) ->
    display(File, not_specified, "illegal typecode ~p used for ~p in native c mode", [Arg, N]);
format_error({error, _, File, {oneway_outparams, X}}) ->
    display(File, ic_forms:get_line(X), 
	    "oneway operations may not have out or inout parameters", []);
format_error({error, _, File, {oneway_raises, X}}) ->
    display(File, ic_forms:get_line(X), "oneway operations may not raise exceptions",
	    []);
format_error({error, _, File, {bad_tk_match, T, TK, V}}) ->
    display(File, ic_forms:get_line(T),
	    "value ~p does not match declared type ~s", [V, pp(TK)]);
format_error({error, _, File, {bad_scope_enum_case, ScopedId}}) ->
    display(File, ic_forms:get_line(ScopedId),
	    "scoped enum identifiers not allowed as case (~s)", 
	    [pp(ScopedId)]);
format_error({error, _, File, {bad_type, Expr, Op, _TypeList, V}}) ->
    display(File, ic_forms:get_line(Expr),
	    "parameter value ~p to ~s is of illegal type", [V, pp(Op)]);
format_error({error, _, File, {bad_case_type, TK, X, Val}}) ->
    display(File, ic_forms:get_line(X), 
	    "case value ~s does not match discriminator type ~s", 
	    [case_pp(X, Val), pp(TK)]);
format_error({error, _, File, {tk_not_found, X}}) ->
    display(File, ic_forms:get_line(X), "undeclared identifier ~s", [pp(X)]);
%%% New format_errors
format_error({error, _, File, {bad_fixed, Format, Args, Line}}) ->
    display(File, Line, Format, Args);
format_error({error, _, File, {illegal_switch_t, Arg, _N}}) ->
    display(File, ic_forms:get_line(Arg), "illegal switch", []);  
format_error({error, _, File, {inherit_resolve, Arg, N}}) ->
    display(File, ic_forms:get_line(Arg), "cannot resolve ~s", [N]);
format_error({error, _, File, {bad_escape_character, Line, Char}}) ->
    display(File, Line, "bad escape character \"~c\"", [Char]);  
format_error({error, _, File, {pragma_code_opt_bad_option_list, Line}}) ->
    display(File, Line, "bad option list on pragma \"CODEOPT\"", []);
format_error({error, _, File, {bad_string, Line}}) ->
    display(File, Line, "bad string", []);
format_error({error, _, File, {create_dir, Path, Reason}}) ->
    display(File, not_specified, "couldn't create directory ~p due to ~p", [Path, Reason]);
format_error({error, _, File, {open_file, Path, Reason}}) ->
    display(File, not_specified, "couldn't open ~p due to ~p", [Path, Reason]);
format_error({error, _, File, {plain_error_string, ErrString}}) ->
    display(File, not_specified, "~s", [ErrString]);
format_error({error, _, File, {plain_error_string, T, ErrString}}) ->
    display(File, ic_forms:get_line(T), "~s", [ErrString]);
format_error({error, _, File, {ErrString, Line}}) ->
    display(File, Line, ErrString, []).


%% Warnings
print_warn(G, Warn) ->
    case {ic_options:get_opt(G, silent), ic_options:get_opt(G, silent2)} of
	{true, _} -> ok;
	{_, true} -> ok;
	_ -> format_warn(Warn)
    end.

format_warn({warn, _, File, {ic_pp_warning, Lines}}) ->
    display(File, "preprocessor warning: ~s", [Lines]);
format_warn({warn, _, _File, {cfg_open, _Reason, File}}) ->
    display(File, "warning: could not open file: ~p", [File]);
format_warn({warn, _, _File, {cfg_read, File}}) ->
    display(File, "warning: syntax error in configuration file", []);
format_warn({warn, _, File, {multi_modules, Id}}) ->
    display(File, ic_forms:get_line(Id), "warning: multiple modules in file", []);
format_warn({warn, _, File, {illegal_opt, Opt}}) ->
    display(File, "warning: unrecognised option: ~p", [Opt]);
format_warn({warn, _, File, {nested_mod, Id}}) ->
    display(File, ic_forms:get_line(Id), "warning: nested module: ~s", [ic_forms:get_id(Id)]);
format_warn({warn, _, File, {inherit_name_shadow, {Orig, Item}, 
			     {Base, NewItem}}}) ->
    display(File, ic_forms:get_line(Item), 
	    "warning: ~s shadows ~s", [pp([ic_forms:get_id2(Item) | Orig]),
				       pp([ic_forms:get_id2(NewItem) | Base])]);
format_warn({warn, _, File, {internal_307, X, Y}}) ->
    %% If global Scope variable is not [] at top level constant
    display(File, ic_forms:get_line(X), "warning: internal 307: ~p ~p", [X, Y]);
format_warn({warn, _, File, {WarnString, Line}}) ->
    display(File, Line, WarnString, []).

%% Display an error or warning
display(File, not_specified, F, A) ->
    io:format("~p : ~s~n", [File, io_lib:format(F, A)]);
display(File, Line, F, A) ->
    io:format("~p on line ~p: ~s~n", [File, Line, io_lib:format(F, A)]).
display(File, F, A) ->
    io:format("~p: ~s~n", [File, io_lib:format(F, A)]).



%%format_warn2(G, WarnStr) ->
%%    case {ic_options:get_opt(G, silent), ic_options:get_opt(G, silent2), 
%%          ic_options:get_opt(G, nowarn)} of
%%	{false, false, false} ->
%%	    io:format("~p: warning: ~s~n", [ic_genobj:idlfile(G), WarnStr]);
%%	_ -> ok
%%    end.

%%format_warn2(G, Line, WarnStr) ->
%%    case {ic_options:get_opt(G, silent), ic_options:get_opt(G, silent2),
%%          ic_options:get_opt(G, nowarn)} of
%%	{false, false, false} ->
%%	    io:format("~p on line ~p: warning: ~s~n", 
%%		      [ic_genobj:idlfile(G), Line, WarnStr]);
%%	_ -> ok
%%    end.




%% pretty print various stuff

pp({tk_string, _}) -> "string";
pp({tk_wstring, _}) -> "wstring";
pp(tk_long) -> "long";
pp(tk_short) -> "short";
pp(tk_ushort) -> "unsigned short";
pp(tk_ulong) -> "unsigned long";
pp(tk_float) -> "float";
pp(tk_double) -> "double";
pp(tk_boolean) -> "boolean";
pp(tk_char) -> "char";
pp(tk_wchar) -> "wchar";
pp(tk_octet) -> "octet";
pp(tk_null) -> "null";
pp(tk_void) -> "void";
pp(tk_any) -> "any";
pp({tk_fixed, _, _}) -> "fixed";
pp({tk_objref, _, _}) -> "object reference";
pp(rshift) -> ">>";
pp(lshift) -> "<<";
pp(X) when element(1, X) == tk_enum -> "enum";
pp(X) when is_record(X, scoped_id) -> ic_util:to_colon(X);
pp(X) when element(1, X) == '<identifier>' -> ic_forms:get_id(X);
pp(X) when is_list(X) andalso is_list(hd(X)) -> ic_util:to_colon(X);
pp({_, Num, Beef}) when is_integer(Num) -> Beef;
pp({Beef, Num}) when is_integer(Num) -> ic_util:to_list(Beef);
pp(X) -> ic_util:to_list(X).

%% special treatment of case label names
case_pp(X, _Val) when is_record(X, scoped_id) -> pp(X);
case_pp(_X, Val) -> pp(Val).



%%-----------------------------------------------------------------
%% Internal functions
%%-----------------------------------------------------------------