Skip to content

Commit 8a52565

Browse files
committed
C++: Improve 'toString' on the most common dataflow nodes.
1 parent 063f69c commit 8a52565

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,41 @@ private string toExprString(Node n) {
492492
result = n.asExpr(0).toString()
493493
or
494494
not exists(n.asExpr()) and
495-
result = n.asIndirectExpr(0, 1).toString() + " indirection"
495+
result = stars(n) + n.asIndirectExpr(0, 1).toString()
496496
)
497497
}
498498

499+
private module NodeStars {
500+
private int getNumberOfIndirections(Node n) {
501+
result = n.(RawIndirectOperand).getIndirectionIndex()
502+
or
503+
result = n.(RawIndirectInstruction).getIndirectionIndex()
504+
or
505+
result = n.(VariableNode).getIndirectionIndex()
506+
or
507+
result = n.(PostUpdateNodeImpl).getIndirectionIndex()
508+
or
509+
result = n.(FinalParameterNode).getIndirectionIndex()
510+
}
511+
512+
private int maxNumberOfIndirections() { result = max(getNumberOfIndirections(_)) }
513+
514+
private string repeatStars(int n) {
515+
n = 0 and result = ""
516+
or
517+
n = [1 .. maxNumberOfIndirections()] and
518+
result = "*" + repeatStars(n - 1)
519+
}
520+
521+
/**
522+
* Gets the number of stars (i.e., `*`s) needed to produce the `toString`
523+
* output for `n`.
524+
*/
525+
string stars(Node n) { result = repeatStars(getNumberOfIndirections(n)) }
526+
}
527+
528+
private import NodeStars
529+
499530
/**
500531
* A class that lifts pre-SSA dataflow nodes to regular dataflow nodes.
501532
*/
@@ -786,10 +817,12 @@ class IndirectParameterNode extends Node instanceof IndirectInstruction {
786817
override Location getLocationImpl() { result = this.getParameter().getLocation() }
787818

788819
override string toStringImpl() {
789-
result = this.getParameter().toString() + " indirection"
790-
or
791-
not exists(this.getParameter()) and
792-
result = "this indirection"
820+
exists(string prefix | prefix = stars(this) |
821+
result = prefix + this.getParameter().toString()
822+
or
823+
not exists(this.getParameter()) and
824+
result = prefix + "this"
825+
)
793826
}
794827
}
795828

@@ -1016,7 +1049,7 @@ private module RawIndirectNodes {
10161049
}
10171050

10181051
override string toStringImpl() {
1019-
result = operandNode(this.getOperand()).toStringImpl() + " indirection"
1052+
result = stars(this) + operandNode(this.getOperand()).toStringImpl()
10201053
}
10211054
}
10221055

@@ -1058,7 +1091,7 @@ private module RawIndirectNodes {
10581091
}
10591092

10601093
override string toStringImpl() {
1061-
result = instructionNode(this.getInstruction()).toStringImpl() + " indirection"
1094+
result = stars(this) + instructionNode(this.getInstruction()).toStringImpl()
10621095
}
10631096
}
10641097

@@ -1151,9 +1184,7 @@ class FinalParameterNode extends Node, TFinalParameterNode {
11511184
result instanceof UnknownDefaultLocation
11521185
}
11531186

1154-
override string toStringImpl() {
1155-
if indirectionIndex > 1 then result = p.toString() + " indirection" else result = p.toString()
1156-
}
1187+
override string toStringImpl() { result = stars(this) + p.toString() }
11571188
}
11581189

11591190
/**
@@ -1787,9 +1818,7 @@ class VariableNode extends Node, TVariableNode {
17871818
result instanceof UnknownDefaultLocation
17881819
}
17891820

1790-
override string toStringImpl() {
1791-
if indirectionIndex = 1 then result = v.toString() else result = v.toString() + " indirection"
1792-
}
1821+
override string toStringImpl() { result = stars(this) + v.toString() }
17931822
}
17941823

17951824
/**
@@ -2249,18 +2278,29 @@ class Content extends TContent {
22492278
abstract predicate impliesClearOf(Content c);
22502279
}
22512280

2281+
private module ContentStars {
2282+
private int maxNumberOfIndirections() { result = max(any(Content c).getIndirectionIndex()) }
2283+
2284+
private string repeatStars(int n) {
2285+
n = 0 and result = ""
2286+
or
2287+
n = [1 .. maxNumberOfIndirections()] and
2288+
result = "*" + repeatStars(n - 1)
2289+
}
2290+
2291+
string contentStars(Content c) { result = repeatStars(c.getIndirectionIndex() - 1) }
2292+
}
2293+
2294+
private import ContentStars
2295+
22522296
/** A reference through a non-union instance field. */
22532297
class FieldContent extends Content, TFieldContent {
22542298
Field f;
22552299
int indirectionIndex;
22562300

22572301
FieldContent() { this = TFieldContent(f, indirectionIndex) }
22582302

2259-
override string toString() {
2260-
indirectionIndex = 1 and result = f.toString()
2261-
or
2262-
indirectionIndex > 1 and result = f.toString() + " indirection"
2263-
}
2303+
override string toString() { result = contentStars(this) + f.toString() }
22642304

22652305
Field getField() { result = f }
22662306

@@ -2289,11 +2329,7 @@ class UnionContent extends Content, TUnionContent {
22892329

22902330
UnionContent() { this = TUnionContent(u, bytes, indirectionIndex) }
22912331

2292-
override string toString() {
2293-
indirectionIndex = 1 and result = u.toString()
2294-
or
2295-
indirectionIndex > 1 and result = u.toString() + " indirection"
2296-
}
2332+
override string toString() { result = contentStars(this) + u.toString() }
22972333

22982334
/** Gets a field of the underlying union of this `UnionContent`, if any. */
22992335
Field getAField() { result = u.getAField() and getFieldSize(result) = bytes }

0 commit comments

Comments
 (0)