Skip to content

Commit d714bb1

Browse files
committed
Merge branch 'topic/48' into 'master'
Handle SubtypeDecl nodes in controlled_type_declarations rule. Closes #48 See merge request eng/libadalang/langkit-query-language!219
2 parents 2ed3cf1 + 8675197 commit d714bb1

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

lkql_checker/share/lkql/controlled_type_declarations.lkql

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@
88
# declared in `Ada.Finalization' but has a controlled component is not
99
# checked.
1010

11+
import stdlib
12+
1113
fun canonical_fully_qualified_name(t) =
1214
t.f_name?.p_basic_decl()?.p_canonical_fully_qualified_name()
1315

14-
selector complete_super_types
15-
| b@BaseTypeDecl when b.p_is_private() =>
16-
rec(*this.p_next_part()?.p_base_types?())
17-
| BaseTypeDecl =>
18-
rec(*this.p_base_types())
19-
| * => ()
20-
2116
@check(message="declaration of controlled type", category="Feature")
2217
fun controlled_type_declarations(node) =
23-
node is BaseTypeDecl (
24-
any complete_super_types: b
18+
node is TypeDecl (
19+
any stdlib.complete_super_types: b
2520
when canonical_fully_qualified_name(b) == "ada.finalization.controlled"
2621
)

lkql_checker/share/lkql/stdlib.lkql

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@
55
|" library.
66

77
selector super_types
8-
|" Yields the chain of super types for the given type
8+
|" Yields the chain of super types of the given type, as viewed from that
9+
|" type. Hence, for a type T which public view derives from a type A but
10+
|" private view derives from a type B (which itself derives from A),
11+
|" invoking this selector on the public view of T will yield A.
912
| BaseTypeDecl => rec(*this.p_base_types())
1013
| * => ()
1114

15+
selector complete_super_types
16+
|" Yields the chain of super types of the given type in their most complete
17+
|" view. Hence, for a type T which public view derives from a type A but
18+
|" private view derives from a type B (which itself derives from A),
19+
|" invoking this selector on the public view of T will yield B and then A.
20+
| b@BaseTypeDecl when b.p_is_private() => {
21+
# We go through `p_base_subtype` to correctly handle SubtypeDecl nodes:
22+
# while they themselves don't have a next part, their base type may.
23+
val np = this.p_base_subtype().p_next_part();
24+
val typ = if np == null then this else np;
25+
rec(*typ.p_base_types())
26+
}
27+
| BaseTypeDecl =>
28+
rec(*this.p_base_types())
29+
| * => ()
30+
1231
selector semantic_parent
1332
|" Return all semantic parent nodes starting from a given node.
1433
| null => ()

testsuite/tests/checks/controlled_type_declarations/controlled_type_declarations.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
with Ada.Finalization;
22
package Foo is
33
type Resource is new Ada.Finalization.Controlled with private; -- FLAG
4+
5+
subtype Sub_Resource is Resource; -- NOFLAG
6+
7+
type New_Resource is new Sub_Resource; -- FLAG
48
private
59
type Resource is new Ada.Finalization.Controlled with null record; -- FLAG
610
type Ressource_Array is new Array (Positive range <>)

testsuite/tests/checks/controlled_type_declarations/test.out

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ controlled_type_declarations.ads:3:9: rule violation: declaration of controlled
22
3 | type Resource is new Ada.Finalization.Controlled with private; -- FLAG
33
| ^^^^^^^^
44

5-
controlled_type_declarations.ads:5:9: rule violation: declaration of controlled type
6-
5 | type Resource is new Ada.Finalization.Controlled with null record; -- FLAG
5+
controlled_type_declarations.ads:7:9: rule violation: declaration of controlled type
6+
7 | type New_Resource is new Sub_Resource; -- FLAG
7+
| ^^^^^^^^^^^^
8+
9+
controlled_type_declarations.ads:9:9: rule violation: declaration of controlled type
10+
9 | type Resource is new Ada.Finalization.Controlled with null record; -- FLAG
711
| ^^^^^^^^
812

0 commit comments

Comments
 (0)