Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codegen: Improve return type of self-typed properties #18635

Merged
merged 5 commits into from
Feb 5, 2025
Merged
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
12 changes: 12 additions & 0 deletions misc/codegen/lib/ql.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Property:
synth: bool = False
type_is_hideable: bool = False
type_is_codegen_class: bool = False
type_is_self: bool = False
internal: bool = False
cfg: bool = False

Expand Down Expand Up @@ -83,6 +84,10 @@ def is_child(self):
def is_indexed(self) -> bool:
return self.is_repeated and not self.is_unordered

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


@dataclass
class Base:
Expand Down Expand Up @@ -116,11 +121,18 @@ def get_bases(bases): return [Base(str(b), str(prev)) for b, prev in zip(bases,
self.bases_impl = get_bases(self.bases_impl)
if self.properties:
self.properties[0].first = True
for prop in self.properties:
if prop.type == self.name:
prop.type_is_self = True

@property
def root(self) -> bool:
return not self.bases

@property
def needs_self_alias(self) -> bool:
return self.root or any(p.type_is_self for p in self.properties)

@property
def path(self) -> pathlib.Path:
return self.dir / self.name
Expand Down
12 changes: 6 additions & 6 deletions misc/codegen/templates/ql_class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ private import {{import_prefix}}.Raw
{{#imports}}
import {{.}}
{{/imports}}
{{#root}}
{{#needs_self_alias}}
private class {{name}}Alias = {{name}};
{{/root}}
{{/needs_self_alias}}
/**
* INTERNAL: This module contains the fully generated definition of `{{name}}` and should not
* be referenced directly.
Expand Down Expand Up @@ -77,7 +77,7 @@ module Generated {
* INTERNAL: Do not use.
{{/internal}}
*/
{{type}} get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}int index{{/is_indexed}}) {
{{type_alias}} get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}int index{{/is_indexed}}) {
{{^synth}}
result = Synth::convert{{type}}FromRaw(Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}}))
{{/synth}}
Expand All @@ -95,7 +95,7 @@ module Generated {
* INTERNAL: Do not use.
{{/internal}}
*/
final {{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
final {{type_alias}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
exists({{type}} immediate | immediate = this.get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}index{{/is_indexed}}) and
{{#hideable}}if exists(this.getResolveStep()) then result = immediate else {{/hideable}}result = immediate.resolve())
}
Expand All @@ -111,7 +111,7 @@ module Generated {
* INTERNAL: Do not use.
{{/internal}}
*/
{{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
{{type_alias}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
{{^synth}}
{{^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}}
{{/synth}}
Expand Down Expand Up @@ -140,7 +140,7 @@ module Generated {
* INTERNAL: Do not use.
{{/internal}}
*/
final {{type}} {{indefinite_getter}}() {
final {{type_alias}} {{indefinite_getter}}() {
result = this.{{getter}}(_)
}
{{^is_optional}}
Expand Down
2 changes: 1 addition & 1 deletion rust/ql/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion rust/ql/lib/codeql/rust/elements/internal/generated/Path.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class StreamCipherInit extends Cryptography::CryptographicOperation::Range {
p.getResolvedCrateOrigin().matches("%/RustCrypto%") and
p.getPath().getText() = ["new", "new_from_slice", "new_from_slices", "new_with_eff_key_len"] and
(
rawAlgorithmName = p.getPath().getQualifier().(Path).getText() or // todo: remove infix cast when codegenerator has been fixed
rawAlgorithmName = p.getPath().getQualifier().getText() or
rawAlgorithmName =
p.getPath()
.getQualifier()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ModeledHashOperation extends Cryptography::CryptographicOperation::Range {
sinkNode(input, "hasher-input") and
call = input.(Node::FlowSummaryNode).getSinkElement().getCall() and
call = this.asExpr().getExpr() and
algorithmName = call.getFunction().(PathExpr).getPath().getQualifier().(Path).getText() // todo: remove infix cast when codegenerator has been fixed
algorithmName = call.getFunction().(PathExpr).getPath().getQualifier().getText()
)
}

Expand Down
8 changes: 4 additions & 4 deletions swift/ql/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions swift/ql/lib/codeql/swift/generated/decl/Decl.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions swift/ql/lib/codeql/swift/generated/decl/ModuleDecl.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions swift/ql/lib/codeql/swift/generated/decl/VarDecl.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions swift/ql/lib/codeql/swift/generated/type/Type.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.