Skip to content

Commit ac7faa4

Browse files
raph-amiardHugoGGuerrier
authored andcommitted
Adapt to changes in LAL tree
More precisely the introduction of the ElsePart node, which broke a few checkers
1 parent 8d8a8c2 commit ac7faa4

File tree

8 files changed

+32
-17
lines changed

8 files changed

+32
-17
lines changed

lkql_checker/share/lkql/control_flow.lkql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fun all_branches(stmt) =
2929
| IfStmt =>
3030
[stmt.f_then_stmts]
3131
& [alt.f_stmts for alt in stmt.f_alternatives.children].to_list
32-
& [stmt.f_else_stmts]
32+
& [stmt.f_else_part?.f_stmts]
3333
| CaseStmt =>
3434
[alt.f_stmts for alt in stmt.f_alternatives.children]
3535
| BaseLoopStmt =>

lkql_checker/share/lkql/duplicate_branches.lkql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ fun gather_stmts(node) =
2020
| IfStmt =>
2121
[node.f_then_stmts] &
2222
[n.f_stmts for n in node.f_alternatives.children].to_list &
23-
# The `f_else_stmts` field will be a StmtList even if there is no else
24-
# branch in the source code, therefore it is safe to include it.
25-
[node.f_else_stmts]
23+
(if node.f_else_part then [node.f_else_part.f_stmts] else [])
2624
| IfExpr =>
2725
[node.f_then_expr] &
2826
[n.f_then_expr for n in node.f_alternatives.children].to_list &

lkql_checker/share/lkql/goto_statements.lkql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ fun goto_statements(node, only_unconditional=false) =
66
node is GotoStmt
77
# If unconditional option is true, only flag unconditional goto statements
88
when not only_unconditional
9-
or not node.parent.parent is (IfStmt | CaseStmtAlternative)
9+
or not node.parent.parent
10+
is (IfStmt | CaseStmtAlternative | ElsePart | ElsifStmtPart)

lkql_checker/share/lkql/metrics.lkql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ fun statement_complexity(body, exempt_case=false) =
4040
| s@SelectStmt(
4141
f_guards: guards,
4242
f_else_stmts: elses_stmts,
43-
f_abort_stmts: abort_stmts
43+
f_then_abort_part: then_abort_part
4444
) => guards.children_count
4545
+ (if elses_stmts.children_count == 0 then 0 else 1)
46-
+ (if abort_stmts.children_count == 0 then 0 else 1)
46+
+ (match then_abort_part
47+
| ThenAbortPart(f_stmts: stmts) => if stmts.children_count == 0 then 0 else 1
48+
| null => 0)
4749
| * => 0;
4850

4951
reduce(

lkql_checker/share/lkql/nested_paths.lkql

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# handler); in this case, the other path needs not be nested inside the if
66
# statement and is flagged.
77

8-
fun has_last_breaking_stmt(list) =
9-
match list[list.children_count]
8+
fun has_last_breaking_stmt(stmts) =
9+
match stmts[stmts.children_count]
1010
| (RaiseStmt | ReturnStmt | ExitStmt(f_cond_expr: null) | GotoStmt) => true
1111
| b@BlockStmt => b.f_stmts.f_exceptions[1] == null and
1212
has_last_breaking_stmt(b.f_stmts.f_stmts)
@@ -15,10 +15,18 @@ fun has_last_breaking_stmt(list) =
1515
@check(message="nested path may be moved outside if statement",
1616
category="Style", subcategory="Programming Practice")
1717
fun nested_paths(node) =
18-
node is StmtList(parent: if_stmt@IfStmt)
19-
when if_stmt.f_alternatives[1] == null
20-
and if_stmt.f_else_stmts[1] != null
18+
# Match stmt lists that are either the then or the else list of an if stmt
19+
node is (StmtList(parent: if_stmt@IfStmt) |
20+
StmtList(parent: ElsePart(parent: if_stmt@IfStmt)))
21+
when
22+
23+
# No "elsif" branches
24+
if_stmt.f_alternatives[1] == null
25+
26+
# Has an "else" part
27+
and if_stmt.f_else_part.f_stmts[1] != null
28+
2129
and not has_last_breaking_stmt(node)
2230
and has_last_breaking_stmt(if if_stmt.f_then_stmts == node
23-
then if_stmt.f_else_stmts
31+
then if_stmt.f_else_part.f_stmts
2432
else if_stmt.f_then_stmts)

lkql_checker/share/lkql/redundant_boolean_expressions.lkql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ fun redundant_boolean_expressions(node) =
3939
}))
4040

4141
| (IfStmt when node.f_alternatives[1] == null
42-
and node.f_else_stmts.children_count == 1
42+
and node.f_else_part?.f_stmts.children_count == 1
4343
and node.f_then_stmts.children_count == 1
4444
and check_then_else(node.f_then_stmts[1],
45-
node.f_else_stmts[1]))
45+
node.f_else_part?.f_stmts[1]))
4646

4747
| (IfExpr when node.f_alternatives[1] == null
4848
and true_and_false(node.f_then_expr, node.f_else_expr))

lkql_checker/share/lkql/use_if_expressions.lkql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ fun simple_assignment(l) =
2525
fun use_if_expressions(node) =
2626
node is IfStmt
2727
when (simple_return(node.f_then_stmts) and
28-
simple_return(node.f_else_stmts) and
28+
simple_return(node.f_else_part.f_stmts) and
2929
not [s for s in node.f_alternatives.children
3030
if not simple_return(s.f_stmts)])
3131
or (simple_assignment(node.f_then_stmts) and
32-
simple_assignment(node.f_else_stmts) and
32+
simple_assignment(node.f_else_part.f_stmts) and
3333
not [s for s in node.f_alternatives.children
3434
if not simple_assignment(s.f_stmts)] and {
3535
val stmts = from node select AssignStmt;

testsuite/tests/checks/unconditional_goto_statements/test_uncond_goto_stmt.adb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ begin
4646
end loop;
4747
end if;
4848

49+
if X > 25 then
50+
X := 25;
51+
elsif X > 26 then
52+
goto Label0; -- NOFLAG: directly contained within if/elsif/else
53+
end if;
54+
4955
<<Label0>>
5056
Ada.Text_IO.Put_Line(Integer'Image(X));
5157
end Main;

0 commit comments

Comments
 (0)