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_undefined, []).   36
   37% A wrapper from library(check)
   38:- use_module(library(apply)).   39:- use_module(library(lists)).   40:- use_module(library(pairs)).   41:- use_module(library(checkers/checker)).   42:- use_module(library(clambda)).   43:- use_module(library(infer_alias)).   44:- use_module(library(normalize_pi)).   45:- use_module(library(codewalk)).   46:- use_module(library(location_utils)).   47:- use_module(library(from_utils)).   48:- use_module(library(referenced_by)).   49:- use_module(library(group_pairs_or_sort)).   50
   51:- multifile
   52    prolog:message//1.   53
   54:- dynamic
   55    undef/3.   56
   57checker:check(undefined, Results, Options) :-
   58    check_undefined(Options, Results).
   59
   60check_undefined(Options, Results) :-
   61    walk_code([source(true),
   62               trace_reference(-),
   63               undefined(trace),
   64               on_trace(collect_undef)|Options]),
   65    findall(File-(AL-(PI-(Loc/['~w'-[CI]]))),
   66            ( retract(undef(PI, CI, From)),
   67              find_alternatives(PI, AL),
   68              from_location(From, Loc),
   69              from_to_file(From, File)
   70            ), Pairs),
   71    group_pairs_or_sort(Pairs, Grouped),
   72    findall(error-(File-(Decl-(PI-LocCI))),
   73            ( member(File-ALPILocCIList, Grouped),
   74              member(AL-PILocCIList, ALPILocCIList),
   75              maplist(\ ((_:F/A)-_)^(F/A)^true, PILocCIList, PIL),
   76              maplist(alternative_decl(PIL), AL, Decl),
   77              member(PI-LocCIList, PILocCIList),
   78              member(LocCI, LocCIList)
   79            ), Results).
   80
   81alternative_decl(PIL, A-EM/EL, Decl/FL) :-
   82    ( EL = []
   83    ->Decl = use_module(A)
   84    ; length(EL,  EN),
   85      length(PIL, IN),
   86      ( EN < IN
   87      ->Decl = use_module(A, except(EL))
   88      ; Decl = use_module(A, PIL)
   89      )
   90    ),
   91    subtract(PIL, EM, FL).
   92
   93hide_undef(M:H) :- hide_undef(H, M).
   94
   95find_alternatives(M:F/A, AL) :-
   96    functor(H, F, A),
   97    findall(AA-EL, ( current_predicate(AM:F/A),
   98                     AM \= M,
   99                     \+ predicate_property(AM:H, imported_from(_)),
  100                     ( module_property(AM, file(AF))
  101                     ->( library_alias(AF, AA)
  102                       ->true
  103                       ; AA = AF
  104                       )
  105                     ; AA=AM
  106                     ),
  107                     exclude_list(M, AM, EL)
  108                   ), AU),
  109    sort(AU, AL).
  110
  111exclude_list(M, AM, ML/EL) :-
  112    module_property(AM, exports(MU)),
  113    sort(MU, ML),
  114    findall(F/A,
  115            ( member(F/A, ML),
  116              functor(H, F, A),
  117              predicate_property(M:H, defined),
  118              \+ predicate_property(M:H, imported_from(AM))
  119            ), EU),
  120    sort(EU, EL).
  121
  122% Hook to hide undef messages:
  123:- multifile hide_undef/2.  124hide_undef(asr_head_prop(_, _, _, _, _, _, _, _), assertions).
  125
  126:- public collect_undef/3.  127collect_undef(MCall, Caller, From) :-
  128    \+ hide_undef(MCall),
  129    normalize_pi(MCall, PI),
  130    normalize_pi(Caller, CI),
  131    update_fact_from(undef(PI, CI), From).
  132
  133prolog:message(acheck(undefined)) -->
  134    ['Undefined Predicates',nl,
  135     '--------------------',nl],
  136    prolog:message(check(undefined_procedures, [])).
  137prolog:message(acheck(undefined, File-ALPILocCIList)) -->
  138    [ 'Undefined predicates in ~w:'-[File], nl],
  139    foldl(show_alternatives, ALPILocCIList),
  140    { pairs_values(ALPILocCIList, PILocCIList) },
  141    foldl(foldl(show_undefined), PILocCIList).
  142
  143show_alternatives(AL-_) -->
  144    ( {AL = []}
  145    ->[]
  146    ; ['  Can be fixed by adding '],
  147      ( {AL = [_]}
  148      ->{Spc=''},
  149        []
  150      ; {Spc='\t'},
  151        ['one of these:', nl]
  152      ),
  153      foldl(show_alternative(Spc), AL)
  154    ).
  155
  156show_alternative(Spc, Decl/FL) -->
  157    ['~a:- ~q.'-[Spc, Decl]],
  158    ( {FL = []}
  159    ->[]
  160    ; [' % add exports: ~q'-[FL]]
  161    ),
  162    [nl].
  163
  164show_undefined(PI-LocCIList) -->
  165    [ '    ~w undefined, '-[PI], 'referenced by', nl],
  166    referenced_by(LocCIList)