Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

CodeQuery

pad edited this page Apr 2, 2014 · 12 revisions

codequery is an interactive tool a la SQL to query information about the structure of the code (the inheritance tree, the call graph, the data graph, etc). The data is the code. The query language is Prolog (http://en.wikipedia.org/wiki/Prolog), a logic-based programming language used mainly in AI but also popular in database (http://en.wikipedia.org/wiki/Datalog). The particular Prolog implementation we use is SWI-prolog (http://www.swi-prolog.org/pldoc/refman/).

By default when you give just a directory to codequery it builds the Prolog database and then enters Prolog's read-eval-print loop. After the ?- prompt, you can enter a query followed by a dot. For instance:

$ cd /tmp/test/
$ cat foo.php
  <?php
  class A {
  }
  class B extends A {
  }
  class C extends B {
  }
$ codequery .
  generating prolog facts in /tmp/test/facts.pl
  compiling prolog facts with swipl in /tmp/test/prolog_compiled_db
  % /tmp/test/facts.pl compiled 0.00 sec, 13,984 bytes
  % /home/pad/pfff/h_program-lang/database_code.pl compiled 0.00 sec, 19,072 bytes
  ...
  Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.11.29-39-g35fdbf2)
  Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam
  SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
  and you are welcome to redistribute it under certain conditions.
  Please visit http://www.swi-prolog.org for details.

  For help, use ?- help(Topic). or ?- apropos(Word).

  ?- children(X, 'A').

Prolog will then try to find a solution to this query by unifying X with something that would satisfy the query given all the facts built into the database (see facts.pl in the same directory). But you'll get only one solution. To get the next solution type a semicolon.

See https://github.com/facebook/pfff/blob/master/main_codequery.ml

The synopsis is:

$ codequery [-lang <string>] <dir>

The facts.pl file generated in the directory will contain the set of facts about your codebase.

pfff/h_programl-lang/database_code.pl contains some helper predicates. See https://github.com/facebook/pfff/blob/master/h_program-lang/database_code.pl to know which predicates are available and what they mean.

See also https://github.com/facebook/pfff/blob/master/lang_php/analyze/foundation/unit_prolog_php.ml for example of queries.

<code> ?- children(X, 'Preparable') AccountAdminData IdentitySwitcherLinkData AccountEmailsDarkView ... </code>

Clone this wiki locally