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(abstract_interpreter)).   40:- use_module(library(from_utils)).   41:- use_module(library(location_utils)).   42:- use_module(library(codewalk)).   43:- use_module(library(dynamic_locations)).   44:- use_module(library(checkable_predicate)).   45
   46:- multifile
   47    prolog:message//1.   48
   49:- dynamic
   50    trivial_fail/2,
   51    ai_cache_result/2.   52
   53checker:check(trivial_fails, Result, Options) :-
   54    check_trivial_fails(Options, Result).
   55
   56check_trivial_fails(Options1, Pairs) :-
   57    select_option(match_ai(MatchAI), Options1, Options2, match_head),
   58    merge_options(Options2,
   59                  [infer_meta_predicates(false),
   60                   autoload(false),
   61                   evaluate(false),
   62                   trace_reference(_),
   63                   module_class([user, system, library])
   64                  ], Options),
   65    dynamic_locations(Options),
   66    walk_code([on_trace(collect_trivial_fails(MatchAI))|Options]),
   67    findall(warning-(Loc-Args),
   68            ( retract(trivial_fail(Args, From)),
   69              from_location(From, Loc)
   70            ), Pairs),
   71    cleanup_f,
   72    !.
   73
   74cleanup_f :-
   75    retractall(ai_cache_result(_, _)).
   76
   77prolog:message(acheck(trivial_fails)) -->
   78    ['Trivial Fails',nl,
   79     '-------------',nl,
   80     'The literals below always fails, due to there are no', nl,
   81     'matching clauses for such calls, which is reported as', nl,
   82     'a trivial fail, or because all paths leads to dead', nl,
   83     'points, in such case the warning reports also the', nl,
   84     'biggest failure chain found', nl, nl].
   85prolog:message(acheck(trivial_fails, Loc-Args)) -->
   86    Loc,
   87    foldl(show_trivial_fail, Args).
   88
   89show_trivial_fail(trivial_fail(Caller, MGoal)) -->
   90    ['In ~q, trivial fail for ~q'-[Caller, MGoal], nl].
   91show_trivial_fail(failure(Caller, MGoal, S)) -->
   92    ['In ~q, failure for ~q, biggest failure chain was ~q'-[Caller, MGoal, S], nl].
   93
   94:- multifile ignore_predicate/2.   95ignore_predicate(H, M) :- \+ predicate_property(M:H, defined), !.
   96ignore_predicate(H, M) :- predicate_property(M:H, multifile), !.
   97ignore_predicate(pce_class(_, _, template, _, _, _), pce_expansion).
   98ignore_predicate(property(system_source_prefix(_)), pce_host).
   99ignore_predicate(verbose, pce_expansion).
  100ignore_predicate(inferred_meta_pred(_, _, _), prolog_metainference).
  101
  102:- public collect_trivial_fails/4.  103:- meta_predicate collect_trivial_fails(7,+,+,+).  104collect_trivial_fails(MatchAI, MGoal, Caller, From) :-
  105    record_location_meta(MGoal, _, From, all_call_refs, cu_caller_hook(MatchAI, Caller)).
  106
  107cu_caller_hook(MatchAI, Caller, H, CM, Type, _, _, From) :-
  108    ground(CM),
  109    callable(H),
  110    predicate_property(CM:H, implementation_module(M)),
  111    MGoal = M:H,
  112    checkable_predicate(MGoal),
  113    \+ ignore_predicate(H, M),
  114    copy_term_nat(CM:H, Term),
  115    variant_sha1(Term, Hash),
  116    ( ai_cache_result(Hash, Data)
  117    ->true
  118    ; ( abstract_interpreter(Term, MatchAI, [location(From)], Result)
  119      ->Data = true(Result)
  120      ; Data = fail
  121      ),
  122      assertz(ai_cache_result(Hash, Data))
  123    ),
  124    Data = fail,
  125    N = 0,
  126    S = [],
  127    ( N = 0
  128    ->Args = trivial_fail(Caller, MGoal)
  129    ; Args = failure(Caller, MGoal, S) % General failure not implemented yet
  130    ),
  131    memberchk(Type, [lit, use]),
  132    update_fact_from(trivial_fail(Args), From)