From 6d3d220f71ddbf2de3cbc9e8f291fad9b81e0fa1 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Sat, 30 May 2026 07:28:28 +0100 Subject: [PATCH] Use shared CFG `getIfInit` --- .../go/controlflow/ControlFlowGraphShared.qll | 29 ++----------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll index f2da7c52f01..77a72093077 100644 --- a/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll +++ b/go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll @@ -128,9 +128,8 @@ module GoCfg { class ExprStmt = Go::ExprStmt; - /** If statements without init (those with init use custom steps). */ class IfStmt extends Stmt { - IfStmt() { this instanceof Go::IfStmt and not exists(this.(Go::IfStmt).getInit()) } + IfStmt() { this instanceof Go::IfStmt } Expr getCondition() { result = this.(Go::IfStmt).getCond() } @@ -139,6 +138,8 @@ module GoCfg { Stmt getElse() { result = this.(Go::IfStmt).getElse() } } + AstNode getIfInit(IfStmt ifstmt) { result = ifstmt.(Go::IfStmt).getInit() } + class LoopStmt extends Stmt { LoopStmt() { this instanceof Go::LoopStmt } @@ -341,8 +342,6 @@ module GoCfg { predicate inConditionalContext(Ast::AstNode n, ConditionKind kind) { kind.isBoolean() and ( - exists(Go::IfStmt ifstmt | ifstmt.getCond() = n and exists(ifstmt.getInit())) - or n = any(Go::ForStmt fs).getCond() or exists(Go::ExpressionSwitchStmt ess | @@ -734,7 +733,6 @@ module GoCfg { } predicate step(PreControlFlowNode n1, PreControlFlowNode n2) { - ifWithInit(n1, n2) or rangeLoop(n1, n2) or switchStmt(n1, n2) or selectStmt(n1, n2) or @@ -1191,27 +1189,6 @@ module GoCfg { ) } - private predicate ifWithInit(PreControlFlowNode n1, PreControlFlowNode n2) { - exists(Go::IfStmt s | exists(s.getInit()) | - n1.isBefore(s) and n2.isBefore(s.getInit()) - or - n1.isAfter(s.getInit()) and n2.isBefore(s.getCond()) - or - n1.isAfterTrue(s.getCond()) and n2.isBefore(s.getThen()) - or - n1.isAfterFalse(s.getCond()) and - ( - n2.isBefore(s.getElse()) - or - not exists(s.getElse()) and n2.isAfter(s) - ) - or - n1.isAfter(s.getThen()) and n2.isAfter(s) - or - n1.isAfter(s.getElse()) and n2.isAfter(s) - ) - } - private predicate rangeLoop(PreControlFlowNode n1, PreControlFlowNode n2) { exists(Go::RangeStmt s | // Use the shared library's auto-created `[LoopHeader]` additional node