Skip to content

Commit 283ff74

Browse files
committed
Merge branch 'topic/kp-18926' into 'master'
Add detector for KP-18926 Closes #258 See merge request eng/libadalang/langkit-query-language!212
2 parents 5ec824e + 2b5d8f2 commit 283ff74

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Flag container aggregates of a type whose `Aggregate` aspect specifies both
2+
# `Add_Unnamed` and `New_Indexed` operations, and where the associations of
3+
# the aggregate contains an iterator specification.
4+
5+
import stdlib
6+
7+
fun aspect_define_op(aspect, op_name) =
8+
|" Returns whether the given aspect spec defines the given operation name
9+
aspect.f_assocs is *(
10+
any children(depth=1): AggregateAssoc(
11+
f_designators: *(any children(depth=1): Name(p_name_is(op_name): true))
12+
)
13+
)
14+
15+
fun is_flagged_aspect(aspect) =
16+
|" Returns whether the given aspect should be flagged by the KP detector
17+
match aspect
18+
| {exists: true, value: v, ...} =>
19+
aspect_define_op(v, "add_unnamed") and aspect_define_op(v, "new_indexed")
20+
| * => false
21+
22+
@check(help="possible occurrence of KP 18926",
23+
message="possible occurrence of KP 18926",
24+
impact="23.*,24.1")
25+
fun kp_18926(node) =
26+
node is BracketAggregate
27+
when is_flagged_aspect(node.p_expression_type().p_get_aspect("Aggregate"))
28+
and stdlib.any([a is IteratedAssoc for a in node.f_assocs.children])
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
with Ada.Containers.Vectors;
2+
3+
procedure Main is
4+
-- Define an integer vector type
5+
6+
package Integer_Vector
7+
is new Ada.Containers.Vectors
8+
(Index_Type => Natural,Element_Type => Integer);
9+
10+
subtype Vector is Integer_Vector.Vector;
11+
12+
-- Define a custom container with only "Add_Unnamed" assoc
13+
14+
type Test_Container_1 is null record
15+
with Aggregate => (Empty => New_Empty,
16+
Add_Unnamed => Append);
17+
18+
function New_Empty return Test_Container_1 is (null record);
19+
procedure Append (Self : in out Test_Container_1;
20+
Value : Integer) is null;
21+
22+
-- Define a custom container with both "Add_Unnamed" and "New_Indexed"
23+
24+
type Test_Container_2 is null record
25+
with Aggregate => (Empty => New_Empty,
26+
New_Indexed => New_Indexed_Cont,
27+
Add_Unnamed => Append,
28+
Assign_Indexed => Insert);
29+
30+
function New_Empty return Test_Container_2 is (null record);
31+
function New_Indexed_Cont(X, Y : Integer) return Test_Container_2 is (null record);
32+
procedure Append (Self : in out Test_Container_2;
33+
Value : Integer) is null;
34+
procedure Insert (Self : in out Test_Container_2;
35+
Index : Integer;
36+
Value : Integer) is null;
37+
38+
-- Create a derived container type
39+
40+
subtype Derived_Container is Test_Container_2;
41+
42+
-- Test container aggregate expressions
43+
44+
V : Vector := [ 1, 2, 3, 4 ]; -- NOFLAG
45+
Copy : Vector := [ for E of V => E ]; -- FLAG
46+
Only_Even : Vector := [ for E of V when E mod 2 = 0 => E ]; -- FLAG
47+
48+
C_1_1 : Test_Container_1 := [ 1, 2, 3, 4 ]; -- NOFLAG
49+
C_1_2 : Test_Container_1 := [ for E of V => E ]; -- NOFLAG
50+
C_2_1 : Test_Container_2 := [ 1, 2, 3, 4 ]; -- NOFLAG
51+
C_2_2 : Test_Container_2 := [ for E of V => E ]; -- FLAG
52+
D_C_1 : Derived_Container := [ 1, 2, 3, 4 ]; -- NOFLAG
53+
D_C_2 : Derived_Container := [ for E of V => E ]; -- FLAG
54+
begin
55+
null;
56+
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
main.adb:45:26: rule violation: possible occurrence of KP 18926
2+
45 | Copy : Vector := [ for E of V => E ]; -- FLAG
3+
| ^^^^^^^^^^^^^^^^^^^
4+
5+
main.adb:46:26: rule violation: possible occurrence of KP 18926
6+
46 | Only_Even : Vector := [ for E of V when E mod 2 = 0 => E ]; -- FLAG
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
9+
main.adb:51:32: rule violation: possible occurrence of KP 18926
10+
51 | C_2_2 : Test_Container_2 := [ for E of V => E ]; -- FLAG
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
main.adb:53:33: rule violation: possible occurrence of KP 18926
14+
53 | D_C_2 : Derived_Container := [ for E of V => E ]; -- FLAG
15+
| ^^^^^^^^^^^^^^^^^^^
16+
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_18926
3+
project: 'prj.gpr'

0 commit comments

Comments
 (0)