1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2023, 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(checkct,
   36          [ call_checkct/4
   37          ]).   38
   39:- use_module(library(lists)).   40:- use_module(library(compound_expand)).   41:- use_module(library(ontrace)).   42:- use_module(library(option)).   43:- use_module(library(solution_sequences)).   44:- use_module(library(in_module_file)).   45:- init_expansors.   46
   47:- thread_local
   48    issue_found/5.
 call_checkct(:Call, +File, +Line, Options) is multi
Meta-predicate to prevent that Call uses dynamic or multifile predicates. This is important to prevent wrong usage of compile-time optimizations on parts of the program intended to be evaluated at run-time. It was part of library(neck), but now it is in its own module to allow its usage elsewhere.
   57:- meta_predicate call_checkct(0,+,+,+).   58
   59call_checkct(Call, File, Line, Options) :-
   60    option(ignore(IgnoreL), Options, []),
   61    ontrace(Call, handle_port(File, Line, IgnoreL), []).
   62
   63clause_pc_location(clause_pc(Clause, PC), Loc) :-
   64    clause_pc_location(Clause, PC, Loc),
   65    !.
   66clause_pc_location(Loc, Loc).
   67
   68term_expansion(end_of_file, _) :-
   69    in_module_file,
   70    forall(distinct([File, Line, Issues, PI, Loc],
   71                    ( retract(issue_found(File, Line, Issues, PI, Loc1)),
   72                      clause_pc_location(Loc1, Loc)
   73                    )),
   74           print_message(
   75               warning,
   76               at_location(
   77                   file(File, Line, -1, _),
   78                   at_location(
   79                       Loc,
   80                       format("~w ~w called at compile time", [Issues, PI]))))),
   81    fail.
   82
   83% Note: this is not called by make/0, since it is wrapped by notrace/1, you
   84% should use make:make_no_trace/0 instead --EMM
   85
   86handle_port(File, Line, IgnoreL, call, Frame, _, _, Loc, Action) :-
   87    prolog_frame_attribute(Frame, goal, Goal),
   88    strip_module(Goal, M, Call),
   89    functor(Call, F, A),
   90    PI = M:F/A,
   91    ( memberchk(PI, IgnoreL)
   92    ->Action = skip
   93    ; findall(Issue,
   94            ( member(Issue, [multifile, dynamic]),
   95              predicate_property(Goal, Issue)
   96            ), IssueL),
   97      IssueL \= [],
   98      atomic_list_concat(IssueL, ',', Issues),
   99      retractall(issue_found(File, Line, Issues, PI, Loc)),
  100      assertz(issue_found(File, Line, Issues, PI, Loc)),
  101      Action = continue
  102    )