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