Ruby: Simplify getValueText logic for StringlikeLiterals

This commit is contained in:
Tom Hvitved
2021-12-17 11:03:07 +01:00
committed by Harry Maclean
parent 799ec23b0d
commit c3fd272f9b
3 changed files with 5 additions and 38 deletions

View File

@@ -10,15 +10,7 @@ private import codeql.ruby.controlflow.CfgNodes
*
* This is the QL root class for all literals.
*/
class Literal extends Expr, TLiteral {
/**
* Gets the source text for this literal, if this is a simple literal.
*
* For complex literals, such as arrays, hashes, and strings with
* interpolations, this predicate has no result.
*/
override string getValueText() { none() }
}
class Literal extends Expr, TLiteral { }
/**
* A numeric literal, i.e. an integer, floating-point, rational, or complex
@@ -411,17 +403,6 @@ class StringlikeLiteral extends Literal, TStringlikeLiteral {
result = ""
}
override string getValueText() {
forall(StringComponent c | c = this.getComponent(_) |
not c instanceof StringInterpolationComponent
) and
result =
concat(StringComponent c, int i | c = this.getComponent(i) | c.getValueText() order by i)
or
exists(this.getComponent(_)) and
result = this.getAControlFlowNode().(ExprNodes::StringlikeLiteralCfgNode).getValueText()
}
override string toString() {
exists(string full, string summary |
full =

View File

@@ -485,7 +485,7 @@ module ExprNodes {
final override StringlikeLiteral getExpr() { result = super.getExpr() }
/** Gets the `n`th component of this `StringlikeLiteral` */
StringComponentCfgNode getComponent(int n) { result.getNode() = e.getComponent(n) }
StringComponentCfgNode getComponent(int n) { e.hasCfgChild(e.getComponent(n), this, result) }
/** Gets a component of this `StringlikeLiteral` */
StringComponentCfgNode getAComponent() { result = this.getComponent(_) }
@@ -494,26 +494,13 @@ module ExprNodes {
// if all interpolations have a known string value, we will get a result
language[monotonicAggregates]
override string getValueText() {
result = e.getValueText()
or
result =
concat(StringComponent c, int i |
c = e.getComponent(i)
concat(StringComponentCfgNode c, int i |
c = this.getComponent(i)
|
getComponentValueText(c) order by i
c.getValueText() order by i
)
}
/**
* Get the `ValueText()` of a `StringComponent`.
* If the component has a CFG node, defer to that (in order to resolve variables in interpolations).
* Otherwise, defer to the AST node.
*/
private string getComponentValueText(StringComponent c) {
exists(StringComponentCfgNode n | n.getNode() = c | result = n.getValueText())
or
not exists(StringComponentCfgNode n | n.getNode() = c) and result = c.getValueText()
}
}
/** A control-flow node that wraps a `StringLiteral` AST expression. */

View File

@@ -614,7 +614,6 @@
| modules/modules.rb:55:8:55:30 | "module Foo::Bar again" | module Foo::Bar again |
| modules/modules.rb:56:17:56:17 | 4 | 4 |
| modules/toplevel.rb:1:6:1:12 | "world" | world |
| modules/toplevel.rb:3:12:3:16 | "!!!" | !!! |
| modules/toplevel.rb:5:14:5:20 | "hello" | hello |
| operations/operations.rb:3:5:3:5 | 0 | 0 |
| operations/operations.rb:4:5:4:5 | 0 | 0 |