Skip to content

Commit a2851f7

Browse files
authored
Merge pull request #18968 from hvitved/rust/cache-to-string
Rust/Swift: Cache `Element.toString`
2 parents 0e3907b + dcd01be commit a2851f7

File tree

203 files changed

+280
-229
lines changed

Some content is hidden

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

203 files changed

+280
-229
lines changed

misc/codegen/templates/ql_class.mustache

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ module Generated {
2929
/**
3030
* Gets the string representation of this element.
3131
*/
32-
string toString() { none() } // overridden by subclasses
32+
cached
33+
final string toString() { result = this.toStringImpl() }
34+
35+
/**
36+
* INTERNAL: Do not use.
37+
*
38+
* Gets the string representation of this element.
39+
*/
40+
abstract string toStringImpl();
3341

3442
/**
3543
* Gets the name of a primary CodeQL class to which this element belongs.

rust/ql/.generated.list

+1-1
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/ArrayListExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ module Impl {
2222
* ```
2323
*/
2424
class ArrayListExpr extends Generated::ArrayListExpr {
25-
override string toString() { result = "[...]" }
25+
override string toStringImpl() { result = "[...]" }
2626
}
2727
}

rust/ql/lib/codeql/rust/elements/internal/ArrayRepeatExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Impl {
2121
* ```
2222
*/
2323
class ArrayRepeatExpr extends Generated::ArrayRepeatExpr {
24-
override string toString() {
24+
override string toStringImpl() {
2525
result =
2626
"[" + this.getRepeatOperand().toAbbreviatedString() + "; " +
2727
this.getRepeatLength().toAbbreviatedString() + "]"

rust/ql/lib/codeql/rust/elements/internal/AwaitExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ module Impl {
2222
* ```
2323
*/
2424
class AwaitExpr extends Generated::AwaitExpr {
25-
override string toString() { result = "await " + this.getExpr().toAbbreviatedString() }
25+
override string toStringImpl() { result = "await " + this.getExpr().toAbbreviatedString() }
2626
}
2727
}

rust/ql/lib/codeql/rust/elements/internal/BecomeExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ module Impl {
2525
* ```
2626
*/
2727
class BecomeExpr extends Generated::BecomeExpr {
28-
override string toString() { result = "become " + this.getExpr().toAbbreviatedString() }
28+
override string toStringImpl() { result = "become " + this.getExpr().toAbbreviatedString() }
2929
}
3030
}

rust/ql/lib/codeql/rust/elements/internal/BinaryExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ module Impl {
2323
* ```
2424
*/
2525
class BinaryExpr extends Generated::BinaryExpr {
26-
override string toString() { result = "... " + this.getOperatorName() + " ..." }
26+
override string toStringImpl() { result = "... " + this.getOperatorName() + " ..." }
2727
}
2828
}

rust/ql/lib/codeql/rust/elements/internal/BoxPatImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ module Impl {
2222
* ```
2323
*/
2424
class BoxPat extends Generated::BoxPat {
25-
override string toString() { result = "box " + this.getPat().toAbbreviatedString() }
25+
override string toStringImpl() { result = "box " + this.getPat().toAbbreviatedString() }
2626
}
2727
}

rust/ql/lib/codeql/rust/elements/internal/BreakExprImpl.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ module Impl {
104104
)
105105
}
106106

107-
override string toString() {
107+
override string toStringImpl() {
108108
result = strictconcat(int i | | this.toStringPart(i), " " order by i)
109109
}
110110

111111
private string toStringPart(int index) {
112112
index = 0 and result = "break"
113113
or
114-
index = 1 and result = this.getLifetime().toString()
114+
index = 1 and result = this.getLifetime().toStringImpl()
115115
or
116116
index = 2 and result = this.getExpr().toAbbreviatedString()
117117
}

rust/ql/lib/codeql/rust/elements/internal/CallExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module Impl {
3434
* ```
3535
*/
3636
class CallExpr extends Generated::CallExpr {
37-
override string toString() { result = this.getFunction().toAbbreviatedString() + "(...)" }
37+
override string toStringImpl() { result = this.getFunction().toAbbreviatedString() + "(...)" }
3838

3939
override Callable getStaticTarget() { result = getResolvedFunction(this) }
4040

rust/ql/lib/codeql/rust/elements/internal/CastExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Impl {
1919
* ```
2020
*/
2121
class CastExpr extends Generated::CastExpr {
22-
override string toString() {
22+
override string toStringImpl() {
2323
result =
2424
this.getExpr().toAbbreviatedString() + " as " + this.getTypeRepr().toAbbreviatedString()
2525
}

rust/ql/lib/codeql/rust/elements/internal/ClosureExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ module Impl {
2525
* ```
2626
*/
2727
class ClosureExpr extends Generated::ClosureExpr {
28-
override string toString() { result = "|...| " + this.getBody().toAbbreviatedString() }
28+
override string toStringImpl() { result = "|...| " + this.getBody().toAbbreviatedString() }
2929
}
3030
}

rust/ql/lib/codeql/rust/elements/internal/CommentImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Impl {
2020
* ```
2121
*/
2222
class Comment extends Generated::Comment {
23-
override string toString() {
23+
override string toStringImpl() {
2424
result = this.getCommentMarker() + "..." + this.getCommentEndMarker()
2525
}
2626

rust/ql/lib/codeql/rust/elements/internal/ContinueExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module Impl {
4949
* ```
5050
*/
5151
class ContinueExpr extends Generated::ContinueExpr {
52-
override string toString() {
52+
override string toStringImpl() {
5353
result = strictconcat(int i | | this.toStringPart(i), " " order by i)
5454
}
5555

rust/ql/lib/codeql/rust/elements/internal/CrateImpl.qll

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module Impl {
1414
private import rust
1515

1616
class Crate extends Generated::Crate {
17-
override string toString() { result = strictconcat(int i | | this.toStringPart(i) order by i) }
17+
override string toStringImpl() {
18+
result = strictconcat(int i | | this.toStringPart(i) order by i)
19+
}
1820

1921
private string toStringPart(int i) {
2022
i = 0 and result = "Crate("

rust/ql/lib/codeql/rust/elements/internal/ElementImpl.qll

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ private import codeql.rust.elements.internal.generated.Element
1212
*/
1313
module Impl {
1414
class Element extends Generated::Element {
15-
override string toString() { result = this.getAPrimaryQlClass() }
15+
override string toStringImpl() { result = this.getAPrimaryQlClass() }
1616

1717
/**
18+
* INTERNAL: Do not use.
19+
*
1820
* Returns a string suitable to be inserted into the name of the parent. Typically `"..."`,
1921
* but may be overridden by subclasses.
20-
*
21-
* INTERNAL: Do not use.
2222
*/
2323
string toAbbreviatedString() { result = "..." }
2424

rust/ql/lib/codeql/rust/elements/internal/EnumImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ module Impl {
1919
* ```
2020
*/
2121
class Enum extends Generated::Enum {
22-
override string toString() { result = "enum " + this.getName().getText() }
22+
override string toStringImpl() { result = "enum " + this.getName().getText() }
2323
}
2424
}

rust/ql/lib/codeql/rust/elements/internal/ExtractorStepImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private import codeql.rust.elements.internal.generated.ExtractorStep
1212
*/
1313
module Impl {
1414
class ExtractorStep extends Generated::ExtractorStep {
15-
override string toString() {
15+
override string toStringImpl() {
1616
result = this.getAction() + "(" + this.getFile().getAbsolutePath() + ")"
1717
or
1818
not this.hasFile() and result = this.getAction()

rust/ql/lib/codeql/rust/elements/internal/FieldExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Impl {
2828
/** Gets the tuple field that this access references, if any. */
2929
TupleField getTupleField() { result = TypeInference::resolveTupleFieldExpr(this) }
3030

31-
override string toString() {
31+
override string toStringImpl() {
3232
exists(string abbr, string name |
3333
abbr = this.getExpr().toAbbreviatedString() and
3434
name = this.getNameRef().getText() and

rust/ql/lib/codeql/rust/elements/internal/FormatArgumentImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module Impl {
3636

3737
FormatArgument() { this = Synth::TFormatArgument(parent, index, kind, name, _, offset) }
3838

39-
override string toString() { result = name }
39+
override string toStringImpl() { result = name }
4040

4141
override Format getParent() { result = Synth::TFormat(parent, index, _, _) }
4242

rust/ql/lib/codeql/rust/elements/internal/FormatImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module Impl {
3636

3737
Format() { this = Synth::TFormat(parent, index, text, offset) }
3838

39-
override string toString() { result = text }
39+
override string toStringImpl() { result = text }
4040

4141
override FormatArgsExpr getParent() { result = Synth::convertFormatArgsExprFromRaw(parent) }
4242

rust/ql/lib/codeql/rust/elements/internal/FormatTemplateVariableAccessImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module Impl {
2929

3030
override Location getLocation() { result = argument.getLocation() }
3131

32-
override string toString() { result = this.getName() }
32+
override string toStringImpl() { result = this.getName() }
3333

3434
/** Gets the name of the variable */
3535
string getName() { result = argument.getName() }

rust/ql/lib/codeql/rust/elements/internal/FunctionImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ module Impl {
2525
* ```
2626
*/
2727
class Function extends Generated::Function {
28-
override string toString() { result = "fn " + this.getName().getText() }
28+
override string toStringImpl() { result = "fn " + this.getName().getText() }
2929
}
3030
}

rust/ql/lib/codeql/rust/elements/internal/GenericArgListImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Impl {
2121
* ```
2222
*/
2323
class GenericArgList extends Generated::GenericArgList {
24-
override string toString() { result = this.toAbbreviatedString() }
24+
override string toStringImpl() { result = this.toAbbreviatedString() }
2525

2626
override string toAbbreviatedString() { result = "<...>" }
2727

rust/ql/lib/codeql/rust/elements/internal/GenericParamListImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Impl {
2121
* ```
2222
*/
2323
class GenericParamList extends Generated::GenericParamList {
24-
override string toString() { result = this.toAbbreviatedString() }
24+
override string toStringImpl() { result = this.toAbbreviatedString() }
2525

2626
override string toAbbreviatedString() { result = "<...>" }
2727

rust/ql/lib/codeql/rust/elements/internal/IdentPatImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Impl {
2828
* ```
2929
*/
3030
class IdentPat extends Generated::IdentPat {
31-
override string toString() {
31+
override string toStringImpl() {
3232
result = strictconcat(int i | | this.toStringPart(i), " " order by i)
3333
}
3434

rust/ql/lib/codeql/rust/elements/internal/IfExprImpl.qll

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ module Impl {
2828
* ```
2929
*/
3030
class IfExpr extends Generated::IfExpr {
31-
override string toString() { result = concat(int i | | this.toStringPart(i), " " order by i) }
31+
override string toStringImpl() {
32+
result = concat(int i | | this.toStringPart(i), " " order by i)
33+
}
3234

3335
private string toStringPart(int index) {
3436
index = 0 and result = "if"

rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Impl {
1919
* ```
2020
*/
2121
class Impl extends Generated::Impl {
22-
override string toString() {
22+
override string toStringImpl() {
2323
exists(string trait |
2424
(
2525
trait = this.getTrait().toAbbreviatedString() + " for "

rust/ql/lib/codeql/rust/elements/internal/IndexExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Impl {
2020
* ```
2121
*/
2222
class IndexExpr extends Generated::IndexExpr {
23-
override string toString() {
23+
override string toStringImpl() {
2424
result =
2525
this.getBase().toAbbreviatedString() + "[" + this.getIndex().toAbbreviatedString() + "]"
2626
}

rust/ql/lib/codeql/rust/elements/internal/InferTypeReprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Impl {
1919
* ```
2020
*/
2121
class InferTypeRepr extends Generated::InferTypeRepr {
22-
override string toString() { result = this.toAbbreviatedString() }
22+
override string toStringImpl() { result = this.toAbbreviatedString() }
2323

2424
override string toAbbreviatedString() { result = "_" }
2525
}

rust/ql/lib/codeql/rust/elements/internal/LabelImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Impl {
2222
* ```
2323
*/
2424
class Label extends Generated::Label {
25-
override string toString() { result = this.getText() }
25+
override string toStringImpl() { result = this.getText() }
2626

2727
override string toAbbreviatedString() { result = this.getText() }
2828

rust/ql/lib/codeql/rust/elements/internal/LabelableExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Impl {
1616
* The base class for expressions that can be labeled (`LoopExpr`, `ForExpr`, `WhileExpr` or `BlockExpr`).
1717
*/
1818
class LabelableExpr extends Generated::LabelableExpr {
19-
final override string toString() {
19+
final override string toStringImpl() {
2020
result = strictconcat(int i | | this.toStringPart(i), " " order by i)
2121
}
2222

rust/ql/lib/codeql/rust/elements/internal/LetElseImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Impl {
1919
* ```
2020
*/
2121
class LetElse extends Generated::LetElse {
22-
override string toString() { result = this.toAbbreviatedString() }
22+
override string toStringImpl() { result = this.toAbbreviatedString() }
2323

2424
override string toAbbreviatedString() { result = "else {...}" }
2525
}

rust/ql/lib/codeql/rust/elements/internal/LetExprImpl.qll

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ module Impl {
2121
* ```
2222
*/
2323
class LetExpr extends Generated::LetExpr {
24-
override string toString() { result = concat(int i | | this.toStringPart(i), " " order by i) }
24+
override string toStringImpl() {
25+
result = concat(int i | | this.toStringPart(i), " " order by i)
26+
}
2527

2628
private string toStringPart(int index) {
2729
index = 0 and result = "let"

rust/ql/lib/codeql/rust/elements/internal/LetStmtImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Impl {
2626
* ```
2727
*/
2828
class LetStmt extends Generated::LetStmt {
29-
override string toString() {
29+
override string toStringImpl() {
3030
result = strictconcat(int i | | this.toStringPart(i), " " order by i)
3131
}
3232

rust/ql/lib/codeql/rust/elements/internal/LifetimeImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Impl {
1919
* ```
2020
*/
2121
class Lifetime extends Generated::Lifetime {
22-
override string toString() {
22+
override string toStringImpl() {
2323
result = "'" + this.getText()
2424
or
2525
not this.hasText() and result = "'_"

rust/ql/lib/codeql/rust/elements/internal/LiteralExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Impl {
2626
* ```
2727
*/
2828
class LiteralExpr extends Generated::LiteralExpr {
29-
override string toString() { result = this.getTrimmedText() }
29+
override string toStringImpl() { result = this.getTrimmedText() }
3030

3131
override string toAbbreviatedString() { result = this.getTrimmedText() }
3232

rust/ql/lib/codeql/rust/elements/internal/LiteralPatImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Impl {
2222
* ```
2323
*/
2424
class LiteralPat extends Generated::LiteralPat {
25-
override string toString() { result = this.toAbbreviatedString() }
25+
override string toStringImpl() { result = this.toAbbreviatedString() }
2626

2727
override string toAbbreviatedString() { result = this.getLiteral().getTrimmedText() }
2828
}

rust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ module Impl {
1919
* ```
2020
*/
2121
class MacroCall extends Generated::MacroCall {
22-
override string toString() { result = this.getPath().toAbbreviatedString() + "!..." }
22+
override string toStringImpl() { result = this.getPath().toAbbreviatedString() + "!..." }
2323
}
2424
}

rust/ql/lib/codeql/rust/elements/internal/MatchArmImpl.qll

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ module Impl {
2828
* ```
2929
*/
3030
class MatchArm extends Generated::MatchArm {
31-
override string toString() { result = concat(int i | | this.toStringPart(i), " " order by i) }
31+
override string toStringImpl() {
32+
result = concat(int i | | this.toStringPart(i), " " order by i)
33+
}
3234

3335
private string toStringPart(int index) {
3436
index = 0 and result = this.getPat().toAbbreviatedString()

rust/ql/lib/codeql/rust/elements/internal/MatchExprImpl.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Impl {
2828
* ```
2929
*/
3030
class MatchExpr extends Generated::MatchExpr {
31-
override string toString() {
31+
override string toStringImpl() {
3232
result = "match " + this.getScrutinee().toAbbreviatedString() + " { ... }"
3333
}
3434

0 commit comments

Comments
 (0)