Skip to content

Commit 2c9a536

Browse files
committed
Fix the 'unconstrained_array_returns' rule to handle derived and subtypes
1 parent f6018d9 commit 2c9a536

File tree

4 files changed

+74
-26
lines changed

4 files changed

+74
-26
lines changed
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
import stdlib
22

3-
fun unconstrained_array_type(type, except_string) =
4-
match type
5-
| SubtypeDecl => (
6-
type.f_subtype.f_constraint is not Constraint and
7-
unconstrained_array_type(type.p_canonical_type(), except_string)
8-
)
9-
| TypeDecl(f_type_def: def@DerivedTypeDef) => (
10-
def.f_subtype_indication.f_constraint is not Constraint and
11-
unconstrained_array_type(type.p_base_type(), except_string)
12-
)
13-
| TypeDecl(
14-
p_is_array_type(): true,
15-
f_type_def: ArrayTypeDef(f_indices: UnconstrainedArrayIndices)
16-
) => if except_string then type.p_root_type().p_fully_qualified_name().to_lower_case != "standard.string"
17-
183
@check(message="function returns unconstrained array",
194
follow_generic_instantiations=true, category="Feature")
205
fun unconstrained_array_returns(node, except_string = false) =
216
|" Flag each function returning an unconstrained array. Function declarations,
227
|" function bodies (and body stubs) having no separate specifications,
238
|" and generic function instantiations are flagged.
24-
|" Function calls and function renamings are
25-
|" not flagged.
9+
|" Function calls and function renamings are not flagged.
2610
|"
2711
|" Generic function declarations, and function declarations in generic
2812
|" packages, are not flagged. Instead, this rule flags the results of
@@ -46,13 +30,31 @@ fun unconstrained_array_returns(node, except_string = false) =
4630
|"
4731
|" function F1 (I : Integer) return Arr; -- FLAG
4832
|" function F2 (I : Integer) return Arr_S;
49-
node is (AbstractSubpDecl | SubpDecl | GenericSubpInternal |
50-
BaseSubpBody(p_previous_part(): null) |
51-
SubpBodyStub(p_previous_part(): null))
52-
53-
when (not stdlib.in_generic_template(node))
33+
{
34+
fun is_unconstrained_array(type_decl) =
35+
|" Get whether the provided TypeDecl is declaring an unconstrained array type.
36+
|" If `type_decl` designate the "standard.string" type and `except_string` is
37+
|" `true`, the result of this function is `false`.
38+
type_decl is TypeDecl(
39+
p_is_array_type(): true,
40+
f_type_def: ArrayTypeDef(f_indices: UnconstrainedArrayIndices)
41+
) when (
42+
if except_string
43+
then type_decl.p_fully_qualified_name().to_lower_case != "standard.string"
44+
);
5445

55-
and node.f_subp_spec is SubpSpec(f_subp_returns:
56-
TypeExpr(p_designated_type_decl():
57-
t@BaseTypeDecl
58-
when unconstrained_array_type(t, except_string)))
46+
node is (
47+
AbstractSubpDecl |
48+
SubpDecl |
49+
GenericSubpInternal |
50+
BaseSubpBody(p_previous_part(): null) |
51+
SubpBodyStub(p_previous_part(): null)
52+
)
53+
and (not stdlib.in_generic_template(node))
54+
and node.f_subp_spec is SubpSpec(
55+
f_subp_returns: TypeExpr(
56+
p_is_definite_subtype(): false,
57+
p_designated_type_decl(): t@BaseTypeDecl when is_unconstrained_array(t.p_root_type())
58+
)
59+
)
60+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Derived is
2+
3+
type Constrained is new String (1 .. 6);
4+
type D_Constrained is new Constrained;
5+
6+
type Unconstrained is new String;
7+
type D_Unconstrained is new Unconstrained;
8+
9+
function Get_Constrained return Constrained is ("123456"); -- NOFLAG
10+
function Get_D_Constrained return D_Constrained is ("123456"); -- NOFLAG
11+
12+
function Get_Unconstrained return Unconstrained is (""); -- FLAG
13+
function Get_D_Unconstrained return D_Unconstrained is (""); -- FLAG
14+
15+
end Derived;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Subtypes is
2+
3+
subtype Constrained is String (1 .. 6);
4+
subtype S_Constrained is Constrained;
5+
6+
subtype Unconstrained is String;
7+
subtype S_Unconstrained is Unconstrained;
8+
9+
function Get_Constrained return Constrained is ("123456"); -- NOFLAG
10+
function Get_S_Constrained return S_Constrained is ("123456"); -- NOFLAG
11+
12+
function Get_Unconstrained return Unconstrained is (""); -- FLAG
13+
function Get_S_Unconstrained return S_Unconstrained is (""); -- FLAG
14+
15+
end Subtypes;

testsuite/tests/checks/unconstrained_array_returns/test.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
derived.ads:12:13: rule violation: function returns unconstrained array
2+
12 | function Get_Unconstrained return Unconstrained is (""); -- FLAG
3+
| ^^^^^^^^^^^^^^^^^
4+
5+
derived.ads:13:13: rule violation: function returns unconstrained array
6+
13 | function Get_D_Unconstrained return D_Unconstrained is (""); -- FLAG
7+
| ^^^^^^^^^^^^^^^^^^^
8+
19
generics.ads:6:13: rule violation: function returns unconstrained array
210
6 | function Image (X : Integer) return String; -- FLAG
311
| ^^^^^
@@ -38,3 +46,11 @@ stub.adb:8:13: rule violation: function returns unconstrained array
3846
8 | function Subp2 (I : Integer) return String is separate; -- FLAG
3947
| ^^^^^
4048

49+
subtypes.ads:12:13: rule violation: function returns unconstrained array
50+
12 | function Get_Unconstrained return Unconstrained is (""); -- FLAG
51+
| ^^^^^^^^^^^^^^^^^
52+
53+
subtypes.ads:13:13: rule violation: function returns unconstrained array
54+
13 | function Get_S_Unconstrained return S_Unconstrained is (""); -- FLAG
55+
| ^^^^^^^^^^^^^^^^^^^
56+

0 commit comments

Comments
 (0)