-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'topic/kp-18926' into 'master'
Add detector for KP-18926 Closes #258 See merge request eng/libadalang/langkit-query-language!212
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
project Prj is | ||
end Prj; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
driver: 'checker' | ||
rule_name: KP_18926 | ||
project: 'prj.gpr' |