# Generating xref info with term expansions?

**URL:** <https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372>\
**Category:** Resources\
**Created:** [October 10, 2019, 5:22am UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372 "2019-10-10T05:22:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [October 10, 2019, 5:22am UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/1 "2019-10-10T05:22:43Z")

</div>

It appears that library(prolog\_xref) doesn’t do term expansion (at least when I ran it, it didn’t find any of the predicates that had been created by term expansion). Is there a way to do cross-referencing of term-expanded files?

If not, I could use current\_predicate/1 and listing/1 to output the predicates (or read\_term/2 and expand\_term/2) because I want to find dead code: `xref_defined(_, Pred, _), \+ xref_called(_, Pred, _)`. But it’d be nice if there’s a better way of doing this.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [October 10, 2019, 6:56am UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/2 "2019-10-10T06:56:00Z")

</div>

> [@peter.ludemann](#):
>
> I want to find dead code

You may be able to use Logtalk’s [`dead_code_scanner`](https://logtalk.org/tools.html#dead-code-scanner) tool.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 10, 2019, 7:15am UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/3 "2019-10-10T07:15:55Z")

</div>

The xref library does use term expansion, unless you blocked it by testing for the Prolog flag `xref`. Also, it doesn’t _define_ the term\_expansion rules, so you must make sure they are available. You can use gxref/0 after loading your program (requires gui).

The other library to use is library(prolog\_codewalk) which runs over all or part of the loaded program and does a callback on all or a selection of the call edges, possibly providing source location information. Using this you can easily record what is called. Next you enumerate what is defined using predicate property and you are done. list\_undefined/0, used by make is built on top of this library.

The codewalk library, working on the loaded program, is more precise than the xref library. It is still not complete of course as it cannot find dynamically constructed and called goals.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [October 10, 2019, 7:10pm UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/4 "2019-10-10T19:10:34Z")

</div>

> [@pmoura](#):
>
> You may be able to use Logtalk’s [`dead_code_scanner`](https://logtalk.org/tools.html#dead-code-scanner) tool.

I couldn’t figure out how to use it – I’ve been able to use the instructions at [Linter - #11 by pmoura](https://swi-prolog.discourse.group/t/linter/1160/11) but what argument should I give to `dead_code_scanner::entity`? (I also tried `directory` and `file` but they didn’t detect anything.)

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [October 10, 2019, 7:25pm UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/5 "2019-10-10T19:25:48Z")

</div>

Sorry, I was pressed on time when I mentioned the tool. Ideally, you would be able to compile your Prolog modules as objects and then use the `dead_code_scanner::entity/1` predicate taking as argument a name of a module (that was compiled as an object). For example, assuming a module named `foo` something like:

```prolog
$ swilgt
...

?- set_logtalk_flag(optimize, on).
true.

?- {dead_code_scanner(loader)}.
...
true.

?- {foo).
...
true

?- dead_code_scanner::entity(foo).

```

This would likely give you some or several false positives, specially depending if you use `use_module/2` vs `use_module/1` to declare module dependencies.

Feel free to DM me with more details on your code and any issues found while trying the steps.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [October 10, 2019, 7:29pm UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/6 "2019-10-10T19:29:23Z")

</div>

> [@jan](#):
>
> The xref library does use term expansion

I’ve figured out why term expansion didn’t happen … I’m using EDCG, which uses `edcg:pred_info/2` facts to trigger it; and of course the xref library doesn’t load those facts, so term expansion doesn’t happen. Multifile `requires_library/2` might be a way around this, but I’ll have to study the source in library(prolog\_source) a bit more to figure out how to use it.

BTW, gxref uses a tiny font … is there any way to make it bigger? (And I might have something misconfigured with X11 – the “drag” functionality of gxref doesn’t seem to work.)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 11, 2019, 10:06am UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/7 "2019-10-11T10:06:25Z")

</div>

> [@peter.ludemann](#):
>
> but I’ll have to study the source in library(prolog\_source) a bit more to figure out how to use it

It is a bit of an old mess with too many hacks, I’m afraid ☹ You can simply preload such libraries though. Probably best is to simply load the entire program before xreffing the files.

> [@peter.ludemann](#):
>
> gxref uses a tiny font … is there any way to make it bigger

Start `?- emacs.`, go to _Edit/Edit editor preferences_ and uncomment and set `font.scale`. Then quit and restart Prolog. This is a general scaling factor that increases/reduces all fonts.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [January 15, 2020, 12:30pm UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/8 "2020-01-15T12:30:35Z")

</div>

Peter,

I am thinking this should be in the category _Help_; would that be wrong?

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [January 15, 2020, 2:24pm UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/9 "2020-01-15T14:24:57Z")

</div>

> [@EricGT](#):
>
> I am thinking this should be in the category _Help_ ; would that be wrong?

Yes.

Also, the tidbit about setting font size should go somewhere (which I had forgotten about when I recently used `tspy`). The generated file is `~/.config/swi-prolog/xpce/Defaults`, BTW.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 15, 2020, 3:16pm UTC](https://swi-prolog.discourse.group/t/generating-xref-info-with-term-expansions/1372/10 "2020-01-15T15:16:29Z")

</div>

> [@peter.ludemann](#):
>
> he generated file is `~/.config/swi-prolog/xpce/Defaults` , BTW.

Be aware though that the old names are still used by the stable branch …
