Skip to content

Commit e87c337

Browse files
committed
one_construct_per_line: Add support for with/use clauses and pragmas
1 parent 5287bb7 commit e87c337

File tree

4 files changed

+110
-58
lines changed

4 files changed

+110
-58
lines changed

lkql_checker/doc/generated/predefined_rules.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6230,10 +6230,9 @@ this is preceding by a declaration of a program unit spec, stub or body.
62306230

62316231
.. index:: One_Construct_Per_Line
62326232

6233-
Flag any statement, declaration or representation clause if the code
6234-
line where this construct starts contains some other Ada code symbols
6235-
preceding or following this construct. The following constructs are not
6236-
flagged:
6233+
Flag any statement, declaration or clause if the code line where this
6234+
construct starts contains some other Ada code symbols preceding or
6235+
following this construct. The following constructs are not flagged:
62376236

62386237
* enumeration literal specification;
62396238
* parameter specifications;

lkql_checker/share/lkql/one_construct_per_line.lkql

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import stdlib
33
@check(message="more than one construct on the same line", remediation="EASY",
44
category="Style", subcategory="Readability")
55
fun one_construct_per_line(node) =
6-
|" Flag any statement, declaration or representation clause if the code
7-
|" line where this construct starts contains some other Ada code symbols
8-
|" preceding or following this construct. The following constructs are not
9-
|" flagged:
6+
|" Flag any statement, declaration or clause if the code line where this
7+
|" construct starts contains some other Ada code symbols preceding or
8+
|" following this construct. The following constructs are not flagged:
109
|"
1110
|" * enumeration literal specification;
1211
|" * parameter specifications;
@@ -31,31 +30,40 @@ fun one_construct_per_line(node) =
3130
|" Tmp := I;
3231
|" I := J; J := Tmp; -- FLAG
3332
|" end Swap;
34-
# Flag any statement, declaration or representation clause
35-
node is (
36-
Stmt
37-
| BasicDecl
38-
| AttributeDefClause
39-
| EnumRepClause
40-
| RecordRepClause
41-
| AtClause
42-
) when node is not (
43-
# Except for enum literal, param spec, discriminant spec
44-
EnumLiteralDecl | ParamSpec | DiscriminantSpec
45-
# Or loop param or entry index.
46-
| ForLoopVarDecl | EntryIndexSpec
47-
# Also ignore anonymous or nested constructs generating false positives.
48-
| SingleTaskTypeDecl
49-
| AnonymousTypeDecl
50-
| LabelDecl
51-
| GenericSubpInternal
52-
| ConcreteFormalSubpDecl
53-
| ExtendedReturnStmtObjectDecl
54-
| NamedStmtDecl
55-
| AcceptStmtBody
56-
# Ignore generic package instantiations when they are in a generic formal
57-
| GenericPackageInstantiation(parent: GenericFormal)
58-
) and (
59-
node.token_end().end_line == stdlib.next_non_blank_token_line(node.token_end())
60-
or node.token_start().start_line == stdlib.previous_non_blank_token_line(node.token_start())
61-
)
33+
{
34+
val token_start = node.token_start();
35+
val token_end = match node
36+
| WithClause when node.next_sibling() is UseClause => node.next_sibling().token_end()
37+
| * => node.token_end();
38+
39+
node is (
40+
Stmt
41+
| BasicDecl
42+
| AttributeDefClause
43+
| EnumRepClause
44+
| RecordRepClause
45+
| AtClause
46+
| PragmaNode
47+
| WithClause
48+
| UseClause(p_matching_with_use_clause(): false)
49+
) and node is not (
50+
# Except for enum literal, param spec, discriminant spec
51+
EnumLiteralDecl | ParamSpec | DiscriminantSpec
52+
# Or loop param or entry index.
53+
| ForLoopVarDecl | EntryIndexSpec
54+
# Also ignore anonymous or nested constructs generating false positives.
55+
| SingleTaskTypeDecl
56+
| AnonymousTypeDecl
57+
| LabelDecl
58+
| GenericSubpInternal
59+
| ConcreteFormalSubpDecl
60+
| ExtendedReturnStmtObjectDecl
61+
| NamedStmtDecl
62+
| AcceptStmtBody
63+
# Ignore generic package instantiations when they are in a generic formal
64+
| GenericPackageInstantiation(parent: GenericFormal)
65+
) and (
66+
token_end.end_line == stdlib.next_non_blank_token_line(token_end)
67+
or token_start.start_line == stdlib.previous_non_blank_token_line(token_start)
68+
)
69+
}

testsuite/tests/checks/one_construct_per_line/line.adb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
with Ada.Text_IO; with Ada.Strings.Unbounded; with Ada.Strings.Fixed; -- FLAG (3)
2+
with Ada.Strings.Wide_Wide_Fixed; use Ada.Strings.Wide_Wide_Fixed; -- NOFLAG
3+
4+
with Ada.Strings.Wide_Fixed; use Ada.Strings.Wide_Fixed; with Ada.Strings.Wide_Unbounded; use Ada.Strings.Wide_Unbounded; -- FLAG (2)
5+
16
package body Line is
27

8+
use Ada.Text_IO; use Ada.Strings.Unbounded; -- FLAG (2)
9+
10+
pragma Annotate (gnatcheck, Exempt_On, "Goto_Statements", "because"); pragma Annotate (gnatcheck, Exempt_Off, "Goto_Statements"); -- FLAG (2)
11+
312
task type Tsk is
413
entry Start (I : Integer; B : Boolean); -- NOFLAG
514
end Tsk;

testsuite/tests/checks/one_construct_per_line/test.out

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,86 @@ line.ads:20:14: rule violation: more than one construct on the same line
1414
20 | procedure Proc (I : in out Integer); end Line; -- FLAG
1515
| ^^^^
1616

17-
line.adb:20:50: rule violation: more than one construct on the same line
18-
20 | accept Start (I : Integer; B : Boolean) do null; -- FLAG
17+
line.adb:1:1: rule violation: more than one construct on the same line
18+
1 | with Ada.Text_IO; with Ada.Strings.Unbounded; with Ada.Strings.Fixed; -- FLAG (3)
19+
| ^^^^^^^^^^^^^^^^^
20+
21+
line.adb:1:19: rule violation: more than one construct on the same line
22+
1 | with Ada.Text_IO; with Ada.Strings.Unbounded; with Ada.Strings.Fixed; -- FLAG (3)
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
line.adb:1:47: rule violation: more than one construct on the same line
26+
1 | with Ada.Text_IO; with Ada.Strings.Unbounded; with Ada.Strings.Fixed; -- FLAG (3)
27+
| ^^^^^^^^^^^^^^^^^^^^^^^
28+
29+
line.adb:4:1: rule violation: more than one construct on the same line
30+
4 | with Ada.Strings.Wide_Fixed; use Ada.Strings.Wide_Fixed; with Ada.Strings.Wide_Unbounded; use Ada.Strings.Wide_Unbounded; -- FLAG (2)
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
line.adb:4:58: rule violation: more than one construct on the same line
34+
4 | with Ada.Strings.Wide_Fixed; use Ada.Strings.Wide_Fixed; with Ada.Strings.Wide_Unbounded; use Ada.Strings.Wide_Unbounded; -- FLAG (2)
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
37+
line.adb:8:4: rule violation: more than one construct on the same line
38+
8 | use Ada.Text_IO; use Ada.Strings.Unbounded; -- FLAG (2)
39+
| ^^^^^^^^^^^^^^^^
40+
41+
line.adb:8:21: rule violation: more than one construct on the same line
42+
8 | use Ada.Text_IO; use Ada.Strings.Unbounded; -- FLAG (2)
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
line.adb:10:4: rule violation: more than one construct on the same line
46+
10 | pragma Annotate (gnatcheck, Exempt_On, "Goto_Statements", "because"); pragma Annotate (gnatcheck, Exempt_Off, "Goto_Statements"); -- FLAG (2)
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
49+
line.adb:10:74: rule violation: more than one construct on the same line
50+
10 | pragma Annotate (gnatcheck, Exempt_On, "Goto_Statements", "because"); pragma Annotate (gnatcheck, Exempt_Off, "Goto_Statements"); -- FLAG (2)
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
53+
line.adb:29:50: rule violation: more than one construct on the same line
54+
29 | accept Start (I : Integer; B : Boolean) do null; -- FLAG
1955
| ^^^^^
2056

21-
line.adb:29:7: rule violation: more than one construct on the same line
22-
29 | B3 : Boolean; B4 : Boolean; -- FLAG (2)
57+
line.adb:38:7: rule violation: more than one construct on the same line
58+
38 | B3 : Boolean; B4 : Boolean; -- FLAG (2)
2359
| ^^
2460

25-
line.adb:29:21: rule violation: more than one construct on the same line
26-
29 | B3 : Boolean; B4 : Boolean; -- FLAG (2)
61+
line.adb:38:21: rule violation: more than one construct on the same line
62+
38 | B3 : Boolean; B4 : Boolean; -- FLAG (2)
2763
| ^^
2864

29-
line.adb:33:10: rule violation: more than one construct on the same line
30-
33 | I := J; if I > 0 then -- FLAG (2)
65+
line.adb:42:10: rule violation: more than one construct on the same line
66+
42 | I := J; if I > 0 then -- FLAG (2)
3167
| ^^^^^^^
3268

33-
line.adb:33:18: rule violation: more than one construct on the same line
34-
33 | I := J; if I > 0 then -- FLAG (2)
69+
line.adb:42:18: rule violation: more than one construct on the same line
70+
42 | I := J; if I > 0 then -- FLAG (2)
3571
| __________________^
36-
34 || I := 0; end if; -- FLAG
72+
43 || I := 0; end if; -- FLAG
3773
||__________________________^
3874

39-
line.adb:34:12: rule violation: more than one construct on the same line
40-
34 | I := 0; end if; -- FLAG
75+
line.adb:43:12: rule violation: more than one construct on the same line
76+
43 | I := 0; end if; -- FLAG
4177
| ^^^^^^^
4278

43-
line.adb:47:18: rule violation: more than one construct on the same line
44-
47 | My_Loop_2: for I in 1 .. 2 loop -- FLAG
79+
line.adb:56:18: rule violation: more than one construct on the same line
80+
56 | My_Loop_2: for I in 1 .. 2 loop -- FLAG
4581
| __________________^
4682
||
4783
|| ~~~ 1 other lines ~~~
4884
||
49-
49 || end loop My_Loop_2;
85+
58 || end loop My_Loop_2;
5086
||_________________________^
5187

52-
line.adb:51:7: rule violation: more than one construct on the same line
53-
51 | Tmp := I; I := I + 1; -- FLAG (2)
88+
line.adb:60:7: rule violation: more than one construct on the same line
89+
60 | Tmp := I; I := I + 1; -- FLAG (2)
5490
| ^^^^^^^^^
5591

56-
line.adb:51:17: rule violation: more than one construct on the same line
57-
51 | Tmp := I; I := I + 1; -- FLAG (2)
92+
line.adb:60:17: rule violation: more than one construct on the same line
93+
60 | Tmp := I; I := I + 1; -- FLAG (2)
5894
| ^^^^^^^^^^^
5995

60-
line.adb:53:7: rule violation: more than one construct on the same line
61-
53 | I := I + 1; end Proc; -- FLAG
96+
line.adb:62:7: rule violation: more than one construct on the same line
97+
62 | I := I + 1; end Proc; -- FLAG
6298
| ^^^^^^^^^^^
6399

0 commit comments

Comments
 (0)