Skip to content

Commit 0b40def

Browse files
committed
Go: support compound condition guards
1 parent 26e22a0 commit 0b40def

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎go/ql/lib/semmle/go/controlflow/Guards.qll‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ private module GuardsInput implements
3131
CfgImpl::Cfg::BasicBlock getBasicBlock() { result = this.getControlFlowNode().getBasicBlock() }
3232
}
3333

34+
predicate booleanOutcomeBlock(Expr guard, CfgImpl::Cfg::BasicBlock outcomeBlock, boolean branch) {
35+
exists(CfgImpl::Cfg::ControlFlowNode outcomeNode |
36+
branch = true and outcomeNode.isAfterTrue(guard)
37+
or
38+
branch = false and outcomeNode.isAfterFalse(guard)
39+
|
40+
outcomeBlock = outcomeNode.getBasicBlock()
41+
)
42+
}
43+
3444
private newtype TConstantValue = TStringValue(string s) { s = any(G::Expr e).getStringValue() }
3545

3646
class ConstantValue extends TConstantValue {

‎go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ controlsResult
55
| guards.go:9:7:9:16 | ...==... | positive | false |
66
| guards.go:9:7:9:16 | ...==... | zero | true |
77
| guards.go:28:5:28:5 | a | compound true | true |
8+
| guards.go:28:5:28:17 | ...&&... | compound false | false |
9+
| guards.go:28:5:28:17 | ...&&... | compound true | true |
10+
| guards.go:28:11:28:16 | ...\|\|... | compound true | true |
811
| guards.go:36:5:36:20 | ...==... | upcast | true |
912
| guards.go:39:5:39:19 | ...==... | downcast | true |
1013
| guards.go:42:5:42:22 | ...==... | upcast narrow | true |
@@ -24,6 +27,9 @@ valueControlsResult
2427
| guards.go:9:7:9:16 | ...==... | positive | false |
2528
| guards.go:9:7:9:16 | ...==... | zero | true |
2629
| guards.go:28:5:28:5 | a | compound true | true |
30+
| guards.go:28:5:28:17 | ...&&... | compound false | false |
31+
| guards.go:28:5:28:17 | ...&&... | compound true | true |
32+
| guards.go:28:11:28:16 | ...\|\|... | compound true | true |
2733
| guards.go:36:5:36:15 | type conversion | upcast | 0 |
2834
| guards.go:36:5:36:20 | ...==... | upcast | true |
2935
| guards.go:36:11:36:14 | wide | upcast | 0 |

0 commit comments

Comments
 (0)