Skip to content

Commit be9464b

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 fe6f904 commit be9464b

32 files changed

Lines changed: 103 additions & 72 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
@@ -304,14 +315,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
304315
bindings: (patternBinding
305316
pattern: (identifierPattern identifier: @@name)
306317
typeAnnotation: (typeAnnotation type: @ty)
307-
accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body)))
318+
accessorBlock: (accessorBlock accessors: (codeBlockItem)+ @body) @@accessor_block))
308319
=>
309320
(accessor_declaration
310321
modifier: (modifier #{spec})
311322
name_node: (identifier #{name})
312323
type: {ty}
313-
accessor_kind: (accessor_kind "get")
314-
body: (block stmt: {body}))
324+
accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", accessor_block)}
325+
body: {block_with_anchor(&mut ctx, body, Some(accessor_block))})
315326
),
316327
// A property with an explicit accessor block. swift-syntax makes both
317328
// shapes plain `accessorDecl`s, so they are told apart by the presence
@@ -567,15 +578,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
567578
signature: (functionSignature
568579
parameterClause: (functionParameterClause parameters: _* @params)
569580
returnClause: (returnClause type: @ret)?)
570-
body: (codeBlock statements: _* @body))
581+
body: (codeBlock) @body)
571582
=>
572583
(function_declaration
573584
modifier: {mods}
574585
name_node: (identifier #{name})
575586
type_parameter: {type_params}
576587
parameter: {params}
577588
return_type: {ret}
578-
body: (block stmt: {body}))
589+
body: {body})
579590
),
580591
rule!(
581592
(functionDecl
@@ -750,14 +761,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
750761
capture: (closureCaptureClause items: _* @captures)?
751762
parameterClause: _* @params
752763
returnClause: (returnClause type: @ret)?)?
753-
statements: _* @body)
764+
statements: _* @body) @@closure
754765
=>
755766
(function_expr
756767
modifier: {attrs}
757768
capture_declaration: {captures}
758769
parameter: {params}
759770
return_type: {ret}
760-
body: (block stmt: {body}))
771+
body: {block_with_anchor(&mut ctx, body, Some(closure))})
761772
),
762773
// A closure capture (`[weak self]`, `[x]`, `[y = expr]`). The optional
763774
// ownership specifier (`weak`/`unowned`) becomes a modifier; the
@@ -1277,22 +1288,33 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
12771288
modifiers: _* @mods
12781289
signature: (functionSignature
12791290
parameterClause: (functionParameterClause parameters: _* @params))
1280-
body: (codeBlock statements: _* @body_stmts)?)
1291+
body: (codeBlock) @body)
12811292
=>
12821293
(constructor_declaration
12831294
modifier: {mods}
12841295
parameter: {params}
1285-
body: (block stmt: {body_stmts}))
1296+
body: {body})
1297+
),
1298+
rule!(
1299+
(initializerDecl
1300+
modifiers: _* @mods
1301+
signature: (functionSignature
1302+
parameterClause: (functionParameterClause parameters: _* @params)))
1303+
=>
1304+
(constructor_declaration
1305+
modifier: {mods}
1306+
parameter: {params}
1307+
body: (block))
12861308
),
12871309
// Deinit declaration → destructor_declaration. Body statements optional.
12881310
rule!(
12891311
(deinitializerDecl
12901312
modifiers: _* @mods
1291-
body: (codeBlock statements: _* @body_stmts))
1313+
body: (codeBlock) @body)
12921314
=>
12931315
(destructor_declaration
12941316
modifier: {mods}
1295-
body: (block stmt: {body_stmts}))
1317+
body: {body})
12961318
),
12971319
// Typealias declaration
12981320
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)