Skip to content

Commit b598264

Browse files
authored
Merge pull request github#18445 from hvitved/rust/format-args-index
Rust: Remove `Format.getArgument`
2 parents 8c3e5b6 + 0795c24 commit b598264

29 files changed

+267
-73
lines changed

rust/ql/.generated.list

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

rust/ql/.gitattributes

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ query predicate multipleParents(Element child, string childClass, Element parent
5252
parentClass = parent.getPrimaryQlClasses()
5353
}
5454

55+
/** Holds if `parent` has multiple children at the same index. */
56+
query predicate multipleChildren(Element parent, int index, Element child1, Element child2) {
57+
child1 = getChildAndAccessor(parent, index, _) and
58+
child2 = getChildAndAccessor(parent, index, _) and
59+
child1 != child2
60+
}
61+
62+
/**
63+
* Holds if `child` has multiple positions amongst the `accessor` children
64+
* of `parent`.
65+
*
66+
* Children are allowed to have multiple positions for _different_ accessors,
67+
* for example in an array repeat expression `[1; 10]`, `1` has positions for
68+
* both `getRepeatOperand()` and `getExpr()`.
69+
*/
70+
query predicate multiplePositions(Element parent, int pos1, int pos2, string accessor, Element child) {
71+
child = getChildAndAccessor(parent, pos1, accessor) and
72+
child = getChildAndAccessor(parent, pos2, accessor) and
73+
pos1 != pos2
74+
}
75+
5576
/**
5677
* Gets counts of abstract syntax tree inconsistencies of each type.
5778
*/
@@ -71,4 +92,10 @@ int getAstInconsistencyCounts(string type) {
7192
or
7293
type = "Multiple parents" and
7394
result = count(Element e | multipleParents(e) | e)
95+
or
96+
type = "Multiple children" and
97+
result = count(Element e | multipleChildren(_, _, e, _) | e)
98+
or
99+
type = "Multiple positions" and
100+
result = count(Element e | multiplePositions(_, _, _, _, e) | e)
74101
}

rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ final class FormatArgsExprCfgNode extends Nodes::FormatArgsExprCfgNode {
195195

196196
/** Gets a format argument of the `i`th format of this format arguments expression (0-based). */
197197
FormatTemplateVariableAccessCfgNode getFormatTemplateVariableAccess(int i) {
198-
exists(FormatTemplateVariableAccess v |
199-
v.getArgument() = node.getFormat(i).getArgument() and
198+
exists(FormatTemplateVariableAccess v, Format f |
199+
f = node.getFormat(i) and
200+
v.getArgument() = [f.getArgumentRef(), f.getWidthArgument(), f.getPrecisionArgument()] and
200201
result.getFormatTemplateVariableAccess() = v and
201202
any(ChildMapping mapping).hasCfgChild(node, v, this, result)
202203
)

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,15 @@ module Impl {
4242

4343
override int getIndex() { result = index }
4444

45-
override FormatArgument getArgument() { result.getParent() = this }
46-
47-
/**
48-
* Gets the name or position reference of this format, if any. For example `name` and `0` in:
49-
* ```rust
50-
* let name = "Alice";
51-
* println!("{name} in wonderland");
52-
* println!("{0} in wonderland", name);
53-
* ```
54-
*/
55-
FormatArgument getArgumentRef() {
45+
override FormatArgument getArgumentRef() {
5646
result.getParent() = this and result = Synth::TFormatArgument(_, _, 0, _, _, _)
5747
}
5848

59-
/**
60-
* Gets the name or position reference of the width parameter in this format, if any. For example `width` and `1` in:
61-
* ```rust
62-
* let width = 6;
63-
* println!("{:width$}", PI);
64-
* println!("{:1$}", PI, width);
65-
* ```
66-
*/
67-
FormatArgument getWidthArgument() {
49+
override FormatArgument getWidthArgument() {
6850
result.getParent() = this and result = Synth::TFormatArgument(_, _, 1, _, _, _)
6951
}
7052

71-
/**
72-
* Gets the name or position reference of the width parameter in this format, if any. For example `prec` and `1` in:
73-
* ```rust
74-
* let prec = 6;
75-
* println!("{:.prec$}", PI);
76-
* println!("{:.1$}", PI, prec);
77-
* ```
78-
*/
79-
FormatArgument getPrecisionArgument() {
53+
override FormatArgument getPrecisionArgument() {
8054
result.getParent() = this and result = Synth::TFormatArgument(_, _, 2, _, _, _)
8155
}
8256
}

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

Lines changed: 45 additions & 4 deletions
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/ParentChild.qll

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

rust/ql/test/extractor-tests/generated/.generated_tests.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.

0 commit comments

Comments
 (0)