Rust: Remove Format.getArgument

This commit is contained in:
Tom Hvitved
2025-01-10 12:51:00 +01:00
parent 0dccbb9349
commit 166f8916cc
16 changed files with 102 additions and 52 deletions

View File

@@ -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)
)

View File

@@ -40,16 +40,6 @@ module Impl {
override Format getParent() { result = Synth::TFormat(parent, index, _, _) }
/** Gets the position of this argument. */
int getPosition() {
this =
rank[result + 1](FormatArgument f, int offs |
f = Synth::TFormatArgument(parent, index, _, _, _, offs)
|
f order by offs
)
}
override FormatTemplateVariableAccess getVariable() { result.getArgument() = this }
}

View File

@@ -42,10 +42,6 @@ module Impl {
override int getIndex() { result = index }
override FormatArgument getArgument(int i) {
result.getParent() = this and i = result.getPosition()
}
/**
* Gets the name or position reference of this format, if any. For example `name` and `0` in:
* ```rust
@@ -54,7 +50,7 @@ module Impl {
* println!("{0} in wonderland", name);
* ```
*/
FormatArgument getArgumentRef() {
override FormatArgument getArgumentRef() {
result.getParent() = this and result = Synth::TFormatArgument(_, _, 0, _, _, _)
}
@@ -66,7 +62,7 @@ module Impl {
* println!("{:1$}", PI, width);
* ```
*/
FormatArgument getWidthArgument() {
override FormatArgument getWidthArgument() {
result.getParent() = this and result = Synth::TFormatArgument(_, _, 1, _, _, _)
}
@@ -78,7 +74,7 @@ module Impl {
* println!("{:.1$}", PI, prec);
* ```
*/
FormatArgument getPrecisionArgument() {
override FormatArgument getPrecisionArgument() {
result.getParent() = this and result = Synth::TFormatArgument(_, _, 2, _, _, _)
}
}

View File

@@ -41,18 +41,33 @@ module Generated {
int getIndex() { none() }
/**
* Gets the `index`th argument of this format (0-based).
* Gets the argument reference of this format, if it exists.
*/
FormatArgument getArgument(int index) { none() }
FormatArgument getArgumentRef() { none() }
/**
* Gets any of the arguments of this format.
* Holds if `getArgumentRef()` exists.
*/
final FormatArgument getAnArgument() { result = this.getArgument(_) }
final predicate hasArgumentRef() { exists(this.getArgumentRef()) }
/**
* Gets the number of arguments of this format.
* Gets the width argument of this format, if it exists.
*/
final int getNumberOfArguments() { result = count(int i | exists(this.getArgument(i))) }
FormatArgument getWidthArgument() { none() }
/**
* Holds if `getWidthArgument()` exists.
*/
final predicate hasWidthArgument() { exists(this.getWidthArgument()) }
/**
* Gets the precision argument of this format, if it exists.
*/
FormatArgument getPrecisionArgument() { none() }
/**
* Holds if `getPrecisionArgument()` exists.
*/
final predicate hasPrecisionArgument() { exists(this.getPrecisionArgument()) }
}
}

View File

@@ -69,18 +69,29 @@ private module Impl {
}
private Element getImmediateChildOfFormat(Format e, int index, string partialPredicateCall) {
exists(int b, int bLocatable, int n, int nArgument |
exists(
int b, int bLocatable, int n, int nArgumentRef, int nWidthArgument, int nPrecisionArgument
|
b = 0 and
bLocatable = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfLocatable(e, i, _)) | i) and
n = bLocatable and
nArgument = n + 1 + max(int i | i = -1 or exists(e.getArgument(i)) | i) and
nArgumentRef = n + 1 and
nWidthArgument = nArgumentRef + 1 and
nPrecisionArgument = nWidthArgument + 1 and
(
none()
or
result = getImmediateChildOfLocatable(e, index - b, partialPredicateCall)
or
result = e.getArgument(index - n) and
partialPredicateCall = "Argument(" + (index - n).toString() + ")"
index = n and result = e.getArgumentRef() and partialPredicateCall = "ArgumentRef()"
or
index = nArgumentRef and
result = e.getWidthArgument() and
partialPredicateCall = "WidthArgument()"
or
index = nWidthArgument and
result = e.getPrecisionArgument() and
partialPredicateCall = "PrecisionArgument()"
)
)
}