mirror of
https://github.com/github/codeql.git
synced 2026-04-22 07:15:15 +02:00
C++: Add a way to test the behavior of 'asExpr' and 'toString' on dataflow nodes.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
testFailures
|
||||
failures
|
||||
40
cpp/ql/test/library-tests/dataflow/asExpr/test-indirect.ql
Normal file
40
cpp/ql/test/library-tests/dataflow/asExpr/test-indirect.ql
Normal 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>
|
||||
23
cpp/ql/test/library-tests/dataflow/asExpr/test.cpp
Normal file
23
cpp/ql/test/library-tests/dataflow/asExpr/test.cpp
Normal 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"
|
||||
}
|
||||
2
cpp/ql/test/library-tests/dataflow/asExpr/test.expected
Normal file
2
cpp/ql/test/library-tests/dataflow/asExpr/test.expected
Normal file
@@ -0,0 +1,2 @@
|
||||
testFailures
|
||||
failures
|
||||
37
cpp/ql/test/library-tests/dataflow/asExpr/test.ql
Normal file
37
cpp/ql/test/library-tests/dataflow/asExpr/test.ql
Normal 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>
|
||||
Reference in New Issue
Block a user