Skip to content

Commit 88eebf1

Browse files
committed
Merge branch 'topic/function_out_parameters' into 'master'
Add the 'function_out_parameters' rule in the GNATcheck rule set Closes #340 See merge request eng/libadalang/langkit-query-language!310
2 parents 81662f8 + 43ec194 commit 88eebf1

File tree

9 files changed

+159
-0
lines changed

9 files changed

+159
-0
lines changed

lkql_checker/doc/generated/list_of_rules.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ GNATcheck rules.
6767
* :ref:`Forbidden_Aspects`
6868
* :ref:`Forbidden_Attributes`
6969
* :ref:`Forbidden_Pragmas`
70+
* :ref:`Function_OUT_Parameters`
7071
* :ref:`Function_Style_Procedures`
7172
* :ref:`Generic_IN_OUT_Objects`
7273
* :ref:`Generics_In_Subprograms`

lkql_checker/doc/generated/predefined_rules.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,38 @@ Here is an example:
27192719
27202720
27212721
2722+
.. _Function_OUT_Parameters:
2723+
2724+
``Function_OUT_Parameters``
2725+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2726+
2727+
.. index:: Function_OUT_Parameters
2728+
2729+
Flag any function declaration, function body declaration, expression function
2730+
declaration, function body stub, or generic function declaration which has at
2731+
least one formal parameter of mode ``out`` or ``in out``.
2732+
2733+
A function body declaration or function body stub is only flagged if there is
2734+
no separate declaration for this function.
2735+
2736+
.. rubric:: Example
2737+
2738+
.. code-block:: ada
2739+
:emphasize-lines: 2, 3, 4
2740+
2741+
function F_1 (I : Integer) return Integer;
2742+
function F_2 (I : out Integer) return Integer; -- FLAG
2743+
function F_3 (I : in out Integer) return Integer; -- FLAG
2744+
function F_4 (I : in out Integer) return Integer is -- FLAG
2745+
(I + 42);
2746+
2747+
function F_2 (I : out Integer) return Integer is -- NOFLAG (declaration has already been flagged)
2748+
begin
2749+
return 0;
2750+
end F_2;
2751+
2752+
2753+
27222754
.. _Global_Variables:
27232755

27242756
``Global_Variables``
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import stdlib
2+
3+
@check(message="function has output parameter(s)",
4+
category="Style",
5+
subcategory="Programming Practice",
6+
rule_name="Function_OUT_Parameters")
7+
fun function_out_parameters(node) =
8+
|" Flag any function declaration, function body declaration, expression function
9+
|" declaration, function body stub, or generic function declaration which has at
10+
|" least one formal parameter of mode ``out`` or ``in out``.
11+
|"
12+
|" A function body declaration or function body stub is only flagged if there is
13+
|" no separate declaration for this function.
14+
|"
15+
|" .. rubric:: Example
16+
|"
17+
|" .. code-block:: ada
18+
|" :emphasize-lines: 2, 3, 4
19+
|"
20+
|" function F_1 (I : Integer) return Integer;
21+
|" function F_2 (I : out Integer) return Integer; -- FLAG
22+
|" function F_3 (I : in out Integer) return Integer; -- FLAG
23+
|" function F_4 (I : in out Integer) return Integer is -- FLAG
24+
|" (I + 42);
25+
|"
26+
|" function F_2 (I : out Integer) return Integer is -- NOFLAG (declaration has already been flagged)
27+
|" begin
28+
|" return 0;
29+
|" end F_2;
30+
node is (SubpBody | ExprFunction | SubpBodyStub | ClassicSubpDecl | GenericSubpInternal)
31+
when (if not node is (ClassicSubpDecl | GenericSubpInternal)
32+
then not node.p_previous_part())
33+
and node.f_subp_spec.f_subp_kind is SubpKindFunction
34+
and stdlib.any([
35+
p
36+
for p in node.f_subp_spec.p_params()
37+
if p.f_mode is (ModeInOut | ModeOut)
38+
])
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
separate (Function_Out)
2+
function Sep_F (I : out Integer) return Integer is -- NOFLAG
3+
begin
4+
return I;
5+
end Sep_F;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package body Function_Out is
2+
function F_1 (I : Integer) return Integer is -- NOFLAG
3+
begin
4+
return I;
5+
end F_1;
6+
7+
function F_2 (I : in Integer) return Integer is -- NOFLAG
8+
begin
9+
return I;
10+
end F_2;
11+
12+
function F_3 (I : out Integer) return Integer is -- NOFLAG
13+
begin
14+
return I;
15+
end F_3;
16+
17+
function F_4 (I : in out Integer) return Integer is -- NOFLAG
18+
begin
19+
return I;
20+
end F_4;
21+
22+
function F_5 (I : out Integer) return Integer is -- FLAG
23+
begin
24+
return I;
25+
end F_5;
26+
27+
function Gen_F (I : out T) return Integer is -- NOFLAG
28+
begin
29+
return 0;
30+
end Gen_F;
31+
32+
function Sep_F (I : out Integer) return Integer -- FLAG
33+
is separate;
34+
35+
procedure P (I : out Integer) is -- NOFLAG
36+
begin
37+
null;
38+
end P;
39+
end Function_Out;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Function_Out is
2+
function F_1 (I : Integer) return Integer; -- NOFLAG
3+
function F_2 (I : in Integer) return Integer; -- NOFLAG
4+
function F_3 (I : out Integer) return Integer; -- FLAG
5+
function F_4 (I : in out Integer) return Integer; -- FLAG
6+
7+
function Expr_F (I : out Integer) return Integer is -- FLAG
8+
(I);
9+
10+
generic
11+
type T is private;
12+
function Gen_F (I : out T) return Integer; -- FLAG
13+
14+
procedure P (I : out Integer); -- NOFLAG
15+
end Function_Out;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function_out.adb:22:13: rule violation: function has output parameter(s)
2+
22 | function F_5 (I : out Integer) return Integer is -- FLAG
3+
| ^^^
4+
5+
function_out.adb:32:13: rule violation: function has output parameter(s)
6+
32 | function Sep_F (I : out Integer) return Integer -- FLAG
7+
| ^^^^^
8+
9+
function_out.ads:4:13: rule violation: function has output parameter(s)
10+
4 | function F_3 (I : out Integer) return Integer; -- FLAG
11+
| ^^^
12+
13+
function_out.ads:5:13: rule violation: function has output parameter(s)
14+
5 | function F_4 (I : in out Integer) return Integer; -- FLAG
15+
| ^^^
16+
17+
function_out.ads:7:13: rule violation: function has output parameter(s)
18+
7 | function Expr_F (I : out Integer) return Integer is -- FLAG
19+
| ^^^^^^
20+
21+
function_out.ads:12:13: rule violation: function has output parameter(s)
22+
12 | function Gen_F (I : out T) return Integer; -- FLAG
23+
| ^^^^^
24+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
driver: checker
2+
rule_name: Function_OUT_Parameters
3+
input_sources: ['function_out.adb', 'function_out.ads']

testsuite/tests/gnatcheck/xml_help/test.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ testsuite_driver: No output file generated by gnatcheck
252252
<check switch="+Rexit_statements_with_no_loop_name:nested_only" label="exit statement with no loop name"/>
253253
<check switch="+Rexits_from_conditional_loops" label="exit from conditional loop"/>
254254
<check switch="+Rfinal_package" label="child package cannot have a final parent package"/>
255+
<check switch="+Rfunction_out_parameters" label="function has output parameter(s)"/>
255256
<field switch="+Rglobal_variables" separator=":" label="declaration of global variable"/>
256257
<check switch="+Rglobal_variables:only_public" label="declaration of global variable"/>
257258
<field switch="+Rgoto_statements" separator=":" label="goto statement"/>
@@ -759,6 +760,7 @@ testsuite_driver: No output file generated by gnatcheck
759760
<check switch="+Rexit_statements_with_no_loop_name:nested_only" label="exit statement with no loop name"/>
760761
<check switch="+Rexits_from_conditional_loops" label="exit from conditional loop"/>
761762
<check switch="+Rfinal_package" label="child package cannot have a final parent package"/>
763+
<check switch="+Rfunction_out_parameters" label="function has output parameter(s)"/>
762764
<field switch="+Rglobal_variables" separator=":" label="declaration of global variable"/>
763765
<check switch="+Rglobal_variables:only_public" label="declaration of global variable"/>
764766
<field switch="+Rgoto_statements" separator=":" label="goto statement"/>

0 commit comments

Comments
 (0)