Skip to content

Commit 2e3f6ed

Browse files
committed
[SB05-065] user_manual: Add doc for lkql_checker in the user manual
Change-Id: Ib6381b125bbf9f2486c228f3032a36f0ad16e1b5
1 parent 7702cf6 commit 2e3f6ed

File tree

3 files changed

+105
-42
lines changed

3 files changed

+105
-42
lines changed

lkql_checker/README.md

+2-42
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,9 @@ Prototype checker/linter based on LKQL and Libadalang.
55
The following instructions are to be interpreted in this `lkql_checker`
66
repo subdirectory.
77

8-
## How to write checks
8+
You will find more information about how to write rules in the LKQL user
9+
manual.
910

10-
Checks are written in the LKQL language, and put in the
11-
[`share/lkql`](share/lkql) directory. Each `.lkql` file represents a distinct
12-
checker. The naming convention of the checkers is
13-
``lowercase_with_underscores``.
14-
15-
Here is a simple checker example, that will just flag every body.
16-
17-
```
18-
@check
19-
fun bodies(node) = node is Body
20-
```
21-
22-
Adding this source in the `body.lkql` file in the `share/lkql` directory will
23-
add a check to LKQL checker dynamically, without need to recompile LKQL
24-
checker.
25-
26-
### Boolean checks
27-
28-
Boolean checks are functions that take a node and return a boolean. They
29-
usually contain an `is` pattern match as the main expression:
30-
31-
```
32-
@check
33-
fun goto(node) = node is GotoStmt
34-
```
35-
36-
But are not limited to this, and can contain arbitrary expressions as long as
37-
they return a boolean.
38-
39-
### Node checks
40-
41-
Node checks are functions that take a node and return another node or null.
42-
They allow more flexibility than boolean checks, but are a bit more verbose.
43-
Here is how you would express the above check with a `@node_check`:
44-
45-
```
46-
@node_check
47-
fun goto(node) = case node is
48-
| GotoStmt => node
49-
| * => null
50-
```
5111
## Running
5212

5313
Running the checker will by default run all the checks.

user_manual/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Welcome to LKQL's documentation!
1212

1313
language_reference
1414
lkql_api_doc
15+
lkql_checker
1516

1617

1718

user_manual/source/lkql_checker.rst

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
LKQL checker
2+
============
3+
4+
LKQL checker is a prototype checker/linter based on LKQL and Libadalang. Its
5+
infrastructure is meant to be used in the next iteration of the GNATcheck
6+
technology.
7+
8+
The following instructions are to be interpreted in this ``lkql_checker`` repo
9+
subdirectory.
10+
11+
How to write checks
12+
-------------------
13+
14+
Checks are written in the LKQL language, and put in the
15+
```share/lkql`` <share/lkql>`__ directory. Each ``.lkql`` file
16+
represents a distinct checker. The naming convention of the checkers is
17+
``lowercase_with_underscores``.
18+
19+
Here is a simple checker example, that will just flag every body.
20+
21+
.. code-block:: lkql
22+
23+
@check
24+
fun bodies(node) = node is Body
25+
26+
Adding this source in the ``body.lkql`` file in the ``share/lkql``
27+
directory will add a check to LKQL checker dynamically, without need to
28+
recompile LKQL checker.
29+
30+
Boolean checks
31+
~~~~~~~~~~~~~~
32+
33+
Boolean checks are functions that take a node and return a boolean. They
34+
usually contain an ``is`` pattern match as the main expression:
35+
36+
.. code-block:: lkql
37+
38+
@check
39+
fun goto(node) = node is GotoStmt
40+
41+
But are not limited to this, and can contain arbitrary expressions as
42+
long as they return a boolean.
43+
44+
Node checks
45+
~~~~~~~~~~~
46+
47+
Node checks are functions that take a node and return another node or
48+
null. They allow more flexibility than boolean checks, but are a bit
49+
more verbose. Here is how you would express the above check with a
50+
``@node_check``:
51+
52+
.. code-block:: lkql
53+
54+
@node_check
55+
fun goto(node) = match node
56+
| GotoStmt => node
57+
| * => null
58+
59+
Checks arguments
60+
~~~~~~~~~~~~~~~~
61+
62+
Checks can take different optional arguments:
63+
64+
* `message`: The custom message that is to be shown for a given check on the
65+
command line.
66+
67+
* `follow_generic_instantiations`: Whether to follow generic instantiations
68+
during the traversal of given Ada units. If `true`, generic instantiations
69+
will be traversed in instantiated form.
70+
71+
Here is an example check:
72+
73+
.. code-block:: lkql
74+
75+
@check(message="integer object declaration", follow_generic_instantiations=true)
76+
fun int_obj_decl(node) =
77+
|" Will flag object declarations for which the type is the standard
78+
|" ``Integer`` type
79+
node is o @ ObjectDecl(
80+
p_type_expression() is SubtypeIndication(
81+
p_designated_type_decl() is t @ * when t == o.p_std_entity("Integer")
82+
)
83+
)
84+
85+
Running
86+
-------
87+
88+
Running the checker will by default run all the checks.
89+
90+
.. code-block:: sh
91+
92+
bin/lkql_checker [-P project | list of files]
93+
94+
If you want to run a specific check, you can add the name of the check
95+
after ``-r``:
96+
97+
.. code-block:: sh
98+
99+
bin/lkql_checker [-P project | list of files] -r rule_name
100+
101+
There is no way to list checks from the command line for now, just
102+
explore the ``share/lkql`` directory.

0 commit comments

Comments
 (0)