Swift: Add named predicates for known `KeyPathComponent` kinds. · github/codeql@64443df · GitHub
Skip to content

Commit 64443df

Browse files
committed
Swift: Add named predicates for known KeyPathComponent kinds.
1 parent b900185 commit 64443df

8 files changed

Lines changed: 94 additions & 5 deletions

File tree

swift/ql/.generated.list

Lines changed: 2 additions & 3 deletions
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.KeyPathComponent
32

4-
class KeyPathComponent extends Generated::KeyPathComponent { }
3+
class KeyPathComponent extends Generated::KeyPathComponent {
4+
/**
5+
* Property access like `.bar` in `\Foo.bar`.
6+
*/
7+
predicate is_property() { getKind() = 3 }
8+
9+
/**
10+
* Array or dictionary subscript like `[1]` or `["a", "b"]`.
11+
*/
12+
predicate is_subscript() { getKind() = 4 }
13+
14+
/**
15+
* Optional forcing `!`.
16+
*/
17+
predicate is_optional_forcing() { getKind() = 5 }
18+
19+
/**
20+
* Optional chaining `?`.
21+
*/
22+
predicate is_optional_chaining() { getKind() = 6 }
23+
24+
/**
25+
* Implicit optional wrapping component inserted by the compiler when an optional chain ends in a non-optional value.
26+
*/
27+
predicate is_optional_wrapping() { getKind() = 7 }
28+
29+
/**
30+
* Reference to the entire object; the `self` in `\Foo.self`.
31+
*/
32+
predicate is_self() { getKind() = 8 }
33+
34+
/**
35+
* Tuple indexing like `.1`.
36+
*/
37+
predicate is_tuple_indexing() { getKind() = 9 }
38+
}

swift/ql/lib/codeql/swift/generated/KeyPathComponent.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Generated {
1616
/**
1717
* Gets the kind of key path component.
1818
*
19+
* INTERNAL: Do not use.
20+
*
1921
* This is 3 for properties, 4 for array and dictionary subscripts, 5 for optional forcing
2022
* (`!`), 6 for optional chaining (`?`), 7 for implicit optional wrapping, 8 for `self`,
2123
* and 9 for tuple element indexing.

swift/ql/lib/codeql/swift/generated/Raw.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ module Raw {
140140
/**
141141
* Gets the kind of key path component.
142142
*
143+
* INTERNAL: Do not use.
144+
*
143145
* This is 3 for properties, 4 for array and dictionary subscripts, 5 for optional forcing
144146
* (`!`), 6 for optional chaining (`?`), 7 for implicit optional wrapping, 8 for `self`,
145147
* and 9 for tuple element indexing.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
| file://:0:0:0:0 | KeyPathComponent | getKind: | 7 | | | | | optional wrapping | | |
2+
| key_path_expr.swift:11:17:11:17 | KeyPathComponent | getKind: | 3 | property | | | | | | |
3+
| key_path_expr.swift:12:24:12:26 | KeyPathComponent | getKind: | 4 | | subscript | | | | | |
4+
| key_path_expr.swift:13:34:13:38 | KeyPathComponent | getKind: | 4 | | subscript | | | | | |
5+
| key_path_expr.swift:14:31:14:31 | KeyPathComponent | getKind: | 5 | | | optional forcing | | | | |
6+
| key_path_expr.swift:14:31:14:31 | KeyPathComponent | getKind: | 8 | | | | | | self | |
7+
| key_path_expr.swift:15:21:15:21 | KeyPathComponent | getKind: | 3 | property | | | | | | |
8+
| key_path_expr.swift:15:24:15:24 | KeyPathComponent | getKind: | 6 | | | | optional chaining | | | |
9+
| key_path_expr.swift:15:26:15:26 | KeyPathComponent | getKind: | 3 | property | | | | | | |
10+
| key_path_expr.swift:16:25:16:25 | KeyPathComponent | getKind: | 3 | property | | | | | | |
11+
| key_path_expr.swift:16:28:16:28 | KeyPathComponent | getKind: | 6 | | | | optional chaining | | | |
12+
| key_path_expr.swift:16:30:16:30 | KeyPathComponent | getKind: | 3 | property | | | | | | |
13+
| key_path_expr.swift:17:16:17:16 | KeyPathComponent | getKind: | 8 | | | | | | self | |
14+
| key_path_expr.swift:18:32:18:32 | KeyPathComponent | getKind: | 9 | | | | | | | tuple indexing |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import codeql.swift.elements
2+
import TestUtils
3+
4+
from
5+
KeyPathComponent x, string property, string subscript, string opt_forcing, string opt_chaining,
6+
string opt_wrapping, string self, string tuple_indexing
7+
where
8+
toBeTested(x) and
9+
not x.isUnknown() and
10+
(if x.is_property() then property = "property" else property = "") and
11+
(if x.is_subscript() then subscript = "subscript" else subscript = "") and
12+
(if x.is_optional_forcing() then opt_forcing = "optional forcing" else opt_forcing = "") and
13+
(if x.is_optional_chaining() then opt_chaining = "optional chaining" else opt_chaining = "") and
14+
(if x.is_optional_wrapping() then opt_wrapping = "optional wrapping" else opt_wrapping = "") and
15+
(if x.is_self() then self = "self" else self = "") and
16+
if x.is_tuple_indexing() then tuple_indexing = "tuple indexing" else tuple_indexing = ""
17+
select x, "getKind:", x.getKind(), property, subscript, opt_forcing, opt_chaining, opt_wrapping,
18+
self, tuple_indexing
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct Bar {
2+
var value : Int
3+
var opt : Int?
4+
}
5+
6+
struct Foo {
7+
var value : Int
8+
var opt : Bar?
9+
}
10+
11+
let prop = \Foo.value
12+
let arrElement = \[Int][0]
13+
let dictElement = \[String : Int]["a"]
14+
let optForce = \Optional<Int>.self!
15+
let optChain = \Foo.opt?.opt
16+
let optChainWrap = \Foo.opt?.value
17+
let slf = \Int.self
18+
let tupleElement = \(Int, Int).0

swift/schema.py

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)