Skip to content

CFG: Add support for patterns - #23170

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

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

Conversation

@hvitved

@hvitved hvitved commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extract shared CFG pattern support from #22864 into a standalone change against main.

The first commit adds the shared Pattern, AndPattern, OrPattern, and NotPattern API and the corresponding nested-pattern control-flow handling. It also removes the old pattern-match-specific special case in shared CFG successor handling.

The C# commit models its pattern expressions with the shared API and updates the affected CFG baselines. The Java commit models binding instanceof and record patterns and includes the updated pattern-instanceof CFG expectation. Separate commits add NotPattern stubs for Actions, Go, Python, Ruby, and Unified. No Rust files are included.

Validation

  • Fully compiled representative shared-CFG consumers for all seven adapters with warnings treated as errors.
  • Passed C# control-flow graph and guard tests, including negated, conjunctive, and disjunctive patterns.
  • Passed Java pattern-instanceof and binding-pattern CFG/dataflow tests, plus focused Actions, Go, Python, and Ruby CFG tests.
  • Unified's CFG query compiles; its runtime test is blocked locally by an extractor/database schema mismatch.
  • A larger Go successor-graph test has local baseline differences that also occur with unchanged main libraries on the same extracted dataset; both versions produce identical results.

@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)

private predicate constantCondition(AstNode n, ConditionalSuccessor t) {
n.(BooleanLiteral).getValue() = t.(BooleanSuccessor).getValue()
private predicate patternAlwaysMatches(AstNode pattern) {
Input1::patternIsExhaustive(pattern)
Comment on lines +1173 to +1175
predicate isAfterMatch(AstNode n) {
this.isAfterValue(n, any(MatchingSuccessor m | m.getValue() = true))
}
result.isAfterMatch(pattern)
}

private predicate conjunctivePatternStep(PreControlFlowNode n1, PreControlFlowNode n2) {
@hvitved
hvitved force-pushed the hvitved-shared-cfg-library-split branch from 673da30 to c42f271 Compare November 19, 2026 10:43
hvitved and others added 8 commits November 19, 2026 13:28
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 c42f271 to b4e1bbb Compare November 19, 2026 12:29

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.

2 participants