Skip to content

CFG: Add support for patterns - #23170

Draft
hvitved wants to merge 11 commits into
mainfrom
hvitved-shared-cfg-library-split
Draft

hvitved wants to merge 11 commits into
mainfrom
hvitved-shared-cfg-library-split

Conversation

@hvitved

@hvitved hvitved commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Shared CFG

Model patterns explicitly, with match and no-match successors. Nested patterns are evaluated left to right with short-circuiting for conjunctions, disjunctions, and negation. Binding and exhaustive patterns avoid impossible failure paths.

A PatternMatchExpr now evaluates its scrutinee followed by its pattern. The pattern’s match and no-match outcomes lead directly to the expression’s true and false outcomes, removing the synthetic intermediate match node:

void test(Object o) {
  if (o instanceof String) {
    chk();
  }
}
CFG before
flowchart TD
1["Entry"]
10["After ...instanceof... [false]"]
11["After ...instanceof... [true]"]
12["Before ...instanceof..."]
13["After { ... }"]
14["{ ... }"]
15["After chk(...)"]
16["Before chk(...)"]
17["chk(...)"]
18["<Expr>;"]
19["After <Expr>;"]
2["Exit"]
3["Normal Exit"]
4["After { ... }"]
5["{ ... }"]
6["After if (...)"]
7["if (...)"]
8["o"]
9["...instanceof..."]

1 --> 5
3 --> 2
4 --> 3
5 --> 7
6 --> 4
7 --> 12
8 --> 9
9 -- false --> 10
9 -- true --> 11
10 --> 6
11 --> 14
12 --> 8
13 --> 6
14 --> 18
15 --> 19
16 --> 17
17 --> 15
18 --> 16
19 --> 13
Loading
CFG after
flowchart TD
1["Entry"]
10["After ...instanceof... [false]"]
11["After ...instanceof... [true]"]
12["After String [match]"]
13["After String [no-match]"]
14["String"]
15["After { ... }"]
16["{ ... }"]
17["After chk(...)"]
18["Before chk(...)"]
19["chk(...)"]
2["Exit"]
20["<Expr>;"]
21["After <Expr>;"]
3["Normal Exit"]
4["After { ... }"]
5["{ ... }"]
6["After if (...)"]
7["if (...)"]
8["o"]
9["...instanceof..."]

1 --> 5
3 --> 2
4 --> 3
5 --> 7
6 --> 4
7 --> 9
8 --> 14
9 --> 8
10 --> 6
11 --> 16
12 -- true --> 11
13 -- false --> 10
14 -- match --> 12
14 -- no-match --> 13
15 --> 6
16 --> 20
17 --> 21
18 --> 19
19 --> 17
20 --> 18
21 --> 15
Loading

Guards and nullness

Update the guards library to reason about pattern outcomes and propagate them through nested patterns. A successful match is treated as an equality-like guard between the scrutinee and the applicable pattern.

Patterns known not to match null are included in NonNullExpr. This lets the existing equality-based guard logic infer non-nullness from successful matches, while null patterns use the same constant-equality reasoning. Consequently, several language-specific null-guard cases are no longer needed.

The C# and Java integrations adopt the new CFG and guard behavior; other language adapters are updated for the expanded API.

@hvitved
hvitved requested review from a team as code owners November 19, 2026 10:06
Copilot AI adaptive review requested due to automatic review settings November 19, 2026 10:06
@hvitved
hvitved marked this pull request as draft November 19, 2026 10:09
@hvitved hvitved changed the title Shared: Add CFG pattern support and language adapter stubs CFG: Add support for patterns Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Missing exhaustiveness handling allows an already-successful pattern match to take a no-match shortcut, skipping binding children while still entering the true branch.

3 open issues
What changed in this PR

Prepares the shared control-flow graph framework for the Rust migration by extracting reusable pattern support, without changing Rust files or multipleSuccessors.

Changes:

  • Adds pattern types, matching-outcome helpers, exhaustiveness hooks, and conjunctive/disjunctive control flow.
  • Adds empty pattern adapters for seven existing consumers, keeping Python’s current pattern handling separate as PythonPattern.
File Description
unified/​ql/​lib/​codeql/​unified/​internal/​ControlFlowGraph.qll Adds empty pattern adapter classes.
shared/​controlflow/​codeql/​controlflow/​ControlFlowGraph.qll Implements shared pattern APIs and control-flow construction.
ruby/​ql/​lib/​codeql/​ruby/​controlflow/​ControlFlowGraph.qll Adds empty pattern adapter classes.
python/​ql/​lib/​semmle/​python/​controlflow/​internal/​AstNodeImpl.qll Renames the existing pattern wrapper and adds separate shared-pattern stubs.
java/​ql/​lib/​semmle/​code/​java/​ControlFlowGraph.qll Adds empty pattern adapter classes.
go/​ql/​lib/​semmle/​go/​controlflow/​ControlFlowGraphImpl.qll Adds empty pattern adapter classes.
csharp/​ql/​lib/​semmle/​code/​csharp/​controlflow/​internal/​ControlFlowGraph.qll Adds empty pattern adapter classes.
actions/​ql/​lib/​codeql/​actions/​controlflow/​internal/​Cfg.qll Adds empty pattern adapter classes.

Review effort: Max (adaptive)

@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch 4 times, most recently from 3bd07eb to 23da6a7 Compare November 20, 2026 15:25
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch 3 times, most recently from b0e5b03 to cbe8c49 Compare November 23, 2026 19:28
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch from cbe8c49 to c043336 Compare November 24, 2026 10:00
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch 2 times, most recently from 5f427d2 to c2467c6 Compare November 30, 2026 09:05
@github-actions github-actions Bot removed the Kotlin label Nov 30, 2026
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch from c2467c6 to 7da0317 Compare December 1, 2026 19:46
@github-actions github-actions Bot added the C++ label Dec 1, 2026
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch 8 times, most recently from 0a210b0 to b6faaac Compare December 3, 2026 09:39
hvitved and others added 2 commits December 3, 2026 11:25
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch from b6faaac to d79a10f Compare December 3, 2026 10:27
@hvitved
hvitved requested an adaptive review from Copilot December 3, 2026 10:30
hvitved and others added 9 commits December 3, 2026 11:46
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch from d79a10f to cbce3e3 Compare December 3, 2026 10:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants