Skip to content

Commit

Permalink
Merge branch 'topic/adapt_to_elsepart' into 'master'
Browse files Browse the repository at this point in the history
Adapt to changes in LAL tree

See merge request eng/libadalang/langkit-query-language!325
  • Loading branch information
HugoGGuerrier committed Nov 14, 2024
2 parents 8d8a8c2 + ac7faa4 commit f960433
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lkql_checker/share/lkql/control_flow.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun all_branches(stmt) =
| IfStmt =>
[stmt.f_then_stmts]
& [alt.f_stmts for alt in stmt.f_alternatives.children].to_list
& [stmt.f_else_stmts]
& [stmt.f_else_part?.f_stmts]
| CaseStmt =>
[alt.f_stmts for alt in stmt.f_alternatives.children]
| BaseLoopStmt =>
Expand Down
4 changes: 1 addition & 3 deletions lkql_checker/share/lkql/duplicate_branches.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ fun gather_stmts(node) =
| IfStmt =>
[node.f_then_stmts] &
[n.f_stmts for n in node.f_alternatives.children].to_list &
# The `f_else_stmts` field will be a StmtList even if there is no else
# branch in the source code, therefore it is safe to include it.
[node.f_else_stmts]
(if node.f_else_part then [node.f_else_part.f_stmts] else [])
| IfExpr =>
[node.f_then_expr] &
[n.f_then_expr for n in node.f_alternatives.children].to_list &
Expand Down
3 changes: 2 additions & 1 deletion lkql_checker/share/lkql/goto_statements.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ fun goto_statements(node, only_unconditional=false) =
node is GotoStmt
# If unconditional option is true, only flag unconditional goto statements
when not only_unconditional
or not node.parent.parent is (IfStmt | CaseStmtAlternative)
or not node.parent.parent
is (IfStmt | CaseStmtAlternative | ElsePart | ElsifStmtPart)
6 changes: 4 additions & 2 deletions lkql_checker/share/lkql/metrics.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ fun statement_complexity(body, exempt_case=false) =
| s@SelectStmt(
f_guards: guards,
f_else_stmts: elses_stmts,
f_abort_stmts: abort_stmts
f_then_abort_part: then_abort_part
) => guards.children_count
+ (if elses_stmts.children_count == 0 then 0 else 1)
+ (if abort_stmts.children_count == 0 then 0 else 1)
+ (match then_abort_part
| ThenAbortPart(f_stmts: stmts) => if stmts.children_count == 0 then 0 else 1
| null => 0)
| * => 0;

reduce(
Expand Down
20 changes: 14 additions & 6 deletions lkql_checker/share/lkql/nested_paths.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# handler); in this case, the other path needs not be nested inside the if
# statement and is flagged.

fun has_last_breaking_stmt(list) =
match list[list.children_count]
fun has_last_breaking_stmt(stmts) =
match stmts[stmts.children_count]
| (RaiseStmt | ReturnStmt | ExitStmt(f_cond_expr: null) | GotoStmt) => true
| b@BlockStmt => b.f_stmts.f_exceptions[1] == null and
has_last_breaking_stmt(b.f_stmts.f_stmts)
Expand All @@ -15,10 +15,18 @@ fun has_last_breaking_stmt(list) =
@check(message="nested path may be moved outside if statement",
category="Style", subcategory="Programming Practice")
fun nested_paths(node) =
node is StmtList(parent: if_stmt@IfStmt)
when if_stmt.f_alternatives[1] == null
and if_stmt.f_else_stmts[1] != null
# Match stmt lists that are either the then or the else list of an if stmt
node is (StmtList(parent: if_stmt@IfStmt) |
StmtList(parent: ElsePart(parent: if_stmt@IfStmt)))
when

# No "elsif" branches
if_stmt.f_alternatives[1] == null

# Has an "else" part
and if_stmt.f_else_part.f_stmts[1] != null

and not has_last_breaking_stmt(node)
and has_last_breaking_stmt(if if_stmt.f_then_stmts == node
then if_stmt.f_else_stmts
then if_stmt.f_else_part.f_stmts
else if_stmt.f_then_stmts)
4 changes: 2 additions & 2 deletions lkql_checker/share/lkql/redundant_boolean_expressions.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ fun redundant_boolean_expressions(node) =
}))

| (IfStmt when node.f_alternatives[1] == null
and node.f_else_stmts.children_count == 1
and node.f_else_part?.f_stmts.children_count == 1
and node.f_then_stmts.children_count == 1
and check_then_else(node.f_then_stmts[1],
node.f_else_stmts[1]))
node.f_else_part?.f_stmts[1]))

| (IfExpr when node.f_alternatives[1] == null
and true_and_false(node.f_then_expr, node.f_else_expr))
Expand Down
4 changes: 2 additions & 2 deletions lkql_checker/share/lkql/use_if_expressions.lkql
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ fun simple_assignment(l) =
fun use_if_expressions(node) =
node is IfStmt
when (simple_return(node.f_then_stmts) and
simple_return(node.f_else_stmts) and
simple_return(node.f_else_part.f_stmts) and
not [s for s in node.f_alternatives.children
if not simple_return(s.f_stmts)])
or (simple_assignment(node.f_then_stmts) and
simple_assignment(node.f_else_stmts) and
simple_assignment(node.f_else_part.f_stmts) and
not [s for s in node.f_alternatives.children
if not simple_assignment(s.f_stmts)] and {
val stmts = from node select AssignStmt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ begin
end loop;
end if;

if X > 25 then
X := 25;
elsif X > 26 then
goto Label0; -- NOFLAG: directly contained within if/elsif/else
end if;

<<Label0>>
Ada.Text_IO.Put_Line(Integer'Image(X));
end Main;

0 comments on commit f960433

Please sign in to comment.