Skip to content

Commit d567ab3

Browse files
authored
Merge pull request github#11231 from github/redsun82/swift-fix-synthesized-wrapper-decls
Swift: fix synthesized wrapper decls
2 parents 6dfa57a + 3bb5505 commit d567ab3

File tree

41 files changed

+1702
-50
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1702
-50
lines changed

swift/codegen/generators/qlgen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class NoClasses(Error):
6363
"repr": "representation",
6464
"param": "parameter",
6565
"int": "integer",
66+
"var": "variable",
6667
}
6768

6869
abbreviations.update({f"{k}s": f"{v}s" for k, v in abbreviations.items()})

swift/extractor/translators/DeclTranslator.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <swift/AST/GenericParamList.h>
44
#include <swift/AST/ParameterList.h>
55
#include "swift/extractor/infra/SwiftDiagnosticKind.h"
6+
#include "swift/AST/PropertyWrappers.h"
67

78
namespace codeql {
89
namespace {
@@ -85,6 +86,11 @@ std::optional<codeql::ParamDecl> DeclTranslator::translateParamDecl(const swift:
8586
}
8687
fillVarDecl(decl, *entry);
8788
entry->is_inout = decl.isInOut();
89+
if (auto wrapped = decl.getPropertyWrapperWrappedValueVar()) {
90+
entry->property_wrapper_local_wrapped_var = dispatcher.fetchLabel(wrapped);
91+
entry->property_wrapper_local_wrapped_var_binding =
92+
dispatcher.fetchLabel(wrapped->getParentPatternBinding());
93+
}
8894
return entry;
8995
}
9096

@@ -355,6 +361,16 @@ void DeclTranslator::fillVarDecl(const swift::VarDecl& decl, codeql::VarDecl& en
355361
dispatcher.fetchOptionalLabel(decl.getPropertyWrapperBackingPropertyType());
356362
}
357363
fillAbstractStorageDecl(decl, entry);
364+
if (auto backing = decl.getPropertyWrapperBackingProperty()) {
365+
entry.property_wrapper_backing_var = dispatcher.fetchLabel(backing);
366+
entry.property_wrapper_backing_var_binding =
367+
dispatcher.fetchLabel(backing->getParentPatternBinding());
368+
}
369+
if (auto projection = decl.getPropertyWrapperProjectionVar()) {
370+
entry.property_wrapper_projection_var = dispatcher.fetchLabel(projection);
371+
entry.property_wrapper_projection_var_binding =
372+
dispatcher.fetchLabel(projection->getParentPatternBinding());
373+
}
358374
}
359375

360376
void DeclTranslator::fillNominalTypeDecl(const swift::NominalTypeDecl& decl,

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,15 +680,39 @@ private module Impl {
680680
}
681681

682682
private Element getImmediateChildOfVarDecl(VarDecl e, int index, string partialPredicateCall) {
683-
exists(int b, int bAbstractStorageDecl, int n |
683+
exists(
684+
int b, int bAbstractStorageDecl, int n, int nPropertyWrapperBackingVarBinding,
685+
int nPropertyWrapperBackingVar, int nPropertyWrapperProjectionVarBinding,
686+
int nPropertyWrapperProjectionVar
687+
|
684688
b = 0 and
685689
bAbstractStorageDecl =
686690
b + 1 + max(int i | i = -1 or exists(getImmediateChildOfAbstractStorageDecl(e, i, _)) | i) and
687691
n = bAbstractStorageDecl and
692+
nPropertyWrapperBackingVarBinding = n + 1 and
693+
nPropertyWrapperBackingVar = nPropertyWrapperBackingVarBinding + 1 and
694+
nPropertyWrapperProjectionVarBinding = nPropertyWrapperBackingVar + 1 and
695+
nPropertyWrapperProjectionVar = nPropertyWrapperProjectionVarBinding + 1 and
688696
(
689697
none()
690698
or
691699
result = getImmediateChildOfAbstractStorageDecl(e, index - b, partialPredicateCall)
700+
or
701+
index = n and
702+
result = e.getImmediatePropertyWrapperBackingVarBinding() and
703+
partialPredicateCall = "PropertyWrapperBackingVarBinding()"
704+
or
705+
index = nPropertyWrapperBackingVarBinding and
706+
result = e.getImmediatePropertyWrapperBackingVar() and
707+
partialPredicateCall = "PropertyWrapperBackingVar()"
708+
or
709+
index = nPropertyWrapperBackingVar and
710+
result = e.getImmediatePropertyWrapperProjectionVarBinding() and
711+
partialPredicateCall = "PropertyWrapperProjectionVarBinding()"
712+
or
713+
index = nPropertyWrapperProjectionVarBinding and
714+
result = e.getImmediatePropertyWrapperProjectionVar() and
715+
partialPredicateCall = "PropertyWrapperProjectionVar()"
692716
)
693717
)
694718
}
@@ -809,14 +833,27 @@ private module Impl {
809833
}
810834

811835
private Element getImmediateChildOfParamDecl(ParamDecl e, int index, string partialPredicateCall) {
812-
exists(int b, int bVarDecl, int n |
836+
exists(
837+
int b, int bVarDecl, int n, int nPropertyWrapperLocalWrappedVarBinding,
838+
int nPropertyWrapperLocalWrappedVar
839+
|
813840
b = 0 and
814841
bVarDecl = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfVarDecl(e, i, _)) | i) and
815842
n = bVarDecl and
843+
nPropertyWrapperLocalWrappedVarBinding = n + 1 and
844+
nPropertyWrapperLocalWrappedVar = nPropertyWrapperLocalWrappedVarBinding + 1 and
816845
(
817846
none()
818847
or
819848
result = getImmediateChildOfVarDecl(e, index - b, partialPredicateCall)
849+
or
850+
index = n and
851+
result = e.getImmediatePropertyWrapperLocalWrappedVarBinding() and
852+
partialPredicateCall = "PropertyWrapperLocalWrappedVarBinding()"
853+
or
854+
index = nPropertyWrapperLocalWrappedVarBinding and
855+
result = e.getImmediatePropertyWrapperLocalWrappedVar() and
856+
partialPredicateCall = "PropertyWrapperLocalWrappedVar()"
820857
)
821858
)
822859
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ module Raw {
235235
Pattern getParentPattern() { var_decl_parent_patterns(this, result) }
236236

237237
Expr getParentInitializer() { var_decl_parent_initializers(this, result) }
238+
239+
PatternBindingDecl getPropertyWrapperBackingVarBinding() {
240+
var_decl_property_wrapper_backing_var_bindings(this, result)
241+
}
242+
243+
VarDecl getPropertyWrapperBackingVar() { var_decl_property_wrapper_backing_vars(this, result) }
244+
245+
PatternBindingDecl getPropertyWrapperProjectionVarBinding() {
246+
var_decl_property_wrapper_projection_var_bindings(this, result)
247+
}
248+
249+
VarDecl getPropertyWrapperProjectionVar() {
250+
var_decl_property_wrapper_projection_vars(this, result)
251+
}
238252
}
239253

240254
class AccessorDecl extends @accessor_decl, FuncDecl {
@@ -285,6 +299,14 @@ module Raw {
285299
override string toString() { result = "ParamDecl" }
286300

287301
predicate isInout() { param_decl_is_inout(this) }
302+
303+
PatternBindingDecl getPropertyWrapperLocalWrappedVarBinding() {
304+
param_decl_property_wrapper_local_wrapped_var_bindings(this, result)
305+
}
306+
307+
VarDecl getPropertyWrapperLocalWrappedVar() {
308+
param_decl_property_wrapper_local_wrapped_vars(this, result)
309+
}
288310
}
289311

290312
class TypeAliasDecl extends @type_alias_decl, GenericTypeDecl {

swift/ql/lib/codeql/swift/generated/decl/ParamDecl.qll

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// generated by codegen/codegen.py
22
private import codeql.swift.generated.Synth
33
private import codeql.swift.generated.Raw
4+
import codeql.swift.elements.decl.PatternBindingDecl
45
import codeql.swift.elements.decl.VarDecl
56

67
module Generated {
@@ -11,5 +12,65 @@ module Generated {
1112
* Holds if this is an `inout` parameter.
1213
*/
1314
predicate isInout() { Synth::convertParamDeclToRaw(this).(Raw::ParamDecl).isInout() }
15+
16+
/**
17+
* Gets the property wrapper local wrapped variable binding of this parameter declaration, if it exists.
18+
*
19+
* This includes nodes from the "hidden" AST. It can be overridden in subclasses to change the
20+
* behavior of both the `Immediate` and non-`Immediate` versions.
21+
*/
22+
PatternBindingDecl getImmediatePropertyWrapperLocalWrappedVarBinding() {
23+
result =
24+
Synth::convertPatternBindingDeclFromRaw(Synth::convertParamDeclToRaw(this)
25+
.(Raw::ParamDecl)
26+
.getPropertyWrapperLocalWrappedVarBinding())
27+
}
28+
29+
/**
30+
* Gets the property wrapper local wrapped variable binding of this parameter declaration, if it exists.
31+
*
32+
* This is the synthesized binding introducing the property wrapper local wrapped projection
33+
* variable for this variable, if any.
34+
*/
35+
final PatternBindingDecl getPropertyWrapperLocalWrappedVarBinding() {
36+
result = getImmediatePropertyWrapperLocalWrappedVarBinding().resolve()
37+
}
38+
39+
/**
40+
* Holds if `getPropertyWrapperLocalWrappedVarBinding()` exists.
41+
*/
42+
final predicate hasPropertyWrapperLocalWrappedVarBinding() {
43+
exists(getPropertyWrapperLocalWrappedVarBinding())
44+
}
45+
46+
/**
47+
* Gets the property wrapper local wrapped variable of this parameter declaration, if it exists.
48+
*
49+
* This includes nodes from the "hidden" AST. It can be overridden in subclasses to change the
50+
* behavior of both the `Immediate` and non-`Immediate` versions.
51+
*/
52+
VarDecl getImmediatePropertyWrapperLocalWrappedVar() {
53+
result =
54+
Synth::convertVarDeclFromRaw(Synth::convertParamDeclToRaw(this)
55+
.(Raw::ParamDecl)
56+
.getPropertyWrapperLocalWrappedVar())
57+
}
58+
59+
/**
60+
* Gets the property wrapper local wrapped variable of this parameter declaration, if it exists.
61+
*
62+
* This is the synthesized local wrapped value, shadowing this parameter declaration in case it
63+
* has a property wrapper.
64+
*/
65+
final VarDecl getPropertyWrapperLocalWrappedVar() {
66+
result = getImmediatePropertyWrapperLocalWrappedVar().resolve()
67+
}
68+
69+
/**
70+
* Holds if `getPropertyWrapperLocalWrappedVar()` exists.
71+
*/
72+
final predicate hasPropertyWrapperLocalWrappedVar() {
73+
exists(getPropertyWrapperLocalWrappedVar())
74+
}
1475
}
1576
}

0 commit comments

Comments
 (0)