1/*  Part of Extended Tools for SWI-Prolog
    2
    3    Author:        Edison Mera Menendez
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xtools
    6    Copyright (C): 2015, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(check_trivial_fails, []).   36
   37:- use_module(library(checkers/checker)).   38:- use_module(library(apply)).   39:- use_module(library(option)).   40:- use_module(library(abstract_interpreter)).   41:- use_module(library(from_utils)).   42:- use_module(library(location_utils)).   43:- use_module(library(codewalk)).   44:- use_module(library(dynamic_locations)).   45:- use_module(library(checkable_predicate)).   46
   47:- multifile
   48    prolog:message//1.   49
   50:- dynamic
   51    trivial_fail/2,
   52    ai_cache_result/2.   53
   54checker:check(trivial_fails, Result, Options) :-
   55    check_trivial_fails(Options, Result).
   56
   57check_trivial_fails(Options1, Pairs) :-
   58    infer_dynl_if_required,
   59    select_option(match_ai(MatchAI), Options1, Options2, match_head),
   60    merge_options(Options2,
   61                  [infer_meta_predicates(false),
   62                   autoload(false),
   63                   evaluate(false),
   64                   trace_reference(_),
   65                   module_class([user, system, library])
   66                  ], Options),
   67    walk_code([on_trace(collect_trivial_fails(MatchAI))|Options]),
   68    findall(warning-(Loc-Args),
   69            ( retract(trivial_fail(Args, From)),
   70              from_location(From, Loc)
   71            ), Pairs),
   72    cleanup_f,
   73    !.
   74
   75cleanup_f :-
   76    retractall(ai_cache_result(_, _)).
   77
   78prolog:message(acheck(trivial_fails)) -->
   79    ['Trivial Fails',nl,
   80     '-------------',nl,
   81     'The literals below always fails, due to there are no', nl,
   82     'matching clauses for such calls, which is reported as', nl,
   83     'a trivial fail, or because all paths leads to dead', nl,
   84     'points, in such case the warning reports also the', nl,
   85     'biggest failure chain found', nl, nl].
   86prolog:message(acheck(trivial_fails, Loc-Args)) -->
   87    Loc,
   88    foldl(show_trivial_fail, Args).
   89
   90show_trivial_fail(trivial_fail(Caller, MGoal)) -->
   91    ['In ~q, trivial fail for ~q'-[Caller, MGoal], nl].
   92show_trivial_fail(failure(Caller, MGoal, S)) -->
   93    ['In ~q, failure for ~q, biggest failure chain was ~q'-[Caller, MGoal, S], nl].
   94
   95:- multifile ignore_predicate/2.   96ignore_predicate(H, M) :- \+ predicate_property(M:H, defined), !.
   97ignore_predicate(H, M) :- predicate_property(M:H, multifile), !.
   98ignore_predicate(pce_class(_, _, template, _, _, _), pce_expansion).
   99ignore_predicate(property(system_source_prefix(_)), pce_host).
  100ignore_predicate(verbose, pce_expansion).
  101ignore_predicate(inferred_meta_pred(_, _, _), prolog_metainference).
  102
  103:- public collect_trivial_fails/4.  104:- meta_predicate collect_trivial_fails(7,+,+,+).  105collect_trivial_fails(MatchAI, MGoal, Caller, From) :-
  106    record_location_meta(MGoal, _, From, all_call_refs, cu_caller_hook(MatchAI, Caller)).
  107
  108cu_caller_hook(MatchAI, Caller, H, CM, Type, _, _, From) :-
  109    ground(CM),
  110    callable(H),
  111    predicate_property(CM:H, implementation_module(M)),
  112    MGoal = M:H,
  113    checkable_predicate(MGoal),
  114    \+ ignore_predicate(H, M),
  115    copy_term_nat(CM:H, Term),
  116    variant_sha1(Term, Hash),
  117    ( ai_cache_result(Hash, Data)
  118    ->true
  119    ; ( abstract_interpreter(Term, MatchAI, [location(From)], Result)
  120      ->Data = true(Result)
  121      ; Data = fail
  122      ),
  123      assertz(ai_cache_result(Hash, Data))
  124    ),
  125    Data = fail,
  126    N = 0,
  127    S = [],
  128    ( N = 0
  129    ->Args = trivial_fail(Caller, MGoal)
  130    ; Args = failure(Caller, MGoal, S) % General failure not implemented yet
  131    ),
  132    memberchk(Type, [lit, use]),
  133    update_fact_from(trivial_fail(Args), From)