|
| 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