Skip to content

Commit 60819ad

Browse files
committed
Add a single predicate that should be used to convert an instruction to an expression.
1 parent 67a0112 commit 60819ad

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,51 @@ class RawIndirectInstruction extends Node, TRawIndirectInstruction {
10421042
}
10431043
}
10441044

1045+
private module GetConvertedResultExpression {
1046+
private import semmle.code.cpp.ir.implementation.raw.internal.TranslatedExpr
1047+
private import semmle.code.cpp.ir.implementation.raw.internal.InstructionTag
1048+
1049+
/**
1050+
* Gets the expression that should be returned as the result expression from `instr`.
1051+
*
1052+
* Note that this predicate may return multiple results in cases where a conversion belond to a
1053+
* different AST element than its operand.
1054+
*/
1055+
Expr getConvertedResultExpression(Instruction instr) {
1056+
// Only fully converted instructions has a result for `asConvertedExpr`
1057+
not conversionFlow(unique( | | getAUse(instr)), _, false, false) and
1058+
result = getConvertedResultExpressionImpl(instr)
1059+
or
1060+
// If the conversion also has a result then we return multiple results
1061+
exists(Operand operand | conversionFlow(operand, instr, false, false) |
1062+
result = getConvertedResultExpressionImpl(operand.getDef())
1063+
or
1064+
result = getConvertedResultExpression(operand.getDef())
1065+
)
1066+
}
1067+
1068+
private Expr getConvertedResultExpressionImpl0(Instruction instr) {
1069+
exists(TranslatedAssignOperation tao |
1070+
result = tao.getExpr() and
1071+
instr = tao.getInstruction(any(AssignmentStoreTag tag))
1072+
)
1073+
or
1074+
exists(TranslatedCrementOperation tco |
1075+
result = tco.getExpr() and
1076+
instr = tco.getInstruction(any(CrementStoreTag tag))
1077+
)
1078+
}
1079+
1080+
private Expr getConvertedResultExpressionImpl(Instruction instr) {
1081+
result = getConvertedResultExpressionImpl0(instr)
1082+
or
1083+
not exists(getConvertedResultExpressionImpl0(instr)) and
1084+
result = instr.getConvertedResultExpression()
1085+
}
1086+
}
1087+
1088+
private import GetConvertedResultExpression
1089+
10451090
/** Holds if `node` is an `OperandNode` that should map `node.asExpr()` to `e`. */
10461091
predicate exprNodeShouldBeOperand(OperandNode node, Expr e) {
10471092
exists(Instruction def |

0 commit comments

Comments
 (0)