Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions unified/extractor/src/languages/swift/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct SwiftContext {
/// in the correct context.
in_function_type: bool,
/// True while translating a pattern. Optional chaining in this context is
/// represented as an `Optional.some` call rather than being unwrapped.
/// represented as an `Optional.some` call rather than a postfix operator.
in_pattern: bool,
/// True when a conditional compilation block should be translated into an
/// if expression.
Expand Down Expand Up @@ -1205,11 +1205,19 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
modifier: [(modifier #{spec})]
pattern: (identifier #{name})
value: {
// Expand [x] into [x = x]
if let Some(val) = val {
let value = if let Some(val) = val {
ctx.translate_one(val)
} else {
tree!((identifier #{name}))
};
if spec.map_or_default(|s| ctx.source_text(s) == "weak") {
tree!((call_expr
callee: (member_access_expr
base: (identifier "Optional")
member_name_node: (identifier "some"))
argument: (argument value: {value})))
} else {
value
}
})
),
Expand Down Expand Up @@ -1537,8 +1545,8 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
}
),
// ---- Optionals and errors ----
// Optional chaining — unwrap the marker
rule!((optionalChainingExpr expression: @inner) => expr {
// Optional patterns wrap a value; optional chaining unwraps it.
rule!((optionalChainingExpr expression: @inner questionMark: @mark) => expr {
let inner = ctx
.translate(inner)
.into_iter()
Expand All @@ -1551,7 +1559,9 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
member_name_node: (identifier "some"))
argument: (argument value: {inner})))
} else {
inner
tree!((unary_expr
operator: (postfix_operator "?" location: {mark})
operand: {inner}))
}
}),
// try/try?/try! expr → unary_expr with operator "try", "try?" or "try!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,26 @@ top_level source="⟨body⟩"
value:
function_expr source="{ [⟨capture_declaration⟩] in ⟨body⟩ }"
capture_declaration:
variable_declaration source="⟨modifier⟩ ⟨pattern⟩⟨value⟩"
variable_declaration source="⟨modifier⟩⟨value⟩⟨pattern⟩"
modifier: modifier "weak" source="weak"
pattern: identifier "self" source="self"
value: identifier "self" source="self"
value:
call_expr source="weak ⟨argument⟩"
callee:
member_access_expr source=""
base: identifier "Optional" source=""
member_name_node: identifier "some" source=""
argument:
argument source="⟨value⟩"
value: identifier "self" source="self"
body:
block source="⟨stmt⟩"
stmt:
call_expr source="⟨callee⟩()"
callee:
member_access_expr source="⟨base⟩?.⟨member_name_node⟩"
base: identifier "self" source="self"
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base:
unary_expr source="⟨operand⟩⟨operator⟩"
operand: identifier "self" source="self"
operator: postfix_operator "?" source="?"
member_name_node: identifier "doThing" source="doThing"
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,11 @@ top_level source="⟨body⟩"
pattern:
call_expr source="⟨callee⟩ ⟨argument⟩"
callee:
member_access_expr source="⟨base⟩?.⟨member_name_node⟩"
base: identifier "values" source="values"
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base:
unary_expr source="⟨operand⟩⟨operator⟩"
operand: identifier "values" source="values"
operator: postfix_operator "?" source="?"
member_name_node: identifier "map" source="map"
argument:
argument source="⟨value⟩"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ top_level source="⟨body⟩"
modifier: modifier "let" source="let"
pattern: identifier "n" source="n"
value:
member_access_expr source="⟨base⟩?.⟨member_name_node⟩"
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base:
member_access_expr source="⟨base⟩?.⟨member_name_node⟩"
base: identifier "obj" source="obj"
member_name_node: identifier "foo" source="foo"
unary_expr source="⟨operand⟩⟨operator⟩"
operand:
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
base:
unary_expr source="⟨operand⟩⟨operator⟩"
operand: identifier "obj" source="obj"
operator: postfix_operator "?" source="?"
member_name_node: identifier "foo" source="foo"
operator: postfix_operator "?" source="?"
member_name_node: identifier "bar" source="bar"
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private class SwiftDataFlowPlugin extends DataFlowPlugin {
exists(UnaryExpr expr |
expr.getOperator().(PrefixOperator).getValue() = "&"
or
expr.getOperator().(PostfixOperator).getValue() = "!"
expr.getOperator().(PostfixOperator).getValue() = ["!", "?"]
Comment thread
asgerf marked this conversation as resolved.
Resolved
|
expr1 = expr.getOperand() and
expr2 = expr
Expand Down Expand Up @@ -163,4 +163,11 @@ private predicate isInWriteBackPosition(Expr expr, boolean direct) {
expr = call.getCallee() and
direct = false
)
or
exists(UnaryExpr unary |
unary.getOperator().(PostfixOperator).getValue() = "?" and
isInWriteBackPosition(unary, _) and
expr = unary.getOperand() and
direct = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ private class SwiftTypeInferencePlugin extends Plugin::TypeInferencePlugin {
prefix1.isEmpty() and
prefix2.isEmpty()
)
or
n2 =
any(UnaryExpr expr |
n1 = expr.getOperand() and
expr.getOperator().(PostfixOperator).getValue() = "?" and
prefix1 = TypePath::singleton(any(OptionalType t).getPositionalTypeParameter(0)) and
prefix2.isEmpty()
Comment thread
asgerf marked this conversation as resolved.
Resolved
)
}

override Type inferType(AstNode n, TypePath path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@
| 129 | cfg.swift:129:3:129:27 | let n14 | 'let n14 -V n14 -> opt -^ ... ! -^ ... .self -^ ... .myInt' |
| 130 | cfg.swift:130:3:130:27 | let n15 | 'let n15 -V n15 -> opt -^ ... ! -^ ... .getMyInt -^ ... .getMyInt(...)' |
| 131 | cfg.swift:131:3:131:32 | let n16 | 'let n16 -V n16 -> opt -^ ... ! -^ ... .self -^ ... .getMyInt -^ ... .getMyInt(...)' |
| 133 | cfg.swift:133:3:133:22 | let n17 | 'let n17 -V n17 -> opt -^ ... .myInt' |
| 134 | cfg.swift:134:3:134:27 | let n18 | 'let n18 -V n18 -> opt -^ ... .self -^ ... .myInt' |
| 135 | cfg.swift:135:3:135:27 | let n19 | 'let n19 -V n19 -> opt -^ ... .getMyInt -^ ... .getMyInt(...)' |
| 136 | cfg.swift:136:3:136:32 | let n20 | 'let n20 -V n20 -> opt -^ ... .self -^ ... .getMyInt -^ ... .getMyInt(...)' |
| 133 | cfg.swift:133:3:133:22 | let n17 | 'let n17 -V n17 -> opt -^ ... ? -^ ... .myInt' |
| 134 | cfg.swift:134:3:134:27 | let n18 | 'let n18 -V n18 -> opt -^ ... ? -^ ... .self -^ ... .myInt' |
| 135 | cfg.swift:135:3:135:27 | let n19 | 'let n19 -V n19 -> opt -^ ... ? -^ ... .getMyInt -^ ... .getMyInt(...)' |
| 136 | cfg.swift:136:3:136:32 | let n20 | 'let n20 -V n20 -> opt -^ ... ? -^ ... .self -^ ... .getMyInt -^ ... .getMyInt(...)' |
| 139 | cfg.swift:139:1:173:1 | func patterns | 'func patterns -V patterns' |
| 139 | cfg.swift:139:15:139:15 | x | 'x -> Block' |
| 140 | cfg.swift:140:3:141:12 | ForEachStmt | 'ForEachStmt -V 0 -> 10 -^ ... ... ...' |
Expand Down Expand Up @@ -384,7 +384,7 @@
| 381 | cfg.swift:381:12:381:12 | c | 'c -^ ReturnExpr' |
| 385 | cfg.swift:385:1:387:1 | func testOptional | 'func testOptional -V testOptional' |
| 385 | cfg.swift:385:19:385:19 | c | 'c -> Block' |
| 386 | cfg.swift:386:10:386:10 | c | 'c -^ ... .getOptional -^ ... .getOptional(...) -^ ... .getMyInt -^ ... .getMyInt(...) -^ ReturnExpr' |
| 386 | cfg.swift:386:10:386:10 | c | 'c -^ ... ? -^ ... .getOptional -^ ... .getOptional(...) -^ ... ? -^ ... .getMyInt -^ ... .getMyInt(...) -^ ReturnExpr' |
| 389 | cfg.swift:389:1:393:1 | func testCapture | 'func testCapture -V testCapture' |
| 389 | cfg.swift:389:18:389:18 | x | 'x -> y -> Block' |
| 390 | cfg.swift:390:13:390:21 | z | 'z -V z -> x -> y -^ ... + ... -> t -V t -> "literal" -^ FunctionExpr -^ ReturnExpr' |
Expand Down
10 changes: 10 additions & 0 deletions unified/ql/test/library-tests/dataflow/capture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ class C {
closure()
}

func capture_weak_self_optional_call() {
x = source("C.5")
let closure = { [weak self] in
sink(self?.getX() ?? "") // $ hasValueFlow=C.5
}
closure()
}

func getX() -> String { x }

func capture_unowned_self() {
x = source("C.4")
let closure = { [unowned self] in
Expand Down
51 changes: 51 additions & 0 deletions unified/ql/test/library-tests/dataflow/optional-chaining.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class OptionalChainBox {
var value: String = ""
var next: OptionalChainBox?

func getValue() -> String {
return value
}
}

struct OptionalChainValue {
var stored: String
var computed: String {
get { stored }
set { stored = newValue }
}
}

class OptionalChainContainer {
private var storedValue: OptionalChainValue? = nil

var optionalValue: OptionalChainValue? {
get { storedValue }
set { storedValue = newValue }
}
}

func testOptionalChaining() {
let box: OptionalChainBox? = Optional.some(OptionalChainBox())
box?.value = source("optional.field")
sink(box?.value) // $ hasValueFlow=optional.field
sink(box?.getValue()) // $ hasValueFlow=optional.field
}

func testOptionalValueTypeSetterWriteBack() {
let container = OptionalChainContainer()
container.optionalValue = Optional.some(OptionalChainValue(stored: ""))
container.optionalValue?.computed = source("optional.writeback")
sink(container.optionalValue?.stored) // $ hasValueFlow=optional.writeback
}

func testRepeatedOptionalChaining() {
let outer: OptionalChainBox? = Optional.some(OptionalChainBox())
outer?.next = Optional.some(OptionalChainBox())
outer?.next?.value = source("optional.chain")
sink(outer?.next?.value) // $ hasValueFlow=optional.chain
}

func testOptionalSubscript() {
let values = Optional.some([source("optional.subscript")])
sink(values?[0]) // $ hasValueFlow=optional.subscript
}
Loading
Loading