Skip to content

Commit 119b058

Browse files
committed
Swift: Handle method and initializer key path components in QL
Note that it does not seem to be possible to write test that exercise this code. Passing `-enable-experimental-feature KeyPathWithMethodMembers` to the extractor results in: ``` error: experimental feature 'KeyPathWithMethodMembers' cannot be enabled in production compiler ```
1 parent b08b123 commit 119b058

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

swift/ql/.generated.list

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swift/ql/lib/codeql/swift/elements/internal/KeyPathComponentImpl.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ module Impl {
77
* A component of a `KeyPathExpr`.
88
*/
99
class KeyPathComponent extends Generated::KeyPathComponent {
10+
/**
11+
* Method or initializer application like `.bar` in `\Foo.bar` with `bar` a method.
12+
*/
13+
predicate isApply() { this.getKind() = 4 }
14+
1015
/**
1116
* Property access like `.bar` in `\Foo.bar`.
1217
*/

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

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swift/ql/test/library-tests/elements/KeyPathComponent/KindAccessors.ql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import codeql.swift.elements
22
import TestUtils
33

44
from
5-
KeyPathComponent x, string property, string subscript, string opt_forcing, string opt_chaining,
6-
string opt_wrapping, string self, string tuple_indexing
5+
KeyPathComponent x, string apply, string property, string subscript, string opt_forcing,
6+
string opt_chaining, string opt_wrapping, string self, string tuple_indexing
77
where
88
toBeTested(x) and
99
not x.isUnknown() and
10+
(if x.isApply() then apply = "apply" else apply = "") and
1011
(if x.isProperty() then property = "property" else property = "") and
1112
(if x.isSubscript() then subscript = "subscript" else subscript = "") and
1213
(if x.isOptionalForcing() then opt_forcing = "optional forcing" else opt_forcing = "") and
1314
(if x.isOptionalChaining() then opt_chaining = "optional chaining" else opt_chaining = "") and
1415
(if x.isOptionalWrapping() then opt_wrapping = "optional wrapping" else opt_wrapping = "") and
1516
(if x.isSelf() then self = "self" else self = "") and
1617
if x.isTupleIndexing() 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
18+
select x, "getKind:", x.getKind(), apply, property, subscript, opt_forcing, opt_chaining,
19+
opt_wrapping, self, tuple_indexing

swift/schema.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,13 @@ class KeyPathComponent(AstNode):
490490
kind: int | doc("kind of key path component") | desc("""
491491
INTERNAL: Do not use.
492492
493-
TODO: Swift 6.2 update with UnresolvedApply and Apply
493+
This is 4 for method or initializer application, 5 for members, 6 for array and dictionary
494+
subscripts, 7 for optional forcing (`!`), 8 for optional chaining (`?`), 9 for implicit
495+
optional wrapping, 10 for `self`, and 11 for tuple element indexing.
494496
495-
This is 5 for properties, 6 for array and dictionary subscripts, 7 for optional forcing
496-
(`!`), 8 for optional chaining (`?`), 9 for implicit optional wrapping, 10 for `self`,
497-
and 11 for tuple element indexing.
498-
499-
The following values should not appear: 0 for invalid components, 2 for unresolved
500-
properties, 3 for unresolved subscripts, 12 for #keyPath dictionary keys, and 13 for
501-
implicit IDE code completion data.
497+
The following values should not appear: 0 for invalid components, 1 for unresolved
498+
method or initializer applications, 2 for unresolved members, 3 for unresolved subscripts,
499+
12 for #keyPath dictionary keys, and 13 for implicit IDE code completion data.
502500
""")
503501
subscript_arguments : list[Argument] | child | doc(
504502
"arguments to an array or dictionary subscript expression")

0 commit comments

Comments
 (0)