Skip to content

Commit d65a704

Browse files
authored
Merge pull request #18635 from hvitved/codegen/self-type-alias
Codegen: Improve return type of self-typed properties
2 parents e4523ef + 3d3b785 commit d65a704

File tree

11 files changed

+44
-22
lines changed

11 files changed

+44
-22
lines changed

misc/codegen/lib/ql.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Property:
4545
synth: bool = False
4646
type_is_hideable: bool = False
4747
type_is_codegen_class: bool = False
48+
type_is_self: bool = False
4849
internal: bool = False
4950
cfg: bool = False
5051

@@ -83,6 +84,10 @@ def is_child(self):
8384
def is_indexed(self) -> bool:
8485
return self.is_repeated and not self.is_unordered
8586

87+
@property
88+
def type_alias(self) -> Optional[str]:
89+
return self.type + "Alias" if self.type_is_self else self.type
90+
8691

8792
@dataclass
8893
class Base:
@@ -116,11 +121,18 @@ def get_bases(bases): return [Base(str(b), str(prev)) for b, prev in zip(bases,
116121
self.bases_impl = get_bases(self.bases_impl)
117122
if self.properties:
118123
self.properties[0].first = True
124+
for prop in self.properties:
125+
if prop.type == self.name:
126+
prop.type_is_self = True
119127

120128
@property
121129
def root(self) -> bool:
122130
return not self.bases
123131

132+
@property
133+
def needs_self_alias(self) -> bool:
134+
return self.root or any(p.type_is_self for p in self.properties)
135+
124136
@property
125137
def path(self) -> pathlib.Path:
126138
return self.dir / self.name

misc/codegen/templates/ql_class.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ private import {{import_prefix}}.Raw
99
{{#imports}}
1010
import {{.}}
1111
{{/imports}}
12-
{{#root}}
12+
{{#needs_self_alias}}
1313
private class {{name}}Alias = {{name}};
14-
{{/root}}
14+
{{/needs_self_alias}}
1515
/**
1616
* INTERNAL: This module contains the fully generated definition of `{{name}}` and should not
1717
* be referenced directly.
@@ -77,7 +77,7 @@ module Generated {
7777
* INTERNAL: Do not use.
7878
{{/internal}}
7979
*/
80-
{{type}} get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}int index{{/is_indexed}}) {
80+
{{type_alias}} get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}int index{{/is_indexed}}) {
8181
{{^synth}}
8282
result = Synth::convert{{type}}FromRaw(Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}}))
8383
{{/synth}}
@@ -95,7 +95,7 @@ module Generated {
9595
* INTERNAL: Do not use.
9696
{{/internal}}
9797
*/
98-
final {{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
98+
final {{type_alias}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
9999
exists({{type}} immediate | immediate = this.get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}index{{/is_indexed}}) and
100100
{{#hideable}}if exists(this.getResolveStep()) then result = immediate else {{/hideable}}result = immediate.resolve())
101101
}
@@ -111,7 +111,7 @@ module Generated {
111111
* INTERNAL: Do not use.
112112
{{/internal}}
113113
*/
114-
{{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
114+
{{type_alias}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
115115
{{^synth}}
116116
{{^is_predicate}}result = {{/is_predicate}}{{#type_is_codegen_class}}Synth::convert{{type}}FromRaw({{/type_is_codegen_class}}Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}}){{#type_is_codegen_class}}){{/type_is_codegen_class}}
117117
{{/synth}}
@@ -140,7 +140,7 @@ module Generated {
140140
* INTERNAL: Do not use.
141141
{{/internal}}
142142
*/
143-
final {{type}} {{indefinite_getter}}() {
143+
final {{type_alias}} {{indefinite_getter}}() {
144144
result = this.{{getter}}(_)
145145
}
146146
{{^is_optional}}

rust/ql/.generated.list

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/generated/Path.qll

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/frameworks/rustcrypto/RustCrypto.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class StreamCipherInit extends Cryptography::CryptographicOperation::Range {
2727
p.getResolvedCrateOrigin().matches("%/RustCrypto%") and
2828
p.getPath().getText() = ["new", "new_from_slice", "new_from_slices", "new_with_eff_key_len"] and
2929
(
30-
rawAlgorithmName = p.getPath().getQualifier().(Path).getText() or // todo: remove infix cast when codegenerator has been fixed
30+
rawAlgorithmName = p.getPath().getQualifier().getText() or
3131
rawAlgorithmName =
3232
p.getPath()
3333
.getQualifier()

rust/ql/lib/codeql/rust/security/WeakSensitiveDataHashingExtensions.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class ModeledHashOperation extends Cryptography::CryptographicOperation::Range {
186186
sinkNode(input, "hasher-input") and
187187
call = input.(Node::FlowSummaryNode).getSinkElement().getCall() and
188188
call = this.asExpr().getExpr() and
189-
algorithmName = call.getFunction().(PathExpr).getPath().getQualifier().(Path).getText() // todo: remove infix cast when codegenerator has been fixed
189+
algorithmName = call.getFunction().(PathExpr).getPath().getQualifier().getText()
190190
)
191191
}
192192

swift/ql/.generated.list

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swift/ql/lib/codeql/swift/generated/type/Type.qll

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)