C++: Add a way to test the behavior of 'asExpr' and 'toString' on dataflow nodes.

This commit is contained in:
Mathias Vorreiter Pedersen
2024-01-24 14:14:38 +00:00
parent 67242278ee
commit 4e18cca0f4
5 changed files with 104 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
testFailures
failures

View File

@@ -0,0 +1,40 @@
import cpp
import TestUtilities.InlineExpectationsTest
import semmle.code.cpp.dataflow.new.DataFlow::DataFlow
bindingset[s]
string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else result = s }
string formatNumberOfNodesForIndirectExpr(Expr e) {
exists(int n | n = strictcount(Node node | node.asIndirectExpr() = e) |
n > 1 and result = ": " + n
)
}
module AsIndirectExprTest implements TestSig {
string getARelevantTag() { result = ["asIndirectExpr", "numberOfIndirectNodes"] }
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(Node n, Expr e, string exprString |
e = n.asIndirectExpr() and
location = e.getLocation() and
element = n.toString() and
exprString = e.toString()
|
tag = "asIndirectExpr" and
(
// The toString on an indirect is often formatted like `***myExpr`.
// If the node's `toString` is of that form then we don't show it in
// the expected output.
if element.matches("%" + exprString)
then value = quote(exprString)
else value = quote(exprString + "(" + element + ")")
)
or
tag = "numberOfIndirectNodes" and
value = quote(exprString + formatNumberOfNodesForIndirectExpr(e))
)
}
}
import MakeTest<AsIndirectExprTest>

View File

@@ -0,0 +1,23 @@
void take_const_ref_int(const int &);
void test_materialize_temp_int()
{
take_const_ref_int(42); // $ asExpr=42 numberOfNodes="42: 2" asIndirectExpr=42 numberOfIndirectNodes="42: 2"
}
struct A {};
A get();
void take_const_ref(const A &);
void test1(){
take_const_ref(get()); // $ asExpr="call to get" numberOfNodes="call to get: 2" asIndirectExpr="call to get" numberOfIndirectNodes="call to get: 2"
}
void take_ref(A &);
A& get_ref();
void test2() {
take_ref(get_ref()); // $ asExpr="call to get_ref" asIndirectExpr="call to get_ref"
}

View File

@@ -0,0 +1,2 @@
testFailures
failures

View File

@@ -0,0 +1,37 @@
import cpp
import TestUtilities.InlineExpectationsTest
import semmle.code.cpp.dataflow.new.DataFlow::DataFlow
bindingset[s]
string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else result = s }
string formatNumberOfNodesForExpr(Expr e) {
exists(int n | n = strictcount(Node node | node.asExpr() = e) | n > 1 and result = ": " + n)
}
module AsExprTest implements TestSig {
string getARelevantTag() { result = ["asExpr", "numberOfNodes"] }
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(Node n, Expr e, string exprString |
e = n.asExpr() and
location = e.getLocation() and
element = n.toString() and
exprString = e.toString()
|
tag = "asExpr" and
(
// If the `toString` on the node is identical to the `toString` of the
// expression then we don't show it in the expected output.
if exprString = element
then value = quote(exprString)
else value = quote(exprString + "(" + element + ")")
)
or
tag = "numberOfNodes" and
value = quote(exprString + formatNumberOfNodesForExpr(e))
)
}
}
import MakeTest<AsExprTest>