Skip to content

Commit 9cd340e

Browse files
committed
Implement KP detector for 19501
1 parent 09458e7 commit 9cd340e

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import stdlib
2+
3+
fun is_static_int_attr_ref(expr) =
4+
|" Returns whether the given expression is an attribute reference which
5+
|" value is a static (compilation known) universal integer.
6+
expr is AttributeRef
7+
when expr.p_expression_type() == expr.p_universal_int_type()
8+
and expr.p_is_static_expr()
9+
10+
fun is_dynamic_subtype_formal(formal) =
11+
|" Returns whether the given formal parameter DefiningName has a dynamic
12+
|" subtype.
13+
formal is DefiningName(p_basic_decl(): decl@BasicDecl)
14+
when decl.f_type_expr is (SubtypeIndication | Name)(
15+
p_is_static_subtype(): false
16+
)
17+
18+
@check(help="possible occurrence of KP 19501",
19+
message="possible occurrence of KP 19501",
20+
impact="7.1.*,7.2.*,7.3.*,7.4.*,17.*,18.*,19.*,20.*,21.*,22.*,23.*,24.*")
21+
fun kp_19501(node) =
22+
|" Flag all call expressions which include at least one known problematic
23+
|" formal/actual parameter pair.
24+
node is CallExpr(p_is_call(): true)
25+
when stdlib.any(
26+
[
27+
is_static_int_attr_ref(p.actual) and
28+
is_dynamic_subtype_formal(p.param)
29+
for p in node.p_call_params()
30+
]
31+
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
procedure Main is
2+
function Id (B : Boolean) return Boolean is (B);
3+
function Id (I : Integer) return Integer is (I);
4+
5+
type Rec (D : Boolean) is null record;
6+
subtype Stat_Const_Rec is Rec (True);
7+
subtype Dyn_Const_Rec is Rec (Id (True));
8+
9+
type Arr is array (Integer range <>) of Integer;
10+
subtype Stat_Const_Arr is Arr (1 .. 3);
11+
subtype Dyn_Const_Arr is Arr (1 .. Id (3));
12+
13+
subtype Stat_Int is Integer range 1 .. 3;
14+
subtype Dyn_Int is Integer range Id (1) .. Id (3);
15+
16+
subtype Stat_Pred_Int is Integer
17+
with Static_Predicate => Stat_Pred_Int in 1 .. 5;
18+
subtype Dyn_Pred_Int is Integer
19+
with Dynamic_Predicate => Dyn_Pred_Int < 50;
20+
21+
S : String := "Hello";
22+
C_S : constant String := "world";
23+
24+
procedure Process_Int (I : Integer) is
25+
begin
26+
null;
27+
end Process_Int;
28+
29+
procedure Process_Stat_Int (I : Stat_Int) is
30+
begin
31+
null;
32+
end Process_Stat_Int;
33+
34+
procedure Process_Dyn_Int (I : Dyn_Int) is
35+
begin
36+
null;
37+
end Process_Dyn_Int;
38+
39+
procedure Process_Stat_Pred_Int (I : Stat_Pred_Int) is
40+
begin
41+
null;
42+
end Process_Stat_Pred_Int;
43+
44+
procedure Process_Dyn_Pred_Int (I : Dyn_Pred_Int) is
45+
begin
46+
null;
47+
end Process_Dyn_Pred_Int;
48+
49+
procedure Process_Multiple (I : Dyn_Pred_Int; J : Stat_Pred_Int) is
50+
begin
51+
null;
52+
end Process_Multiple;
53+
begin
54+
Process_Int (S'Length); -- NOFLAG
55+
Process_Int (S'Size); -- NOFLAG
56+
Process_Int (C_S'Length); -- NOFLAG
57+
Process_Int (C_S'Size); -- NOFLAG
58+
Process_Stat_Int (S'Length); -- NOFLAG
59+
Process_Stat_Int (S'Size); -- NOFLAG
60+
Process_Stat_Int (C_S'Length); -- NOFLAG
61+
Process_Stat_Int (C_S'Size); -- NOFLAG
62+
Process_Dyn_Int (S'Length); -- NOFLAG
63+
Process_Dyn_Int (S'Size); -- NOFLAG
64+
Process_Dyn_Int (C_S'Length); -- FLAG
65+
Process_Dyn_Int (C_S'Size); -- NOFLAG
66+
Process_Stat_Pred_Int (S'Length); -- NOFLAG
67+
Process_Stat_Pred_Int (S'Size); -- NOFLAG
68+
Process_Stat_Pred_Int (C_S'Length); -- NOFLAG
69+
Process_Stat_Pred_Int (C_S'Size); -- NOFLAG
70+
Process_Dyn_Pred_Int (S'Length); -- NOFLAG
71+
Process_Dyn_Pred_Int (S'Size); -- NOFLAG
72+
Process_Dyn_Pred_Int (C_S'Length); -- FLAG
73+
Process_Dyn_Pred_Int (C_S'Size); -- NOFLAG
74+
75+
Process_Multiple (S'Length, S'Size); -- NOFLAG
76+
Process_Multiple (C_S'Length, C_S'Size); -- FLAG
77+
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
main.adb:64:4: rule violation: possible occurrence of KP 19501
2+
64 | Process_Dyn_Int (C_S'Length); -- FLAG
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
5+
main.adb:72:4: rule violation: possible occurrence of KP 19501
6+
72 | Process_Dyn_Pred_Int (C_S'Length); -- FLAG
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
9+
main.adb:76:4: rule violation: possible occurrence of KP 19501
10+
76 | Process_Multiple (C_S'Length, C_S'Size); -- FLAG
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
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_19501
3+
project: prj.gpr

testsuite/tests/gnatcheck/xml_help/test.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ testsuite_driver: No output file generated by gnatcheck
8282
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
8383
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
8484
<check switch="+Rkp_19447" label="possible occurrence of KP 19447"/>
85+
<check switch="+Rkp_19501" label="possible occurrence of KP 19501"/>
8586
<check switch="+Rkp_ob03_009" label="possible occurrence of KP OB03-009"/>
8687
<check switch="+Rkp_p226_024" label="possible occurrence of KP P226-024 - global analysis required"/>
8788
<check switch="+Rkp_q309_014" label="possible occurrence of KP Q309-014"/>
@@ -587,6 +588,7 @@ testsuite_driver: No output file generated by gnatcheck
587588
<check switch="+Rkp_19341" label="possible occurrence of KP 19341"/>
588589
<check switch="+Rkp_19423" label="possible occurrence of KP 19423"/>
589590
<check switch="+Rkp_19447" label="possible occurrence of KP 19447"/>
591+
<check switch="+Rkp_19501" label="possible occurrence of KP 19501"/>
590592
<check switch="+Rkp_ob03_009" label="possible occurrence of KP OB03-009"/>
591593
<check switch="+Rkp_p226_024" label="possible occurrence of KP P226-024 - global analysis required"/>
592594
<check switch="+Rkp_q309_014" label="possible occurrence of KP Q309-014"/>

0 commit comments

Comments
 (0)