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

Rust: add some toString implementations #18035

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions rust/ql/.generated.list

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

2 changes: 0 additions & 2 deletions rust/ql/.gitattributes

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

4 changes: 2 additions & 2 deletions rust/ql/lib/codeql/rust/elements/internal/BreakExprImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ module Impl {
override string toString() {
exists(string label, string expr |
(
result = " " + this.getLifetime().toString()
label = " " + this.getLifetime().toString()
or
not this.hasLifetime() and result = ""
not this.hasLifetime() and label = ""
) and
(if this.hasExpr() then expr = " ..." else expr = "") and
result = "break" + label + expr
Expand Down
22 changes: 16 additions & 6 deletions rust/ql/lib/codeql/rust/elements/internal/CommentImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,30 @@ module Impl {
* ```
*/
class Comment extends Generated::Comment {
override string getText() { result = this.getCommentMarker() + " ..." }
override string toString() {
result = this.getCommentMarker() + "..." + this.getCommentEndMarker()
}

/**
* Gets the text of this comment, excluding the comment marker.
* Gets the text of this comment, excluding the comment markers.
*/
string getCommentText() {
exists(string s | s = this.getText() | result = s.regexpCapture("///?\\s*(.*)", 1))
exists(string s | s = this.getText() |
result =
[
s.regexpCapture("///?\\s*(.*)", 1),
s.regexpCapture("(?s)/\\*\\*?\\s*(.*?)\\s*\\*/", 1)
]
)
}

/**
* Gets the marker of this comment, that is `//` or `///`.
* Gets the marker of this comment, that is `"//"`, `"///"`, `"/*"` or `"/**"`.
*/
string getCommentMarker() {
exists(string s | s = this.getText() | result = s.regexpCapture("(///?).*", 1))
string getCommentMarker() { result = this.getText().regexpCapture("(?s)(///?|/\\*\\*?).*", 1) }

private string getCommentEndMarker() {
if this.getCommentMarker() = ["//", "///"] then result = "" else result = "*/"
}
}
}
6 changes: 4 additions & 2 deletions rust/ql/lib/codeql/rust/elements/internal/ForExprImpl.qll
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// generated by codegen, remove this comment if you wish to edit this file
/**
* This module provides a hand-modifiable wrapper around the generated class `ForExpr`.
*
Expand All @@ -12,11 +11,14 @@ private import codeql.rust.elements.internal.generated.ForExpr
* be referenced directly.
*/
module Impl {
// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* A ForExpr. For example:
* ```rust
* todo!()
* ```
*/
class ForExpr extends Generated::ForExpr { }
class ForExpr extends Generated::ForExpr {
override string toString() { result = "for " + this.getPat().toString() + " in ... { ... }" }
}
}
6 changes: 5 additions & 1 deletion rust/ql/lib/codeql/rust/elements/internal/LifetimeImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module Impl {
* ```
*/
class Lifetime extends Generated::Lifetime {
override string toString() { result = "'" + this.getText() }
override string toString() {
result = "'" + this.getText()
or
not this.hasText() and result = "'_"
}
}
}
9 changes: 8 additions & 1 deletion rust/ql/lib/codeql/rust/elements/internal/ParamImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ module Impl {
*/
class Param extends Generated::Param {
override string toString() {
result = this.getPat().toString() + ": " + this.getTy().toString()
exists(string ty |
(
ty = ": " + this.getTy().toString()
or
not this.hasTy() and ty = ""
) and
result = this.getPat().toString() + ty
)
}
}
}
6 changes: 4 additions & 2 deletions rust/ql/lib/codeql/rust/elements/internal/UnionImpl.qll
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// generated by codegen, remove this comment if you wish to edit this file
/**
* This module provides a hand-modifiable wrapper around the generated class `Union`.
*
Expand All @@ -12,11 +11,14 @@ private import codeql.rust.elements.internal.generated.Union
* be referenced directly.
*/
module Impl {
// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* A Union. For example:
* ```rust
* todo!()
* ```
*/
class Union extends Generated::Union { }
class Union extends Generated::Union {
override string toString() { result = "union " + this.getName().toString() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursion.

}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
| gen_break_expr.rs:7:13:7:17 | (no string representation) | getNumberOfAttrs: | 0 | hasExpr: | no | hasLifetime: | no |
| gen_break_expr.rs:12:13:12:27 | (no string representation) | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
| gen_break_expr.rs:17:13:17:27 | (no string representation) | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
| gen_break_expr.rs:7:13:7:17 | break | getNumberOfAttrs: | 0 | hasExpr: | no | hasLifetime: | no |
| gen_break_expr.rs:12:13:12:27 | break ''label ... | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
| gen_break_expr.rs:17:13:17:27 | break ''label ... | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| gen_break_expr.rs:12:13:12:27 | (no string representation) | gen_break_expr.rs:12:26:12:27 | 42 |
| gen_break_expr.rs:17:13:17:27 | (no string representation) | gen_break_expr.rs:17:26:17:27 | 42 |
| gen_break_expr.rs:12:13:12:27 | break ''label ... | gen_break_expr.rs:12:26:12:27 | 42 |
| gen_break_expr.rs:17:13:17:27 | break ''label ... | gen_break_expr.rs:17:26:17:27 | 42 |
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| gen_break_expr.rs:12:13:12:27 | (no string representation) | gen_break_expr.rs:12:19:12:24 | ''label |
| gen_break_expr.rs:17:13:17:27 | (no string representation) | gen_break_expr.rs:17:19:17:24 | ''label |
| gen_break_expr.rs:12:13:12:27 | break ''label ... | gen_break_expr.rs:12:19:12:24 | ''label |
| gen_break_expr.rs:17:13:17:27 | break ''label ... | gen_break_expr.rs:17:19:17:24 | ''label |
Loading