Skip to content

Commit 0bf705a

Browse files
committed
Unified: Add locations for blocks
Blocks are slightly awkward, since we destructure them (as codeBlock) in the query, and then reconstruct them (as `block`) in the rule body, meaning the location of the block is (by default) assigned to the wrong place by the heuristic. To get around this, I added a helper function that updates the location appropriately, including handling cases where we only optionally match a block. (Also, in some cases we can fix this by just not destructuring `codeBlock`s in the first place -- there's already a rule that maps `codeBlock` to `block`.)
1 parent c651c5f commit 0bf705a

33 files changed

Lines changed: 107 additions & 76 deletions

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use codeql_extractor::extractor::desugaring;
2-
use yeast::{ConcreteDesugarer, DesugaringConfig, PhaseKind, Rule, rule, tree};
2+
use yeast::{ConcreteDesugarer, DesugaringConfig, PhaseKind, Rule, rule, tree, tree_at};
33

44
/// User context propagated from outer rules down to the inner rules that
55
/// emit the corresponding output declarations, so that each emitted node
@@ -60,6 +60,17 @@ impl SwiftContext {
6060
}
6161
}
6262

63+
fn block_with_anchor(
64+
ctx: &mut yeast::build::BuildCtx<'_, SwiftContext>,
65+
statements: Vec<yeast::Id>,
66+
anchor: Option<yeast::Id>,
67+
) -> yeast::Id {
68+
match anchor {
69+
Some(anchor) => tree_at!(ctx, anchor, (block stmt: {statements})),
70+
None => tree!(ctx, (block stmt: {statements})),
71+
}
72+
}
73+
6374
/// Build a freshly-created `chained_declaration` modifier node if
6475
/// `ctx.is_chained`, else `None`. Used by inner declaration rules to
6576
/// emit the chained tag for non-first children of a flattening outer
@@ -298,14 +309,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
298309
bindings: (patternBinding
299310
pattern: (identifierPattern identifier: @@name)
300311
typeAnnotation: (typeAnnotation type: @ty)
301-
accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body)))
312+
accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body) @@accessor_block))
302313
=>
303314
(accessor_declaration
304315
modifier: (modifier #{spec})
305316
name_node: (identifier #{name})
306317
type: {ty}
307-
accessor_kind: (accessor_kind "get")
308-
body: (block stmt: {body}))
318+
accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", accessor_block)}
319+
body: {block_with_anchor(&mut ctx, body, Some(accessor_block))})
309320
),
310321
// A property with an explicit accessor block. swift-syntax makes both
311322
// shapes plain `accessorDecl`s, so they are told apart by the presence
@@ -561,15 +572,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
561572
signature: (functionSignature
562573
parameterClause: (functionParameterClause parameters: _* @params)
563574
returnClause: (returnClause type: @ret)?)
564-
body: (codeBlock statements: _* @body))
575+
body: (codeBlock) @body)
565576
=>
566577
(function_declaration
567578
modifier: {mods}
568579
name_node: (identifier #{name})
569580
type_parameter: {type_params}
570581
parameter: {params}
571582
return_type: {ret}
572-
body: (block stmt: {body}))
583+
body: {body})
573584
),
574585
rule!(
575586
(functionDecl
@@ -749,14 +760,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
749760
capture: (closureCaptureClause items: _* @captures)?
750761
parameterClause: _* @params
751762
returnClause: (returnClause type: @ret)?)?
752-
statements: _* @body)
763+
statements: _* @body) @@closure
753764
=>
754765
(function_expr
755766
modifier: {attrs}
756767
capture_declaration: {captures}
757768
parameter: {params}
758769
return_type: {ret}
759-
body: (block stmt: {body}))
770+
body: {block_with_anchor(&mut ctx, body, Some(closure))})
760771
),
761772
// A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional
762773
// ownership specifier (`weak`/`unowned`) becomes a modifier; the
@@ -1278,23 +1289,34 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
12781289
modifiers: _* @mods
12791290
signature: (functionSignature
12801291
parameterClause: (functionParameterClause parameters: _* @params))
1281-
body: (codeBlock statements: _* @body_stmts)?)
1292+
body: (codeBlock) @body)
12821293
=>
12831294
(constructor_declaration
12841295
modifier: {mods}
12851296
name_node: (identifier #{initK})
12861297
parameter: {params}
1287-
body: (block stmt: {body_stmts}))
1298+
body: {body})
1299+
),
1300+
rule!(
1301+
(initializerDecl
1302+
modifiers: _* @mods
1303+
signature: (functionSignature
1304+
parameterClause: (functionParameterClause parameters: _* @params)))
1305+
=>
1306+
(constructor_declaration
1307+
modifier: {mods}
1308+
parameter: {params}
1309+
body: (block))
12881310
),
12891311
// Deinit declaration → destructor_declaration. Body statements optional.
12901312
rule!(
12911313
(deinitializerDecl
12921314
modifiers: _* @mods
1293-
body: (codeBlock statements: _* @body_stmts))
1315+
body: (codeBlock) @body)
12941316
=>
12951317
(destructor_declaration
12961318
modifier: {mods}
1297-
body: (block stmt: {body_stmts}))
1319+
body: {body})
12981320
),
12991321
// Typealias declaration
13001322
rule!(

‎unified/extractor/tests/corpus/swift/closures/closure-with-capture-list.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ top_level source="⟨body⟩"
6868
modifier: modifier "let" source="let"
6969
pattern: identifier "f" source="f"
7070
value:
71-
function_expr source="{ [⟨capture_declaration⟩] in ⟨body⟩ }"
71+
function_expr source="⟨body⟩⟨capture_declaration⟩"
7272
capture_declaration:
7373
variable_declaration source="⟨modifier⟩ ⟨pattern⟩"
7474
modifier: modifier "weak" source="weak"
7575
pattern: identifier "self" source="self"
7676
body:
77-
block source="⟨stmt⟩"
77+
block source="{ [weak self] in ⟨stmt⟩ }"
7878
stmt:
7979
call_expr source="⟨callee⟩()"
8080
callee:

‎unified/extractor/tests/corpus/swift/closures/closure-with-explicit-parameters.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ top_level source="⟨body⟩"
7070
modifier: modifier "let" source="let"
7171
pattern: identifier "f" source="f"
7272
value:
73-
function_expr source="{ (⟨parameter⟩) -> ⟨return_type⟩ in ⟨body⟩ }"
73+
function_expr source="⟨body⟩⟨parameter⟩⟨return_type⟩"
7474
parameter:
7575
parameter source="⟨pattern⟩: ⟨type⟩"
7676
type: identifier "Int" source="Int"
7777
pattern: identifier "x" source="x"
7878
return_type: identifier "Int" source="Int"
7979
body:
80-
block source="⟨stmt⟩"
80+
block source="{ (x: Int) -> Int in ⟨stmt⟩ }"
8181
stmt:
8282
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
8383
left: identifier "x" source="x"

‎unified/extractor/tests/corpus/swift/closures/closure-with-shorthand-parameters.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ top_level source="⟨body⟩"
4747
modifier: modifier "let" source="let"
4848
pattern: identifier "f" source="f"
4949
value:
50-
function_expr source="{ ⟨body⟩ }"
50+
function_expr source="⟨body⟩"
5151
body:
52-
block source="⟨stmt⟩"
52+
block source="{ ⟨stmt⟩ }"
5353
stmt:
5454
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
5555
left: identifier "$0" source="$0"

‎unified/extractor/tests/corpus/swift/closures/multi-statement-closure.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ top_level source="⟨body⟩"
101101
modifier: modifier "let" source="let"
102102
pattern: identifier "f" source="f"
103103
value:
104-
function_expr source="{ (⟨parameter⟩) -> ⟨return_type⟩ in\n ⟨body⟩\n}"
104+
function_expr source="⟨body⟩⟨parameter⟩⟨return_type⟩"
105105
parameter:
106106
parameter source="⟨pattern⟩: ⟨type⟩"
107107
type: identifier "Int" source="Int"
108108
pattern: identifier "x" source="x"
109109
return_type: identifier "Int" source="Int"
110110
body:
111-
block source="⟨stmt⟩\n ⟨stmt⟩"
111+
block source="{ (x: Int) -> Int in\n ⟨stmt⟩\n ⟨stmt⟩\n}"
112112
stmt:
113113
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
114114
modifier: modifier "let" source="let"

‎unified/extractor/tests/corpus/swift/closures/trailing-closure.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ top_level source="⟨body⟩"
5151
argument:
5252
argument source="⟨value⟩"
5353
value:
54-
function_expr source="{ ⟨body⟩ }"
54+
function_expr source="⟨body⟩"
5555
body:
56-
block source="⟨stmt⟩"
56+
block source="{ ⟨stmt⟩ }"
5757
stmt:
5858
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
5959
left: identifier "$0" source="$0"

‎unified/extractor/tests/corpus/swift/control-flow/defer-statement.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ top_level source="⟨body⟩"
7979
body:
8080
block source="⟨stmt⟩"
8181
stmt:
82-
function_declaration source="func ⟨name_node⟩() {\n ⟨body⟩\n}"
82+
function_declaration source="func ⟨name_node⟩() ⟨body⟩"
8383
name_node: identifier "withCleanup" source="withCleanup"
8484
body:
85-
block source="⟨stmt⟩\n ⟨stmt⟩"
85+
block source="{\n ⟨stmt⟩\n ⟨stmt⟩\n}"
8686
stmt:
8787
unsupported_node "defer { print(\"cleanup\") }" source="defer { print(\"cleanup\") }"
8888
call_expr source="⟨callee⟩(⟨argument⟩)"

‎unified/extractor/tests/corpus/swift/control-flow/discard-statement.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ top_level source="⟨body⟩"
7474
base_type source="⟨type⟩"
7575
type: unsupported_node "~Copyable" source="~Copyable"
7676
member:
77-
function_declaration source="⟨modifier⟩ func ⟨name_node⟩() {\n ⟨body⟩\n }"
77+
function_declaration source="⟨modifier⟩ func ⟨name_node⟩() ⟨body⟩"
7878
modifier: modifier "consuming" source="consuming"
7979
name_node: identifier "close" source="close"
8080
body:
81-
block source="⟨stmt⟩"
81+
block source="{\n ⟨stmt⟩\n }"
8282
stmt: unsupported_node "discard self" source="discard self"

‎unified/extractor/tests/corpus/swift/control-flow/fallthrough.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ top_level source="⟨body⟩"
8686
body:
8787
block source="⟨stmt⟩"
8888
stmt:
89-
function_declaration source="func ⟨name_node⟩(⟨parameter⟩) {\n ⟨body⟩\n}"
89+
function_declaration source="func ⟨name_node⟩(⟨parameter⟩) ⟨body⟩"
9090
name_node: identifier "classify" source="classify"
9191
parameter:
9292
parameter source="⟨external_name_node⟩ ⟨pattern⟩: ⟨type⟩"
9393
external_name_node: identifier "_" source="_"
9494
type: identifier "Int" source="Int"
9595
pattern: identifier "x" source="x"
9696
body:
97-
block source="⟨stmt⟩"
97+
block source="{\n ⟨stmt⟩\n}"
9898
stmt:
9999
switch_expr source="switch ⟨value⟩ {\n ⟨case⟩\n ⟨case⟩\n }"
100100
value: identifier "x" source="x"

‎unified/extractor/tests/corpus/swift/expressions/array-type-constructor.output‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ top_level source="⟨body⟩"
158158
value: int_literal "1" source="1"
159159
argument source="⟨value⟩"
160160
value:
161-
function_expr source="{ ⟨parameter⟩ ⟨parameter⟩ in\n\t⟨body⟩\n}"
161+
function_expr source="⟨body⟩⟨parameter⟩⟨parameter⟩"
162162
parameter:
163163
parameter source="⟨pattern⟩,"
164164
pattern: identifier "_" source="_"
165165
parameter source="⟨pattern⟩"
166166
pattern: identifier "count" source="count"
167167
body:
168-
block source="⟨stmt⟩"
168+
block source="{ _, count in\n\t⟨stmt⟩\n}"
169169
stmt:
170170
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
171171
left: identifier "count" source="count"

0 commit comments

Comments
 (0)