Skip to content

Commit 86075c9

Browse files
author
Alvaro Muñoz
committed
Improve ExpressionNode Location handling
1 parent 4f7cce9 commit 86075c9

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

ql/lib/codeql/actions/Ast.qll

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,25 @@ class ExpressionNode extends AstNode, TExpressionNode {
6868

6969
Job getJob() { result.getAChildNode*() = n }
7070

71+
/**
72+
* Gets the length of each line in the StringValue .
73+
*/
7174
int lineLength(int idx) {
7275
exists(string line | line = n.getValue().splitAt("\n", idx) and result = line.length() + 1)
7376
}
7477

75-
bindingset[i]
76-
int unboundPartialLineLengthSum(int i) {
77-
result = sum(int j, int length | j in [0 .. i] and length = this.lineLength(j) | length)
78-
}
79-
78+
/**
79+
* Gets the sum of the length of the lines up to the given index.
80+
*/
8081
int partialLineLengthSum(int i) {
8182
i in [0 .. count(n.getValue().splitAt("\n"))] and
82-
result = this.unboundPartialLineLengthSum(i)
83+
result = sum(int j, int length | j in [0 .. i] and length = this.lineLength(j) | length)
8384
}
8485

85-
predicate expressionOffsets(int sl, int sc, int el, int ec) {
86+
/**
87+
* Gets the absolute coordinates of the expression.
88+
*/
89+
predicate expressionLocation(int sl, int sc, int el, int ec) {
8690
exists(int lineDiff, string style, Location loc |
8791
loc = n.asYamlNode().getLocation() and
8892
lineDiff = loc.getEndLine() - loc.getStartLine() and
@@ -164,7 +168,7 @@ class ExpressionNode extends AstNode, TExpressionNode {
164168

165169
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
166170
path = n.asYamlNode().getFile().getAbsolutePath() and
167-
this.expressionOffsets(sl, sc, el, ec)
171+
this.expressionLocation(sl, sc, el, ec)
168172
}
169173

170174
override File getFile() { result = n.asYamlNode().getFile() }

ql/lib/codeql/actions/controlflow/internal/Cfg.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,13 @@ private class RunTree extends StandardPreOrderTree instanceof Run {
274274
private class StringValueTree extends StandardPreOrderTree instanceof StringValue {
275275
override ControlFlowTree getChildNode(int i) {
276276
result =
277-
rank[i](ExpressionNode child, int sl, int el, int sc, int ec, string path |
278-
child = super.getAChildNode() and child.hasLocationInfo(path, sl, sc, el, ec)
277+
rank[i](ExpressionNode child, Location l |
278+
child = super.getAChildNode() and
279+
l = child.getLocation()
279280
|
280-
child order by sl, sc, ec, el, child.toString()
281+
child
282+
order by
283+
l.getStartLine(), l.getStartColumn(), l.getEndColumn(), l.getEndLine(), child.toString()
281284
)
282285
}
283286
}

0 commit comments

Comments
 (0)