Skip to content

Commit

Permalink
Merge branch 'topic/kp-18926' into 'master'
Browse files Browse the repository at this point in the history
Add detector for KP-18926

Closes #258

See merge request eng/libadalang/langkit-query-language!212
  • Loading branch information
HugoGGuerrier committed Apr 30, 2024
2 parents 5ec824e + 2b5d8f2 commit 283ff74
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lkql_checker/share/lkql/kp/KP-18926.lkql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Flag container aggregates of a type whose `Aggregate` aspect specifies both
# `Add_Unnamed` and `New_Indexed` operations, and where the associations of
# the aggregate contains an iterator specification.

import stdlib

fun aspect_define_op(aspect, op_name) =
|" Returns whether the given aspect spec defines the given operation name
aspect.f_assocs is *(
any children(depth=1): AggregateAssoc(
f_designators: *(any children(depth=1): Name(p_name_is(op_name): true))
)
)

fun is_flagged_aspect(aspect) =
|" Returns whether the given aspect should be flagged by the KP detector
match aspect
| {exists: true, value: v, ...} =>
aspect_define_op(v, "add_unnamed") and aspect_define_op(v, "new_indexed")
| * => false

@check(help="possible occurrence of KP 18926",
message="possible occurrence of KP 18926",
impact="23.*,24.1")
fun kp_18926(node) =
node is BracketAggregate
when is_flagged_aspect(node.p_expression_type().p_get_aspect("Aggregate"))
and stdlib.any([a is IteratedAssoc for a in node.f_assocs.children])
56 changes: 56 additions & 0 deletions testsuite/tests/checks/KP-18926/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
with Ada.Containers.Vectors;

procedure Main is
-- Define an integer vector type

package Integer_Vector
is new Ada.Containers.Vectors
(Index_Type => Natural,Element_Type => Integer);

subtype Vector is Integer_Vector.Vector;

-- Define a custom container with only "Add_Unnamed" assoc

type Test_Container_1 is null record
with Aggregate => (Empty => New_Empty,
Add_Unnamed => Append);

function New_Empty return Test_Container_1 is (null record);
procedure Append (Self : in out Test_Container_1;
Value : Integer) is null;

-- Define a custom container with both "Add_Unnamed" and "New_Indexed"

type Test_Container_2 is null record
with Aggregate => (Empty => New_Empty,
New_Indexed => New_Indexed_Cont,
Add_Unnamed => Append,
Assign_Indexed => Insert);

function New_Empty return Test_Container_2 is (null record);
function New_Indexed_Cont(X, Y : Integer) return Test_Container_2 is (null record);
procedure Append (Self : in out Test_Container_2;
Value : Integer) is null;
procedure Insert (Self : in out Test_Container_2;
Index : Integer;
Value : Integer) is null;

-- Create a derived container type

subtype Derived_Container is Test_Container_2;

-- Test container aggregate expressions

V : Vector := [ 1, 2, 3, 4 ]; -- NOFLAG
Copy : Vector := [ for E of V => E ]; -- FLAG
Only_Even : Vector := [ for E of V when E mod 2 = 0 => E ]; -- FLAG

C_1_1 : Test_Container_1 := [ 1, 2, 3, 4 ]; -- NOFLAG
C_1_2 : Test_Container_1 := [ for E of V => E ]; -- NOFLAG
C_2_1 : Test_Container_2 := [ 1, 2, 3, 4 ]; -- NOFLAG
C_2_2 : Test_Container_2 := [ for E of V => E ]; -- FLAG
D_C_1 : Derived_Container := [ 1, 2, 3, 4 ]; -- NOFLAG
D_C_2 : Derived_Container := [ for E of V => E ]; -- FLAG
begin
null;
end Main;
2 changes: 2 additions & 0 deletions testsuite/tests/checks/KP-18926/prj.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
project Prj is
end Prj;
16 changes: 16 additions & 0 deletions testsuite/tests/checks/KP-18926/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
main.adb:45:26: rule violation: possible occurrence of KP 18926
45 | Copy : Vector := [ for E of V => E ]; -- FLAG
| ^^^^^^^^^^^^^^^^^^^

main.adb:46:26: rule violation: possible occurrence of KP 18926
46 | Only_Even : Vector := [ for E of V when E mod 2 = 0 => E ]; -- FLAG
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

main.adb:51:32: rule violation: possible occurrence of KP 18926
51 | C_2_2 : Test_Container_2 := [ for E of V => E ]; -- FLAG
| ^^^^^^^^^^^^^^^^^^^

main.adb:53:33: rule violation: possible occurrence of KP 18926
53 | D_C_2 : Derived_Container := [ for E of V => E ]; -- FLAG
| ^^^^^^^^^^^^^^^^^^^

3 changes: 3 additions & 0 deletions testsuite/tests/checks/KP-18926/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
driver: 'checker'
rule_name: KP_18926
project: 'prj.gpr'

0 comments on commit 283ff74

Please sign in to comment.