Skip to content

Commit

Permalink
Merge branch 'topic/float_eq_follow_renamings' into 'master'
Browse files Browse the repository at this point in the history
Enhance the Float_Equality_Checks rule to be able to follow renamings

See merge request eng/libadalang/langkit-query-language!128
  • Loading branch information
raph-amiard committed Oct 30, 2023
2 parents 21898f3 + 8b366a4 commit f794335
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
15 changes: 9 additions & 6 deletions lkql_checker/doc/gnatcheck_rm/predefined_rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7192,13 +7192,16 @@ This rule has no parameters.

Flag all explicit calls to the predefined equality operations for
floating-point types and private types whose completions are floating-point
types. Both '``=``' and '``/=``' operations are checked.
User-defined equality operations are not flagged, nor are uses of operators
that are renamings of the predefined equality operations.
Also, the '``=``' and '``/=``' operations for fixed-point types
are not flagged.
types. Both '=' and '/=' operations are checked. User-defined equality
operations are not flagged. Also, the '=' and '/=' operations for fixed-point
types are not flagged. Uses of operators that are renamings of the predefined
equality operations will be flagged if `Follow_Renamings` is true.

This rule has the following (optional) parameter for the ``+R`` option:

*Follow_Renamings*
Take renamings of predefined equality operations into account.

This rule has no parameters.

.. rubric:: Example

Expand Down
16 changes: 9 additions & 7 deletions lkql_checker/share/lkql/float_equality_checks.lkql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Flag all explicit calls to the predefined equality operations for
# floating-point types and private types whose completions are floating-point
# types. Both '=' and '/=' operations are checked. User-defined equality
# operations are not flagged, nor are uses of operators that are renamings of
# the predefined equality operations. Also, the '=' and '/=' operations for
# fixed-point types are not flagged.
# operations are not flagged. Also, the '=' and '/=' operations for fixed-point
# types are not flagged. Uses of operators that are renamings of the predefined
# equality operations will be flagged if `follow_renamings` is true.

import stdlib

Expand All @@ -12,11 +12,13 @@ fun is_float(n) =
when t.p_full_view().p_is_float_type()

@check(message="use of equality operation for float values", category="Feature")
fun float_equality_checks(node) =
node is (RelationOp(f_op is op@(OpEq or OpNeq))
when stdlib.is_predefined_op(op) and is_float(node.f_left))
fun float_equality_checks(node, follow_renamings=false) =
node is (
RelationOp(f_op is op@(OpEq or OpNeq))
when stdlib.is_predefined_op(op, follow_renamings) and is_float(node.f_left)
)
or CallExpr
when (node.f_name.p_name_is("\"=\"") or
node.f_name.p_name_is("\"/=\""))
and stdlib.is_predefined_op(node)
and stdlib.is_predefined_op(node, follow_renamings)
and is_float(node.f_suffix[1].f_r_expr)
5 changes: 3 additions & 2 deletions lkql_checker/share/lkql/stdlib.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ fun param_pos(n, pos: int = 0) =
if not n then pos else param_pos(n?.previous_sibling(), pos+1)

# TODO: move this in LAL
fun is_predefined_op(op) =
fun is_predefined_op(op, follow_renamings=false) =
|" Return true if op is a predefined operator
{
val ref = op.p_referenced_decl();
val real_ref = if follow_renamings then ultimate_subprogram_alias(ref) else ref;
# TODO: remove null check once all operators are synthesized (UB16-053)
not ref or ref is SyntheticSubpDecl
not real_ref or real_ref is SyntheticSubpDecl
}

fun is_standard_numeric(n) =
Expand Down

0 comments on commit f794335

Please sign in to comment.