From 32a6187434b4baedaeb2b73cf75c91d8e37a036e Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 21 May 2026 12:52:18 +0100 Subject: [PATCH] Use shared CFG implementation of for loops --- .../go/controlflow/ControlFlowGraphShared.qll | 52 ++----------------- 1 file changed, 4 insertions(+), 48 deletions(-) diff --git a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll index 27c273677be..3f2ad84e55c 100644 --- a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll +++ b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll @@ -159,13 +159,13 @@ module GoCfg { } class ForStmt extends LoopStmt { - ForStmt() { none() } + ForStmt() { this instanceof Go::ForStmt } - AstNode getInit(int index) { none() } + AstNode getInit(int index) { index = 0 and result = this.(Go::ForStmt).getInit() } - Expr getCondition() { none() } + Expr getCondition() { result = this.(Go::ForStmt).getCond() } - AstNode getUpdate(int index) { none() } + AstNode getUpdate(int index) { index = 0 and result = this.(Go::ForStmt).getPost() } } class ForeachStmt extends LoopStmt { @@ -735,7 +735,6 @@ module GoCfg { predicate step(PreControlFlowNode n1, PreControlFlowNode n2) { ifWithInit(n1, n2) or - forLoop(n1, n2) or rangeLoop(n1, n2) or switchStmt(n1, n2) or selectStmt(n1, n2) or @@ -1200,49 +1199,6 @@ module GoCfg { ) } - private predicate forLoop(PreControlFlowNode n1, PreControlFlowNode n2) { - exists(Go::ForStmt s | - exists(PreControlFlowNode cond | - ( - cond.isBefore(s.getCond()) - or - not exists(s.getCond()) and cond.isBefore(s.getBody()) - ) - | - n1.isBefore(s) and - ( - n2.isBefore(s.getInit()) - or - not exists(s.getInit()) and n2 = cond - ) - or - n1.isAfter(s.getInit()) and n2 = cond - or - n1.isAfterTrue(s.getCond()) and n2.isBefore(s.getBody()) - or - n1.isAfterFalse(s.getCond()) and n2.isAfter(s) - or - not exists(s.getCond()) and - n1.isAfter(s.getBody()) and - ( - n2.isBefore(s.getPost()) - or - not exists(s.getPost()) and n2.isBefore(s.getBody()) - ) - or - exists(s.getCond()) and - n1.isAfter(s.getBody()) and - ( - n2.isBefore(s.getPost()) - or - not exists(s.getPost()) and n2 = cond - ) - or - n1.isAfter(s.getPost()) and n2 = cond - ) - ) - } - private predicate rangeLoop(PreControlFlowNode n1, PreControlFlowNode n2) { exists(Go::RangeStmt s | n1.isBefore(s) and n2.isBefore(s.getDomain())