Skip to content

Commit 06341f7

Browse files
committed
Implement KP detector for 19279
1 parent 6fed34e commit 06341f7

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import stdlib
2+
3+
fun contains_dynamic_bounds(agg) =
4+
|" Returns whether the given Aggregate node has dynamic bounds.
5+
|" Recurse on association expressions whose value is an Aggregate.
6+
agg is Aggregate
7+
when stdlib.any(
8+
[
9+
match assoc
10+
| IteratedAssoc => true
11+
| AggregateAssoc =>
12+
stdlib.any(
13+
[
14+
match design
15+
| BinOp =>
16+
not design.f_left.p_is_static_expr() or
17+
not design.f_right.p_is_static_expr()
18+
| AttributeRef =>
19+
design.f_attribute.p_name_is("Range") and
20+
(
21+
match design.f_prefix
22+
| tn@Name(p_name_designated_type(): not null) =>
23+
not tn.p_is_static_subtype()
24+
| n@Name =>
25+
n.p_referenced_decl()?.p_type_expression() is SubtypeIndication(
26+
p_is_static_subtype(): false
27+
)
28+
)
29+
| Name(p_name_designated_type(): not null) =>
30+
not design.p_is_static_subtype()
31+
for design in assoc.f_designators.children
32+
]
33+
)
34+
for assoc in agg.f_assocs.children
35+
]
36+
) or stdlib.any(
37+
[
38+
contains_dynamic_bounds(assoc.f_r_expr)
39+
for assoc in agg.f_assocs.children
40+
if assoc.f_r_expr is Aggregate
41+
]
42+
)
43+
44+
@check(help="possible occurrence of KP 19279",
45+
message="possible occurrence of KP 19279",
46+
impact="23.*,24.*")
47+
fun kp_19279(node) =
48+
|" Flag object intialization / assignment where the assigned expression is a
49+
|" multi-dimention array aggregate with one of the sub-aggregate having
50+
|" dynamic bounds.
51+
node is Aggregate(p_expression_type(): t@BaseTypeDecl)
52+
when t.p_root_type().f_type_def is arr@ArrayTypeDef
53+
when match arr.f_indices
54+
| c@ConstrainedArrayIndices => c.f_list.children_count > 1
55+
| u@UnconstrainedArrayIndices => u.f_types.children_count > 1
56+
and stdlib.any(
57+
[
58+
contains_dynamic_bounds(assoc.f_r_expr)
59+
for assoc in node.f_assocs.children
60+
if assoc.f_r_expr is Aggregate
61+
]
62+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
procedure Main is
2+
type Simple is array (Natural range <>) of Integer;
3+
type Matrix is array (Natural range <>, Natural range <>) of Integer;
4+
type Tensor is array (Natural range <>, Natural range <>, Natural range <>) of Integer;
5+
6+
subtype Sub_Matrix is Matrix (0 .. 2, 0 .. 2);
7+
type Der_Matrix is new Sub_Matrix;
8+
subtype Sub_Tensor is Tensor (0 .. 2, 0 .. 2, 0 .. 2);
9+
type Der_Tensor is new Sub_Tensor;
10+
11+
function Id (X : Integer) return Integer is (X);
12+
procedure Process_Sub_Matrix (M : Sub_Matrix) is
13+
begin
14+
null;
15+
end Process_Sub_Matrix;
16+
17+
subtype U is Integer range 0 .. Id (2);
18+
19+
I : Integer := 1;
20+
A_1 : Simple (1 .. 3) := (1, 2, 3);
21+
A_2 : Simple (1 .. Id (3)) := (1, 2, 3);
22+
23+
S_1 : Simple (0 .. 2) := (0 .. 2 => 42); -- NOFLAG
24+
S_2 : Simple (0 .. 2) := (Id (3) .. Id (5) => 42); -- NOFLAG
25+
26+
M_1 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (0 .. 2 => 42)); -- NOFLAG
27+
M_2 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (Id (3) .. Id (5) => 42)); -- FLAG
28+
M_3 : Matrix (0 .. 2, 0 .. 2) :=
29+
((for I in Id (3) .. Id (5) => I), (42, 42, 42), (42, 42, 42)); -- FLAG
30+
M_4 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (U'Range => 42)); -- FLAG
31+
M_5 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (A_1'Range => 42)); -- NOFLAG
32+
M_6 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (A_2'Range => 42)); -- FLAG
33+
M_7 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (U => 42)); -- FLAG
34+
M_8 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (I => 42)); -- NOFLAG
35+
S_M_1 : Sub_Matrix := (0 .. 2 => (Id (3) .. Id (5) => 42)); -- FLAG
36+
S_M_2 : Sub_Matrix := (0 .. 2 => (3 .. Id (5) => 42)); -- FLAG
37+
S_M_3 : Sub_Matrix := (Id (3) .. Id (5) => (0 .. 2 => 42)); -- NOFLAG
38+
D_M_1 : Der_Matrix := (0 .. 2 => (Id (3) .. Id (5) => 42)); -- FLAG
39+
40+
T_1 : Tensor (0 .. 2, 0 .. 2, 0 .. 2) :=
41+
(0 .. 2 => (0 .. 2 => (0 .. 2 => 42))); -- NOFLAG
42+
T_2 : Tensor (0 .. 2, 0 .. 2, 0 .. 2) :=
43+
(0 .. 2 => (0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
44+
S_T_1 : Sub_Tensor := (0 .. 2 => (0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
45+
S_T_2 : Sub_Tensor := (0 .. 2 => (Id (3) .. Id (5) => (0 .. 2 => 42))); -- FLAG
46+
S_T_3 : Sub_Tensor := (Id (3) .. Id (5) => (0 .. 2 => (0 .. 2 => 42))); -- NOFLAG
47+
D_T_1 : Der_Tensor := (0 .. 2 => (0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
48+
begin
49+
Process_Sub_Matrix (Sub_Matrix'(0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
50+
end Main;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project Prj is
2+
end Prj;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
main.adb:27:37: rule violation: possible occurrence of KP 19279
2+
27 | M_2 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (Id (3) .. Id (5) => 42)); -- FLAG
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
5+
main.adb:29:6: rule violation: possible occurrence of KP 19279
6+
29 | ((for I in Id (3) .. Id (5) => I), (42, 42, 42), (42, 42, 42)); -- FLAG
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
9+
main.adb:30:37: rule violation: possible occurrence of KP 19279
10+
30 | M_4 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (U'Range => 42)); -- FLAG
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
main.adb:32:37: rule violation: possible occurrence of KP 19279
14+
32 | M_6 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (A_2'Range => 42)); -- FLAG
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
main.adb:33:37: rule violation: possible occurrence of KP 19279
18+
33 | M_7 : Matrix (0 .. 2, 0 .. 2) := (0 .. 2 => (U => 42)); -- FLAG
19+
| ^^^^^^^^^^^^^^^^^^^^^
20+
21+
main.adb:35:26: rule violation: possible occurrence of KP 19279
22+
35 | S_M_1 : Sub_Matrix := (0 .. 2 => (Id (3) .. Id (5) => 42)); -- FLAG
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
main.adb:36:26: rule violation: possible occurrence of KP 19279
26+
36 | S_M_2 : Sub_Matrix := (0 .. 2 => (3 .. Id (5) => 42)); -- FLAG
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
29+
main.adb:38:26: rule violation: possible occurrence of KP 19279
30+
38 | D_M_1 : Der_Matrix := (0 .. 2 => (Id (3) .. Id (5) => 42)); -- FLAG
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
main.adb:43:6: rule violation: possible occurrence of KP 19279
34+
43 | (0 .. 2 => (0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
37+
main.adb:44:26: rule violation: possible occurrence of KP 19279
38+
44 | S_T_1 : Sub_Tensor := (0 .. 2 => (0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
41+
main.adb:45:26: rule violation: possible occurrence of KP 19279
42+
45 | S_T_2 : Sub_Tensor := (0 .. 2 => (Id (3) .. Id (5) => (0 .. 2 => 42))); -- FLAG
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
main.adb:47:26: rule violation: possible occurrence of KP 19279
46+
47 | D_T_1 : Der_Tensor := (0 .. 2 => (0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
49+
main.adb:49:35: rule violation: possible occurrence of KP 19279
50+
49 | Process_Sub_Matrix (Sub_Matrix'(0 .. 2 => (Id (3) .. Id (5) => 42))); -- FLAG
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
driver: 'checker'
2+
rule_name: KP_19279
3+
project: 'prj.gpr'

testsuite/tests/gnatcheck/xml_help/test.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ testsuite_driver: No output file generated by gnatcheck
154154
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
155155
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
156156
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
157+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
157158
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
158159
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
159160
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
@@ -303,6 +304,7 @@ testsuite_driver: No output file generated by gnatcheck
303304
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
304305
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
305306
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
307+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
306308
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
307309
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
308310
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
@@ -464,6 +466,7 @@ testsuite_driver: No output file generated by gnatcheck
464466
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
465467
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
466468
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
469+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
467470
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
468471
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
469472
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
@@ -647,6 +650,7 @@ testsuite_driver: No output file generated by gnatcheck
647650
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
648651
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
649652
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
653+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
650654
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
651655
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
652656
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
@@ -839,6 +843,7 @@ testsuite_driver: No output file generated by gnatcheck
839843
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
840844
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
841845
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
846+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
842847
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
843848
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
844849
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
@@ -1076,6 +1081,7 @@ testsuite_driver: No output file generated by gnatcheck
10761081
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
10771082
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
10781083
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
1084+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
10791085
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
10801086
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
10811087
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
@@ -1433,6 +1439,7 @@ testsuite_driver: No output file generated by gnatcheck
14331439
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
14341440
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
14351441
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
1442+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
14361443
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
14371444
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
14381445
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
@@ -1801,6 +1808,7 @@ testsuite_driver: No output file generated by gnatcheck
18011808
<check switch="+Rkp_19159" label="possible occurrence of KP 19159"/>
18021809
<check switch="+Rkp_19198" label="possible occurrence of KP 19198"/>
18031810
<check switch="+Rkp_19237" label="possible occurrence of KP 19237"/>
1811+
<check switch="+Rkp_19279" label="possible occurrence of KP 19279"/>
18041812
<check switch="+Rkp_19312" label="possible occurrence of KP 19312"/>
18051813
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
18061814
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>

0 commit comments

Comments
 (0)