Skip to content

Commit a3b903f

Browse files
committed
Rust: simplify synth constructors
1 parent 8843a7c commit a3b903f

File tree

7 files changed

+28
-27
lines changed

7 files changed

+28
-27
lines changed

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/FormatArgumentConstructor.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ private import codeql.rust.elements.internal.FormatConstructor
1212
* The characteristic predicate of `FormatArgument` synthesized instances.
1313
* INTERNAL: Do not use.
1414
*/
15-
predicate constructFormatArgument(Raw::FormatArgsExpr parent, int index, int kind) {
16-
formatArgument(parent, index, kind, _, _, _)
17-
}
18-
19-
predicate formatArgument(
15+
predicate constructFormatArgument(
2016
Raw::FormatArgsExpr parent, int index, int kind, string value, boolean positional, int offset
2117
) {
2218
exists(string text, int formatOffset, int group |

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ module Impl {
3232
string name;
3333
private int offset;
3434

35-
FormatArgument() {
36-
this = Synth::TFormatArgument(parent, index, kind) and
37-
formatArgument(parent, index, kind, name, _, offset)
38-
}
35+
FormatArgument() { this = Synth::TFormatArgument(parent, index, kind, name, _, offset) }
3936

4037
override string toString() { result = name }
4138

@@ -53,7 +50,7 @@ module Impl {
5350
endcolumn = startcolumn + name.length() - 1
5451
}
5552

56-
override Format getParent() { result = Synth::TFormat(parent, index) }
53+
override Format getParent() { result = Synth::TFormat(parent, index, _, _) }
5754
}
5855

5956
/**
@@ -64,7 +61,7 @@ module Impl {
6461
* ```
6562
*/
6663
class PositionalFormatArgument extends FormatArgument {
67-
PositionalFormatArgument() { formatArgument(parent, index, kind, _, true, _) }
64+
PositionalFormatArgument() { this = Synth::TFormatArgument(_, _, _, _, true, _) }
6865

6966
/** Gets the index of this positional argument */
7067
int getIndex() { result = name.toInt() }
@@ -78,7 +75,7 @@ module Impl {
7875
* ```
7976
*/
8077
class NamedFormatArgument extends FormatArgument {
81-
NamedFormatArgument() { formatArgument(parent, index, kind, _, false, _) }
78+
NamedFormatArgument() { this = Synth::TFormatArgument(_, _, _, _, false, _) }
8279

8380
/** Gets the name of this named argument */
8481
string getName() { result = name }

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ private import codeql.rust.elements.internal.generated.Raw
99
/**
1010
* The characteristic predicate of `Format` synthesized instances.
1111
* INTERNAL: Do not use.
12+
*
13+
* Match an element of a format string, either text (`Hello`) or a format placeholder (`{}`).
1214
*/
13-
predicate constructFormat(Raw::FormatArgsExpr parent, int index) {
14-
formatElement(parent, index, _).regexpMatch(formatRegex())
15+
predicate constructFormat(Raw::FormatArgsExpr parent, int index, string text, int offset) {
16+
text = formatElement(parent, index, offset) and
17+
text.charAt(0) = "{" and
18+
text.charAt(1) != "{"
1519
}
1620

1721
/**
@@ -28,9 +32,11 @@ string formatElement(Raw::FormatArgsExpr parent, int occurrenceIndex, int occurr
2832
}
2933

3034
/**
31-
* A regular expression for matching format elements in a formatting template. The
35+
* A regular expression for matching format elements in a formatting template. The syntax of
36+
* formatting templates is defined at https://doc.rust-lang.org/stable/std/fmt/#syntax . The
3237
* regular expression is generated from the following python code:
3338
*
39+
*
3440
* ```python
3541
* identifier = "([A-Za-z_][A-Za-z0-9_]*)"
3642
* integer = "([0-9]+)"

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ module Impl {
2828
private int index;
2929
private int offset;
3030

31-
Format() {
32-
this = Synth::TFormat(parent, index) and text = formatElement(parent, index, offset)
33-
}
31+
Format() { this = Synth::TFormat(parent, index, text, offset) }
3432

3533
override string toString() { result = text }
3634

@@ -58,7 +56,7 @@ module Impl {
5856
* ```
5957
*/
6058
FormatArgument getArgumentRef() {
61-
result.getParent() = this and result = Synth::TFormatArgument(_, _, 0)
59+
result.getParent() = this and result = Synth::TFormatArgument(_, _, 0, _, _, _)
6260
}
6361

6462
/**
@@ -70,7 +68,7 @@ module Impl {
7068
* ```
7169
*/
7270
FormatArgument getWidthArgument() {
73-
result.getParent() = this and result = Synth::TFormatArgument(_, _, 1)
71+
result.getParent() = this and result = Synth::TFormatArgument(_, _, 1, _, _, _)
7472
}
7573

7674
/**
@@ -82,7 +80,7 @@ module Impl {
8280
* ```
8381
*/
8482
FormatArgument getPrecisionArgument() {
85-
result.getParent() = this and result = Synth::TFormatArgument(_, _, 2)
83+
result.getParent() = this and result = Synth::TFormatArgument(_, _, 2, _, _, _)
8684
}
8785
}
8886
}

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

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

rust/schema/annotations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ class FormatTemplateVariableAccess(Expr):
17631763

17641764

17651765
@qltest.skip
1766-
@synth.on_arguments(parent=FormatArgsExpr, index=int)
1766+
@synth.on_arguments(parent=FormatArgsExpr, index=int, text=string, offset=int)
17671767
class Format(Locatable):
17681768
"""
17691769
A format element in a formatting template. For example the `{}` in:
@@ -1776,7 +1776,7 @@ class Format(Locatable):
17761776

17771777

17781778
@qltest.skip
1779-
@synth.on_arguments(parent=FormatArgsExpr, index=int, kind=int)
1779+
@synth.on_arguments(parent=FormatArgsExpr, index=int, kind=int, name=string, positional=boolean, offset=int)
17801780
class FormatArgument(Locatable):
17811781
"""
17821782
An argument in a format element in a formatting template. For example the `width`, `precision`, and `value` in:

0 commit comments

Comments
 (0)