Skip to content

Commit 9353549

Browse files
committed
Swift: fixes responding to comments
1 parent 052a008 commit 9353549

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ predicate storeStep(Node node1, ContentSet c, Node node2) {
563563
exists(EnumElementExpr enum, int pos |
564564
node1.asExpr() = enum.getArgument(pos).getExpr() and
565565
node2.asExpr() = enum and
566-
c.isSingleton(any(Content::EnumContent ec | ec.getField() = enum.getElement().getParam(pos)))
566+
c.isSingleton(any(Content::EnumContent ec | ec.getParam() = enum.getElement().getParam(pos)))
567567
)
568568
or
569569
FlowSummaryImpl::Private::Steps::summaryStoreStep(node1, c, node2)
@@ -588,14 +588,14 @@ predicate readStep(Node node1, ContentSet c, Node node2) {
588588
)
589589
or
590590
// read of an enum member via `case let .variant(v1, v2)` pattern matching
591-
exists(Expr enumExpr, ParamDecl enumField, VarDecl boundVar |
591+
exists(Expr enumExpr, ParamDecl enumParam, VarDecl boundVar |
592592
node1.asExpr() = enumExpr and
593593
node2.asDefinition().getSourceVariable() = boundVar and
594-
c.isSingleton(any(Content::EnumContent ec | ec.getField() = enumField))
594+
c.isSingleton(any(Content::EnumContent ec | ec.getParam() = enumParam))
595595
|
596596
exists(EnumElementPattern enumPat, NamedPattern namePat, int idx |
597597
enumPat.getMatchingExpr() = enumExpr and
598-
enumPat.getElement().getParam(idx) = enumField and
598+
enumPat.getElement().getParam(idx) = enumParam and
599599
namePat.getIdentityPreservingEnclosingPattern*() = enumPat.getSubPattern(idx) and
600600
namePat.getVarDecl() = boundVar
601601
)

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPublic.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ module Content {
172172
override string toString() { result = "Tuple element at index " + index.toString() }
173173
}
174174

175-
/** A field of an enum element. */
175+
/** A parameter of an enum element. */
176176
class EnumContent extends Content, TEnumContent {
177-
private ParamDecl f;
177+
private ParamDecl p;
178178

179-
EnumContent() { this = TEnumContent(f) }
179+
EnumContent() { this = TEnumContent(p) }
180180

181-
/** Gets the declaration of the enum field. */
182-
ParamDecl getField() { result = f }
181+
/** Gets the declaration of the enum parameter. */
182+
ParamDecl getParam() { result = p }
183183

184184
override string toString() {
185-
exists(EnumElementDecl d, int pos | d.getParam(pos) = f | result = d.toString() + ":" + pos)
185+
exists(EnumElementDecl d, int pos | d.getParam(pos) = p | result = d.toString() + ":" + pos)
186186
}
187187
}
188188
}

swift/ql/lib/codeql/swift/elements/expr/EnumElementExpr.qll

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ private import codeql.swift.elements.decl.EnumElementDecl
88
/**
99
* An expression that constructs a case of an enum.
1010
*/
11-
abstract class EnumElementExpr extends Expr {
11+
class EnumElementExpr extends Expr {
1212
EnumElementDecl decl;
1313

14+
EnumElementExpr() {
15+
this.(NullaryEnumElementExpr).getElement() = decl or
16+
this.(NonNullaryEnumElementExpr).getElement() = decl
17+
}
18+
1419
/** Gets the declaration of the enum element that this expression creates. */
1520
EnumElementDecl getElement() { result = decl }
1621

1722
/** Gets the `i`th argument passed to this enum element expression (0-based). */
18-
Argument getArgument(int i) { none() }
23+
Argument getArgument(int i) { result = this.(NonNullaryEnumElementExpr).getArgument(i) }
1924

2025
/** Gets an argument passed to this enum element expression, if any. */
2126
final Argument getAnArgument() { result = this.getArgument(_) }
@@ -24,23 +29,28 @@ abstract class EnumElementExpr extends Expr {
2429
final int getNumberOfArguments() { result = count(this.getArgument(_)) }
2530
}
2631

32+
/**
33+
* An expression that refers to an enum element, either directly in the case of a nullary enum element,
34+
* or referring to the enum element constructor in the case of a non-nullary enum element.
35+
*/
2736
private class EnumElementLookupExpr extends MethodLookupExpr {
2837
EnumElementLookupExpr() { this.getMember() instanceof EnumElementDecl }
2938
}
3039

31-
private class NullaryEnumElementExpr extends EnumElementExpr instanceof EnumElementLookupExpr {
32-
NullaryEnumElementExpr() {
33-
this.getMember() = decl and not exists(CallExpr ce | ce.getFunction() = this)
34-
}
40+
/** An expression creating an enum with no arguments */
41+
private class NullaryEnumElementExpr extends EnumElementLookupExpr {
42+
/** Gets the declaration of the enum element that this expression creates. */
43+
EnumElementDecl getElement() { this.getMember() = result }
44+
45+
NullaryEnumElementExpr() { not exists(CallExpr ce | ce.getFunction() = this) }
3546
}
3647

37-
private class NonNullaryEnumElementExpr extends EnumElementExpr instanceof CallExpr {
38-
NonNullaryEnumElementExpr() {
39-
exists(EnumElementLookupExpr eele |
40-
this.getFunction() = eele and
41-
eele.getMember() = decl
42-
)
43-
}
48+
/** An expression creating an enum with arguments */
49+
private class NonNullaryEnumElementExpr extends CallExpr {
50+
EnumElementLookupExpr eele;
51+
52+
/** Gets the declaration of the enum element that this expression creates. */
53+
EnumElementDecl getElement() { eele.getMember() = result }
4454

45-
override Argument getArgument(int i) { result = CallExpr.super.getArgument(i) }
55+
NonNullaryEnumElementExpr() { this.getFunction() = eele }
4656
}

swift/ql/lib/codeql/swift/elements/pattern/NamedPattern.qll

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
private import codeql.swift.generated.pattern.NamedPattern
22
private import codeql.swift.elements.decl.VarDecl
33

4+
/**
5+
* A pattern that corresponds to a fresh variable binding.
6+
*
7+
* For example, `x` as in `if case let .some(x) = ...` is a `NamedPattern`,
8+
* whereas `y` as in `if case .some(y) = ...` is instead an `ExprPattern`.
9+
*/
410
class NamedPattern extends Generated::NamedPattern {
5-
/** Holds if this named pattern has a corresponding `VarDecl` */
11+
/**
12+
* Holds if this named pattern has a corresponding `VarDecl`.
13+
* This will be the case as long as the variable is subsequently used.
14+
*/
615
predicate hasVarDecl() { exists(this.getVarDecl()) }
716

8-
/** Gets the `VarDecl` bound by this named pattern, if any. */
17+
/**
18+
* Gets the `VarDecl` bound by this named pattern, if any.
19+
* This will be the case as long as the variable is subsequently used.
20+
*/
921
VarDecl getVarDecl() {
1022
this.getEnclosingPattern*() = result.getParentPattern().getFullyUnresolved() and
1123
result.getName() = this.getName()

0 commit comments

Comments
 (0)