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