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