Repository navigation
Guards: Update Guards-CFG integration to recognize MatchSuccessors. - #23182
Conversation
f66a7ca to
03d59fb
Compare
03d59fb to
facf1b7
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Java and Go guard consumers still require Boolean outcomes, dropping match-valued switch constraints and regressing numeric-refinement and equality reasoning.
2 open issues
What changed in this PR
This PR updates the shared Guards library to use control-flow matching outcomes for switch cases and patterns, improving precision across language integrations while preserving legacy C++ support.
Changes:
- Adds match/no-match guard values, pattern-based equalities, and expression-less switch handling.
- Updates Java, Go, and C# integrations, including Java type-flow and dispatch consumers.
- Updates test queries, inline expectations, and expected results for pattern-based guards.
| File | Description |
|---|---|
| shared/controlflow/codeql/controlflow/Guards.qll | Adds matching outcomes and pattern-based switch reasoning. |
| java/ql/test/library-tests/switch-default-impossible-dispatch/Test.java | Clarifies dispatch expectation comments. |
| java/ql/test/library-tests/switch-default-impossible-dispatch/test.expected | Updates impossible-dispatch results. |
| java/ql/test/library-tests/guards12/guard.ql | Excludes pattern expressions from equality output. |
| java/ql/test/library-tests/guards12/guard.expected | Records pattern-level matching outcomes. |
| java/ql/test/library-tests/guards/guardslogic.ql | Includes matching outcomes in test results. |
| java/ql/test/library-tests/guards/guardslogic.expected | Updates switch guard results. |
| java/ql/test/library-tests/guards/GuardsInline.ql | Removes case-specific Boolean formatting. |
| java/ql/test/library-tests/guards/GuardsInline.expected | Updates inline guard results. |
| java/ql/test/library-tests/guards/Guards.java | Updates inline expectation comments. |
| java/ql/lib/semmle/code/java/dispatch/VirtualDispatch.qll | Uses match-aware type-test outcomes. |
| java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll | Carries guard values through type refinement. |
| java/ql/lib/semmle/code/java/controlflow/Guards.qll | Exposes case patterns and match-aware type tests. |
| go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected | Updates switch and expression-less switch results. |
| go/ql/lib/semmle/go/controlflow/Guards.qll | Replaces case-edge hooks with pattern access. |
| csharp/ql/test/query-tests/Nullness/NullCheck.expected | Adds pattern-based null-check results. |
| csharp/ql/test/query-tests/Nullness/EqualityCheck.expected | Records match-valued switch equalities. |
| csharp/ql/test/library-tests/controlflow/guards/GuardedExpr.expected | Updates pattern-based expression guards. |
| csharp/ql/test/library-tests/controlflow/guards/GuardedControlFlowNode.expected | Updates pattern-based control-flow guards. |
| csharp/ql/test/library-tests/controlflow/guards/BooleanGuardedExpr.expected | Removes switch-arm Boolean guard results. |
| csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll | Adapts case patterns and nullness implications. |
| cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll | Implements the legacy case-match hook. |
Review effort: Balanced (adaptive)
| bb1.getASuccessor(successor) = bb2 and successor.getValue() = false | ||
| )) | ||
| } | ||
| AstNode getAPattern() { result = this.(G::CaseClause).getAnExpr() } |
| exprMatch(e1, this) and this = e2 and eqval.isMatch() | ||
| or | ||
| exprMatch(e2, this) and this = e1 and eqval.isMatch() |
There was a problem hiding this comment.
This is true (and has a minor impact on query results), but Tom and I decided to leave this as followup work for when we next deal with Range Analysis.
|
Dca shows minor alert changes for Java and C# (looks like the general trend is improvements with some back and forth). C++ and Go are completely uneventful. |
| typeTest.appliesTypeTest(v.getARead(), t, false) and | ||
| guardControls_v1(typeTest, q.getBasicBlock(), false) and | ||
| typeTest.appliesTypeTest(v.getARead(), t, false, gv) and | ||
| guardControls_v1(typeTest, q.getBasicBlock(), gv.getDualValue()) and |
There was a problem hiding this comment.
Can you explain why this is gv.getDualValue() and not gv? Before both conjuncts had false.
There was a problem hiding this comment.
Yes. The two false values are unrelated. The appliesTypeTest predicate has gotten a new column with the relevant GuardValue, which was implicitly true before and the guardControls_v1 was implicitly negating that, which was then just written as false. The other false value that remains in the third column of appliesTypeTest is about whether the type test has additional nested patterns, which needs to be false in order for the test to be soundly negatable.
|
The C# integration test failure looks unrelated. Merging. |

This PR reimplements the way that switch-case guards work in the shared Guards library. Given the now much more uniform handling of switch statements and patterns in the shared CFG, we can depend on MatchingSuccessors to more precisely indicate matching patterns. When these patterns occur in a switch-case context then we can have them satisfy
isEquality, which then provides the relevant implications. Support for expression-less switch statements is also added (they're considered to switch on the implicit constanttrue).A legacy hook is kept in order to not drop support for C++ switch-case guards.