Skip to content

Commit

Permalink
Merge pull request #18445 from hvitved/rust/format-args-index
Browse files Browse the repository at this point in the history
Rust: Remove `Format.getArgument`
  • Loading branch information
hvitved authored Jan 10, 2025
2 parents 8c3e5b6 + 0795c24 commit b598264
Show file tree
Hide file tree
Showing 29 changed files with 267 additions and 73 deletions.
10 changes: 6 additions & 4 deletions 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/.gitattributes

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

27 changes: 27 additions & 0 deletions rust/ql/lib/codeql/rust/AstConsistency.qll
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ query predicate multipleParents(Element child, string childClass, Element parent
parentClass = parent.getPrimaryQlClasses()
}

/** Holds if `parent` has multiple children at the same index. */
query predicate multipleChildren(Element parent, int index, Element child1, Element child2) {
child1 = getChildAndAccessor(parent, index, _) and
child2 = getChildAndAccessor(parent, index, _) and
child1 != child2
}

/**
* Holds if `child` has multiple positions amongst the `accessor` children
* of `parent`.
*
* Children are allowed to have multiple positions for _different_ accessors,
* for example in an array repeat expression `[1; 10]`, `1` has positions for
* both `getRepeatOperand()` and `getExpr()`.
*/
query predicate multiplePositions(Element parent, int pos1, int pos2, string accessor, Element child) {
child = getChildAndAccessor(parent, pos1, accessor) and
child = getChildAndAccessor(parent, pos2, accessor) and
pos1 != pos2
}

/**
* Gets counts of abstract syntax tree inconsistencies of each type.
*/
Expand All @@ -71,4 +92,10 @@ int getAstInconsistencyCounts(string type) {
or
type = "Multiple parents" and
result = count(Element e | multipleParents(e) | e)
or
type = "Multiple children" and
result = count(Element e | multipleChildren(_, _, e, _) | e)
or
type = "Multiple positions" and
result = count(Element e | multiplePositions(_, _, _, _, e) | e)
}
5 changes: 3 additions & 2 deletions rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ final class FormatArgsExprCfgNode extends Nodes::FormatArgsExprCfgNode {

/** Gets a format argument of the `i`th format of this format arguments expression (0-based). */
FormatTemplateVariableAccessCfgNode getFormatTemplateVariableAccess(int i) {
exists(FormatTemplateVariableAccess v |
v.getArgument() = node.getFormat(i).getArgument() and
exists(FormatTemplateVariableAccess v, Format f |
f = node.getFormat(i) and
v.getArgument() = [f.getArgumentRef(), f.getWidthArgument(), f.getPrecisionArgument()] and
result.getFormatTemplateVariableAccess() = v and
any(ChildMapping mapping).hasCfgChild(node, v, this, result)
)
Expand Down
32 changes: 3 additions & 29 deletions rust/ql/lib/codeql/rust/elements/internal/FormatImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,15 @@ module Impl {

override int getIndex() { result = index }

override FormatArgument getArgument() { result.getParent() = this }

/**
* Gets the name or position reference of this format, if any. For example `name` and `0` in:
* ```rust
* let name = "Alice";
* println!("{name} in wonderland");
* println!("{0} in wonderland", name);
* ```
*/
FormatArgument getArgumentRef() {
override FormatArgument getArgumentRef() {
result.getParent() = this and result = Synth::TFormatArgument(_, _, 0, _, _, _)
}

/**
* Gets the name or position reference of the width parameter in this format, if any. For example `width` and `1` in:
* ```rust
* let width = 6;
* println!("{:width$}", PI);
* println!("{:1$}", PI, width);
* ```
*/
FormatArgument getWidthArgument() {
override FormatArgument getWidthArgument() {
result.getParent() = this and result = Synth::TFormatArgument(_, _, 1, _, _, _)
}

/**
* Gets the name or position reference of the width parameter in this format, if any. For example `prec` and `1` in:
* ```rust
* let prec = 6;
* println!("{:.prec$}", PI);
* println!("{:.1$}", PI, prec);
* ```
*/
FormatArgument getPrecisionArgument() {
override FormatArgument getPrecisionArgument() {
result.getParent() = this and result = Synth::TFormatArgument(_, _, 2, _, _, _)
}
}
Expand Down
49 changes: 45 additions & 4 deletions rust/ql/lib/codeql/rust/elements/internal/generated/Format.qll

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

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

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

Loading

0 comments on commit b598264

Please sign in to comment.