Skip to content

Commit 84c8b2a

Browse files
aschackmullCopilot
andauthored
Unified: Add Guards inline test ported from Java.
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent a0197d8 commit 84c8b2a

4 files changed

Lines changed: 365 additions & 0 deletions

File tree

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
struct GuardError: Error {
2+
let msg: String
3+
}
4+
5+
class C {}
6+
7+
func chk() { }
8+
9+
func g(_ lbl: Any) -> Bool { return "\(lbl)".count > 10 }
10+
11+
func checkTrue(_ b: Bool, _ msg: String) throws {
12+
if !b { throw GuardError(msg: msg) }
13+
}
14+
15+
func checkFalse(_ b: Bool, _ msg: String) throws {
16+
try checkTrue(!b, msg)
17+
}
18+
19+
func t1(_ a: [Int]?, _ s0: String) {
20+
var s = s0
21+
if g("A") {
22+
chk() // $ guarded=g(A):true
23+
} else {
24+
chk() // $ guarded=g(A):false
25+
}
26+
27+
let b = g(1) ? g(2) : true
28+
if b != false {
29+
chk() // $ guarded=IfExpr:true guarded='b != false:true' guarded=b:true
30+
} else {
31+
chk() // $ guarded=IfExpr:false guarded='b != false:false' guarded=b:false guarded=g(1):true guarded=g(2):false
32+
}
33+
let sz = a != nil ? a!.count : 0
34+
var i = 0
35+
while i < sz {
36+
chk() // $ guarded='i < sz:true' MISSING: guarded='a != nil:true' guarded='sz:Lower bound 1' guarded='IfExpr:Lower bound 1' guarded='... .count:Lower bound 1' guarded='a:not null'
37+
let e = a![i]
38+
if e > 2 { break }
39+
i += 1
40+
}
41+
chk() // nothing guards here
42+
43+
if g(3) {
44+
s = "bar"
45+
}
46+
switch s {
47+
case "bar":
48+
chk() // $ guarded='"bar":match' guarded='s:bar'
49+
case "foo":
50+
chk() // $ guarded='"bar":no match' guarded='s:not bar' guarded='s0:not bar' guarded='"foo":match' guarded='s:foo' guarded='s0:foo' guarded=g(3):false
51+
default:
52+
chk() // $ guarded='"bar":no match' guarded='"foo":no match' guarded='s:not bar' guarded='s:not foo' guarded='s0:not bar' guarded=g(3):false
53+
}
54+
55+
let o: Any? = g(4) ? nil : s
56+
if o is String {
57+
chk() // $ guarded=TypeTestExpr:true MISSING: guarded='o:not null' guarded='IfExpr:not null' guarded=g(4):false guarded='s:not null'
58+
}
59+
}
60+
61+
func t2() throws {
62+
try checkTrue(g(1), "A")
63+
try checkFalse(g(2), "B")
64+
chk() // $ guarded='checkTrue(...):no exception' guarded='checkFalse(...):no exception' SPURIOUS: guarded='g(...):no exception' MISSING: guarded=g(1):true guarded=g(2):false
65+
}
66+
67+
func t3() {
68+
var b = g(1) && (g(2) || g(3))
69+
if b {
70+
chk() // $ guarded=b:true guarded='g(...) && ... \|\| ...:true' guarded=g(1):true guarded='g(...) \|\| g(...):true'
71+
} else {
72+
chk() // $ guarded=b:false guarded='g(...) && ... \|\| ...:false'
73+
}
74+
b = g(4) || !g(5)
75+
if b {
76+
chk() // $ guarded=b:true guarded='g(...) \|\| ! ...:true'
77+
} else {
78+
chk() // $ guarded=b:false guarded='g(...) \|\| ! ...:false' guarded=g(4):false guarded='! ...:false' guarded=g(5):true
79+
}
80+
}
81+
82+
enum Val {
83+
case E1
84+
case E2
85+
case E3
86+
}
87+
88+
func t4() {
89+
var x: Val? = nil // unique value
90+
if g(1) { x = Val.E1 } // unique value
91+
if g(2) { x = Val.E2 }
92+
if g("Alt2") { x = Val.E2 }
93+
if g(3) { x = Val.E3 } // unique value
94+
if x == nil {
95+
chk() // $ guarded='x == nil:true' guarded='x:null' MISSING: guarded=g(1):false guarded=g(2):false guarded=g(Alt2):false guarded=g(3):false
96+
}
97+
switch x {
98+
case .E1:
99+
chk() // $ guarded='... .E1:match' MISSING: guarded='x:E1' guarded=g(1):true guarded=g(2):false guarded=g(Alt2):false guarded=g(3):false
100+
case .E2:
101+
chk() // $ guarded='... .E1:no match' guarded='... .E2:match' MISSING: guarded='x:not E1' guarded='x:E2' guarded=g(3):false
102+
case .E3:
103+
chk() // $ guarded='... .E1:no match' guarded='... .E2:no match' guarded='... .E3:match' MISSING: guarded='x:not E1' guarded='x:not E2' guarded='x:E3' guarded=g(3):true
104+
case nil:
105+
break
106+
}
107+
let o: C? = g(4) ? C() : nil
108+
if o == nil {
109+
chk() // $ guarded='o == nil:true' guarded='o:null' guarded='IfExpr:null' MISSING: guarded=g(4):false
110+
} else {
111+
chk() // $ guarded='o == nil:false' guarded='o:not null' guarded='IfExpr:not null' guarded='C(...):not null' guarded=g(4):true
112+
}
113+
}
114+
115+
func t5(_ foo: String?) {
116+
var base = foo
117+
if base == nil {
118+
base = "/user"
119+
}
120+
if base == "/" {
121+
chk() // $ guarded='base == "/":true' guarded='base:/' guarded='base:not null' guarded='base == nil:false' guarded='foo:/' guarded='foo:not null'
122+
}
123+
}
124+
125+
func t6() {
126+
var o: C? = nil
127+
if g(1) {
128+
o = C()
129+
if g(2) { }
130+
}
131+
if o != nil {
132+
chk() // $ guarded='o != nil:true' guarded='o:not null' guarded='C(...):not null' guarded=g(1):true
133+
} else {
134+
chk() // $ guarded='o != nil:false' guarded='o:null' MISSING: guarded=g(1):false
135+
}
136+
}
137+
138+
func t7(_ a: [Int]) {
139+
var found = false
140+
var i = 0
141+
while i < a.count {
142+
let answer = a[i] == 42
143+
if answer {
144+
found = true
145+
}
146+
if found {
147+
chk() // $ guarded=found:true guarded='i < ... .count:true' MISSING: guarded='... .count:Lower bound 1'
148+
}
149+
i += 1
150+
}
151+
if found {
152+
chk() // $ guarded=found:true guarded='i < ... .count:false' MISSING: guarded='i:Lower bound 0'
153+
}
154+
}
155+
156+
func testNotNull1(_ input: String?) -> Bool {
157+
return input != nil && input!.count > 0
158+
}
159+
160+
func testNotNull2(_ input: String?) -> Bool {
161+
if input == nil { return false }
162+
return input!.count > 0
163+
}
164+
165+
func getNumOrDefault(_ number: Int?) -> Int {
166+
return number == nil ? 0 : number!
167+
}
168+
169+
func concatNonNull(_ s1: String?, _ s2: String?) -> String? {
170+
if s1 == nil || s2 == nil { return nil }
171+
return s1! + s2!
172+
}
173+
174+
func testEnumWrapper(_ flag: Bool) -> Status {
175+
return flag ? Status.SUCCESS : Status.FAILURE
176+
}
177+
178+
enum Status { case SUCCESS, FAILURE }
179+
180+
func testWrappers(_ s: String?, _ i: Int?) {
181+
if testNotNull1(s) {
182+
chk() // $ guarded=testNotNull1(...):true MISSING: guarded='s:not null'
183+
} else {
184+
chk() // $ guarded=testNotNull1(...):false
185+
}
186+
187+
if testNotNull2(s) {
188+
chk() // $ guarded=testNotNull2(...):true MISSING: guarded='s:not null'
189+
} else {
190+
chk() // $ guarded=testNotNull2(...):false
191+
}
192+
193+
if 0 == getNumOrDefault(i) {
194+
chk() // $ guarded='0 == getNumOrDefault(...):true' guarded='getNumOrDefault(...):0'
195+
} else {
196+
chk() // $ guarded='0 == getNumOrDefault(...):false' guarded='getNumOrDefault(...):not 0' MISSING: guarded='i:not 0' guarded='i:not null'
197+
}
198+
199+
if nil == concatNonNull(s, "suffix") {
200+
chk() // $ guarded='concatNonNull(...):null' guarded='nil == concatNonNull(...):true'
201+
} else {
202+
chk() // $ guarded='concatNonNull(...):not null' guarded='nil == concatNonNull(...):false' MISSING: guarded='s:not null'
203+
}
204+
205+
switch testEnumWrapper(g(1)) {
206+
case .SUCCESS:
207+
chk() // $ guarded='... .SUCCESS:match' MISSING: guarded='testEnumWrapper(...):SUCCESS' guarded=g(1):true
208+
case .FAILURE:
209+
chk() // $ guarded='... .SUCCESS:no match' MISSING: guarded='testEnumWrapper(...):not SUCCESS' guarded='testEnumWrapper(...):FAILURE' guarded='... .FAILURE:match' guarded=g(1):false
210+
}
211+
}
212+
213+
func ensureNotNull(_ o: Any?) throws {
214+
if o == nil { throw GuardError(msg: "null") }
215+
}
216+
217+
func testExceptionWrapper(_ s: String?) throws {
218+
chk() // nothing guards here
219+
try ensureNotNull(s)
220+
chk() // $ guarded='ensureNotNull(...):no exception' MISSING: guarded='s:not null'
221+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
| Guards.swift:22:5:22:9 | chk(...) | g(A):true |
2+
| Guards.swift:24:5:24:9 | chk(...) | g(A):false |
3+
| Guards.swift:29:5:29:9 | chk(...) | 'b != false:true' |
4+
| Guards.swift:29:5:29:9 | chk(...) | IfExpr:true |
5+
| Guards.swift:29:5:29:9 | chk(...) | b:true |
6+
| Guards.swift:31:5:31:9 | chk(...) | 'b != false:false' |
7+
| Guards.swift:31:5:31:9 | chk(...) | IfExpr:false |
8+
| Guards.swift:31:5:31:9 | chk(...) | b:false |
9+
| Guards.swift:31:5:31:9 | chk(...) | g(1):true |
10+
| Guards.swift:31:5:31:9 | chk(...) | g(2):false |
11+
| Guards.swift:36:5:36:9 | chk(...) | 'i < sz:true' |
12+
| Guards.swift:48:5:48:9 | chk(...) | '"bar":match' |
13+
| Guards.swift:48:5:48:9 | chk(...) | 's:bar' |
14+
| Guards.swift:50:5:50:9 | chk(...) | '"bar":no match' |
15+
| Guards.swift:50:5:50:9 | chk(...) | '"foo":match' |
16+
| Guards.swift:50:5:50:9 | chk(...) | 's0:foo' |
17+
| Guards.swift:50:5:50:9 | chk(...) | 's0:not bar' |
18+
| Guards.swift:50:5:50:9 | chk(...) | 's:foo' |
19+
| Guards.swift:50:5:50:9 | chk(...) | 's:not bar' |
20+
| Guards.swift:50:5:50:9 | chk(...) | g(3):false |
21+
| Guards.swift:52:5:52:9 | chk(...) | '"bar":no match' |
22+
| Guards.swift:52:5:52:9 | chk(...) | '"foo":no match' |
23+
| Guards.swift:52:5:52:9 | chk(...) | 's0:not bar' |
24+
| Guards.swift:52:5:52:9 | chk(...) | 's:not bar' |
25+
| Guards.swift:52:5:52:9 | chk(...) | 's:not foo' |
26+
| Guards.swift:52:5:52:9 | chk(...) | g(3):false |
27+
| Guards.swift:57:5:57:9 | chk(...) | TypeTestExpr:true |
28+
| Guards.swift:64:3:64:7 | chk(...) | 'checkFalse(...):no exception' |
29+
| Guards.swift:64:3:64:7 | chk(...) | 'checkTrue(...):no exception' |
30+
| Guards.swift:64:3:64:7 | chk(...) | 'g(...):no exception' |
31+
| Guards.swift:70:5:70:9 | chk(...) | 'g(...) && ... \|\| ...:true' |
32+
| Guards.swift:70:5:70:9 | chk(...) | 'g(...) \|\| g(...):true' |
33+
| Guards.swift:70:5:70:9 | chk(...) | b:true |
34+
| Guards.swift:70:5:70:9 | chk(...) | g(1):true |
35+
| Guards.swift:72:5:72:9 | chk(...) | 'g(...) && ... \|\| ...:false' |
36+
| Guards.swift:72:5:72:9 | chk(...) | b:false |
37+
| Guards.swift:76:5:76:9 | chk(...) | 'g(...) \|\| ! ...:true' |
38+
| Guards.swift:76:5:76:9 | chk(...) | b:true |
39+
| Guards.swift:78:5:78:9 | chk(...) | '! ...:false' |
40+
| Guards.swift:78:5:78:9 | chk(...) | 'g(...) \|\| ! ...:false' |
41+
| Guards.swift:78:5:78:9 | chk(...) | b:false |
42+
| Guards.swift:78:5:78:9 | chk(...) | g(4):false |
43+
| Guards.swift:78:5:78:9 | chk(...) | g(5):true |
44+
| Guards.swift:95:5:95:9 | chk(...) | 'x == nil:true' |
45+
| Guards.swift:95:5:95:9 | chk(...) | 'x:null' |
46+
| Guards.swift:99:5:99:9 | chk(...) | '... .E1:match' |
47+
| Guards.swift:101:5:101:9 | chk(...) | '... .E1:no match' |
48+
| Guards.swift:101:5:101:9 | chk(...) | '... .E2:match' |
49+
| Guards.swift:103:5:103:9 | chk(...) | '... .E1:no match' |
50+
| Guards.swift:103:5:103:9 | chk(...) | '... .E2:no match' |
51+
| Guards.swift:103:5:103:9 | chk(...) | '... .E3:match' |
52+
| Guards.swift:109:5:109:9 | chk(...) | 'IfExpr:null' |
53+
| Guards.swift:109:5:109:9 | chk(...) | 'o == nil:true' |
54+
| Guards.swift:109:5:109:9 | chk(...) | 'o:null' |
55+
| Guards.swift:111:5:111:9 | chk(...) | 'C(...):not null' |
56+
| Guards.swift:111:5:111:9 | chk(...) | 'IfExpr:not null' |
57+
| Guards.swift:111:5:111:9 | chk(...) | 'o == nil:false' |
58+
| Guards.swift:111:5:111:9 | chk(...) | 'o:not null' |
59+
| Guards.swift:111:5:111:9 | chk(...) | g(4):true |
60+
| Guards.swift:121:5:121:9 | chk(...) | 'base == "/":true' |
61+
| Guards.swift:121:5:121:9 | chk(...) | 'base == nil:false' |
62+
| Guards.swift:121:5:121:9 | chk(...) | 'base:/' |
63+
| Guards.swift:121:5:121:9 | chk(...) | 'base:not null' |
64+
| Guards.swift:121:5:121:9 | chk(...) | 'foo:/' |
65+
| Guards.swift:121:5:121:9 | chk(...) | 'foo:not null' |
66+
| Guards.swift:132:5:132:9 | chk(...) | 'C(...):not null' |
67+
| Guards.swift:132:5:132:9 | chk(...) | 'o != nil:true' |
68+
| Guards.swift:132:5:132:9 | chk(...) | 'o:not null' |
69+
| Guards.swift:132:5:132:9 | chk(...) | g(1):true |
70+
| Guards.swift:134:5:134:9 | chk(...) | 'o != nil:false' |
71+
| Guards.swift:134:5:134:9 | chk(...) | 'o:null' |
72+
| Guards.swift:147:7:147:11 | chk(...) | 'i < ... .count:true' |
73+
| Guards.swift:147:7:147:11 | chk(...) | found:true |
74+
| Guards.swift:152:5:152:9 | chk(...) | 'i < ... .count:false' |
75+
| Guards.swift:152:5:152:9 | chk(...) | found:true |
76+
| Guards.swift:182:5:182:9 | chk(...) | testNotNull1(...):true |
77+
| Guards.swift:184:5:184:9 | chk(...) | testNotNull1(...):false |
78+
| Guards.swift:188:5:188:9 | chk(...) | testNotNull2(...):true |
79+
| Guards.swift:190:5:190:9 | chk(...) | testNotNull2(...):false |
80+
| Guards.swift:194:5:194:9 | chk(...) | '0 == getNumOrDefault(...):true' |
81+
| Guards.swift:194:5:194:9 | chk(...) | 'getNumOrDefault(...):0' |
82+
| Guards.swift:196:5:196:9 | chk(...) | '0 == getNumOrDefault(...):false' |
83+
| Guards.swift:196:5:196:9 | chk(...) | 'getNumOrDefault(...):not 0' |
84+
| Guards.swift:200:5:200:9 | chk(...) | 'concatNonNull(...):null' |
85+
| Guards.swift:200:5:200:9 | chk(...) | 'nil == concatNonNull(...):true' |
86+
| Guards.swift:202:5:202:9 | chk(...) | 'concatNonNull(...):not null' |
87+
| Guards.swift:202:5:202:9 | chk(...) | 'nil == concatNonNull(...):false' |
88+
| Guards.swift:207:5:207:9 | chk(...) | '... .SUCCESS:match' |
89+
| Guards.swift:209:5:209:9 | chk(...) | '... .SUCCESS:no match' |
90+
| Guards.swift:220:3:220:7 | chk(...) | 'ensureNotNull(...):no exception' |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import unified
2+
import codeql.unified.internal.controlflow.Guards
3+
import codeql.util.Boolean
4+
5+
private string ppLiteral(Expr e) {
6+
result = e.getStringValue()
7+
or
8+
result = e.(IntLiteral).getValue()
9+
}
10+
11+
string ppGuard(Guard g, Boolean branch) {
12+
exists(CallExpr call, Argument arg |
13+
call = g and
14+
call.getAnArgument() = arg and
15+
result =
16+
call.getCallee().(Token).getValue() + "(" + ppLiteral(arg.getValue()) + ")" + ":" + branch
17+
)
18+
or
19+
exists(BinaryExpr bin |
20+
bin = g and
21+
result =
22+
"'" + bin.getLeft() + " " + bin.getOperator().getValue() + " " + bin.getRight() + ":" + branch
23+
+ "'"
24+
)
25+
}
26+
27+
bindingset[s]
28+
private string quoteIfSpaces(string s) {
29+
if s.matches("% %") then result = "'" + s + "'" else result = s
30+
}
31+
32+
query predicate guarded(CallExpr call, string guard) {
33+
call.getCallee().(Token).getValue() = "chk" and
34+
exists(Guard g, BasicBlock bb, boolean branch |
35+
g.controls(bb, branch) and
36+
call.getBasicBlock() = bb
37+
|
38+
guard = ppGuard(g, branch)
39+
or
40+
not exists(ppGuard(g, branch)) and
41+
guard = quoteIfSpaces(g.toString() + ":" + branch)
42+
)
43+
or
44+
call.getCallee().(Token).getValue() = "chk" and
45+
exists(Guard g, BasicBlock bb, GuardValue val |
46+
g.valueControls(bb, val) and
47+
not exists(val.asBooleanValue()) and
48+
call.getBasicBlock() = bb
49+
|
50+
guard = "'" + g.toString() + ":" + val + "'"
51+
)
52+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: GuardsInline.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

0 commit comments

Comments
 (0)