Skip to content

Commit fa451c6

Browse files
committed
Auto-fix: Add support for 'use_record_aggregates'
1 parent 83e872d commit fa451c6

File tree

4 files changed

+106
-4
lines changed

4 files changed

+106
-4
lines changed

lkql_checker/share/lkql/use_record_aggregates.lkql

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,34 @@ fun assign_stmts(n, prefix) =
66
=> [n] & assign_stmts(n.next_sibling(), prefix)
77
| * => []
88

9+
fun replace_by_aggregate(node, ctx) =
10+
|" Replace all consecutive AssignStmt, starting from the ``node`` by a
11+
|" record aggregate.
12+
{
13+
val assigns = assign_stmts(node, node.f_dest.f_prefix);
14+
assigns.reduce(
15+
(ctx, a) => ctx.remove(a),
16+
ctx.insert_before(
17+
assigns[1],
18+
new AssignStmt(
19+
f_dest=node.f_dest.f_prefix,
20+
f_expr=new Aggregate(
21+
f_ancestor_expr=null,
22+
f_assocs=new AssocList([
23+
new AggregateAssoc(
24+
f_designators=new AlternativesList([a.f_dest.f_suffix]),
25+
f_r_expr=a.f_expr
26+
) for a in assigns
27+
].to_list)
28+
)
29+
)
30+
)
31+
)
32+
}
33+
934
@check(message="component assignments may be replaced by an aggregate",
10-
category="Style", subcategory="Programming Practice")
35+
category="Style", subcategory="Programming Practice",
36+
auto_fix=replace_by_aggregate)
1137
fun use_record_aggregates(node) =
1238
|" Flag the first statement in the sequence of assignment statements if the targets
1339
|" of all these assignment statements are components of the same record objects,

testsuite/tests/checks/use_record_aggregates/assign.adb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ procedure Assign is
1313
begin
1414
null;
1515
O1.Comp1 := 1; -- FLAG
16-
O1.Comp2 := 1; -- NOFLAG (flag only first assignment)
16+
O1.Comp2 := 2; -- NOFLAG (flag only first assignment)
1717

1818
O2.Comp1 := 1; -- NOFLAG (single component)
1919

@@ -26,7 +26,7 @@ begin
2626
begin
2727
null;
2828
O3.Comp1 := 1; -- FLAG
29-
O3.Comp2 := 1; -- NOFLAG
29+
O3.Comp2 := 2; -- NOFLAG
3030
end;
3131

3232
begin
@@ -56,7 +56,7 @@ begin
5656

5757
Obj.A := 1; -- NOFLAG (tagged)
5858
Obj.B := 1; -- NOFLAG
59-
end
59+
end;
6060

6161
declare
6262
type List is record

testsuite/tests/checks/use_record_aggregates/test.out

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,78 @@ assign.adb:28:7: rule violation: component assignments may be replaced by an agg
66
28 | O3.Comp1 := 1; -- FLAG
77
| ^^^^^^^^^^^^^^
88

9+
Patched "assign.adb":
10+
=====================
11+
12+
procedure Assign is
13+
type Rec1 is record
14+
Comp1, Comp2 : Integer;
15+
end record;
16+
17+
type Rec2 is record
18+
Comp1 : Integer;
19+
end record;
20+
21+
O1, O3 : Rec1;
22+
O2 : Rec2;
23+
24+
begin
25+
null;
26+
O1:=(Comp1 =>1,Comp2 =>2);O2.Comp1 := 1; -- NOFLAG (single component)
27+
28+
begin
29+
O1.Comp1 := 1; -- NOFLAG (not consecutive assignments on O1)
30+
O2.Comp2 := 1;
31+
O1.Comp2 := 1;
32+
end;
33+
34+
begin
35+
null;
36+
O3:=(Comp1 =>1,Comp2 =>2);end;
37+
38+
begin
39+
O3.Comp1 := 1; -- NOFLAG
40+
41+
if True then
42+
null;
43+
end if;
44+
45+
O3.Comp2 := 1; -- NOFLAG
46+
end;
47+
48+
declare
49+
type Rec_Discr (D : Integer) is record
50+
A, B : Integer;
51+
end record;
52+
53+
type Rec_Tagged is tagged record
54+
A, B : Integer;
55+
end record;
56+
57+
Discr : Rec_Discr (1);
58+
Obj : Rec_Tagged;
59+
begin
60+
Discr.A := 1; -- NOFLAG (discriminants)
61+
Discr.B := 1; -- NOFLAG
62+
63+
Obj.A := 1; -- NOFLAG (tagged)
64+
Obj.B := 1; -- NOFLAG
65+
end;
66+
67+
declare
68+
type List is record
69+
Prev, Next : Integer;
70+
end record;
71+
72+
type Rec is record
73+
L : List;
74+
end record;
75+
76+
Obj1, Obj2 : Rec;
77+
78+
begin
79+
Obj1.L.Next := 0; -- NOFLAG
80+
Obj2.L.Prev := 0; -- NOFLAG
81+
end;
82+
end Assign;
83+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
driver: 'checker'
22
rule_name: use_record_aggregates
33
project: 'prj.gpr'
4+
auto_fix: True

0 commit comments

Comments
 (0)