From 3327f8a8dc739ac2e6bb99f6ef1becd331791baa Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Thu, 3 Dec 2026 14:20:15 +0100 Subject: [PATCH 1/2] CFG: Allow for test output to be restricted --- .../codeql/controlflow/test/TestCfg.qll | 651 +++++++++--------- 1 file changed, 337 insertions(+), 314 deletions(-) diff --git a/shared/controlflow/codeql/controlflow/test/TestCfg.qll b/shared/controlflow/codeql/controlflow/test/TestCfg.qll index 27ed56140e2f..ed3216c18af2 100644 --- a/shared/controlflow/codeql/controlflow/test/TestCfg.qll +++ b/shared/controlflow/codeql/controlflow/test/TestCfg.qll @@ -85,368 +85,391 @@ module Make Cfg, InputSig { + /** + * Gets the rank of `n` within `bb` restricted to nodes that are canonical + * representatives of AST nodes. + */ + private int bbRank(ControlFlowNode n, BasicBlock bb) { + relevantCallable(n.getEnclosingCallable()) and + n = + rank[result](ControlFlowNode n0, int i | + n0.injects(_) and bb.getNode(i) = n0 + | + n0 order by i + ) + } - /** Gets the start line of `n`. */ - private int getLine(ControlFlowNode n) { n.getLocation().getStartLine() = result } + /** Gets the start line of `n`. */ + private int getLine(ControlFlowNode n) { n.getLocation().getStartLine() = result } - /** Holds if `n` is the first node of a slice of `bb` at the given line. */ - private predicate sliceStart(int line, ControlFlowNode n, BasicBlock bb) { - line = getLine(n) and - 1 = bbRank(n, bb) - or - exists(ControlFlowNode n0 | + /** Holds if `n` is the first node of a slice of `bb` at the given line. */ + private predicate sliceStart(int line, ControlFlowNode n, BasicBlock bb) { line = getLine(n) and - bbRank(n0, bb) + 1 = bbRank(n, bb) and - getLine(n0) != line - ) - } + 1 = bbRank(n, bb) + or + exists(ControlFlowNode n0 | + line = getLine(n) and + bbRank(n0, bb) + 1 = bbRank(n, bb) and + getLine(n0) != line + ) + } - private newtype TSlice = - TMkSlice(int line, ControlFlowNode n, BasicBlock bb) { sliceStart(line, n, bb) } + private newtype TSlice = + TMkSlice(int line, ControlFlowNode n, BasicBlock bb) { sliceStart(line, n, bb) } - /** A slice of a basic block at a specific line. */ - private class Slice extends TSlice { - private int line; - private ControlFlowNode start; - private BasicBlock bb; + /** A slice of a basic block at a specific line. */ + private class Slice extends TSlice { + private int line; + private ControlFlowNode start; + private BasicBlock bb; - Slice() { this = TMkSlice(line, start, bb) } + Slice() { this = TMkSlice(line, start, bb) } - string toString() { result = start.toString() } + string toString() { result = start.toString() } - int getLine() { result = line } + int getLine() { result = line } - ControlFlowNode getNode(int i) { - i = 0 and result = start - or - bbRank(this.getNode(i - 1), bb) + 1 = bbRank(result, bb) and - not sliceStart(_, result, bb) - } + ControlFlowNode getNode(int i) { + i = 0 and result = start + or + bbRank(this.getNode(i - 1), bb) + 1 = bbRank(result, bb) and + not sliceStart(_, result, bb) + } - ControlFlowNode getLast() { - exists(int i | result = this.getNode(i) and not exists(this.getNode(i + 1))) - } + ControlFlowNode getLast() { + exists(int i | result = this.getNode(i) and not exists(this.getNode(i + 1))) + } - predicate step(ControlFlowNode n1, ControlFlowNode n2) { - exists(int i | n1 = this.getNode(i) and n2 = this.getNode(i + 1)) + predicate step(ControlFlowNode n1, ControlFlowNode n2) { + exists(int i | n1 = this.getNode(i) and n2 = this.getNode(i + 1)) + } } - } - /** - * A direction in the location-induced AST restricted to a single line, that - * is, the tree arising from location-nesting. - * - * - `Up` and `Down` indicate location nesting. - * - `Right` indicates a non-overlapping location to the right. - * - `Id` indicates the same location. - * - `Other` most likely indicates a non-overlapping location to the left, - * but remains a catch-all for any other case. - */ - private newtype Dir = - Down() or - Up() or - Right() or - Id() or - Other() + /** + * A direction in the location-induced AST restricted to a single line, that + * is, the tree arising from location-nesting. + * + * - `Up` and `Down` indicate location nesting. + * - `Right` indicates a non-overlapping location to the right. + * - `Id` indicates the same location. + * - `Other` most likely indicates a non-overlapping location to the left, + * but remains a catch-all for any other case. + */ + private newtype Dir = + Down() or + Up() or + Right() or + Id() or + Other() - /** - * Holds if `n1` steps to `n2` within a basic block line slice and that the - * corresponding AST nodes are related with one being a transitive parent of - * the other. The direction in the AST is given by `dir`. - */ - private predicate astUpDownStep(ControlFlowNode n1, ControlFlowNode n2, Dir dir) { - exists(AstNode a1, AstNode a2 | - n1.injects(a1) and - n2.injects(a2) and - any(Slice s).step(n1, n2) - | - if Input::getParent+(a1) = a2 - then dir = Up() - else - if Input::getParent+(a2) = a1 - then dir = Down() - else none() - ) - } + /** + * Holds if `n1` steps to `n2` within a basic block line slice and that the + * corresponding AST nodes are related with one being a transitive parent of + * the other. The direction in the AST is given by `dir`. + */ + private predicate astUpDownStep(ControlFlowNode n1, ControlFlowNode n2, Dir dir) { + exists(AstNode a1, AstNode a2 | + n1.injects(a1) and + n2.injects(a2) and + any(Slice s).step(n1, n2) + | + if Input::getParent+(a1) = a2 + then dir = Up() + else + if Input::getParent+(a2) = a1 + then dir = Down() + else none() + ) + } - bindingset[l1, l2] - pragma[inline_late] - private predicate endsLessThan(Location l1, Location l2) { - l1.getEndLine() < l2.getEndLine() - or - l1.getEndLine() = l2.getEndLine() and - l1.getEndColumn() <= l2.getEndColumn() - } + bindingset[l1, l2] + pragma[inline_late] + private predicate endsLessThan(Location l1, Location l2) { + l1.getEndLine() < l2.getEndLine() + or + l1.getEndLine() = l2.getEndLine() and + l1.getEndColumn() <= l2.getEndColumn() + } - private predicate oneline(Location l) { l.getStartLine() = l.getEndLine() } + private predicate oneline(Location l) { l.getStartLine() = l.getEndLine() } - /** - * Holds if `n1` steps to `n2` within a basic block line slice `slice` and - * that the step in locations is given by `dir`. Some identical locations may - * be further resolved by peeking at the AST structure. - */ - private predicate singleLineBlockStep(Slice slice, ControlFlowNode n1, ControlFlowNode n2, Dir dir) { - slice.step(n1, n2) and - exists(Location l1, Location l2 | n1.getLocation() = l1 and n2.getLocation() = l2 | - if oneline(l1) and l1.getEndColumn() < l2.getStartColumn() - then dir = Right() - else - if - l1.getStartColumn() <= l2.getStartColumn() and - endsLessThan(l2, l1) and - l1 != l2 - then dir = Down() + /** + * Holds if `n1` steps to `n2` within a basic block line slice `slice` and + * that the step in locations is given by `dir`. Some identical locations may + * be further resolved by peeking at the AST structure. + */ + private predicate singleLineBlockStep( + Slice slice, ControlFlowNode n1, ControlFlowNode n2, Dir dir + ) { + slice.step(n1, n2) and + exists(Location l1, Location l2 | n1.getLocation() = l1 and n2.getLocation() = l2 | + if oneline(l1) and l1.getEndColumn() < l2.getStartColumn() + then dir = Right() else if - l2.getStartColumn() <= l1.getStartColumn() and - endsLessThan(l1, l2) and + l1.getStartColumn() <= l2.getStartColumn() and + endsLessThan(l2, l1) and l1 != l2 - then dir = Up() + then dir = Down() else - if l1 != l2 - then dir = Other() - else ( - astUpDownStep(n1, n2, dir) - or - not astUpDownStep(n1, n2, _) and dir = Id() - ) - ) - } + if + l2.getStartColumn() <= l1.getStartColumn() and + endsLessThan(l1, l2) and + l1 != l2 + then dir = Up() + else + if l1 != l2 + then dir = Other() + else ( + astUpDownStep(n1, n2, dir) + or + not astUpDownStep(n1, n2, _) and dir = Id() + ) + ) + } - /** - * Holds if the slice `slice` is simple left-to-right evaluation order. - * - * Both pre-order and post-order traversal is allowed and allowed to be - * mixed. `Up` indicates the last part of a post-order traversal, and `Down` - * indicates the first part of a pre-order traversal, so an `Up` step - * followed by a `Down` step is inconsistent with simple left-to-right - * evaluation order. - */ - private predicate simpleLeftToRightBlock(Slice slice) { - forall(ControlFlowNode n1, ControlFlowNode n2 | singleLineBlockStep(slice, n1, n2, _) | - exists(Dir dir | singleLineBlockStep(slice, n1, n2, dir) | - dir != Other() and - dir != Id() and - (dir = Up() implies not singleLineBlockStep(slice, n2, _, Down())) + /** + * Holds if the slice `slice` is simple left-to-right evaluation order. + * + * Both pre-order and post-order traversal is allowed and allowed to be + * mixed. `Up` indicates the last part of a post-order traversal, and `Down` + * indicates the first part of a pre-order traversal, so an `Up` step + * followed by a `Down` step is inconsistent with simple left-to-right + * evaluation order. + */ + private predicate simpleLeftToRightBlock(Slice slice) { + forall(ControlFlowNode n1, ControlFlowNode n2 | singleLineBlockStep(slice, n1, n2, _) | + exists(Dir dir | singleLineBlockStep(slice, n1, n2, dir) | + dir != Other() and + dir != Id() and + (dir = Up() implies not singleLineBlockStep(slice, n2, _, Down())) + ) ) - ) - } + } - private string outgoingArrow(ControlFlowNode n) { - exists(Dir dir | singleLineBlockStep(_, n, _, dir) | - dir = Down() and result = " -V " - or - dir = Up() and result = " -^ " - or - dir = Right() and result = " -> " - or - dir = Id() and result = " -I " - or - dir = Other() and result = " -? " - ) - } + private string outgoingArrow(ControlFlowNode n) { + exists(Dir dir | singleLineBlockStep(_, n, _, dir) | + dir = Down() and result = " -V " + or + dir = Up() and result = " -^ " + or + dir = Right() and result = " -> " + or + dir = Id() and result = " -I " + or + dir = Other() and result = " -? " + ) + } + + /** Provides a query for dumping every line-based slice of a basic block. */ + module BlockSlices { + /** + * Holds if `blockSlice` is a string representation of a `line` slice of a + * basic block. `first` is the first node in the slice. + */ + query predicate blockSlice(int line, ControlFlowNode first, string blockSlice) { + exists(Slice slice | + first = slice.getNode(0) and + line = slice.getLine() and + blockSlice = + "'" + + strictconcat(ControlFlowNode n, int i, int j, string s | + slice.getNode(i) = n and + ( + j = 0 and s = n.toString() + or + j = 1 and s = outgoingArrow(n) + ) + | + s order by i, j + ) + "'" + ) + } + } + + /** A `ControlFlowNode` with its location trimmed to a single line. */ + class ControlFlowNode1line extends FinalControlFlowNode { + /** + * Holds if this element is at the specified location. + * The location spans column `sc` of line `sl` to + * column `ec` of line `el` in file `file`. + * For more information, see + * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). + */ + predicate hasLocationInfo(string file, int sl, int sc, int el, int ec) { + exists(int el0, int ec0 | + super.getLocation().hasLocationInfo(file, sl, sc, el0, ec0) and + if el0 != sl then el = sl and ec = sc else (el = el0 and ec = ec0) + ) + } + } - /** Provides a query for dumping every line-based slice of a basic block. */ - module BlockSlices { /** - * Holds if `blockSlice` is a string representation of a `line` slice of a - * basic block. `first` is the first node in the slice. + * Holds if `blockSlice` is a string representation of a line slice of a + * basic block that does not follow simple left-to-right evaluation order. + * `first` is the first node in the slice. */ - query predicate blockSlice(int line, ControlFlowNode first, string blockSlice) { + query predicate nonSimple(ControlFlowNode1line first, string blockSlice) { exists(Slice slice | - first = slice.getNode(0) and - line = slice.getLine() and - blockSlice = - "'" + - strictconcat(ControlFlowNode n, int i, int j, string s | - slice.getNode(i) = n and - ( - j = 0 and s = n.toString() - or - j = 1 and s = outgoingArrow(n) - ) - | - s order by i, j - ) + "'" + BlockSlices::blockSlice(_, first, blockSlice) and + slice.getNode(0) = first and + not simpleLeftToRightBlock(slice) ) } - } - - final private class FinalControlFlowNode = ControlFlowNode; - /** A `ControlFlowNode` with its location trimmed to a single line. */ - class ControlFlowNode1line extends FinalControlFlowNode { /** - * Holds if this element is at the specified location. - * The location spans column `sc` of line `sl` to - * column `ec` of line `el` in file `file`. - * For more information, see - * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). + * Holds if some AST node on `line` has a corresponding CFG node within the + * callable `c`. */ - predicate hasLocationInfo(string file, int sl, int sc, int el, int ec) { - exists(int el0, int ec0 | - super.getLocation().hasLocationInfo(file, sl, sc, el0, ec0) and - if el0 != sl then el = sl and ec = sc else (el = el0 and ec = ec0) + private predicate lineHasCfg(int line, Callable c) { + exists(ControlFlowNode n, Location loc | + n.getLocation() = loc and + loc.getStartLine() = line and + oneline(loc) and + n.injects(_) and + n.getEnclosingCallable() = c ) } - } - /** - * Holds if `blockSlice` is a string representation of a line slice of a - * basic block that does not follow simple left-to-right evaluation order. - * `first` is the first node in the slice. - */ - query predicate nonSimple(ControlFlowNode1line first, string blockSlice) { - exists(Slice slice | - BlockSlices::blockSlice(_, first, blockSlice) and - slice.getNode(0) = first and - not simpleLeftToRightBlock(slice) - ) - } + /** + * Holds if `n1` and `n2` are consecutive nodes in a basic block (skipping + * over non-AST nodes), but are located on different lines, such that `n1` + * and `n2` link two line slices of a basic block. Additionally, the link is + * required to be non-trivial in the sense that it either goes backwards (the + * `lineDelta` is negative) or it skips over some lines with other CFG nodes. + */ + private predicate nonTrivialSliceLink( + int line, ControlFlowNode n1, ControlFlowNode n2, int lineDelta + ) { + line = n1.getLocation().getStartLine() and + exists(BasicBlock bb | + n1 = any(Slice slice).getLast() and + bbRank(n1, bb) + 1 = bbRank(n2, bb) + ) and + lineDelta = n2.getLocation().getStartLine() - n1.getLocation().getStartLine() and + (lineDelta < 0 or lineHasCfg([line + 1 .. line + lineDelta - 1], n1.getEnclosingCallable())) + } - /** - * Holds if some AST node on `line` has a corresponding CFG node within the - * callable `c`. - */ - private predicate lineHasCfg(int line, Callable c) { - exists(ControlFlowNode n, Location loc | - n.getLocation() = loc and - loc.getStartLine() = line and - oneline(loc) and - n.injects(_) and - n.getEnclosingCallable() = c - ) - } + bindingset[lineDelta] + private string ppDelta(int lineDelta) { + if lineDelta < 0 + then result = "(" + lineDelta.toString() + ")" + else result = "(+" + lineDelta.toString() + ")" + } - /** - * Holds if `n1` and `n2` are consecutive nodes in a basic block (skipping - * over non-AST nodes), but are located on different lines, such that `n1` - * and `n2` link two line slices of a basic block. Additionally, the link is - * required to be non-trivial in the sense that it either goes backwards (the - * `lineDelta` is negative) or it skips over some lines with other CFG nodes. - */ - private predicate nonTrivialSliceLink( - int line, ControlFlowNode n1, ControlFlowNode n2, int lineDelta - ) { - line = n1.getLocation().getStartLine() and - exists(BasicBlock bb | - n1 = any(Slice slice).getLast() and - bbRank(n1, bb) + 1 = bbRank(n2, bb) - ) and - lineDelta = n2.getLocation().getStartLine() - n1.getLocation().getStartLine() and - (lineDelta < 0 or lineHasCfg([line + 1 .. line + lineDelta - 1], n1.getEnclosingCallable())) - } + /** + * Holds if the line slice ending at `n1` continues to another line slice via + * a non-trivial link. That is, it does not simply continue to the next line. + */ + query predicate bbContinues(ControlFlowNode1line n1, string link) { + exists(ControlFlowNode n2, int lineDelta | + nonTrivialSliceLink(_, n1, n2, lineDelta) and + link = "'" + n1.toString() + " goto " + n2.toString() + ppDelta(lineDelta) + "'" + ) + } - bindingset[lineDelta] - private string ppDelta(int lineDelta) { - if lineDelta < 0 - then result = "(" + lineDelta.toString() + ")" - else result = "(+" + lineDelta.toString() + ")" - } + /** + * Holds if `bb` only includes synthetic nodes, that is, no AST nodes are + * canonically represented in it. + */ + private predicate synthBlock(BasicBlock bb) { not exists(bbRank(_, bb)) } - /** - * Holds if the line slice ending at `n1` continues to another line slice via - * a non-trivial link. That is, it does not simply continue to the next line. - */ - query predicate bbContinues(ControlFlowNode1line n1, string link) { - exists(ControlFlowNode n2, int lineDelta | - nonTrivialSliceLink(_, n1, n2, lineDelta) and - link = "'" + n1.toString() + " goto " + n2.toString() + ppDelta(lineDelta) + "'" - ) - } + /** + * Holds if `bb1` transitively reaches `bb2` through a sequence of basic + * block steps where `bb2` is the only non-`synthBlock`. + */ + private predicate synthStep(BasicBlock bb1, BasicBlock bb2, string successorSuffix) { + bb1 = bb2 and successorSuffix = "" and not synthBlock(bb2) + or + exists(BasicBlock mid, SuccessorType t, string s | + synthBlock(bb1) and + bb1.getASuccessor(t) = mid and + synthStep(mid, bb2, s) and + if t instanceof DirectSuccessor + then successorSuffix = s + else successorSuffix = "," + t.toString() + s + ) + } - /** - * Holds if `bb` only includes synthetic nodes, that is, no AST nodes are - * canonically represented in it. - */ - private predicate synthBlock(BasicBlock bb) { not exists(bbRank(_, bb)) } + /** + * Holds if there is a basic block step from `n1` to `n2` with successor + * type `t` originating at the given line. + */ + private predicate bbStep( + int line, ControlFlowNode n1, ControlFlowNode n2, SuccessorType t, string successorSuffix, + int lineDelta + ) { + exists(int last, BasicBlock bb1, BasicBlock mid, BasicBlock bb2 | + line = n1.getLocation().getStartLine() and + last = bbRank(n1, bb1) and + not last + 1 = bbRank(_, bb1) and + bb1.getASuccessor(t) = mid and + synthStep(mid, bb2, successorSuffix) and + 1 = bbRank(n2, bb2) and + lineDelta = n2.getLocation().getStartLine() - n1.getLocation().getStartLine() + ) + } - /** - * Holds if `bb1` transitively reaches `bb2` through a sequence of basic - * block steps where `bb2` is the only non-`synthBlock`. - */ - private predicate synthStep(BasicBlock bb1, BasicBlock bb2, string successorSuffix) { - bb1 = bb2 and successorSuffix = "" and not synthBlock(bb2) - or - exists(BasicBlock mid, SuccessorType t, string s | - synthBlock(bb1) and - bb1.getASuccessor(t) = mid and - synthStep(mid, bb2, s) and - if t instanceof DirectSuccessor - then successorSuffix = s - else successorSuffix = "," + t.toString() + s - ) - } + /** + * Holds if there is a basic block step from `n1` described by `next`. + */ + query predicate bbStep(ControlFlowNode1line n1, string next) { + exists(ControlFlowNode n2, SuccessorType t, string s, int lineDelta | + bbStep(_, n1, n2, t, s, lineDelta) and + next = + "'" + n1.toString() + " : " + t + s + " -> " + n2.toString() + ppDelta(lineDelta) + "'" + ) + } - /** - * Holds if there is a basic block step from `n1` to `n2` with successor - * type `t` originating at the given line. - */ - private predicate bbStep( - int line, ControlFlowNode n1, ControlFlowNode n2, SuccessorType t, string successorSuffix, - int lineDelta - ) { - exists(int last, BasicBlock bb1, BasicBlock mid, BasicBlock bb2 | - line = n1.getLocation().getStartLine() and - last = bbRank(n1, bb1) and - not last + 1 = bbRank(_, bb1) and - bb1.getASuccessor(t) = mid and - synthStep(mid, bb2, successorSuffix) and - 1 = bbRank(n2, bb2) and - lineDelta = n2.getLocation().getStartLine() - n1.getLocation().getStartLine() - ) - } + /** + * Holds if no CFG nodes exist in `c` on `line` and `a` is an AST node on + * that line. + */ + private predicate unreachable(int line, Callable c, AstNode a) { + oneline(a.getLocation()) and + a.getLocation().getStartLine() = line and + Input::getEnclosingCallable(a) = c and + relevantCallable(c) and + not exists(ControlFlowNode n | + n.injects(_) and + oneline(n.getLocation()) and + n.getLocation().getStartLine() = line and + n.getEnclosingCallable() = c + ) + } - /** - * Holds if there is a basic block step from `n1` described by `next`. - */ - query predicate bbStep(ControlFlowNode1line n1, string next) { - exists(ControlFlowNode n2, SuccessorType t, string s, int lineDelta | - bbStep(_, n1, n2, t, s, lineDelta) and - next = "'" + n1.toString() + " : " + t + s + " -> " + n2.toString() + ppDelta(lineDelta) + "'" - ) - } + /** + * Holds if no CFG nodes exist in `c` on `line` and `a` is the first AST + * node on that line. + */ + private predicate firstUnreachable(int line, Callable c, AstNode a) { + a = + min(AstNode a0, Location loc | + unreachable(line, c, a0) and loc = a0.getLocation() + | + a0 order by loc.getStartColumn(), loc.getEndColumn() + ) + } - /** - * Holds if no CFG nodes exist in `c` on `line` and `a` is an AST node on - * that line. - */ - private predicate unreachable(int line, Callable c, AstNode a) { - oneline(a.getLocation()) and - a.getLocation().getStartLine() = line and - Input::getEnclosingCallable(a) = c and - not exists(ControlFlowNode n | - n.injects(_) and - oneline(n.getLocation()) and - n.getLocation().getStartLine() = line and - n.getEnclosingCallable() = c - ) + /** + * Holds if no CFG nodes exist in the callable of `a` on the same line as + * `a`, and `a` is the first AST node on that line. + */ + query predicate noCfg(AstNode a) { firstUnreachable(_, _, a) } } - /** - * Holds if no CFG nodes exist in `c` on `line` and `a` is the first AST - * node on that line. - */ - private predicate firstUnreachable(int line, Callable c, AstNode a) { - a = - min(AstNode a0, Location loc | - unreachable(line, c, a0) and loc = a0.getLocation() - | - a0 order by loc.getStartColumn(), loc.getEndColumn() - ) - } + private predicate unrestricted(Cfg::Callable c) { any() } - /** - * Holds if no CFG nodes exist in the callable of `a` on the same line as - * `a`, and `a` is the first AST node on that line. - */ - query predicate noCfg(AstNode a) { firstUnreachable(_, _, a) } + private module Unrestricted = Restricted; + + import Unrestricted } From 41992f60609545c5862f099e4a32d79e0fb9f6b5 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Thu, 3 Dec 2026 14:21:21 +0100 Subject: [PATCH 2/2] Unified: Also construct CFGs for generated callables --- .../ql/lib/codeql/unified/internal/ControlFlowGraph.qll | 4 ++-- .../library-tests/controlflow/basicblock-slices.expected | 4 ++++ .../test/library-tests/controlflow/basicblock-slices.ql | 6 +++++- unified/ql/test/library-tests/controlflow/cfg.ql | 9 ++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll b/unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll index 165cf5300c2c..3b51a465cd30 100644 --- a/unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll +++ b/unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll @@ -32,7 +32,7 @@ module Ast implements AstSig { or e instanceof Operator or - not e.fromSource() + not e.getFile().fromSource() } private AstNode adjustedGetChild(AstNode n, int index) { deferScope(n, index, result) } @@ -55,7 +55,7 @@ module Ast implements AstSig { Callable getEnclosingCallable(AstNode node) { result = node.getEnclosingCallable() } class Callable extends U::Callable { - Callable() { this.fromSource() } + Callable() { this.getFile().fromSource() } } AstNode callableGetBody(Callable c) { result = c.getBody() } diff --git a/unified/ql/test/library-tests/controlflow/basicblock-slices.expected b/unified/ql/test/library-tests/controlflow/basicblock-slices.expected index df7e456642ac..91a00f76f46d 100644 --- a/unified/ql/test/library-tests/controlflow/basicblock-slices.expected +++ b/unified/ql/test/library-tests/controlflow/basicblock-slices.expected @@ -433,8 +433,10 @@ | 447 | cfg.swift:447:10:447:10 | MyLocalEnum.B | 'MyLocalEnum.B -V B' | | 450 | cfg.swift:450:3:450:22 | var myLocalVar | 'var myLocalVar -V myLocalVar -> Int' | | 452 | cfg.swift:452:10:452:10 | 0 | '0 -^ ReturnExpr' | +| 455 | cfg.swift:455:1:455:0 | B.init | 'B.init' | | 455 | cfg.swift:455:1:457:1 | struct B | 'struct B -V B' | | 456 | cfg.swift:456:3:456:13 | var B.x | 'var B.x -V x -> Int' | +| 459 | cfg.swift:459:1:459:0 | A.init | 'A.init' | | 459 | cfg.swift:459:1:463:1 | struct A | 'struct A -V A' | | 460 | cfg.swift:460:3:460:11 | var A.b | 'var A.b -V b -> B' | | 461 | cfg.swift:461:3:461:14 | var A.bs | 'var A.bs -V bs -> Array -> B -^ GenericTypeExpr' | @@ -523,6 +525,7 @@ | 574 | cfg.swift:574:1:576:1 | protocol MyProtocol | 'protocol MyProtocol -V MyProtocol' | | 575 | cfg.swift:575:2:575:1 | Block | 'Block' | | 575 | cfg.swift:575:2:575:21 | func MyProtocol.source | 'func MyProtocol.source -V source' | +| 578 | cfg.swift:578:1:578:0 | MyProcotolImpl.init | 'MyProcotolImpl.init' | | 578 | cfg.swift:578:1:580:1 | class MyProcotolImpl | 'class MyProcotolImpl -V MyProcotolImpl -> BaseType -V MyProtocol' | | 579 | cfg.swift:579:2:579:34 | func MyProcotolImpl.source | 'func MyProcotolImpl.source -V source' | | 579 | cfg.swift:579:23:579:34 | Block | 'Block -V 0 -^ ReturnExpr' | @@ -548,6 +551,7 @@ | 600 | cfg.swift:600:9:601:18 | IfExpr | 'IfExpr -V x -> 42 -^ ... < ...' | | 600 | cfg.swift:600:21:600:25 | Block | 'Block -V 1' | | 601 | cfg.swift:601:14:601:18 | Block | 'Block -V 2' | +| 605 | cfg.swift:605:1:605:0 | ValueGenericsStruct.init | 'ValueGenericsStruct.init' | | 605 | cfg.swift:605:1:607:1 | struct ValueGenericsStruct | 'struct ValueGenericsStruct -V ValueGenericsStruct -> TypeParameter -V N -> Int' | | 606 | cfg.swift:606:5:606:13 | var ValueGenericsStruct.x | 'var ValueGenericsStruct.x -V x -> N' | | 609 | cfg.swift:609:1:613:1 | func valueGenericsFn | 'func valueGenericsFn -V valueGenericsFn' | diff --git a/unified/ql/test/library-tests/controlflow/basicblock-slices.ql b/unified/ql/test/library-tests/controlflow/basicblock-slices.ql index 1b57b4b5d5e2..0312820177ac 100644 --- a/unified/ql/test/library-tests/controlflow/basicblock-slices.ql +++ b/unified/ql/test/library-tests/controlflow/basicblock-slices.ql @@ -1,2 +1,6 @@ import unified -import ControlFlow::TestCfgInline::BlockSlices +import codeql.unified.internal.ControlFlowGraph + +private predicate relevantCallable(Ast::Callable c) { c.fromSource() } + +import ControlFlow::TestCfgInline::Restricted::BlockSlices diff --git a/unified/ql/test/library-tests/controlflow/cfg.ql b/unified/ql/test/library-tests/controlflow/cfg.ql index e86a681954e8..0a3fb7ca8add 100644 --- a/unified/ql/test/library-tests/controlflow/cfg.ql +++ b/unified/ql/test/library-tests/controlflow/cfg.ql @@ -1,2 +1,9 @@ import unified -import ControlFlow::TestCfgInline +import codeql.unified.internal.ControlFlowGraph + +private predicate relevantCallable(Ast::Callable c) { + not c instanceof TopLevel and + c.fromSource() +} + +import ControlFlow::TestCfgInline::Restricted