Skip to content

Commit de3b111

Browse files
committed
C++: Map a parameter node to the summarized callable if it exists.
1 parent fc39df2 commit de3b111

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

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

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,34 @@ abstract private class AbstractParameterNode extends Node {
13061306
* implicit `this` parameter is considered to have position `-1`, and
13071307
* pointer-indirection parameters are at further negative positions.
13081308
*/
1309-
abstract predicate isParameterOf(DataFlowCallable f, ParameterPosition pos);
1309+
predicate isSourceParameterOf(Function f, ParameterPosition pos) { none() }
1310+
1311+
/**
1312+
* Holds if this node is the parameter of `sc` at the specified position. The
1313+
* implicit `this` parameter is considered to have position `-1`, and
1314+
* pointer-indirection parameters are at further negative positions.
1315+
*/
1316+
predicate isSummaryParameterOf(
1317+
FlowSummaryImpl::Public::SummarizedCallable sc, ParameterPosition pos
1318+
) {
1319+
none()
1320+
}
1321+
1322+
/**
1323+
* Holds if this node is the parameter of `c` at the specified position. The
1324+
* implicit `this` parameter is considered to have position `-1`, and
1325+
* pointer-indirection parameters are at further negative positions.
1326+
*/
1327+
final predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) {
1328+
this.isSummaryParameterOf(c.asSummarizedCallable(), pos)
1329+
or
1330+
exists(Function f | this.isSourceParameterOf(f, pos) |
1331+
not exists(TSummarizedCallable(f)) and
1332+
c.asSourceCallable() = f
1333+
or
1334+
c.asSummarizedCallable() = f
1335+
)
1336+
}
13101337

13111338
/** Gets the `Parameter` associated with this node, if it exists. */
13121339
Parameter getParameter() { none() } // overridden by subclasses
@@ -1366,8 +1393,8 @@ private class IndirectInstructionParameterNode extends AbstractIndirectParameter
13661393

13671394
override Declaration getFunction() { result = init.getEnclosingFunction() }
13681395

1369-
override predicate isParameterOf(DataFlowCallable f, ParameterPosition pos) {
1370-
this.getEnclosingCallable() = f.getUnderlyingCallable() and
1396+
override predicate isSourceParameterOf(Function f, ParameterPosition pos) {
1397+
this.getEnclosingCallable() = f and
13711398
exists(int argumentIndex, int indirectionIndex |
13721399
indirectPositionHasArgumentIndexAndIndex(pos, argumentIndex, indirectionIndex) and
13731400
indirectParameterNodeHasArgumentIndexAndIndex(this, argumentIndex, indirectionIndex)
@@ -1424,9 +1451,8 @@ private class ExplicitParameterInstructionNode extends AbstractExplicitParameter
14241451
{
14251452
ExplicitParameterInstructionNode() { exists(instr.getParameter()) }
14261453

1427-
override predicate isParameterOf(DataFlowCallable f, ParameterPosition pos) {
1428-
f.getUnderlyingCallable().(Function).getParameter(pos.(DirectPosition).getIndex()) =
1429-
instr.getParameter()
1454+
override predicate isSourceParameterOf(Function f, ParameterPosition pos) {
1455+
f.getParameter(pos.(DirectPosition).getIndex()) = instr.getParameter()
14301456
}
14311457

14321458
override string toStringImpl() { result = instr.getParameter().toString() }
@@ -1440,9 +1466,9 @@ class ThisParameterInstructionNode extends AbstractExplicitParameterNode,
14401466
{
14411467
ThisParameterInstructionNode() { instr.getIRVariable() instanceof IRThisVariable }
14421468

1443-
override predicate isParameterOf(DataFlowCallable f, ParameterPosition pos) {
1469+
override predicate isSourceParameterOf(Function f, ParameterPosition pos) {
14441470
pos.(DirectPosition).getIndex() = -1 and
1445-
instr.getEnclosingFunction() = f.getUnderlyingCallable()
1471+
instr.getEnclosingFunction() = f
14461472
}
14471473

14481474
override string toStringImpl() { result = "this" }
@@ -1460,8 +1486,10 @@ class SummaryParameterNode extends AbstractParameterNode, FlowSummaryNode {
14601486
FlowSummaryImpl::Private::summaryParameterNode(this.getSummaryNode(), result)
14611487
}
14621488

1463-
override predicate isParameterOf(DataFlowCallable c, ParameterPosition p) {
1464-
c.getUnderlyingCallable() = this.getSummarizedCallable() and
1489+
override predicate isSummaryParameterOf(
1490+
FlowSummaryImpl::Public::SummarizedCallable c, ParameterPosition p
1491+
) {
1492+
c = this.getSummarizedCallable() and
14651493
p = this.getPosition()
14661494
}
14671495
}
@@ -1471,12 +1499,9 @@ private class DirectBodyLessParameterNode extends AbstractExplicitParameterNode,
14711499
{
14721500
DirectBodyLessParameterNode() { indirectionIndex = 0 }
14731501

1474-
override predicate isParameterOf(DataFlowCallable f, ParameterPosition pos) {
1475-
exists(Function func |
1476-
this.getFunction() = func and
1477-
f.asSourceCallable() = func and
1478-
func.getParameter(pos.(DirectPosition).getIndex()) = p
1479-
)
1502+
override predicate isSourceParameterOf(Function f, ParameterPosition pos) {
1503+
this.getFunction() = f and
1504+
f.getParameter(pos.(DirectPosition).getIndex()) = p
14801505
}
14811506

14821507
override Parameter getParameter() { result = p }
@@ -1487,12 +1512,11 @@ private class IndirectBodyLessParameterNode extends AbstractIndirectParameterNod
14871512
{
14881513
IndirectBodyLessParameterNode() { not this instanceof DirectBodyLessParameterNode }
14891514

1490-
override predicate isParameterOf(DataFlowCallable f, ParameterPosition pos) {
1491-
exists(Function func, int argumentPosition |
1492-
this.getFunction() = func and
1493-
f.asSourceCallable() = func and
1494-
indirectPositionHasArgumentIndexAndIndex(pos, argumentPosition, indirectionIndex) and
1495-
func.getParameter(argumentPosition) = p
1515+
override predicate isSourceParameterOf(Function f, ParameterPosition pos) {
1516+
exists(int argumentPosition |
1517+
this.getFunction() = f and
1518+
f.getParameter(argumentPosition) = p and
1519+
indirectPositionHasArgumentIndexAndIndex(pos, argumentPosition, indirectionIndex)
14961520
)
14971521
}
14981522

0 commit comments

Comments
 (0)