Skip to content

Commit d39f6df

Browse files
committed
Merge branch 'topic/rules/fix_one_construct_per_line' into 'master'
Fix the "One_Construct_Per_Line" rule Closes eng/codepeer/gnatcheck#63 See merge request eng/libadalang/langkit-query-language!587
2 parents 5287bb7 + 248a68b commit d39f6df

File tree

4 files changed

+127
-58
lines changed

4 files changed

+127
-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: 38 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,38 @@ 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+
| AspectClause
43+
| ComponentClause
44+
| PragmaNode
45+
| WithClause
46+
| UseClause(p_matching_with_use_clause(): false)
47+
) and node is not (
48+
# Except for enum literal, param spec, discriminant spec
49+
EnumLiteralDecl | ParamSpec | DiscriminantSpec
50+
# Or loop param or entry index.
51+
| ForLoopVarDecl | EntryIndexSpec
52+
# Also ignore anonymous or nested constructs generating false positives.
53+
| SingleTaskTypeDecl
54+
| AnonymousTypeDecl
55+
| LabelDecl
56+
| GenericSubpInternal
57+
| ConcreteFormalSubpDecl
58+
| ExtendedReturnStmtObjectDecl
59+
| NamedStmtDecl
60+
| AcceptStmtBody
61+
# Ignore generic package instantiations when they are in a generic formal
62+
| GenericPackageInstantiation(parent: GenericFormal)
63+
) and (
64+
token_end.end_line == stdlib.next_non_blank_token_line(token_end)
65+
or token_start.start_line == stdlib.previous_non_blank_token_line(token_start)
66+
)
67+
}

testsuite/tests/checks/one_construct_per_line/line.adb

Lines changed: 20 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;
@@ -21,6 +30,17 @@ package body Line is
2130
end Start;
2231
end Other_Tsk;
2332

33+
type R is record
34+
A : Boolean;
35+
B : Boolean;
36+
C : Boolean;
37+
end record;
38+
39+
for R use record
40+
A at 1 range 1 .. 2; B at 2 range 2 .. 3; -- FLAG (2);
41+
C at 3 range 3 .. 4; -- NOFLAG
42+
end record;
43+
2444
procedure Proc (I : in out Integer) is
2545
Tmp : Integer;
2646

testsuite/tests/checks/one_construct_per_line/test.out

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,94 @@ 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:40:7: rule violation: more than one construct on the same line
58+
40 | A at 1 range 1 .. 2; B at 2 range 2 .. 3; -- FLAG (2);
59+
| ^^^^^^^^^^^^^^^^^^^^
60+
61+
line.adb:40:28: rule violation: more than one construct on the same line
62+
40 | A at 1 range 1 .. 2; B at 2 range 2 .. 3; -- FLAG (2);
63+
| ^^^^^^^^^^^^^^^^^^^^
64+
65+
line.adb:49:7: rule violation: more than one construct on the same line
66+
49 | B3 : Boolean; B4 : Boolean; -- FLAG (2)
2367
| ^^
2468

25-
line.adb:29:21: rule violation: more than one construct on the same line
26-
29 | B3 : Boolean; B4 : Boolean; -- FLAG (2)
69+
line.adb:49:21: rule violation: more than one construct on the same line
70+
49 | B3 : Boolean; B4 : Boolean; -- FLAG (2)
2771
| ^^
2872

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)
73+
line.adb:53:10: rule violation: more than one construct on the same line
74+
53 | I := J; if I > 0 then -- FLAG (2)
3175
| ^^^^^^^
3276

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)
77+
line.adb:53:18: rule violation: more than one construct on the same line
78+
53 | I := J; if I > 0 then -- FLAG (2)
3579
| __________________^
36-
34 || I := 0; end if; -- FLAG
80+
54 || I := 0; end if; -- FLAG
3781
||__________________________^
3882

39-
line.adb:34:12: rule violation: more than one construct on the same line
40-
34 | I := 0; end if; -- FLAG
83+
line.adb:54:12: rule violation: more than one construct on the same line
84+
54 | I := 0; end if; -- FLAG
4185
| ^^^^^^^
4286

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
87+
line.adb:67:18: rule violation: more than one construct on the same line
88+
67 | My_Loop_2: for I in 1 .. 2 loop -- FLAG
4589
| __________________^
4690
||
4791
|| ~~~ 1 other lines ~~~
4892
||
49-
49 || end loop My_Loop_2;
93+
69 || end loop My_Loop_2;
5094
||_________________________^
5195

52-
line.adb:51:7: rule violation: more than one construct on the same line
53-
51 | Tmp := I; I := I + 1; -- FLAG (2)
96+
line.adb:71:7: rule violation: more than one construct on the same line
97+
71 | Tmp := I; I := I + 1; -- FLAG (2)
5498
| ^^^^^^^^^
5599

56-
line.adb:51:17: rule violation: more than one construct on the same line
57-
51 | Tmp := I; I := I + 1; -- FLAG (2)
100+
line.adb:71:17: rule violation: more than one construct on the same line
101+
71 | Tmp := I; I := I + 1; -- FLAG (2)
58102
| ^^^^^^^^^^^
59103

60-
line.adb:53:7: rule violation: more than one construct on the same line
61-
53 | I := I + 1; end Proc; -- FLAG
104+
line.adb:73:7: rule violation: more than one construct on the same line
105+
73 | I := I + 1; end Proc; -- FLAG
62106
| ^^^^^^^^^^^
63107

0 commit comments

Comments
 (0)