|
| 1 | +import stdlib |
| 2 | + |
| 3 | +@memoized |
| 4 | +fun range(n) = |
| 5 | + |" Quick and dirty range function. Made to work-around the absence of |
| 6 | + |" iterator.enumerate in GNATcheck 24.3 |
| 7 | + if n == 0 then [] else range(n - 1) & [n] |
| 8 | + |
| 9 | +fun match_signature(child_prim, parent_prim, child_type, parent_type) = |
| 10 | + |" Custom signature matching function, made to handle the fact that in 24.3 |
| 11 | + |" LAL's base_subp_declarations doesn't work. |
| 12 | +{ |
| 13 | + |
| 14 | + fun match_types(child_ptype, parent_ptype) = |
| 15 | + # Regular match types, except in the case where the parameter types |
| 16 | + # match the controlling type |
| 17 | + child_ptype.p_matching_type(parent_ptype) |
| 18 | + or (child_ptype.p_matching_type(child_type) |
| 19 | + and parent_ptype.p_matching_type(parent_type)); |
| 20 | + |
| 21 | + # Check that the names are the same |
| 22 | + child_prim.p_defining_name().p_name_matches(parent_prim.p_defining_name()) |
| 23 | + |
| 24 | + and { |
| 25 | + val child_spec = child_prim.p_subp_spec_or_null(); |
| 26 | + val parent_spec = parent_prim.p_subp_spec_or_null(); |
| 27 | + |
| 28 | + val child_params = child_spec.p_formal_params(); |
| 29 | + val parent_params = parent_spec.p_formal_params(); |
| 30 | + |
| 31 | + # Check that return type is the same |
| 32 | + ((child_spec.p_returns() is null and parent_spec.p_returns() is null) |
| 33 | + or (child_spec.p_returns() is not null and parent_spec.p_returns() is not null |
| 34 | + and match_types(child_spec.p_returns().p_designated_type_decl(), |
| 35 | + parent_spec.p_returns().p_designated_type_decl()))) |
| 36 | + |
| 37 | + # Check that parameters types are the same |
| 38 | + and child_params.length == parent_params.length |
| 39 | + and stdlib.all([match_types(child_params[i].p_basic_decl().p_formal_type(), |
| 40 | + parent_params[i].p_basic_decl().p_formal_type()) |
| 41 | + for i in range(child_params.length)]) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +@check(category="Style", subcategory="Readability", message="Missing overriding mark") |
| 46 | +fun overriding_marks(node) = |
| 47 | + |" Check that overriding subprograms are explicitly marked as such. |
| 48 | + |" |
| 49 | + |" This applies to all subprograms of a derived type that override a |
| 50 | + |" primitive operation of the type, for both tagged and untagged types. In |
| 51 | + |" particular, the declaration of a primitive operation of a type extension |
| 52 | + |" that overrides an inherited operation must carry an overriding |
| 53 | + |" indicator. Another case is the declaration of a function that overrides |
| 54 | + |" a predefined operator (such as an equality operator). |
| 55 | + |" |
| 56 | + |" .. attention:: This doesn't apply to primitives of multiple untagged |
| 57 | + |" types, and as such, won't ever flag such overriding primitives. |
| 58 | + |" |
| 59 | + |" .. rubric:: Example |
| 60 | + |" |
| 61 | + |" .. code-block:: ada |
| 62 | + |" :emphasize-lines: 7 |
| 63 | + |" |
| 64 | + |" package Foo is |
| 65 | + |" type A is null record; |
| 66 | + |" procedure Prim (Self : A) is null; |
| 67 | + |" |
| 68 | + |" type B is new A; |
| 69 | + |" |
| 70 | + |" procedure Prim (Self : B) is null; -- FLAG |
| 71 | + |" end Foo; |
| 72 | + # Select primitives subprograms |
| 73 | + node is (BasicSubpDecl | BaseSubpBody) ( |
| 74 | + p_subp_spec_or_null(): BaseSubpSpec( |
| 75 | + p_primitive_subp_first_type(): t@TypeDecl( |
| 76 | + p_base_type(): bt@TypeDecl( |
| 77 | + p_get_primitives(): primitives@(not null) |
| 78 | + when stdlib.any([p for p in primitives if match_signature(node, p, t, bt)]) |
| 79 | + ) |
| 80 | + ) |
| 81 | + ), |
| 82 | + f_overriding: OverridingUnspecified |
| 83 | + ) |
| 84 | + |
| 85 | + # Body stubs can also take an "overriding" indicator. In that case, check |
| 86 | + # the body. |
| 87 | + or node is SubpBodyStub(p_previous_part_for_decl(): dcl) when overriding_marks(dcl) |
0 commit comments