Skip to content

Commit 6d8e2f8

Browse files
authored
Merge pull request #20017 from jketema/final
C++: Add dataflow predicate for checking if a node is the final value of a parameter
2 parents 3a0def7 + 96c379a commit 6d8e2f8

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* Added a `isFinalValueOfParameter` predicate to DataFlow::Node which holds when a dataflow node represents the final value of an output parameter of a function.

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,23 @@ class Node extends TIRDataFlowNode {
488488
result = this.(IndirectParameterNode).getParameter()
489489
}
490490

491+
/**
492+
* Holds if this node represents the `indirectionIndex`'th indirection of
493+
* the value of an output parameter `p` just before reaching the end of a function.
494+
*/
495+
predicate isFinalValueOfParameter(Parameter p, int indirectionIndex) {
496+
exists(FinalParameterNode n | n = this |
497+
p = n.getParameter() and
498+
indirectionIndex = n.getIndirectionIndex()
499+
)
500+
}
501+
502+
/**
503+
* Holds if this node represents the value of an output parameter `p`
504+
* just before reaching the end of a function.
505+
*/
506+
predicate isFinalValueOfParameter(Parameter p) { this.isFinalValueOfParameter(p, _) }
507+
491508
/**
492509
* Gets the variable corresponding to this node, if any. This can be used for
493510
* modeling flow in and out of global variables.
@@ -1225,7 +1242,7 @@ import RawIndirectNodes
12251242
/**
12261243
* INTERNAL: do not use.
12271244
*
1228-
* A node representing the value of an update parameter
1245+
* A node representing the value of an output parameter
12291246
* just before reaching the end of a function.
12301247
*/
12311248
class FinalParameterNode extends Node, TFinalParameterNode {

cpp/ql/test/library-tests/dataflow/dataflow-tests/has-parameter-flow-out.ql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,24 @@ module AstTest {
2424

2525
module IRTest {
2626
private import semmle.code.cpp.ir.dataflow.DataFlow
27-
private import semmle.code.cpp.ir.dataflow.internal.DataFlowUtil
2827

2928
private string stars(int k) {
30-
k = [0 .. max(FinalParameterNode n | | n.getIndirectionIndex())] and
29+
k = [0 .. max(DataFlow::Node n, int i | n.isFinalValueOfParameter(_, i) | i)] and
3130
(if k = 0 then result = "" else result = "*" + stars(k - 1))
3231
}
3332

3433
module IRParameterDefTest implements TestSig {
3534
string getARelevantTag() { result = "ir-def" }
3635

3736
predicate hasActualResult(Location ___location, string element, string tag, string value) {
38-
exists(Function f, Parameter p, FinalParameterNode n |
37+
exists(Function f, Parameter p, DataFlow::Node n, int i |
3938
p.isNamed() and
40-
n.getParameter() = p and
39+
n.isFinalValueOfParameter(p, i) and
4140
n.getFunction() = f and
4241
___location = f.getLocation() and
4342
element = p.toString() and
4443
tag = "ir-def" and
45-
value = stars(n.getIndirectionIndex()) + p.getName()
44+
value = stars(i) + p.getName()
4645
)
4746
}
4847
}

0 commit comments

Comments
 (0)