1 | | % (c) 2009-2025 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, |
2 | | % Heinrich Heine Universitaet Duesseldorf |
3 | | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) |
4 | | |
5 | | |
6 | | :- module(error_manager,[init_error_manager/0, |
7 | | add_warning/2, add_warning/3, add_warning/4, |
8 | | reset_errors/0, clear_wd_errors/0, |
9 | | add_error/2, add_error/3, add_error/4, |
10 | | add_error_and_fail/3, add_error_and_fail/2, |
11 | | add_error_fail/3, |
12 | | add_failed_call_error/1, add_internal_error/2, add_internal_error/4, |
13 | | register_optional_error_or_warning/1, reset_optional_errors_or_warnings/0, |
14 | | assert_true/1, |
15 | | add_debug_message/3, add_debug_message/4, |
16 | | add_msg_warn_or_err/5, |
17 | | add_message/2, add_message/3, add_message/4, |
18 | | add_all_perrors/1, add_all_perrors/2, add_all_perrors/3, add_all_perrors/4, |
19 | | get_perror/4, |
20 | | no_real_perror_occurred/1, |
21 | | get_all_errors/1, tcltk_get_all_errors/4, |
22 | | get_tk_table_position_info/2, |
23 | | get_all_errors_and_reset/1, % call if you want to start with a fresh top-level error scope |
24 | | get_all_errors_with_span_info_and_reset/1, % ditto + obtain location info; excludes messages |
25 | | get_all_errors_with_span_and_context_and_reset/1, % ditt + global context and call stack |
26 | | get_all_errors_and_clear/1, |
27 | | get_error/2, get_error_with_span/3, |
28 | | get_warning/2, check_error_occured/2, check_warning_occured/2, |
29 | | get_error_span_linecol/5, get_error_span_linecol/4, |
30 | | simplify_span/2, |
31 | | tk_get_error_span_linecol_for_main_file/4, |
32 | | register_error_span/2, |
33 | | check_error_span_file_linecol/6, |
34 | | position_is_not_in_main_file/1, |
35 | | error_or_warning_occured/0, |
36 | | real_error_occurred/0, reset_real_error_occurred/0, assert_real_error_occurred/1, |
37 | | warning_occurred/0, % true if a warning was raised |
38 | | wd_error_occured/0, |
39 | | test_error_occurred/1, count_errors_occurred/2, |
40 | | get_total_number_of_errors/1, |
41 | | reset_error_spans/0, |
42 | | |
43 | | backup_errors/0, backed_up_error/4, |
44 | | |
45 | | set_error_context/1, clear_error_context/0, |
46 | | get_error_context/1, restore_error_context/1, |
47 | | print_error_context/0, |
48 | | |
49 | | extract_span_description/2, extract_symbolic_label_pos/2, extract_symbolic_label/2, |
50 | | extract_line_col/5, extract_file_line_col/6, |
51 | | extract_all_line_col/5, |
52 | | extract_line_col_for_main_file/5, |
53 | | extract_subsidiary_tail_file_name/2, extract_tail_file_name/2, |
54 | | extract_file_number_and_name/3, |
55 | | extract_additional_description/2, extract_pos_context/4, |
56 | | extract_definition_call_stack/2, |
57 | | print_error_span/1, print_message_span/1, |
58 | | |
59 | | turn_show_error_source_on/0, turn_show_error_source_off/0, |
60 | | |
61 | | enter_new_error_scope/1, enter_new_error_scope/2, |
62 | | enter_new_clean_error_scope/1, |
63 | | get_current_error_scope/1, |
64 | | exit_error_scope/2, exit_error_scope/3, |
65 | | error_occurred_at_level/1, specific_event_occurred_at_level/2, |
66 | | %throw_errors_in_scope/3, |
67 | | add_new_event_in_error_scope/1, add_new_event_in_error_scope/2, |
68 | | event_occurred_in_error_scope/1, error_occurred_in_error_scope/0, |
69 | | clear_events_in_error_scope/2, clear_events_in_current_error_scope/1, |
70 | | translate_error_event/2, |
71 | | clear_enumeration_warning_in_error_scope/1, |
72 | | clear_all_errors_in_error_scope/1, |
73 | | call_in_fresh_error_scope_for_one_solution/1, |
74 | | enumeration_warning_occured_in_error_scope/0, |
75 | | is_enumeration_warning_exception/1, |
76 | | abort_error_occured_in_error_scope/0, |
77 | | on_enumeration_warning/2, on_enumeration_warning_with_continue/3, |
78 | | throw_enumeration_warnings_in_current_scope/0, |
79 | | throw_enumeration_warnings_in_scope/3, |
80 | | catch_enumeration_warning_exceptions/2, catch_enumeration_warning_exceptions/3, |
81 | | catch_enumeration_warning_exceptions/4, catch_enumeration_warning_exceptions_and_wd_failures/5, |
82 | | call_with_enumeration_warning/1, |
83 | | observe_enumeration_warnings/2, |
84 | | critical_enumeration_warning_occured_in_error_scope/4, |
85 | | critical_enumeration_warning_occured_in_error_scope/0, |
86 | | virtual_time_out_occured_in_error_scope/1, % variation which returns a virtual_time_out(.) term |
87 | | clear_enumeration_warnings/0, |
88 | | translate_events_in_current_scope_to_warnings/2, |
89 | | time_out_with_enum_warning_one_solution/3, % use this for finding one solution |
90 | | time_out_with_enum_warning_one_solution/4, |
91 | | time_out_with_enum_warning_one_solution_no_new_error_scope/3, |
92 | | is_time_out_result/1, |
93 | | time_out_with_enum_warning_for_findall/3, % use this if you cycle through all solutions |
94 | | time_out_with_enum_warning_for_findall_in_current_error_scope/4, |
95 | | get_virtual_time_out_from_exception/2, % used by time_out_with_enum_warning_for_findall |
96 | | clpfd_overflow_msg/1, |
97 | | is_clpfd_overflow_representation_error/2, is_clpfd_overflow_representation_error_msg/1, |
98 | | logged_error/4, |
99 | | display_error_statistics/0, |
100 | | set_error_manager_silent_mode/1 |
101 | | ]). |
102 | | |
103 | | % meta_predicate annotations should appear before loading any code: |
104 | | :- meta_predicate add_failed_call_error(0). |
105 | | :- meta_predicate add_internal_error(-,:). |
106 | | :- meta_predicate add_new_event_in_error_scope(-,2). |
107 | | |
108 | | :- meta_predicate on_enumeration_warning(0,0). |
109 | | :- meta_predicate on_enumeration_warning_with_continue(0,0,0). |
110 | | |
111 | | :- meta_predicate catch_enumeration_warning_exceptions(0,0). |
112 | | :- meta_predicate catch_enumeration_warning_exceptions(0,0,-). |
113 | | :- meta_predicate catch_enumeration_warning_exceptions(0,0,-,-). |
114 | | :- meta_predicate catch_enumeration_warning_exceptions_and_wd_failures(0,0,-,0,-). |
115 | | |
116 | | :- meta_predicate observe_enumeration_warnings(0,0). |
117 | | |
118 | | :- meta_predicate time_out_with_enum_warning_one_solution(0,-,-). |
119 | | :- meta_predicate time_out_with_enum_warning_one_solution(0,-,-,-). |
120 | | |
121 | | :- meta_predicate call_in_fresh_error_scope_for_one_solution(0). |
122 | | |
123 | | :- meta_predicate call_with_enumeration_warning(0). |
124 | | |
125 | | :- meta_predicate time_out_with_enum_warning_one_solution_no_new_error_scope(0,-,-). |
126 | | |
127 | | :- meta_predicate time_out_with_enum_warning_for_findall(0,-,-). |
128 | | :- meta_predicate time_out_with_enum_warning_for_findall2(-,0,-,-). |
129 | | |
130 | | :- meta_predicate time_out_with_enum_warning_for_findall_in_current_error_scope(-,0,-,-). |
131 | | |
132 | | :- meta_predicate assert_true(0). |
133 | | :- meta_predicate catch_wd(-,0,-). |
134 | | |
135 | | % ------------ |
136 | | |
137 | | :- use_module(library(lists)). |
138 | | |
139 | | :- use_module(tools,[get_tail_filename/2]). |
140 | | :- use_module(tools_lists,[count_occurences/2]). |
141 | | :- use_module(tools_printing,[print_error/1,print_message_on_stream/3,format_error_with_nl/2,format_with_colour/4]). |
142 | | :- use_module(tools_strings,[ajoin/2, string_concatenate/3]). |
143 | | |
144 | | :- use_module(module_information,[module_info/2]). |
145 | | :- module_info(group,infrastructure). |
146 | | :- module_info(description,'The error_manager controls error messages. Error messages can be added, retrieved, counted,etc. The module also takes care of extracting source spans from error messages.'). |
147 | | |
148 | | %:- use_module(translate). |
149 | | :- use_module(debug, [debug_mode/1, debug_println/1, debug_println/2, debug_format/3]). |
150 | | :- use_module(preferences,[get_preference/2,reset_temporary_preference/2, |
151 | | temporary_set_preference/3]). |
152 | | |
153 | | |
154 | | :- use_module(extension('counter/counter'), |
155 | | [counter_init/0, new_counter/1, get_counter/2, inc_counter/2, set_counter/2]). |
156 | | |
157 | | % cur_block_level is now stored as a C counter, which needs to be initialised |
158 | | init_error_manager :- counter_init, new_counter(cur_block_level). |
159 | | |
160 | | |
161 | | :- dynamic optional_error_or_warning/1. |
162 | | register_optional_error_or_warning(Type) :- |
163 | | (optional_error_or_warning(Type) -> true ; assertz(optional_error_or_warning(Type))). |
164 | | reset_optional_errors_or_warnings :- retractall(optional_error_or_warning(_)). |
165 | | |
166 | | % INTERNAL ERRORS |
167 | | |
168 | | |
169 | | add_failed_call_error(Call) :- !, |
170 | | add_internal_error('Internal Error; call failed: ',Call). |
171 | | |
172 | | add_internal_error(Module,Msg,Term,Span) :- |
173 | | add_error(internal_error(Module),Msg,Term,Span). |
174 | | add_internal_error(Msg,Module:Call) :- !, |
175 | | add_error(internal_error(Module),Msg,Call). |
176 | | add_internal_error(Msg,Call) :- |
177 | | add_error(internal_error(unknown_module),Msg,Call). |
178 | | |
179 | | |
180 | | % ERROR SCOPING |
181 | | |
182 | | %:- dynamic cur_block_level/1. |
183 | | %cur_block_level(0). % no realised as a C counter |
184 | | :- volatile error_occurred_at_level/1. |
185 | | :- dynamic error_occurred_at_level/1. |
186 | | :- volatile throw_error/3. |
187 | | :- dynamic throw_error/3. |
188 | | :- volatile do_not_print_error/2. |
189 | | :- dynamic do_not_print_error/2. |
190 | | |
191 | | |
192 | | get_current_error_scope(Level) :- get_counter(cur_block_level,Level). |
193 | | |
194 | | enter_new_error_scope(Level,_PP) :- |
195 | | enter_new_error_scope(Level). %, tools_printing:print_term_summary(enter(Level,PP)),nl. |
196 | | enter_new_error_scope(Level) :- |
197 | | enter_new_clean_error_scope(Level), |
198 | | L1 is Level - 1, |
199 | | copy_throw_errors(L1,Level). |
200 | | copy_throw_errors(L1,L) :- |
201 | ? | throw_error(L1,Error,Span), %print(copy(throw(L1,L,Error))),nl, |
202 | | assertz(throw_error(L,Error,Span)),fail. |
203 | | copy_throw_errors(L1,L) :- |
204 | ? | do_not_print_error(L1,Error), |
205 | | assertz(do_not_print_error(L,Error)),fail. |
206 | | copy_throw_errors(_,_). |
207 | | enter_new_clean_error_scope(Level) :- |
208 | | inc_counter(cur_block_level,L1), |
209 | ? | (retract(throw_error(L1,_,_)) -> retractall(throw_error(L1,_,_)) ; true), % DO WE NEED THIS ?? |
210 | | (retract(do_not_print_error(L1,_)) -> retractall(do_not_print_error(L1,_)) ; true), |
211 | | % print(enter(L1)),nl,trace, |
212 | | Level=L1. |
213 | | |
214 | | |
215 | | % mark that everything unifying with Error should be thrown rather than collected |
216 | | throw_errors_in_scope(Level,Error,Span) :- |
217 | | (\+ throw_error(Level,Error,Span) % TO DO: check that we are an instance of existing error ! |
218 | | -> assertz(throw_error(Level,Error,Span)) |
219 | | ; true). |
220 | | |
221 | | % state that this event/error should not be printd |
222 | | silent_enum_warnings_in_scope(Level,Critical) :- |
223 | | Error = enumeration_warning(_,_,_,_,Critical), |
224 | | assertz(do_not_print_error(Level,Error)). |
225 | | |
226 | | clear_all_errors_in_error_scope(Level) :- %print(clearing(Level)),nl, |
227 | | clear_events_in_error_scope(Level,_), |
228 | | error_occurred_at_level(L1), L1>Level, %print(clearing(L1)),nl, |
229 | | clear_events_in_error_scope(Level,_),fail. |
230 | | clear_all_errors_in_error_scope(_). |
231 | | |
232 | | % useful in call_cleanup; when error block may already be exited |
233 | | exit_error_scope_if_necessary(Level,PP) :- get_counter(cur_block_level,CurLevel), |
234 | | (Level =< CurLevel |
235 | | -> exit_error_scope(Level,_,PP) %,print(exit(Level,CurLevel,PP)),nl |
236 | | ; true). |
237 | | |
238 | | exit_error_scope(Level,ErrorOcc) :- exit_error_scope(Level,ErrorOcc,unknown). |
239 | | exit_error_scope(Level,ErrorOcc,PP) :- get_counter(cur_block_level,CurL), |
240 | | (CurL = Level -> NewL is CurL-1 |
241 | | ; Level < CurL -> NewL is Level-1, |
242 | | %print_error_scopes, |
243 | | format('*** exiting multiple error blocks (~w -> ~w) for ~w ***~n',[CurL,NewL,PP]) |
244 | | ; format('*** block ~w already exited (currently at ~w) for ~w ***~n',[Level,CurL,PP]), |
245 | | NewL is CurL |
246 | | ), set_counter(cur_block_level,NewL), |
247 | | copy_error_scope_events(CurL,NewL), |
248 | ? | (retract(throw_error(CurL,_,_)) -> retractall(throw_error(CurL,_,_)) ; true), |
249 | ? | (retract(do_not_print_error(CurL,_)) -> retractall(do_not_print_error(CurL,_)) ; true), |
250 | | (retract(error_occurred_at_level(CurL)) |
251 | | -> ErrorOcc=true, |
252 | | (error_occurred_at_level(NewL) -> true ; assertz(error_occurred_at_level(NewL))) |
253 | | ; ErrorOcc=false). |
254 | | %indent(Level), format('exit level ~w for ~w, cur level ~w, error occ = ~w~n',[Level,PP,CurL,ErrorOcc]). |
255 | | |
256 | | |
257 | | % versions of enter_new_error_scope and exit_error_scope which re_enter blocks and clean up upon backtracking |
258 | | %enter_new_error_scope_bt(Level,PP) :- enter_new_error_scope(Level,PP), exit_upon_fail(Level,PP). |
259 | | %exit_upon_fail(_,_). |
260 | | %exit_upon_fail(Level,PP) :- print(exit_upon_backtrack(Level,PP)),nl, |
261 | | % exit_error_scope(Level,_,PP),fail. |
262 | | % |
263 | | %exit_error_scope_bt(Level,ErrorOcc,PP) :- exit_error_scope(Level,ErrorOcc,PP). |
264 | | %exit_error_scope_bt(Level,_ErrorOcc,PP) :- print(re_enter_block(Level,PP)),nl, |
265 | | % enter_new_error_scope(Level2,PP), |
266 | | % (Level2=Level -> true ; print(mismatch_when_reentering_block(Level,PP)),nl), |
267 | | % fail. |
268 | | |
269 | | % --------------------------- |
270 | | |
271 | | |
272 | | |
273 | | assert_real_error_in_current_block(Source) :- get_counter(cur_block_level,L),L>0, |
274 | | %print(assert_real_error_in_current_block(L)),nl, |
275 | | (specific_event_occurred_at_level(L,Source) -> true |
276 | | ; assertz(specific_event_occurred_at_level(L,Source))), % ensures that clear_events_in_current_error_scope does not remove error_occurred_at_level if used for enumeration warnings, ... |
277 | | \+ error_occurred_at_level(L), assertz(error_occurred_at_level(L)),!. |
278 | | assert_real_error_in_current_block(_). |
279 | | |
280 | | |
281 | | reset_error_scopes :- |
282 | | (set_counter(cur_block_level,0) -> true |
283 | | ; format(user_error,'~n*** set_counter(cur_block_level,0) failed!!~n',[]),fail |
284 | | ), % try debug random gitlab failures; something really bad happened |
285 | | retractall(error_occurred_at_level(_)), |
286 | | retractall(specific_event_occurred_at_level(_,_)). |
287 | | |
288 | | % for debugging / portraying error scopes: |
289 | | % portray_error_scopes |
290 | | :- public print_error_scopes/0. |
291 | | print_error_scopes :- |
292 | | if(get_counter(cur_block_level,L), |
293 | | format(' Cur Block Level = ~w~n',[L]), |
294 | | format(' *** NO Cur Block Level Information !!',[]) |
295 | | ), |
296 | | fail. |
297 | | print_error_scopes :- |
298 | | findall(L,error_occurred_at_level(L),LL), |
299 | | format(' Errors occured at those levels: ~w~n',[LL]), |
300 | | format(' Specific events at levels:~n',[]), |
301 | | specific_event_occurred_at_level(Level,Err), |
302 | | format(' ~w --> ~w~n',[Level,Err]), |
303 | | fail. |
304 | | print_error_scopes. |
305 | | |
306 | | % call a predicate in fresh error scope (assuming one solution is searched for only; failure is also ok) |
307 | | % and then discard all errors |
308 | | call_in_fresh_error_scope_for_one_solution(Call) :- |
309 | | enter_new_error_scope(Level,call_in_fresh_error_scope_for_one_solution), |
310 | | call_cleanup(Call, |
311 | | (clear_all_errors_in_error_scope(_), |
312 | | exit_error_scope(Level,_,call_in_fresh_error_scope_for_one_solution))). |
313 | | |
314 | | :- volatile specific_event_occurred_at_level/2. |
315 | | :- dynamic specific_event_occurred_at_level/2. |
316 | | |
317 | | % can be used to keep track of whether specific events occurred within an error_scope |
318 | | add_new_event_in_error_scope(Event) :- |
319 | | add_new_event_in_error_scope(Event,do_nothing). |
320 | | do_nothing(_,_). |
321 | | add_new_event_in_error_scope(Event,PrintMessageCall) :- |
322 | | get_counter(cur_block_level,L), |
323 | ? | (specific_event_occurred_at_level(L,Event) -> fail |
324 | ? | ; (throw_error(L,Event,Span) |
325 | | -> (debug_mode(off) -> true ; print_message(informational,throwing(Event))), % trace, % <--- |
326 | | (do_not_print_error(L,Event), debug_mode(off) -> true ; call(PrintMessageCall,throwing,Span)), |
327 | | % Note: event is not asserted ! See e.g. tests 1089, 1519, 1522, 1661, 1199, 1291, ... |
328 | | throw(Event) |
329 | | ; assertz(specific_event_occurred_at_level(L,Event)), |
330 | | (do_not_print_error(L,Event), debug_mode(off) -> true ; call(PrintMessageCall,not_throwing,unknown)) |
331 | | ) |
332 | | ). |
333 | | |
334 | | % call if you just want to add the error information; without throwing |
335 | | assert_event_in_error_scope_without_throw(Event) :- get_counter(cur_block_level,L), |
336 | | (specific_event_occurred_at_level(L,Event) -> true |
337 | | ; assertz(specific_event_occurred_at_level(L,Event))). |
338 | | |
339 | | error_occurred_in_error_scope :- get_counter(cur_block_level,L),error_occurred_at_level(L). |
340 | ? | event_occurred_in_error_scope(Event) :- get_counter(cur_block_level,L),specific_event_occurred_at_level(L,Event). |
341 | | |
342 | ? | copy_error_scope_events(FromLevel,ToLevel) :- retract(specific_event_occurred_at_level(FromLevel,Event)), |
343 | ? | \+ specific_event_occurred_at_level(ToLevel,Event), |
344 | | % print(copying_event(FromLevel,ToLevel,Event)),nl, |
345 | | assertz(specific_event_occurred_at_level(ToLevel,Event)), % should we throw if this level demands it ? |
346 | | (throw_error(ToLevel,Event,_Span) |
347 | | -> debug_println(20,throw_copied_event(Event)), throw(Event)), |
348 | | fail. |
349 | | copy_error_scope_events(_,_). |
350 | | |
351 | | clear_events_in_current_error_scope(Event) :- get_counter(cur_block_level,L),clear_events_in_error_scope(L,Event). |
352 | | |
353 | | %clear_events_in_error_scope(L,Event) :-retract(specific_event_occurred_at_level(L,Event)), print(retract(L,Event)),nl,fail. |
354 | | |
355 | | clear_enumeration_warning_in_error_scope(Level) :- |
356 | | clear_events_in_error_scope(Level,enumeration_warning(_,_,_,_,_)). |
357 | | |
358 | | clear_events_in_error_scope(L,Event) :- |
359 | ? | (specific_event_occurred_at_level(L,_) |
360 | | -> retractall(specific_event_occurred_at_level(L,Event)), |
361 | ? | (specific_event_occurred_at_level(L,_) -> true % we have not removed all events |
362 | | ; retract(error_occurred_at_level(L)) -> true |
363 | | ; true |
364 | | ) |
365 | | ; true). % no errors can have been there |
366 | | |
367 | | throw_enumeration_warnings_in_current_scope :- |
368 | | get_counter(cur_block_level,L), throw_enumeration_warnings_in_scope(L,_,unknown). |
369 | | |
370 | | throw_enumeration_warnings_in_scope(Level,Critical,Span) :- |
371 | | simplify_span(Span,SimplifiedSpan), % relevant for test 1562 |
372 | | throw_errors_in_scope(Level,enumeration_warning(_,_,_,_,Critical),SimplifiedSpan). |
373 | | |
374 | | on_enumeration_warning(Call, BackupCall) :- |
375 | ? | on_enumeration_warning_with_continue(Call, BackupCall, true). |
376 | | % sets up a new error scope and forces enumeration_warnings to be thrown |
377 | | % if an enumeration_warning happens the BackupCall is executed |
378 | | on_enumeration_warning_with_continue(Call, _, _) :- is_true_call(Call),!. % happens e.g., for card_for_specific_closure3 |
379 | | on_enumeration_warning_with_continue(Call, BackupCall, TrueContinuation) :- |
380 | | enter_new_error_scope(Level,on_enumeration_warning_with_continue), |
381 | | throw_enumeration_warnings_in_scope(Level,_,unknown), |
382 | ? | call_cleanup( |
383 | | catch( |
384 | | if(Call, |
385 | | (exit_error_scope(Level,_,on_enumeration_warning_with_continue_true), |
386 | | % TODO: we need a version that re-instates error_scope upon backtracking?! |
387 | | call(TrueContinuation)), |
388 | | (exit_error_scope(Level,_,on_enumeration_warning_with_continue_false),fail) |
389 | | ), |
390 | | Exc, |
391 | | (exit_error_scope(Level,_,on_enumeration_warning_with_continue_exception), % We do not catch timeouts |
392 | | (Exc=enumeration_warning(_,_,_,_,_) -> call(BackupCall) ; throw(Exc)) |
393 | | ) |
394 | | ), |
395 | | exit_error_scope_if_necessary(Level,on_enumeration_warning_with_continue_timeout) % TIMEOUT |
396 | | ). |
397 | | |
398 | | % check if we are simply calling on_enumeration_warning with trivial true call; avoid call_cleanup, error_scope. |
399 | | is_true_call(_:X) :- !, nonvar(X), is_true_call(X). |
400 | | is_true_call(call(X)) :- !, is_true_call(X). |
401 | | is_true_call(true). |
402 | | |
403 | | catch_enumeration_warning_exceptions(Call, BackupCall) :- |
404 | ? | catch_enumeration_warning_exceptions_and_wd_failures(Call,BackupCall,false,fail,unknown). |
405 | | catch_enumeration_warning_exceptions(Call, BackupCall, AddAsEventError) :- |
406 | | catch_enumeration_warning_exceptions_and_wd_failures(Call,BackupCall,AddAsEventError,fail,unknown). |
407 | | catch_enumeration_warning_exceptions(Call, BackupCall, AddAsEventError,Source) :- |
408 | ? | catch_enumeration_warning_exceptions_and_wd_failures(Call,BackupCall,AddAsEventError,fail,Source). |
409 | | |
410 | | % can be used to check if an exception is an enumeration warning |
411 | | is_enumeration_warning_exception(enumeration_warning(_Enumerating,_Type,_Range,_RestrRange,_Critical)). |
412 | | |
413 | | % removed usage of call_cleanup; can cause huge runtime in debug_mode, cf SPRM-21383 but also e.g. for ProPlanGross.mch |
414 | | % but simple_call_cleanup requires that C does not backtrack and CleanupCall called in case of exception |
415 | | % does not catch timeouts, relevant for test 2355 |
416 | | % but using call_cleanup in SICStus 4.9 for PlanProGross.mch results in timeout after several minutes in setup constants |
417 | | :- meta_predicate simple_call_cleanup(0,0). |
418 | ? | simple_call_cleanup(C,CleanupCall) :- if(C,CleanupCall, (CleanupCall,fail)). |
419 | | |
420 | | % AddAsEventError = true means that caught exceptions are added as event_errors in scope |
421 | | % (this can be important if we later want to decide whether an enumeration warning occured) |
422 | | % WDCall: this gets called when a well_definedness_error is raised by the Call |
423 | | % assumes Call does not backtrack to use simple_call_cleanup |
424 | | catch_enumeration_warning_exceptions_and_wd_failures(Call, BackupCall,AddAsEventError,WDCall,Source) :- |
425 | | enter_new_error_scope(Level,catch_enumeration_warning_exceptions), |
426 | | % print(enter(Level)),nl, |
427 | | % difference: we do not force to throw_errors_in_scope |
428 | | (Source = ignore(_) -> silent_enum_warnings_in_scope(Level,critical) ; true), |
429 | ? | catch( |
430 | | simple_call_cleanup(if(Call,true,catch_wd(Level,WDCall,Source)), |
431 | | (%print(exit(Level)),nl, |
432 | | exit_error_scope(Level,_,catch_enumeration_warning_exceptions))), |
433 | | % call_cleanup can be tricky in case of success of Call: the error scope may not be exited immediately; TO DO: maybe rewrite using same style as above (using if + catch) |
434 | | enumeration_warning(A,B,C,D,E), |
435 | | (exit_error_scope(Level,_,catch_enumeration_warning_exceptions), % added due to simple_call_cleanup |
436 | | (AddAsEventError=false |
437 | | -> (Source=ignore(Src) |
438 | | -> debug_format(19,'%%% Ignoring unbounded enumeration warning (source:~w).~n',[Src]) |
439 | | ; true) |
440 | | % inform that enum warning was not serious: there is a fall-back solution |
441 | | ; assert_event_in_error_scope_without_throw(enumeration_warning(A,B,C,D,E))), |
442 | | BackupCall)). |
443 | | |
444 | | catch_wd(Level,WDCall,Source) :- |
445 | | WDCall \= fail, |
446 | | % print(check_wd(Level,Source)),nl, portray_events_in_error_scope,nl, |
447 | | specific_event_occurred_at_level(Level,well_definedness_error),!, |
448 | | formatsilentmsg(user_output,'%%% well_definedness_error (source:~w).~n',[Source]), |
449 | | call(WDCall). |
450 | | catch_wd(_,_,_) :- fail. |
451 | | |
452 | | |
453 | | % execute some code when enumeration warning occurs; and then throw exception again |
454 | | observe_enumeration_warnings(Call,ObserverCallToExecute) :- |
455 | | catch(Call, enumeration_warning(A,B,C,D,E), |
456 | | call_cleanup(ObserverCallToExecute, throw(enumeration_warning(A,B,C,D,E)))). |
457 | | |
458 | | |
459 | | % sets up a new error scope and forces enumeration_warnings to be thrown |
460 | | % the exceptions are not caught ! |
461 | | call_with_enumeration_warning(Call) :- |
462 | | enter_new_error_scope(Level,call_with_enumeration_warning), |
463 | | throw_enumeration_warnings_in_scope(Level,_,unknown), |
464 | | call_cleanup( |
465 | | (Call, % the assumption is that Call will only have one solution |
466 | | exit_error_scope(Level,_,call_with_enumeration_warning_true)), |
467 | | exit_error_scope_if_necessary(Level,call_with_enumeration_warning_timeout) % failure, TIMEOUT or Exception |
468 | | ). |
469 | | |
470 | | |
471 | | % a version of time_out which treats enumeration_warnings like time-outs |
472 | | % and also checks enumeration_warnings in case of failure |
473 | | % assumes we only look for one solution (otherwise error scope cleanup has to be changed !! ) |
474 | | time_out_with_enum_warning_one_solution(Call,TO,TimeOutRes,ClearEnumWarnings) :- |
475 | | enter_new_error_scope(Level,time_out_with_enum_warning_one_solution), |
476 | | call_cleanup( |
477 | | catch( |
478 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,ClearEnumWarnings,perform_cut), |
479 | | enumeration_warning(A,B,C,D,E), |
480 | | % no need to clear: we leave error_scope below |
481 | | TimeOutRes=virtual_time_out(thrown_enumeration_warning(A,B,C,D,E))), |
482 | | exit_error_scope(Level,_,time_out_with_enum_warning_one_solution)). |
483 | | |
484 | | % here enumeration warnings are not cleared and propagated to outer error scope |
485 | | time_out_with_enum_warning_one_solution(Call,TO,TimeOutRes) :- |
486 | | time_out_with_enum_warning_one_solution(Call,TO,TimeOutRes,no_clear). |
487 | | |
488 | | |
489 | | enumeration_warning_occured_in_error_scope :- |
490 | ? | event_occurred_in_error_scope(enumeration_warning(_,_,_,_,_)). |
491 | | |
492 | | abort_error_occured_in_error_scope :- |
493 | | event_occurred_in_error_scope(abort_error(_)). % is this still being generated?? |
494 | | abort_error_occured_in_error_scope :- |
495 | | event_occurred_in_error_scope(well_definedness_error). |
496 | | |
497 | | |
498 | | % just like above; but does not set up new error_scope (for performance) and clears enumeration warnings in case of failure and success |
499 | | time_out_with_enum_warning_one_solution_no_new_error_scope(Call,TO,TimeOutRes) :- |
500 | | (enumeration_warning_occured_in_error_scope |
501 | | -> %trace, |
502 | | format('Events in error block:~n',[]),portray_events_in_error_scope, |
503 | | add_internal_error('Error block already has enumeration warnings',time_out_with_enum_warning_one_solution_no_new_error_scope(Call,TO,TimeOutRes)) |
504 | | ; true), |
505 | | % Note: will call safe_time_out which will not call time_out when time_out_preference_disabled / disable_time_out |
506 | | catch( |
507 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,clear_errors,perform_cut), % clear errors so we can continue using same error scope |
508 | | enumeration_warning(A,B,C,D,E), |
509 | | %% (print(enum_warning),nl,TimeOutRes=time_out)). %% |
510 | | TimeOutRes=virtual_time_out(thrown_enumeration_warning(A,B,C,D,E))). |
511 | | |
512 | | |
513 | | % check if a result is a time_out: |
514 | | is_time_out_result(time_out). |
515 | | is_time_out_result(virtual_time_out(_)). |
516 | | |
517 | | % a version which is to be used not for a single call but for backtracking |
518 | | % generate virtual time_out at the end if enumeration warning occured |
519 | | |
520 | | time_out_with_enum_warning_for_findall(Call,TO,TimeOutRes) :- |
521 | | enter_new_error_scope(Level,time_out_with_enum_warning_for_findall), |
522 | ? | time_out_with_enum_warning_for_findall2(Level,Call,TO,TimeOutRes). |
523 | | |
524 | | time_out_with_enum_warning_for_findall2(_Level,Call,TO,TimeOutRes) :- %print(enter(_Level)),nl, |
525 | ? | catch( |
526 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,no_clear,no_cut), |
527 | | Exc, |
528 | | get_virtual_time_out_from_exception(Exc,TimeOutRes)). % virtual time_out |
529 | | time_out_with_enum_warning_for_findall2(Level,_Call,_TO,_TimeOutRes) :- % print(exit(Level)),nl, |
530 | | clear_events_in_error_scope(Level,enumeration_warning(_,_,_,_,_)), |
531 | | exit_error_scope(Level,_,time_out_with_enum_warning_for_findall2),fail. |
532 | | |
533 | | % a version of time_out_with_enum_warning_for_findall where you manually have to enter_new_error_scope before |
534 | | % and exit_error_scope afterwards; useful if you repeatedly call this predicate and want to set up the block only once |
535 | | time_out_with_enum_warning_for_findall_in_current_error_scope(_Level,Call,TO,TimeOutRes) :- %print(enter(_Level)),nl, |
536 | ? | catch( |
537 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,no_clear,no_cut), |
538 | | Exc, |
539 | | get_virtual_time_out_from_exception(Exc,TimeOutRes)). % virtual time_out |
540 | | time_out_with_enum_warning_for_findall_in_current_error_scope(Level,_Call,_TO,_TimeOutRes) :- |
541 | | % does not exit level |
542 | | clear_events_in_error_scope(Level,enumeration_warning(_,_,_,_,_)),fail. |
543 | | |
544 | | % ------------ |
545 | | |
546 | | % see if an exception corresponds to a virtual_timeout in the form of an enumeration warning |
547 | | % also treats a CLPFD overflow as a virtual time-out in the sense that computation was aborted |
548 | | get_virtual_time_out_from_exception(enumeration_warning(Cause,_,_,_,_),TimeOutRes) :- !, |
549 | | get_virtual_time_out_res(Cause,TimeOutRes). |
550 | | get_virtual_time_out_from_exception(error(_ErrMsg,ErrTerm),TimeOutRes) :- |
551 | | is_clpfd_overflow_representation_error(ErrTerm,Cause),!, |
552 | | clpfd_overflow_msg(Msg), |
553 | | add_warning(clpfd_overflow,Msg), |
554 | | TimeOutRes = virtual_time_out(Cause). |
555 | | get_virtual_time_out_from_exception(Exc,_) :- % not detected as virtual time-out: throw again |
556 | | throw(Exc). |
557 | | |
558 | | get_virtual_time_out_res(kodkod_timeout,Res) :- !, Res= virtual_time_out(kodkod_timeout). |
559 | | get_virtual_time_out_res(_,virtual_time_out(exception)). |
560 | | |
561 | | |
562 | | is_clpfd_overflow_representation_error(representation_error(_Constraint,_,CauseMsg),CauseMsg) :- |
563 | | is_clpfd_overflow_representation_error_msg(CauseMsg). |
564 | | is_clpfd_overflow_representation_error(domain_error(list_to_fdset(_FDLIST,_),_,_,_List),CauseMsg) :- |
565 | | CauseMsg = 'CLPFD integer overflow'. |
566 | | is_clpfd_overflow_representation_error_msg('CLPFD integer overflow'). |
567 | | is_clpfd_overflow_representation_error_msg('max_clpfd_integer'). |
568 | | is_clpfd_overflow_representation_error_msg('min_clpfd_integer'). |
569 | | |
570 | | clpfd_overflow_msg(M) :- clpfd_interface:is_64_bit_system, !, |
571 | | (get_preference(use_clpfd_solver,true) |
572 | | -> M = 'A CLPFD integer overflow occurred.\nSet CLPFD preference to FALSE (Animation Preferences).' |
573 | | ; M = 'A CLPFD integer overflow occurred (despite CLPFD preference being FALSE).' % should normally not happen |
574 | | ). |
575 | | clpfd_overflow_msg('A CLPFD integer overflow occurred.\nSet CLPFD preference to FALSE (Animation Preferences) or use a 64 bit version of ProB.'). |
576 | | |
577 | | % ----------------- |
578 | | |
579 | | :- use_module(tools_meta,[safe_time_out/3]). |
580 | | % a version of time_out which upon failure checks if enumeration warnings were generated |
581 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,ClearErrors,CutAfterFirstSol) :- |
582 | ? | safe_time_out(Call,TO,TimeOutRes), % a safe version which checks that time-out value TO is not too large |
583 | | %call(Call), TimeOutRes=ok, |
584 | | (TimeOutRes = time_out -> ! %, print(real_timeout),nl |
585 | | ; (ClearErrors=clear_errors -> clear_enumeration_warnings ; true), |
586 | | (CutAfterFirstSol=perform_cut -> ! ; true) |
587 | | %,print(sol),nl |
588 | | ). |
589 | | time_out_check_enum_warning_for_failure(_Call,_TO,TimeOutRes,ClearErrors,_) :- % print(failure),nl, |
590 | ? | virtual_time_out_occured_in_error_scope(TimeOutRes), |
591 | | (ClearErrors=clear_errors -> clear_enumeration_warnings ; true). |
592 | | |
593 | | virtual_time_out_occured_in_error_scope(TimeOutRes) :- |
594 | ? | event_occurred_in_error_scope(enumeration_warning(A,B,C,D,critical)), % print(virtual),nl, |
595 | | TimeOutRes = virtual_time_out(failure_enumeration_warning(A,B,C,D,critical)). % we have not investigated all possibilities; generate virtual time_out |
596 | | |
597 | | critical_enumeration_warning_occured_in_error_scope :- |
598 | ? | critical_enumeration_warning_occured_in_error_scope(_,_,_,_). |
599 | | critical_enumeration_warning_occured_in_error_scope(A,B,C,D) :- |
600 | ? | event_occurred_in_error_scope(enumeration_warning(A,B,C,D,critical)). |
601 | | |
602 | | clear_enumeration_warnings :- |
603 | | clear_events_in_current_error_scope(enumeration_warning(_,_,_,_,_)). |
604 | | |
605 | | % retract events and raise real warnings |
606 | | % Src and Msg used if these are not enumeration warnings |
607 | | translate_events_in_current_scope_to_warnings(Src,Msg) :- |
608 | | get_counter(cur_block_level,L), |
609 | ? | retract(specific_event_occurred_at_level(L,Event)), |
610 | | (translate_enumeration_warning(Event,EMsg,Span) |
611 | | -> add_warning(enumeration_warning,EMsg,'',Span) |
612 | | ; add_warning(Src,Msg,Event)), |
613 | | fail. |
614 | | translate_events_in_current_scope_to_warnings(_,_). |
615 | | |
616 | | % see also: print_enum_warning |
617 | | translate_enumeration_warning(enumeration_warning(Enumerating,Type,Range,RestrictedRange,_Throwing), Msg,Span) :- |
618 | | translate_enumeration_info(Enumerating,Info,Span), |
619 | | ajoin(['Unbounded enumeration of ',Info,':',Type,', narrowing range: ',Range,' ---> ',RestrictedRange],Msg). |
620 | | % Throwing=throwing -> print_pending_abort_info ? |
621 | | |
622 | | translate_enumeration_info(enumerating(Info),Res,Span) :- !, translate_enumeration_info(Info,Res,Span). % TO DO: call get_trigger_info |
623 | | translate_enumeration_info(b(identifier(Id),_,Info),Res,Span) :- !, Res=Id,Span=Info. |
624 | | translate_enumeration_info(I,I,I). |
625 | | |
626 | | % show all events in current error scope |
627 | | portray_events_in_error_scope :- event_occurred_in_error_scope(E), |
628 | | portray_event(E),fail. |
629 | | portray_events_in_error_scope. |
630 | | |
631 | | portray_event(enumeration_warning(Source,Type,Range,RestrictedRange,Critical)) :- |
632 | | translate_enumeration_info(Source,Res,_Span), !, |
633 | | format(' *** Enumeration Warning (~w): Source ~w, Type: ~w, Range reduction: ~w ---> ~w~n', |
634 | | [Critical, Res, Type, Range, RestrictedRange]). |
635 | | portray_event(E) :- format(' *** ~w~n',[E]). |
636 | | |
637 | | % translate an event error to a short human readable form: |
638 | | translate_error_event(Error,TransError) :- |
639 | | (translate_enumeration_warning(Error,Msg,_) |
640 | | -> TransError=Msg |
641 | | ; TransError=Error). % TODO: translate other errors |
642 | | |
643 | | % ----------------------- |
644 | | |
645 | | :- volatile logged_error_hash/5, real_error_occurred/0, logged_error_spans/2, saved_subsidiary_span_pos/4. |
646 | | :- dynamic logged_error_hash/5. |
647 | | :- dynamic logged_message/4. |
648 | | :- dynamic real_error_occurred/0, warning_occurred/0. |
649 | | :- dynamic logged_error_spans/2. |
650 | | :- dynamic saved_subsidiary_span_pos/4. % for Tcl/Tk |
651 | | |
652 | | |
653 | | reset_real_error_occurred :- |
654 | | (retract(real_error_occurred) -> true ; true), |
655 | | (retract(warning_occurred) -> true ; true). |
656 | | |
657 | | reset_errors :- |
658 | | (reset_errors_but_not_scopes -> true |
659 | | ; format(user_error,'~n*** reset_errors_but_not_scopes failed!!~n',[]), |
660 | | fail % something really bad happened |
661 | | ), |
662 | | reset_error_limit_reached, |
663 | | reset_error_scopes. % call if you want to start again at a fresh top-level, without error scopes |
664 | | reset_errors_but_not_scopes :- |
665 | | reset_real_error_occurred, |
666 | | retractall(logged_error_hash(_,_,_,_,_)), |
667 | | retractall(logged_message(_,_,_,_)), |
668 | | reset_error_spans, |
669 | | clear_error_context. |
670 | | |
671 | | reset_error_span_if_required :- |
672 | ? | (logged_error_hash(_,_,_,_,_) -> true |
673 | | ; retractall(logged_error_spans(_,_))). |
674 | | |
675 | | reset_error_spans :- |
676 | | retractall(logged_error_spans(_,_)), |
677 | | retractall(saved_subsidiary_span_pos(_,_,_,_)). |
678 | | |
679 | | wd_error_occured :- |
680 | | test_error_occurred(well_definedness_error). |
681 | | |
682 | | clear_wd_errors :- |
683 | | clear_errors_with_source(well_definedness_error). |
684 | | clear_errors_with_source(Source) :- |
685 | | retractall(logged_error_spans(Source,_)), |
686 | | retractall(logged_error_hash(_,Source,_S1,_TContext1,_Span1)), |
687 | | retractall(logged_message(Source,_,_,_)), |
688 | | (logged_error_hash(_,_Source2,_S2,_TContext2,_Span2) -> true |
689 | | ; reset_real_error_occurred). |
690 | | |
691 | | print_error_span(Span) :- print_error_span(Span,error). |
692 | | print_error_span(unknown,_) :- !. |
693 | | print_error_span(none,_) :- !. % from Rodin |
694 | | print_error_span([],_) :- !. % empty info list |
695 | | print_error_span(Span,Kind) :- |
696 | | extract_span_description(Span,PosMsg), !, % also detects info lists |
697 | | print_error(Kind,PosMsg). |
698 | | print_error_span(b(E,T,I),Kind) :- |
699 | | translate_bexpr(b(E,T,I),S),!, |
700 | | print_error(Kind,S). |
701 | | print_error_span(span_predicate(P,_,_),Kind) :- translate_bexpr(P,S),!, |
702 | | % can happen in Event-B when no position info can be extracted |
703 | | print_error(Kind,S). |
704 | | print_error_span(List,Kind) :- List = [_|_], % info list which was not recognised; probably Event-B mode |
705 | | debug_mode(off), |
706 | | !, |
707 | | include(useful_info,List,UsefulList), % only show useful infos |
708 | | (UsefulList = [] -> true |
709 | | ; print_error(Kind,UsefulList)). |
710 | | print_error_span(Span,Kind) :- print_error(Kind,Span). % span not recognized ! |
711 | | |
712 | | useful_info(section(_)). |
713 | | useful_info(used_ids(_)). |
714 | | |
715 | | |
716 | | translate_bexpr(P,S) :- |
717 | | (debug_mode(on) -> Lim=3000 ; Lim=1000), |
718 | | translate:translate_subst_or_bexpr_with_limit(P,Lim,S). |
719 | | |
720 | | % the same, but not using user_error and red and only printing when span recognised |
721 | | % more detailed than translate:print_span |
722 | | print_message_span(unknown) :- !. |
723 | | print_message_span(Span) :- |
724 | | extract_span_description(Span,PosMsg), !, print(PosMsg). |
725 | | print_message_span(_). % span not recognized ! |
726 | | |
727 | | % try and extract a string (aka atom) describing the position |
728 | | extract_span_description(span_predicate(P,_,_),PosMsg) :- !, |
729 | | extract_span_description(P,PosMsg). |
730 | | extract_span_description(b(E,_,I),PosMsg) :- !, |
731 | | (extract_span_description(I,PosMsg) -> true |
732 | | ; first_sub_expr(E,A) -> extract_span_description(A,PosMsg)). |
733 | | extract_span_description(Span,PosMsg) :- |
734 | ? | extract_line_col(Span,StartLine,StartCol,EndLine,EndCol),!, |
735 | | (empty_line_col(StartLine,StartCol,EndLine,EndCol) |
736 | | -> POS = ['Unknown position'|POS2] |
737 | | ; POS = ['Line: ',StartLine,' Column: ',StartCol, |
738 | | ' until Line: ',EndLine,' Column: ',EndCol|POS2] |
739 | | ), |
740 | | (extract_file_name(Span,Filename) |
741 | | -> POS2 = [' in file: ',Filename|POS3] ; POS2 = POS3), |
742 | ? | (extract_additional_description(Span,ContextMsg) |
743 | | -> POS3 = [' within ',ContextMsg] ; POS3=[]), |
744 | | ajoin(POS,PosMsg). |
745 | | extract_span_description(Span,PosMsg) :- |
746 | | extract_symbolic_span_desc(Span,Symbolic),!, |
747 | | PosMsg=Symbolic. |
748 | | extract_span_description(Span,PosMsg) :- |
749 | | % try if we have an unknown position with a known context (e.g., pos_context(unknown,call_stack(...)) |
750 | | extract_additional_description(Span,ContextMsg),!, |
751 | | %POS = ['Unknown position within ',ContextMsg], |
752 | | POS = ['Within ',ContextMsg], |
753 | | ajoin(POS,PosMsg). |
754 | | % Note: Atom Plugin is quite sensitive to above error message and capitalization |
755 | | % (https://github.com/wysiib/language-b-eventb) |
756 | | |
757 | | extract_symbolic_span_desc(Span,PosMsg) :- |
758 | | extract_symbolic_label_pos(Span,Symbolic),!, |
759 | | % TO DO: we could try and find section for Rodin |
760 | | ajoin(['@label: ',Symbolic],PosMsg). |
761 | | |
762 | | |
763 | | % detect if a position is empty; Line 0, Column -1 .. Line 0, Column -2 used for non available Library Definition files |
764 | | % happens, e.g., when typing CHOOSE({1}\{1}) in REPL |
765 | | empty_line_col(0,N1,0,N2) :- integer(N1), integer(N2), N1>N2. |
766 | | |
767 | | assert_true(Code) :- |
768 | | (call(Code) -> true ; add_internal_error('Assertion failed: ',Code)). |
769 | | |
770 | | add_error_fail(Source,ErrMsg, ErrTerm) :- add_error_and_fail(Source,ErrMsg, ErrTerm). |
771 | | add_error_and_fail(Source,ErrMsg, ErrTerm) :- add_error(Source,ErrMsg, ErrTerm),fail. |
772 | | add_error_and_fail(Source,ErrMsg) :- add_error(Source,ErrMsg),fail. |
773 | | |
774 | | % add_error/2, add_error/3 and add_error/4 |
775 | | add_error(Source,ErrMsg) :- add_error4(Source,ErrMsg, '',unknown). |
776 | | add_error(Source,ErrMsg, ErrTerm) :- add_error4(Source,ErrMsg, ErrTerm,unknown). |
777 | | add_error(Source,ErrMsg,ErrTerm,Span) :- add_error4(Source,ErrMsg,ErrTerm,Span). |
778 | | |
779 | | add_error4(Source,ErrMsg,ErrTerm,Span) :- |
780 | | assert_real_error_occurred(Source), |
781 | | combine_msg_and_error_term(ErrMsg,ErrTerm,Span,S), |
782 | | add_error_or_warning(Source,S,Span). |
783 | | |
784 | | %:- use_module(translate, [translate_error_term/2, translate_context/2]). |
785 | | combine_msg_and_error_term(Msg,ErrTerm,_,Res) :- ErrTerm=='', !, Res=Msg. |
786 | | combine_msg_and_error_term(ErrMsg,ErrTerm,Span,S) :- |
787 | | string_concatenate(ErrMsg,' ',ST), |
788 | | safe_translate_error_term(ErrTerm,Span,RString), |
789 | | string_concatenate(ST,RString,S). |
790 | | |
791 | | |
792 | | :- dynamic show_error_source/0. |
793 | | show_error_source. % True if error source and span to be printed |
794 | | turn_show_error_source_on :- (show_error_source -> true ; assertz(show_error_source)). |
795 | | turn_show_error_source_off :- retractall(show_error_source). |
796 | | |
797 | | trace_if_user_wants_it :- (preferences:get_preference(trace_upon_error,true) -> safe_trace ; true). |
798 | | |
799 | | % try Prolog's trace; if this fails (compiled code), then ask user for input. |
800 | | safe_trace :- translate:install_b_portray_hook, |
801 | | catch(trace, error(existence_error(_,_),_), type_to_continue). |
802 | | |
803 | | :- dynamic do_not_interrupt_user_anymore/0. |
804 | | |
805 | | type_to_continue :- do_not_interrupt_user_anymore,!. |
806 | | type_to_continue :- format(user_output,'(type <RETURN> to continue, z<RETURN> to zip) ===> ',[]), |
807 | | read_line(user_input,X), |
808 | | (zip(X) -> assertz(do_not_interrupt_user_anymore) ; true). |
809 | | |
810 | | zip([122|_]). %starts with z |
811 | | |
812 | | |
813 | | print_error_source(well_definedness_error) :- !,print_error('A well-definedness error occured !'). |
814 | | print_error_source(warning(Source)) :- show_error_source,!, |
815 | | format_error_with_nl('~n! A warning occurred (source: ~w) !',[Source]). |
816 | | print_error_source(internal_error(Source)) :- show_error_source,!, |
817 | | flush_output, % internal error should not happen; be sure to flush all other pending outputs so that we can see where it happened |
818 | | format_error_with_nl('~n! An internal error occurred (source: ~w) !',[Source]). |
819 | | print_error_source(Source) :- show_error_source,!, |
820 | | format_error_with_nl('~n! An error occurred (source: ~w) !',[Source]). |
821 | | print_error_source(_). |
822 | | |
823 | | print_error_context(Kind,Context) :- ((Context = '' ; \+show_error_source) -> true ; print_error(Kind,Context)). |
824 | | |
825 | | print_error(message,Term) :- !, print_message_on_stream(user_output,[blue],Term). |
826 | | print_error(_,Term) :- print_error(Term). |
827 | | |
828 | | safe_translate_error_term(ErrTerm,Span,RString) :- |
829 | | temporary_set_preference(expand_avl_upto,5,CHNG), |
830 | | translate:translate_error_term(ErrTerm,Span,RString), |
831 | | reset_temporary_preference(expand_avl_upto,CHNG). |
832 | | |
833 | | safe_translate_context(Result) :- |
834 | | % avoid infinite loop when error occurs inside translate_context |
835 | | current_context(Context), |
836 | | !, |
837 | | set_error_context(translate_context), |
838 | | temporary_set_preference(expand_avl_upto,4,CHNG), |
839 | | (translate:translate_context(Context,TContext) |
840 | | -> true ; TContext=Context), |
841 | | reset_temporary_preference(expand_avl_upto,CHNG), |
842 | | restore_error_context(Context), |
843 | | Result=TContext. |
844 | | safe_translate_context(''). % no context |
845 | | |
846 | | is_optional_error_or_warning(warning(Src)) :- !, optional_error_or_warning(Src). |
847 | | is_optional_error_or_warning(Src) :- optional_error_or_warning(Src). |
848 | | |
849 | | add_error_or_warning(Source,ErrMsg,Span) :- |
850 | | safe_translate_context(Context), |
851 | | reset_error_span_if_required, |
852 | | simplify_span(Span,SSpan), % for better hashing, assertz, retract |
853 | | assertz_logged_error_if_new(Source,ErrMsg,Context,SSpan,New), |
854 | | log_error_or_msg(Source,ErrMsg,SSpan), |
855 | | (New=false -> true |
856 | | ; is_optional_error_or_warning(Source) -> |
857 | | (extract_span_description(SSpan,PosMsg) -> true ; PosMsg=''), |
858 | | formatsilentmsg(user_output,'% Optional error ~w occured: ~w ~w~n',[Source,ErrMsg,PosMsg]), |
859 | | register_error_span(Source,SSpan,_) |
860 | | ; register_error_span(Source,SSpan,NewSrcSpan), |
861 | | (NewSrcSpan=false, |
862 | | error_limit_reached, |
863 | | Source \= internal_error(_) % always show internal errors |
864 | | -> true % this is not a new Source,SSpan combination and we have already several errors; |
865 | | ; print_error_source(Source), |
866 | | print_error(ErrMsg), |
867 | | print_error_context(error,Context), |
868 | | print_error_span(SSpan) |
869 | | ), |
870 | | trace_if_user_wants_it |
871 | | ). |
872 | | |
873 | | % optionally log errors in to a ndsjon logfile |
874 | | log_error_or_msg(Source,ErrMsg,Span) :- get_preference(error_log_file,LogFile), |
875 | | LogFile \= '', |
876 | | decompose_error_source(Source,Type,Src), |
877 | | !, |
878 | | ndjson_log(Type,Src,ErrMsg,Span,LogFile). |
879 | | log_error_or_msg(_,_,_). |
880 | | |
881 | | % log error, warning or message to NDJSON log file |
882 | | % Type is warning, error, info |
883 | | :- use_module(tools_io,[intelligent_open/4]). |
884 | | ndjson_log(Type,Src,ErrMsg,Span,LogFile) :- |
885 | | force_extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol), |
886 | | catch(intelligent_open(LogFile,append,S,[encoding(utf8)]), E, |
887 | | (format(user_error,'~n*** Cannot open error log file: ~w~n*** Exception: ~w~n',[LogFile,E]),fail)), |
888 | | !, |
889 | | ndjson_write(S,Type,ErrMsg,Src,Filename,StartLine,StartCol,EndLine,EndCol), |
890 | | (extract_additional_pos_and_descr(Span,SubPos,_Desc), |
891 | | extract_position_from_span(SubPos,Filename2,StartLine2,StartCol2,EndLine2,EndCol2), |
892 | | ndjson_write(S,Type,ErrMsg,Src,Filename2,StartLine2,StartCol2,EndLine2,EndCol2), |
893 | | fail |
894 | | ; |
895 | | true |
896 | | ), |
897 | | close(S). |
898 | | ndjson_log(_,_,_,_,_). |
899 | | |
900 | | % assign position to main file if we cannot extract the precise position |
901 | | force_extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
902 | | extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol),!. |
903 | | force_extract_position_from_span(_Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
904 | | bmachine:b_get_main_filenumber(MainFN), |
905 | | get_filename(MainFN,Filename),!, |
906 | | % generate a virtual position, so that global errors without position are shown in Atom, VSCode,... |
907 | | StartLine=1,StartCol=1,EndLine=1,EndCol=1. |
908 | | force_extract_position_from_span(_Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
909 | | specfile:currently_opened_file_status(Filename,_), |
910 | | StartLine='-1',StartCol='-1',EndLine='-1',EndCol='-1'. |
911 | | |
912 | | %ndjson format http://ndjson.org |
913 | | % one JSON term per line |
914 | | :- use_module(extrasrc(json_parser),[json_write_stream/2]). |
915 | | ndjson_write(S,Type,ErrMsg,Src,Filename,StartLine,StartCol,EndLine,EndCol) :- |
916 | | version:version_str(Version), |
917 | | Term = json([type=string(Type), % Type is warning, error, info |
918 | | message=string(ErrMsg), |
919 | | reason=string(Src), |
920 | | file=string(Filename), |
921 | | start=json([line=number(StartLine),col=number(StartCol)]), |
922 | | end=json([line=number(EndLine),col=number(EndCol)]), |
923 | | version=string(Version) |
924 | | ]), |
925 | | json_write_stream(S,Term), nl(S). |
926 | | |
927 | | decompose_error_source(warning(Source),warning,Res) :- !, Res=Source. |
928 | | decompose_error_source(internal_error(Source),internal_error,Res) :- !, Res=Source. |
929 | | decompose_error_source(message(Source),information,Res) :- !, Res=Source. % mainly for NDJSON |
930 | | decompose_error_source(Msg,error,Msg). |
931 | | |
932 | | |
933 | | % the total number of errors since start; no way to reset this; useful for validation reports |
934 | | % TO DO: maybe use counter extension; but tricky to get dependency and initialisation right |
935 | | get_total_number_of_errors(X) :- |
936 | | (bb_get(total_number_of_errors,R) -> X=R |
937 | | ; X=0). |
938 | | |
939 | | inc_total_number_of_errors :- |
940 | | (bb_get(total_number_of_errors,R) |
941 | | -> R1 is R+1, bb_put(total_number_of_errors,R1) |
942 | | ; bb_put(total_number_of_errors,1)). |
943 | | |
944 | | |
945 | | total_number_of_errors_at_last_reset(Res) :- |
946 | | (bb_get(total_number_of_errors_at_last_reset,R) -> Res=R ; Res=0). |
947 | | |
948 | | reset_error_limit_reached :- |
949 | | bb_put(error_limit_reached_printed,false), |
950 | | get_total_number_of_errors(T), |
951 | | bb_put(total_number_of_errors_at_last_reset,T). |
952 | | |
953 | | error_limit_reached :- bb_get(error_limit_reached_printed,true),!. |
954 | | error_limit_reached :- get_total_number_of_errors(Nr), Nr >= 100, |
955 | | total_number_of_errors_at_last_reset(Last), Delta is Nr-Last, Delta >= 100, |
956 | | format_error_with_nl('Error limit reached (total: ~w, delta: ~w); only printing new error source and locations!',[Nr,Delta]), |
957 | | bb_put(error_limit_reached_printed,true). |
958 | | |
959 | | assert_warning_occured :- |
960 | | (preferences:get_preference(strict_raise_warnings,true) |
961 | | -> assert_real_error_occurred(warning) |
962 | | ; warning_occurred -> true |
963 | | ; assertz(warning_occurred)). |
964 | | assert_real_error_occurred(Source) :- |
965 | | (real_error_occurred -> true ; assertz(real_error_occurred)), |
966 | | inc_total_number_of_errors, |
967 | | assert_real_error_in_current_block(Source). |
968 | | |
969 | | :- use_module(hashing,[term_depth_hash/3]). |
970 | | assertz_logged_error_if_new(Source,S,TContext,Span,New) :- |
971 | | % limit depth mainly for TContext/Span; avoiding generating many similar error messages with same msg |
972 | | % however, for WD errors often the msg varies (e.g. argument for function application); so this will not prevent a lot of errors |
973 | | term_depth_hash(logged_error_hash(Source,S,TContext,Span),4,Hash), |
974 | | (logged_error_hash(Hash,Source,S,TContext,Span) |
975 | | -> debug_println(repeated_error(Hash)),New=false |
976 | | ; %print(new(Hash,Source,S,TContext,Span)),nl, |
977 | | assertz(logged_error_hash(Hash,Source,S,TContext,Span)), |
978 | | New=true). |
979 | | |
980 | ? | logged_error(Source,S,TContext,Span) :- logged_error_hash(_Hash,Source,S,TContext,Span). |
981 | | |
982 | | add_warning(Source,ErrMsg, ErrTerm) :- |
983 | | add_warning(Source,ErrMsg, ErrTerm,unknown). |
984 | | add_warning(Source,ErrMsg, ErrTerm,Span) :- |
985 | | safe_translate_error_term(ErrTerm,Span,RString), |
986 | | string_concatenate(ErrMsg,RString,S), |
987 | | assert_warning_occured, |
988 | | add_error_or_warning(warning(Source),S,Span). |
989 | | add_warning(Source,Message) :- |
990 | | assert_warning_occured, |
991 | | add_error_or_warning(warning(Source),Message,unknown). |
992 | | |
993 | | % MESSAGES |
994 | | |
995 | | % add message in debug mode |
996 | | add_debug_message(Source,ErrMsg, ErrTerm) :- |
997 | | add_debug_message(Source,ErrMsg, ErrTerm,unknown). |
998 | | add_debug_message(Source,ErrMsg, ErrTerm, Span) :- |
999 | | debug_mode(on),!, |
1000 | | add_message(Source,ErrMsg, ErrTerm, Span). |
1001 | | add_debug_message(_,_,_,_). |
1002 | | |
1003 | | |
1004 | | % a predicate to flexibly call add_error, add_warning or add_message |
1005 | | add_msg_warn_or_err(debug_message,Src,Msg,Term,Pos) :- !, add_debug_message(Src,Msg,Term,Pos). |
1006 | | add_msg_warn_or_err(message,Src,Msg,Term,Pos) :- !, add_message(Src,Msg,Term,Pos). |
1007 | | add_msg_warn_or_err(warning,Src,Msg,Term,Pos) :- !, add_warning(Src,Msg,Term,Pos). |
1008 | | add_msg_warn_or_err(_Kind,Src,Msg,Term,Pos) :- add_error(Src,Msg,Term,Pos). |
1009 | | |
1010 | | add_message(Source,ErrMsg, ErrTerm) :- /* same as add_error but does not add err msg */ |
1011 | | add_message(Source,ErrMsg, ErrTerm,unknown). |
1012 | | |
1013 | | add_message(_,_, _,unknown) :- error_manager_silent_mode(on),!. |
1014 | | add_message(Source,ErrMsg, ErrTerm,Span) :- /* same as add_error but does not add err msg */ |
1015 | | safe_translate_error_term(ErrTerm,Span,RString), |
1016 | | string_concatenate(ErrMsg,RString,S), |
1017 | | add_message_direct(Source, S), |
1018 | | safe_translate_context(Context), |
1019 | | print_error_context(message,Context), |
1020 | | print_error_span(Span,message), |
1021 | | log_error_or_msg(message(Source),S,Span), |
1022 | | assertz(logged_message(Source,S,Context,Span)). |
1023 | | |
1024 | | add_message(_,_) :- error_manager_silent_mode(on),!. |
1025 | | add_message(Source,ErrMsg) :- add_message_direct(Source,ErrMsg). |
1026 | | |
1027 | | add_message_direct(Source,ErrMsg) :- |
1028 | | (show_error_source -> format_with_colour(user_output,[light_gray],'! Message (source: ~w):~n',[Source]) ; true), |
1029 | | print_error(message,ErrMsg). |
1030 | | |
1031 | | |
1032 | | % adds a list of errors with position information |
1033 | | % [error(Msg,Pos)] or [warning(Msg,Pos)...] |
1034 | | no_real_perror_occurred([]). |
1035 | | no_real_perror_occurred([warning(_,_)|T]) :- no_real_perror_occurred(T). |
1036 | | |
1037 | | add_all_perrors(Errors) :- add_all_perrors(Errors,[],bmachine). |
1038 | | add_all_perrors(Errors,Files) :- add_all_perrors(Errors,Files,bmachine). |
1039 | | add_all_perrors(Errors,Files,Context) :- add_all_perrors(Errors,Files,Context,0,100,error). |
1040 | | add_all_perrors(Errors,Files,Context,WarnOrErr) :- |
1041 | | add_all_perrors(Errors,Files,Context,0,100,WarnOrErr). |
1042 | | |
1043 | | add_all_perrors([],_,_,_,_,_). |
1044 | | add_all_perrors([H|Rest],_Filenames,ContextOfError,NrOfErrors,MaxErrors,WarnOrErr) :- |
1045 | | NrOfErrors >= MaxErrors,!, |
1046 | | length([H|Rest],Len), |
1047 | | add_msg_warn_or_err(WarnOrErr,ContextOfError, |
1048 | | 'Reached maximium number of errors; number of errors not reported: ', |
1049 | | Len,unknown). |
1050 | | add_all_perrors([Error|Rest],Filenames,ContextOfError,NrOfErrors,MaxErrors,WarnOrErr) :- |
1051 | | get_perror(Error,ParseErrOrWarn,Msg,Pos), |
1052 | | extract_perror_pos(Pos,Filenames,NewPos), |
1053 | | (ParseErrOrWarn == warning -> add_warning(ContextOfError,Msg,'',NewPos) |
1054 | | ; ParseErrOrWarn = internal_error -> add_internal_error(ContextOfError,Msg,'',NewPos) |
1055 | | ; add_msg_warn_or_err(WarnOrErr,ContextOfError,Msg,'',NewPos)), |
1056 | | N1 is NrOfErrors+1, |
1057 | | add_all_perrors(Rest,Filenames,ContextOfError,N1,MaxErrors,WarnOrErr). |
1058 | | |
1059 | | extract_perror_pos(Pos,Filenames,ResPOS) :- |
1060 | | get_position_row_cols(Pos,Srow,Scol,Erow,Ecol), |
1061 | | get_position_filenumber(Pos,Filenumber),!, |
1062 | | ( get_filename(Filenumber,Filenames,Filename) |
1063 | | %Ecol1 is Ecol+1, |
1064 | | -> ResPOS = src_span(Filenumber,Filename,Srow,Scol,Erow,Ecol,[],[]) |
1065 | | % ; Filenumber = -1, Filenames=[FN] -> % just a single filename, e.g., in VisB parsing |
1066 | | % % TODO: we need to adjust line and column numbers using a reference position |
1067 | | % POS = src_position_with_filename_and_ec(Srow,Scol,Erow,Ecol,FN) |
1068 | | ; ResPOS = src_span(Srow,Scol,Erow,Ecol,[],[]) |
1069 | | ). |
1070 | | extract_perror_pos(pos(Srow,Scol,Filename),Files,POS) :- !, % deprecated ? |
1071 | | adapt_null_file_name(Filename,Files,Filename2), |
1072 | | S1 is Scol+0, % zero width span |
1073 | | POS = src_position_with_filename_and_ec(Srow,Scol,Srow,S1,Filename2). |
1074 | | extract_perror_pos(pos(Srow,Scol,Erow,Ecol,Filename),Files,POS) :- !, |
1075 | | adapt_null_file_name(Filename,Files,Filename2), |
1076 | | %Ecol1 is Ecol+1, |
1077 | | POS = src_position_with_filename_and_ec(Srow,Scol,Erow,Ecol,Filename2). |
1078 | | extract_perror_pos(pos(Srow,Scol),_,POS) :- !, print(deprecated_pos(Srow,Scol)),nl, |
1079 | | POS = src_position(Srow,Scol,none,1). |
1080 | | extract_perror_pos(Pos,_,Pos). |
1081 | | |
1082 | | % adapt filename |
1083 | | adapt_null_file_name(null,[File],Res) :- |
1084 | | !, % the error is not associated to a given file, but we have a file list with just a single file; |
1085 | | % occurs e.g. when parsing VisB JSON files |
1086 | | Res=File. |
1087 | | adapt_null_file_name(F,_,F). |
1088 | | |
1089 | | get_perror(error(Msg,Pos),error,Msg,Pos) :- !. |
1090 | | get_perror(internal_error(Msg,Pos),internal_error,Msg,Pos) :- !. |
1091 | | get_perror(warning(Msg,Pos),warning,Msg,Pos) :- !. |
1092 | | get_perror(M,_,M,unknown) :- format(user_error,'*** unknown perror: ~w~n',[M]). |
1093 | | |
1094 | | % version without list of filenames |
1095 | | get_filename(Filenumber,Filename) :- |
1096 | | bmachine:b_get_all_used_filenames(FN), |
1097 | | get_filename(Filenumber,FN,Filename). |
1098 | | |
1099 | | get_filename(Filenumber,Filenames,Filename) :- |
1100 | | number(Filenumber),!, |
1101 | | nth1(Filenumber,Filenames,Filename). |
1102 | | get_filename(N,F,R) :- |
1103 | | add_internal_error('Cannot get filename: ',get_filename(N,F,R)), |
1104 | | N=1,F=[R|_]. % return first file if possible |
1105 | | |
1106 | | % check if error/warning occured and retract matching error/warning |
1107 | | get_error_with_span(Source,ErrMsg,Span) :- |
1108 | ? | retract(logged_error_hash(_Hash,Source,ErrMsg,_Context,Span)). |
1109 | | get_error(Source,ErrMsg) :- |
1110 | ? | retract(logged_error_hash(_,Source,ErrMsg,_,_)). |
1111 | | get_warning(Source,ErrMsg) :- |
1112 | | retract(logged_error_hash(_,warning(Source),ErrMsg,_,_)). |
1113 | | |
1114 | | % now the same without actually retracting the error/warning: |
1115 | ? | check_error_occured(Source,ErrMsg) :- logged_error(Source,ErrMsg,_,_). |
1116 | | check_warning_occured(Source,ErrMsg) :- logged_error(warning(Source),ErrMsg,_,_). |
1117 | | |
1118 | | get_next_error_or_warning(Msg) :- get_error_or_message(_Src,Type,Msg), |
1119 | | Type \= message. % retract message, but do not return it |
1120 | | get_error_or_message(Source,Type,Msg) :- |
1121 | | retract_logged_error_or_message(Source,Type,ErrMsg,Context,Span), |
1122 | | (extract_line_col(Span,StartLine,StartCol,EndLine,EndCol) |
1123 | | -> POS = ['\n ### Line: ',StartLine,', Column: ',StartCol|POS1], |
1124 | | ((StartLine,StartCol) \= (EndLine,EndCol) |
1125 | | -> (StartLine=EndLine -> POS1 = [' until ',EndCol|POS2] |
1126 | | ; POS1 = ['\n ### until Line: ',EndLine,', Column: ',EndCol|POS2] |
1127 | | ) |
1128 | | ; POS1 = POS2 |
1129 | | ), |
1130 | | (extract_additional_description(Span,CMsg) |
1131 | | -> POS2 = ['\n ### within: ',CMsg,'\n\n'] |
1132 | | ; POS2 = ['\n\n'] |
1133 | | ), |
1134 | | (extract_relative_file_name(Span,Filename) |
1135 | | -> PosMsg = ['\n ### File: ',Filename | POS] |
1136 | | ; PosMsg = POS |
1137 | | ), |
1138 | | tools:ajoin(PosMsg,EndStr) |
1139 | | ; extract_symbolic_span_desc(Span,CMsg) |
1140 | | -> tools:ajoin(['\n ### ',CMsg,'\n\n'],EndStr) |
1141 | | ; EndStr = '\n\n' |
1142 | | ), |
1143 | | (empty_error_context(Context) |
1144 | | -> string_concatenate(ErrMsg,EndStr,S0) |
1145 | | ; safe_translate_error_term(Context,Span,CString), |
1146 | | string_concatenate(CString,EndStr,CString2), |
1147 | | string_concatenate('\n',CString2,CString3), % prefix used to be ': Context =' |
1148 | | string_concatenate(ErrMsg,CString3,S0) |
1149 | | ), |
1150 | | (debug_mode(off) -> Msg = S0 |
1151 | | ; decompose_error_source(Source,_,SrcAtom), |
1152 | | string_concatenate(SrcAtom,': ',S1), |
1153 | | string_concatenate(S1,S0,Msg) |
1154 | | ). |
1155 | | |
1156 | | empty_error_context(''). |
1157 | | empty_error_context('???'). |
1158 | | |
1159 | | |
1160 | | % retract an error |
1161 | | retract_logged_error_or_message(Source,Type,ErrMsg,Context,Span) :- Type \== message, |
1162 | | retract(logged_error_hash(_Hash,Source,ErrMsg,Context,Span)), |
1163 | | (Source = warning(_) -> Type=warning |
1164 | | ; Source = internal_error(_) -> Type=internal_error |
1165 | | ; Type=error). |
1166 | | retract_logged_error_or_message(message(Source),message,ErrMsg,Context,Span) :- |
1167 | | retract(logged_message(Source,ErrMsg,Context,Span)). |
1168 | | |
1169 | | |
1170 | | get_error_with_span_info(FullErrMsg,Type,ErrLocations) :- |
1171 | | retract_logged_error_or_message(_Source,Type,ErrMsg,Context,Span), |
1172 | | (empty_error_context(Context) -> FullErrMsg=ErrMsg |
1173 | | ; safe_translate_error_term(Context,Span,CString), |
1174 | | ajoin([ErrMsg,'\n',CString],FullErrMsg) |
1175 | | ), |
1176 | | findall( error_span(Filename,StartLine,StartCol,EndLine,EndCol), |
1177 | | extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol), |
1178 | | ErrLocations). |
1179 | | get_error_with_span_and_context(ErrMsg,Type,ErrLocations,GlobalContext,SpanContext) :- |
1180 | | % Global context: top-level info about what ProB was doing |
1181 | | % Local SpanContext: definition call hierarchy or call stack |
1182 | | retract_logged_error_or_message(_Source,Type,ErrMsg,GlobalContext,Span), |
1183 | | (extract_additional_description(Span,SpanContext) -> true ; SpanContext=''), |
1184 | | findall( error_span(Filename,StartLine,StartCol,EndLine,EndCol), |
1185 | | extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol), |
1186 | | ErrLocations). |
1187 | | |
1188 | | extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
1189 | | extract_line_col(Span,StartLine,StartCol,EndLine,EndCol), |
1190 | | (extract_file_name(Span,FName) -> Filename=FName ; Filename=''). |
1191 | | |
1192 | | % Tcl/Tk interface of SICStus cannot deal with certain strange Unicode symbols |
1193 | | % see prob_examples/public_examples/B/Puzzles/LogicQuestion.mch where code 55349 appears |
1194 | | tk_escape(Atom,EAtom) :- atom(Atom),!, |
1195 | | atom_codes(Atom,C), |
1196 | | maplist(tk_escape_code,C,EC), |
1197 | | atom_codes(EAtom,EC). |
1198 | | tk_escape(list(L),list(EL)) :- !, maplist(tk_escape,L,EL). |
1199 | | tk_escape(X,X). |
1200 | | |
1201 | | tk_escape_code(StrangeUnicode,Res) :- |
1202 | | StrangeUnicode > 55000,!, % not sure what the limit should be here |
1203 | | Res = 9633. % white box |
1204 | | tk_escape_code(Code,Code). |
1205 | | |
1206 | | tcltk_get_all_errors(Limit,list(ERes),NrErrorsShown,NrErrors) :- |
1207 | | get_all_errors(All), |
1208 | | length(All,NrErrors), |
1209 | | (NrErrors =< Limit -> Res = All, NrErrorsShown = NrErrors |
1210 | | ; prefix_length(All,Res,Limit), % truncate |
1211 | | NrErrorsShown = Limit |
1212 | | ), |
1213 | | tk_escape(list(Res),list(ERes)). |
1214 | | |
1215 | | |
1216 | | get_all_errors(All) :- |
1217 | | findall(Err,get_next_error_or_warning(Err),All), |
1218 | | All \= [], |
1219 | | clear_error_context. |
1220 | | |
1221 | | get_all_errors_and_clear(All) :- |
1222 | | findall(Err,get_next_error_or_warning(Err),All), |
1223 | | reset_errors_but_not_scopes, |
1224 | | All \= []. |
1225 | | get_all_errors_and_reset(All) :- |
1226 | | findall(Err,get_next_error_or_warning(Err),All), |
1227 | | reset_errors, % fresh top-level |
1228 | | All \= []. |
1229 | | get_all_errors_with_span_info_and_reset(All) :- |
1230 | | findall(error(ErrMsg,ErrType,ErrLocations), |
1231 | | get_error_with_span_info(ErrMsg,ErrType,ErrLocations),All), |
1232 | | reset_errors. |
1233 | | get_all_errors_with_span_and_context_and_reset(All) :- |
1234 | | findall(error(ErrMsg,ErrType,ErrLocations,GlobalContext,SpanContext), |
1235 | | get_error_with_span_and_context(ErrMsg,ErrType,ErrLocations,GlobalContext,SpanContext),All), |
1236 | | reset_errors. |
1237 | | |
1238 | | |
1239 | | error_or_warning_occured :- logged_error(_Source,_,_,_),!. |
1240 | ? | test_error_occurred(Source) :- logged_error(Source,_,_,_),real_error_occurred,!. |
1241 | | |
1242 | | %:- use_module(library(lists),[length/2]). length is builtin |
1243 | | |
1244 | | count_errors_occurred(Source,NrOfErrors) :- |
1245 | | findall(1,logged_error(Source,_,_,_),Ls), |
1246 | | length(Ls,NrOfErrors). |
1247 | | |
1248 | | display_error_statistics :- findall(Source,logged_error(Source,_,_,_),L), |
1249 | | count_occurences(L,Occ), |
1250 | | format('Error manager summary: ~w~n',[Occ]). |
1251 | | |
1252 | | |
1253 | | |
1254 | | |
1255 | | % ERROR CONTEXTS |
1256 | | % -------------- |
1257 | | |
1258 | | /* add context information; will be added to any error messages */ |
1259 | | % Note: Each Prolog module maintains its own blackboard for bb_get/bb_put |
1260 | | set_error_context(Ctx) :- bb_put(error_manager_context,Ctx). |
1261 | | |
1262 | | clear_error_context :- (bb_delete(error_manager_context,_) -> true ; true). |
1263 | | % retractall with dynamic predicate seems to slow down |
1264 | | |
1265 | | current_context(C) :- bb_get(error_manager_context,C). |
1266 | | |
1267 | | get_error_context(C) :- (current_context(C) -> true ; C='???'). |
1268 | | print_error_context :- (safe_translate_context(R), R\='' -> print_error(error,R) ; true). |
1269 | | |
1270 | | % restore an error context obtained by get_error_context |
1271 | | restore_error_context('???') :- !, clear_error_context. |
1272 | | restore_error_context(C) :- set_error_context(C). |
1273 | | |
1274 | | |
1275 | | % ----------------- |
1276 | | |
1277 | | get_error_span(Source,Span) :- |
1278 | | retract(logged_error_spans(Source,Span)). |
1279 | | |
1280 | | % a way for external modules to register error locations, e.g., for display by Tcl/Tk HighlightSyntaxErrors |
1281 | | register_error_span(Source,Span) :- register_error_span(Source,Span,_). |
1282 | | register_error_span(_,unknown,New) :- !,New=false. |
1283 | | register_error_span(Source,Span,New) :- logged_error_spans(Source,Span),!,New=false. |
1284 | | register_error_span(Source,Span,New) :- assertz(logged_error_spans(Source,Span)),New=true. |
1285 | | |
1286 | | get_error_span_linecol(Line,Col,EndLine,EndCol) :- get_error_span_linecol(_Source,Line,Col,EndLine,EndCol). |
1287 | | get_error_span_linecol(Source,Line,Col,EndLine,EndCol) :- |
1288 | | get_error_span(Source,Span), % looks at logged_error_spans and retracts |
1289 | | extract_line_col(Span,Line,Col,EndLine,EndCol). |
1290 | | |
1291 | | % gets repeatedly called from Tcl/Tk to get all error locations |
1292 | | tk_get_error_span_linecol_for_main_file(Line,Col,EndLine,EndCol) :- |
1293 | | retract(saved_subsidiary_span_pos(Line,Col,EndLine,EndCol)). |
1294 | | tk_get_error_span_linecol_for_main_file(Line,Col,EndLine,EndCol) :- |
1295 | | get_error_span(_Source,Span), % looks at logged_error_spans and retracts even if not in main file ! |
1296 | | (extract_primary_line_col_for_main_file(Span,Line,Col,EndLine,EndCol) |
1297 | | -> (extract_additional_pos_and_descr(Span,SubPos,_), |
1298 | | extract_line_col_for_main_file(SubPos,SLine,SCol,SEndLine,SEndCol), |
1299 | | assertz(saved_subsidiary_span_pos(SLine,SCol,SEndLine,SEndCol)), % store for later lookup |
1300 | | fail |
1301 | | ; true) |
1302 | | ; % try finding subsidiary position which is in main file: |
1303 | | extract_additional_pos_and_descr(Span,SubPos,_), |
1304 | | extract_line_col_for_main_file(SubPos,Line,Col,EndLine,EndCol) |
1305 | | ). |
1306 | | |
1307 | | |
1308 | | % just like get_error_span_linecol but does not retract and gets Filename |
1309 | | check_error_span_file_linecol(Source, FullFilename,Line,Col,EndLine,EndCol) :- |
1310 | ? | logged_error_spans(Source,Span), |
1311 | | (extract_file_line_col(Span,FullFilename,Line,Col,EndLine,EndCol) |
1312 | | ; |
1313 | ? | extract_additional_pos_and_descr(Span,SubPos,_), |
1314 | | extract_file_line_col(SubPos,FullFilename,Line,Col,EndLine,EndCol)). |
1315 | | |
1316 | | extract_file_line_col(Span,FullFilename,Line,Col,EndLine,EndCol) :- |
1317 | | (extract_file_number_and_name(Span,_,FullFilename) |
1318 | | -> true |
1319 | | ; FullFilename = unknown), % e.g. something like src_span/6; should we return unknown('?') |
1320 | ? | extract_line_col(Span,Line,Col,EndLine,EndCol). |
1321 | | |
1322 | | % try and extract symbolic (Rodin label) information |
1323 | | extract_symbolic_label_pos(span_predicate(P,_,_),PosMsg) :- !, |
1324 | | extract_symbolic_label_pos(P,PosMsg). |
1325 | | extract_symbolic_label_pos(b(E,_,I),PosMsg) :- !, |
1326 | | (extract_symbolic_label_pos(I,PosMsg) -> true |
1327 | | ; first_sub_expr(E,A) -> extract_symbolic_label_pos(A,PosMsg)). |
1328 | | extract_symbolic_label_pos([],_) :- !,fail. |
1329 | | extract_symbolic_label_pos([H|T],SymbolicPos) :- !, |
1330 | | posinfo_member(N,[H|T]), |
1331 | | extract_symbolic_label_pos(N,SymbolicPos),!. |
1332 | | extract_symbolic_label_pos(rodinpos(Label,_),Label). % see also get_texpr_labels in bsyntaxtree |
1333 | | extract_symbolic_label_pos(rodinpos(Model,Name,_),Label) :- ajoin([Model,':',Name],Label). |
1334 | | extract_symbolic_label_pos(rodin_derived_context_pos(Model,Context,LabelName),Label) :- |
1335 | | ajoin([Model,'.',Context,':',LabelName],Label). |
1336 | | extract_symbolic_label_pos(label(Label),Label). |
1337 | | |
1338 | | % just extract label, without model |
1339 | | extract_symbolic_label(span_predicate(P,_,_),PosMsg) :- !, |
1340 | | extract_symbolic_label(P,PosMsg). |
1341 | | extract_symbolic_label(b(E,_,I),PosMsg) :- !, |
1342 | | (extract_symbolic_label(I,PosMsg) -> true |
1343 | | ; first_sub_expr(E,A) -> extract_symbolic_label(A,PosMsg)). |
1344 | | extract_symbolic_label([H|T],SymbolicPos) :- !, |
1345 | | posinfo_member(N,[H|T]), |
1346 | | extract_symbolic_label(N,SymbolicPos),!. |
1347 | | extract_symbolic_label(rodinpos(Label,_),Label) :- Label \= []. % see also get_texpr_labels in bsyntaxtree |
1348 | | extract_symbolic_label(rodinpos(_Model,Label,_),Label) :- Label \= []. |
1349 | | extract_symbolic_label(rodin_derived_context_pos(_Model,_Context,Label),Label) :- Label \= []. |
1350 | | extract_symbolic_label(label(Label),Label) :- Label \= []. |
1351 | | |
1352 | | % quickly try and simplify a span, e.g., before asserting it in a fact (as it may become big) |
1353 | | simplify_span(b(E,_,Infos),Res) :- !, |
1354 | ? | (definite_posinfo_member(N,Infos) -> Res=N |
1355 | | ; first_sub_expr(E,A) -> simplify_span(A,Res) |
1356 | | ; Res=unknown). |
1357 | | simplify_span(Infos,Res) :- Infos = [_|_],!, |
1358 | ? | (definite_posinfo_member(N,Infos) -> Res=N ; Res=unknown). |
1359 | | simplify_span(span_predicate(P,_,_),Res) :- !, simplify_span(P,Res). |
1360 | | simplify_span(S,S). |
1361 | | |
1362 | | % choose first sub-expression in case top-expression has no position info (it would be better if extract_info/3 in bsyntaxtree merges position info) |
1363 | | first_sub_expr(negation(A,_),A). |
1364 | | first_sub_expr(conjunct(A,_),A). |
1365 | | first_sub_expr(disjunct(A,_),A). |
1366 | | first_sub_expr(implication(A,_),A). |
1367 | | first_sub_expr(equivalence(A,_),A). |
1368 | | first_sub_expr(exists([A|_],_),A). |
1369 | | first_sub_expr(forall([A|_],_),A). |
1370 | | first_sub_expr(let_predicate([A|_],_,_),A). |
1371 | | first_sub_expr(let_expression([A|_],_,_),A). |
1372 | | first_sub_expr(let_expression_global([A|_],_,_),A). |
1373 | | first_sub_expr(sequence([A|_]),A). |
1374 | | first_sub_expr(parallel([A|_]),A). |
1375 | | first_sub_expr(assign([A|_],_),A). |
1376 | | first_sub_expr(var([A|_],_),A). |
1377 | | first_sub_expr(equal(A,_),A). |
1378 | | first_sub_expr(not_equal(A,_),A). |
1379 | | first_sub_expr(function(A,_),A). |
1380 | | first_sub_expr(less(A,_),A). |
1381 | | first_sub_expr(less_equal(A,_),A). |
1382 | | % TO DO: use bsyntaxtree:syntaxtraversion |
1383 | | |
1384 | | :- use_module(tools_positions,[get_position_filenumber/2, get_position_row_cols/5]). |
1385 | | |
1386 | | % extract_line_col(S,_,_,_,_) :- print(extract(S)),nl,fail. |
1387 | | extract_line_col(Var,_,_,_,_) :- var(Var),!, write(variable_span(Var)),nl, |
1388 | | fail. |
1389 | | extract_line_col([],_,_,_,_) :- !,fail. |
1390 | | extract_line_col([H|T],Srow,Scol,Erow,Ecol) :- !, |
1391 | | posinfo_member(N,[H|T]), |
1392 | | extract_line_col(N,Srow,Scol,Erow,Ecol),!. |
1393 | | extract_line_col(closure(_,_,B),Srow,Scol,Erow,Ecol) :- !, |
1394 | | extract_line_col(B,Srow,Scol,Erow,Ecol). |
1395 | | extract_line_col(b(E,_,Infos),Srow,Scol,Erow,Ecol) :- !, |
1396 | | (extract_line_col(Infos,Srow,Scol,Erow,Ecol) -> true |
1397 | | ; first_sub_expr(E,A) -> extract_line_col(A,Srow,Scol,Erow,Ecol)). |
1398 | | extract_line_col(span_predicate(P,_,_),Srow,Scol,Erow,Ecol) :- !, |
1399 | | extract_line_col(P,Srow,Scol,Erow,Ecol). |
1400 | | extract_line_col(PosTerm,Srow,S1,Erow,E1) :- |
1401 | | get_position_row_cols(PosTerm,Srow,Scol,Erow,Ecol), |
1402 | | number(Srow), |
1403 | | % _Filenumber is an index into list provided by b_get_all_used_filenames |
1404 | | number(Scol), number(Erow), number(Ecol), |
1405 | | !, |
1406 | | (Scol>0 -> S1 is Scol-1 ; S1 = Scol), % the BParser starts column numbering at 1 |
1407 | | (Ecol>0 -> E1 is Ecol-1 ; E1 = Ecol). |
1408 | | %E1=Ecol. %Somehow the Ecol info is ok for Tcl |
1409 | | extract_line_col(unknown,_,_,_,_) :- !,fail. |
1410 | | extract_line_col(pos_context(A,_,_),StartLine,StartCol,EndLine,EndCol) :- !, |
1411 | | extract_line_col(A,StartLine,StartCol,EndLine,EndCol). |
1412 | | extract_line_col(src_span(_Filenr,_Filename,StartLine,SC,EndLine,EC,Offset,LenBytes),StartLine,StartCol,EndLine,EndCol) |
1413 | | :- !, |
1414 | | extract_line_col(src_span(StartLine,SC,EndLine,EC,Offset,LenBytes),StartLine,StartCol,EndLine,EndCol). |
1415 | | extract_line_col(src_span(SL,SC,EL,EC,_Off,_LenBytes),StartLine,StartCol,EndLine,EndCol) :- !, |
1416 | | StartLine=SL, StartCol is SC-1, |
1417 | | EndLine = EL, EndCol is EC-1. |
1418 | | %:- !, StartCol is SC-1, EndCol is StartCol+LenBytes, EndLine = StartLine. %% fix because new library generates erroneous EndLine/EndCol info |
1419 | | % EndCol is EC-1. |
1420 | ? | extract_line_col(span_info(_Info,Span),SL,SC,EL,EC) :- !, extract_line_col(Span,SL,SC,EL,EC). |
1421 | | extract_line_col(src_span_operator(SP1,SP2),SL,SC,EL,EC) :- !, |
1422 | | (extract_line_col(SP1,SL,SC,EL,EC) ; extract_line_col(SP2,SL,SC,EL,EC)). |
1423 | | extract_line_col(multi_span(SL,SC,EL,EC,_Off,_Len,TAIL),StartLine,StartCol,EndLine,EndCol) :- !, |
1424 | | % TO DO: replace by multi_span/2 |
1425 | | ( StartLine = SL, StartCol is SC-1, |
1426 | | EndLine = EL, EndCol is EC-1 |
1427 | | ; extract_line_col(TAIL,StartLine,StartCol,EndLine,EndCol)). |
1428 | | %extract_line_col(multi_span(A,B),StartLine,StartCol,EndLine,EndCol) :- !, |
1429 | | % (extract_line_col(A,StartLine,StartCol,EndLine,EndCol) ; |
1430 | | % extract_line_col(B,StartLine,StartCol,EndLine,EndCol)). |
1431 | | extract_line_col(src_position(StartLine,SC,_Off,LenBytes),StartLine,StartCol,EndLine,EndCol) :- !, |
1432 | | StartCol is SC-1, EndCol is StartCol+LenBytes, EndLine is StartLine. |
1433 | | extract_line_col(src_position_with_filename(StartLine,SC,LenBytes,_Filename),StartLine,StartCol,EndLine,EndCol) :- |
1434 | | !, |
1435 | | StartCol is SC-1, EndCol is StartCol+LenBytes, EndLine is StartLine. |
1436 | | extract_line_col(src_position_with_filename_and_ec(SL,SC,EL,EC,_Fname),StartLine,StartCol,EndLine,EndCol) :- |
1437 | | !, |
1438 | | StartLine=SL,StartCol is SC-1,EndLine = EL, EndCol is EC-1. |
1439 | | extract_line_col(lineCol(StartLine,StartCol),StartLine,StartCol,EndLine,EndCol) :- !, |
1440 | | EndLine=StartLine, EndCol=StartCol. |
1441 | | extract_line_col(span_context(Span,_),StartLine,StartCol,EndLine,EndCol) :- !, |
1442 | | extract_line_col(Span,StartLine,StartCol,EndLine,EndCol). |
1443 | | extract_line_col(Pos,_,_,_,_) :- is_symbolic_position(Pos),!,fail. % cannot extract line/col infos |
1444 | | %extract_line_col(Struct,StartLine,StartCol,EndLine,EndCol) :- |
1445 | | % is_argument_of_compound_term(Struct,Arg), |
1446 | | % functor(Arg,FF,NN),functor(Struct,FS,NS), write(detected(FF,NN,in,FS,NS)),nl,trace, |
1447 | | % /* recursively look for error spans inside */ |
1448 | | % extract_line_col(Arg,StartLine,StartCol,EndLine,EndCol),!. |
1449 | | %extract_line_col(S,_,_,_,_) :- print('COULD NOT EXTRACT LINE/COL INFO: '), print(S), |
1450 | | % functor(S,F,N), print(' '),print(F/N),nl,nl,fail. |
1451 | | |
1452 | | %:- use_module(library(between),[between/3]). |
1453 | | %is_argument_of_compound_term(Term,Arg) :- functor(Term,_,Arity), |
1454 | | % between(1,Arity,ArgNr), arg(ArgNr,Term,Arg), |
1455 | | % % this is quite dangerous, and possibly expensive |
1456 | | % ground(Arg). |
1457 | | |
1458 | | is_symbolic_position(rodinpos(_,_)). |
1459 | | is_symbolic_position(rodinpos(_,_,_)). |
1460 | | is_symbolic_position(rodin_derived_context_pos(_,_,_)). |
1461 | | is_symbolic_position(label(_)). |
1462 | | |
1463 | | |
1464 | | /* For TCL TK: extract all line_col information in one go and in 4 lists */ |
1465 | | extract_all_line_col(Span, StartLineList,StartColList,EndLineList,EndColList) :- |
1466 | | findall(lc(SL,SC,EL,EC), extract_line_col(Span,SL,SC,EL,EC),All), |
1467 | | %print(all(All,Span)),nl, |
1468 | | split_lc(All,StartLineList,StartColList,EndLineList,EndColList). |
1469 | | |
1470 | | split_lc([], [],[],[],[]). |
1471 | | split_lc([lc(SL,SC,EL,EC)|T],[SL|T1],[SC|T2],[EL|T3],[EC|T4]) :- |
1472 | | split_lc(T,T1,T2,T3,T4). |
1473 | | |
1474 | | % TO DO: merge with line_col and in Tcl/Tk use info to highlight proper file ! |
1475 | | |
1476 | | |
1477 | | % TODO: maybe add a preference to always get full filename path (cf always_print_filename in translate) |
1478 | | % TODO: do not show filename if there is only one file ? |
1479 | | extract_relative_file_name(Pos,RelFilename) :- |
1480 | | extract_file_name(Pos,Filename), |
1481 | | (specfile:currently_opened_file_status(Main,_), |
1482 | | tools:get_parent_directory(Main,MainDir), |
1483 | | tools:gen_relative_path(Filename,MainDir,Rel) |
1484 | | -> RelFilename = Rel |
1485 | | ; RelFilename = Filename). |
1486 | | extract_file_name(Pos,Filename) :- |
1487 | | extract_valid_file_number_and_name(Pos,_,Filename), |
1488 | | Filename \=null. % Filename = null occurs in eval_strings !? |
1489 | | extract_tail_file_name(Pos,Filename) :- |
1490 | | extract_valid_file_number_and_name(Pos,_,FullFilename), |
1491 | | tools:get_tail_filename(FullFilename,Filename). |
1492 | | extract_subsidiary_tail_file_name(Pos,Filename) :- |
1493 | | extract_valid_file_number_and_name(Pos,Nr,FullFilename), |
1494 | | not_main_file(Nr,FullFilename), |
1495 | | tools:get_tail_filename(FullFilename,Filename). |
1496 | | |
1497 | | extract_valid_file_number_and_name(Pos,Nr,Filename) :- |
1498 | | extract_file_number_and_name(Pos,Nr,Filename), |
1499 | | valid_filename(Filename). |
1500 | | valid_filename(F) :- atom(F). % not unknown(-1) which can come from REPL |
1501 | | % should we detect null here? |
1502 | | |
1503 | | % a version of extract_line_col which fails if the primary error location is not in the main file |
1504 | | extract_primary_line_col_for_main_file(Span,Line,Col,EndLine,EndCol) :- |
1505 | | \+((extract_file_number_and_name(Span,Nr,FullFilename), |
1506 | | not_main_file(Nr,FullFilename))), |
1507 | | extract_line_col(Span,Line,Col,EndLine,EndCol). |
1508 | | |
1509 | | % a version of extract_line_col which fails if the error or subsidiary location is not in the main file |
1510 | | extract_line_col_for_main_file(Span,Line,Col,EndLine,EndCol) :- |
1511 | | (extract_file_number_and_name(Span,Nr,FullFilename), |
1512 | | not_main_file(Nr,FullFilename) |
1513 | | -> extract_additional_pos_and_descr(Span,SubPos,_), |
1514 | | extract_line_col_for_main_file(SubPos,Line,Col,EndLine,EndCol) |
1515 | | ; extract_line_col(Span,Line,Col,EndLine,EndCol) |
1516 | | ). |
1517 | | |
1518 | | extract_file_number_and_name(V,_,_) :- var(V),!,fail. |
1519 | | extract_file_number_and_name(value(_),_,_) :- !,fail. |
1520 | | extract_file_number_and_name(pos_context(A,_,_), Filenumber,Filename) :- !, |
1521 | | extract_file_number_and_name(A, Filenumber,Filename). |
1522 | | extract_file_number_and_name(span_predicate(P,_,_), Filenumber,Filename) :- !, |
1523 | | extract_file_number_and_name(P, Filenumber,Filename). |
1524 | | extract_file_number_and_name(Pos, ResFileNr,Res) :- |
1525 | | get_position_filenumber(Pos,Filenumber),!, |
1526 | | (get_filename(Filenumber,Filename) |
1527 | | -> ResFileNr=Filenumber,Res=Filename |
1528 | | ; ResFileNr=Filenumber,Res=unknown(Filenumber) % unknown(-1) which can come from REPL |
1529 | | ). |
1530 | | extract_file_number_and_name(src_span(_,Filename,_,_,_,_,_,_), ResFileNr, Res) :- !,ResFileNr=unknown, Res=Filename. |
1531 | | extract_file_number_and_name(src_span(_,_,_, _,_,_), _ResFileNr, _Res) :- !,fail. % src_span/6, No file stored |
1532 | | extract_file_number_and_name(src_position_with_filename(_,_,_,Filename), ResFileNr, Res) :- !,ResFileNr=unknown,Res=Filename. |
1533 | | extract_file_number_and_name(src_position_with_filename_and_ec(_,_,_,_,Filename), ResFileNr, Res) :- !,ResFileNr=unknown,Res=Filename. |
1534 | | extract_file_number_and_name(section(RodinFilename), ResFileNr, Res) :- !,ResFileNr=unknown,Res=RodinFilename. |
1535 | | extract_file_number_and_name([H|T],Filenumber,Filename) :- !, |
1536 | | posinfo_member(Arg,[H|T]), |
1537 | | extract_file_number_and_name(Arg,Filenumber,Filename),!. |
1538 | | extract_file_number_and_name(closure(_,_,B),Filenumber,Filename) :- !, |
1539 | | extract_file_number_and_name(B,Filenumber,Filename). |
1540 | | extract_file_number_and_name(b(E,_,Infos),Filenumber,Filename) :- !, |
1541 | | (extract_file_number_and_name(Infos,Filenumber,Filename) -> true |
1542 | | ; first_sub_expr(E,A) -> extract_file_number_and_name(A,Filenumber,Filename)). |
1543 | | extract_file_number_and_name(span_context(Span,_),Filenumber,Filename) :- !, |
1544 | | extract_file_number_and_name(Span,Filenumber,Filename). |
1545 | | %extract_file_number_and_name(Struct,Filenumber,Filename) :- |
1546 | | % /* recursively look for error spans inside */ |
1547 | | % is_argument_of_compound_term(Struct,Arg), |
1548 | | % extract_file_number_and_name(Arg,Filenumber,Filename),!. |
1549 | | |
1550 | ? | definite_posinfo_member(POS,Args) :- member(nodeid(POS),Args). |
1551 | | posinfo_member(Arg,Args) :- % look for a nodeid(.) member |
1552 | ? | (member(nodeid(POS),Args) -> Arg=POS). % no longer allow other members in lists ; member(Arg,Args)). |
1553 | | |
1554 | | %:- use_module(bmachine,[b_get_all_used_filenames/1,b_get_main_filenumber/1]). |
1555 | | position_is_not_in_main_file(Pos) :- |
1556 | | get_position_filenumber(Pos,Filenumber),!, % B position info |
1557 | | not_main_filenumber(Filenumber). |
1558 | | position_is_not_in_main_file(src_span(_,Filename,_,_,_,_,_,_)) :- !, |
1559 | | specfile:currently_opened_file_status(Main,_), Main \= Filename. |
1560 | | position_is_not_in_main_file(src_position_with_filename(_,_,_,Filename)) :- !, |
1561 | | specfile:currently_opened_file_status(Main,_), Main \= Filename. |
1562 | | position_is_not_in_main_file(src_position_with_filename_ec(_,_,_,_,Filename)) :- !, |
1563 | | specfile:currently_opened_file_status(Main,_), Main \= Filename. |
1564 | | position_is_not_in_main_file(Struct) :- \+ atomic(Struct), Struct =.. [_|Args], |
1565 | | /* recursively look for error spans inside */ |
1566 | ? | member(Arg,Args), position_is_not_in_main_file(Arg),!. |
1567 | | |
1568 | | |
1569 | | not_main_file(Filenumber,_) :- number(Filenumber),!, % B Position info |
1570 | | not_main_filenumber(Filenumber). |
1571 | | not_main_file(_,Filename) :- % probably CSP position info; or unknown |
1572 | | specfile:currently_opened_file_status(Main,_), |
1573 | | \+ same_file_name(Main,Filename). %Main \= Filename. |
1574 | | |
1575 | | :- use_module(tools,[get_tail_filename/2]). |
1576 | | same_file_name(F1,F2) :- |
1577 | | (F1=F2 -> true |
1578 | | ; atom(F1), atom(F2), % we can have a non-atomic name like package(event_b_project) |
1579 | | get_tail_filename(F1,T1), |
1580 | | get_tail_filename(F2,T2), T1=T2). |
1581 | | |
1582 | | not_main_filenumber(Filenumber) :- |
1583 | | (bmachine:b_get_main_filenumber(MainFN) -> MainFN \= Filenumber |
1584 | | ; true). % there is no main file number, this must be an additionally loaded file (e.g., VisB .def file) |
1585 | | |
1586 | | % --------------------------------- |
1587 | | |
1588 | | |
1589 | | |
1590 | | % find a sub-position matching a given target like context_pos |
1591 | | find_sub_position_match(Pos,Target) :- Target=Pos,!. |
1592 | | find_sub_position_match(b(E,_,Infos),Target) :- !, |
1593 | | (find_sub_position_match(Infos,Target) -> true |
1594 | | ; first_sub_expr(E,A),find_sub_position_match(A,Target)). |
1595 | | find_sub_position_match(span_predicate(P,_,_),Target) :- !, |
1596 | | find_sub_position_match(P,Target). |
1597 | | find_sub_position_match([H|T],Target) :- !, |
1598 | | posinfo_member(N,[H|T]), |
1599 | | find_sub_position_match(N,Target). |
1600 | | find_sub_position_match(closure(_,_,B),Target) :- !, |
1601 | | find_sub_position_match(B,Target). |
1602 | | |
1603 | | extract_pos_context(Span,MainPos,Context,CtxtPos) :- |
1604 | | find_sub_position_match(Span,pos_context(M,C,P)),!, |
1605 | | MainPos=M, Context=C, CtxtPos=P. |
1606 | | |
1607 | | % extract additional information about error span, e.g., Definition call context, other spans,... |
1608 | | extract_additional_description(Span,FullDesc) :- |
1609 | ? | extract_additional_pos_and_descr(Span,_Position,FullDesc). |
1610 | | |
1611 | | extract_additional_pos_and_descr(Span,CtxtPos,FullDesc) :- |
1612 | | extract_pos_context(Span,MainPos,Context,CtxtPos), |
1613 | | %nl,print(pos_context(MainPos,Context,CtxtPos)),nl, |
1614 | ? | extract_context_msg(Context,MainPos,CMsg), |
1615 | | (extract_span_description(CtxtPos,PosMsg) |
1616 | | -> ajoin([CMsg,' at ',PosMsg],FullDesc) |
1617 | | ; FullDesc=CMsg). % no position info available |
1618 | | |
1619 | | extract_context_msg(call_stack(Stack),MainPos,Msg) :- !, |
1620 | ? | translate:translate_call_stack([top_level_call(MainPos)|Stack],Msg). % dynamic call stack; we add subsidiary DEFINITION calls to the Call stack |
1621 | | % see result e.g., for public_examples/B/FeatureChecks/DEFINITIONS/DefCallStackDisplay.mch |
1622 | | % TODO: should we also add_inner_context_description for use-cases like Atom editor |
1623 | | extract_context_msg(definition_call(Name),MainPos,Msg) :- !, % static Definition macro expansion call stack |
1624 | | ajoin(['DEFINITION call of ',Name],OuterMsg), |
1625 | | add_inner_context_description(MainPos,OuterMsg,Msg). |
1626 | | extract_context_msg(C,MainPos,Msg) :- ajoin(['CONTEXT: ',C],OuterMsg), |
1627 | | add_inner_context_description(MainPos,OuterMsg,Msg). |
1628 | | |
1629 | | |
1630 | | add_inner_context_description(Span,OuterCMsg,FullDesc) :- |
1631 | | % used to have this check: Span = pos_context(MainPos,Context,CtxtPos), |
1632 | | extract_additional_pos_and_descr(Span,_Pos,InnerCMsg),!, |
1633 | | ajoin([InnerCMsg,' within ',OuterCMsg],FullDesc). |
1634 | | add_inner_context_description(_,FullDesc,FullDesc). |
1635 | | |
1636 | | % try and extract a definition call stack |
1637 | | extract_definition_call_stack(pos_context(_Main,definition_call(Name),AuxPos),[Name|T]) :- !, |
1638 | | extract_definition_call_stack(AuxPos,T). |
1639 | | extract_definition_call_stack(_,[]). |
1640 | | |
1641 | | |
1642 | | % --------------------------------- |
1643 | | |
1644 | | % Get Source Position Info string for use in Tcl/Tk tables (column must be called 'Source'): |
1645 | | get_tk_table_position_info(Span,PosStr) :- |
1646 | | extract_line_col_for_main_file(Span,StartRow,StartCol,EndRow,EndCol), !, |
1647 | | ajoin([' at line ',StartRow,':',StartCol,' - ', EndRow, ':', EndCol],PosStr). % recognised by Tk Table viewer |
1648 | | get_tk_table_position_info(Span,PosStr) :- extract_span_description(Span,PosStr),!. |
1649 | | get_tk_table_position_info(_,''). |
1650 | | |
1651 | | |
1652 | | % --------------------------------- |
1653 | | |
1654 | | :- dynamic backed_up_error/4. |
1655 | | |
1656 | | % copy current error facts into backed_up_error facts |
1657 | | backup_errors :- |
1658 | | retractall(backed_up_error(_,_,_,_)), |
1659 | | logged_error(Source,S,TContext,Span), |
1660 | | assertz(backed_up_error(Source,S,TContext,Span)), |
1661 | | fail. |
1662 | | backup_errors. |
1663 | | |
1664 | | % --------------------------------- |
1665 | | |
1666 | | :- dynamic error_manager_silent_mode/1. |
1667 | | % separate from debug:silent_mode to avoid dependency to debug module |
1668 | | error_manager_silent_mode(off). |
1669 | | |
1670 | | set_error_manager_silent_mode(X) :- retractall(error_manager_silent_mode(_)), |
1671 | | assertz(error_manager_silent_mode(X)). |
1672 | | |
1673 | | |
1674 | | formatsilentmsg(Stream,FS,Args) :- error_manager_silent_mode(off) -> format(Stream,FS,Args) ; true. |
1675 | | |
1676 | | % ------------------------------------------- |
1677 | | |
1678 | | :- use_module(eventhandling,[register_event_listener/3]). |
1679 | | :- register_event_listener(reset_prob,reset_errors, |
1680 | | 'Reset Error Manager just like after starup_prob (except total_number_of_errors)'). |