Skip to content

Commit aeb6293

Browse files
committed
C++: Rewrite flow test common to use inline expectation test module
This also rewrites all uses of flow test common to use `DataFlow::ConfigSig`. Note that the removed deprecated aliases are 14 months old by now and, hence, can be safely removed.
1 parent ee36d32 commit aeb6293

File tree

13 files changed

+83
-79
lines changed

13 files changed

+83
-79
lines changed

cpp/ql/test/TestUtilities/dataflow/FlowTestCommon.qll

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ private import semmle.code.cpp.ir.dataflow.DataFlow::DataFlow as IRDataFlow
1616
private import semmle.code.cpp.dataflow.DataFlow::DataFlow as AstDataFlow
1717
import TestUtilities.InlineExpectationsTest
1818

19-
class IRFlowTest extends InlineExpectationsTest {
20-
IRFlowTest() { this = "IRFlowTest" }
19+
module IRFlowTest<IRDataFlow::GlobalFlowSig Flow> implements TestSig {
20+
string getARelevantTag() { result = "ir" }
2121

22-
override string getARelevantTag() { result = "ir" }
23-
24-
override predicate hasActualResult(Location location, string element, string tag, string value) {
25-
exists(IRDataFlow::Node source, IRDataFlow::Node sink, IRDataFlow::Configuration conf, int n |
22+
predicate hasActualResult(Location location, string element, string tag, string value) {
23+
exists(IRDataFlow::Node source, IRDataFlow::Node sink, int n |
2624
tag = "ir" and
27-
conf.hasFlow(source, sink) and
25+
Flow::flow(source, sink) and
2826
n =
2927
strictcount(int line, int column |
30-
conf.hasFlow(any(IRDataFlow::Node otherSource |
28+
Flow::flow(any(IRDataFlow::Node otherSource |
3129
otherSource.hasLocationInfo(_, line, column, _, _)
3230
), sink)
3331
) and
@@ -47,20 +45,16 @@ class IRFlowTest extends InlineExpectationsTest {
4745
}
4846
}
4947

50-
class AstFlowTest extends InlineExpectationsTest {
51-
AstFlowTest() { this = "ASTFlowTest" }
52-
53-
override string getARelevantTag() { result = "ast" }
48+
module AstFlowTest<AstDataFlow::GlobalFlowSig Flow> implements TestSig {
49+
string getARelevantTag() { result = "ast" }
5450

55-
override predicate hasActualResult(Location location, string element, string tag, string value) {
56-
exists(
57-
AstDataFlow::Node source, AstDataFlow::Node sink, AstDataFlow::Configuration conf, int n
58-
|
51+
predicate hasActualResult(Location location, string element, string tag, string value) {
52+
exists(AstDataFlow::Node source, AstDataFlow::Node sink, int n |
5953
tag = "ast" and
60-
conf.hasFlow(source, sink) and
54+
Flow::flow(source, sink) and
6155
n =
6256
strictcount(int line, int column |
63-
conf.hasFlow(any(AstDataFlow::Node otherSource |
57+
Flow::flow(any(AstDataFlow::Node otherSource |
6458
otherSource.hasLocationInfo(_, line, column, _, _)
6559
), sink)
6660
) and
@@ -79,6 +73,3 @@ class AstFlowTest extends InlineExpectationsTest {
7973
)
8074
}
8175
}
82-
83-
/** DEPRECATED: Alias for AstFlowTest */
84-
deprecated class ASTFlowTest = AstFlowTest;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures

cpp/ql/test/library-tests/dataflow/dataflow-tests/test.ql

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ module AstTest {
1616
}
1717

1818
/** Common data flow configuration to be used by tests. */
19-
class AstTestAllocationConfig extends DataFlow::Configuration {
20-
AstTestAllocationConfig() { this = "ASTTestAllocationConfig" }
21-
22-
override predicate isSource(DataFlow::Node source) {
19+
module AstTestAllocationConfig implements DataFlow::ConfigSig {
20+
predicate isSource(DataFlow::Node source) {
2321
source.asExpr().(FunctionCall).getTarget().getName() = "source"
2422
or
2523
source.asParameter().getName().matches("source%")
@@ -32,18 +30,20 @@ module AstTest {
3230
exists(source.asUninitialized())
3331
}
3432

35-
override predicate isSink(DataFlow::Node sink) {
33+
predicate isSink(DataFlow::Node sink) {
3634
exists(FunctionCall call |
3735
call.getTarget().getName() = ["sink", "indirect_sink"] and
3836
sink.asExpr() = call.getAnArgument()
3937
)
4038
}
4139

42-
override predicate isBarrier(DataFlow::Node barrier) {
40+
predicate isBarrier(DataFlow::Node barrier) {
4341
barrier.asExpr().(VariableAccess).getTarget().hasName("barrier") or
4442
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getABarrierNode()
4543
}
4644
}
45+
46+
module AstFlow = DataFlow::Global<AstTestAllocationConfig>;
4747
}
4848

4949
module IRTest {
@@ -67,10 +67,8 @@ module IRTest {
6767
}
6868

6969
/** Common data flow configuration to be used by tests. */
70-
class IRTestAllocationConfig extends DataFlow::Configuration {
71-
IRTestAllocationConfig() { this = "IRTestAllocationConfig" }
72-
73-
override predicate isSource(DataFlow::Node source) {
70+
module IRTestAllocationConfig implements DataFlow::ConfigSig {
71+
predicate isSource(DataFlow::Node source) {
7472
source.asExpr().(FunctionCall).getTarget().getName() = "source"
7573
or
7674
source.asIndirectExpr(1).(FunctionCall).getTarget().getName() = "indirect_source"
@@ -82,7 +80,7 @@ module IRTest {
8280
exists(source.asUninitialized())
8381
}
8482

85-
override predicate isSink(DataFlow::Node sink) {
83+
predicate isSink(DataFlow::Node sink) {
8684
exists(FunctionCall call, Expr e | e = call.getAnArgument() |
8785
call.getTarget().getName() = "sink" and
8886
sink.asExpr() = e
@@ -92,7 +90,7 @@ module IRTest {
9290
)
9391
}
9492

95-
override predicate isBarrier(DataFlow::Node barrier) {
93+
predicate isBarrier(DataFlow::Node barrier) {
9694
exists(Expr barrierExpr | barrierExpr in [barrier.asExpr(), barrier.asIndirectExpr()] |
9795
barrierExpr.(VariableAccess).getTarget().hasName("barrier")
9896
)
@@ -102,4 +100,8 @@ module IRTest {
102100
barrier = DataFlow::BarrierGuard<testBarrierGuard/3>::getAnIndirectBarrierNode()
103101
}
104102
}
103+
104+
module IRFlow = DataFlow::Global<IRTestAllocationConfig>;
105105
}
106+
107+
import MakeTest<MergeTests<AstFlowTest<AstTest::AstFlow>, IRFlowTest<IRTest::IRFlow>>>
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
private import semmle.code.cpp.dataflow.DataFlow
22
private import DataFlow
33

4-
class AstConf extends Configuration {
5-
AstConf() { this = "ASTFieldFlowConf" }
6-
7-
override predicate isSource(Node src) {
4+
module AstConfig implements ConfigSig {
5+
predicate isSource(Node src) {
86
src.asExpr() instanceof NewExpr
97
or
108
src.asExpr().(Call).getTarget().hasName("user_input")
@@ -15,14 +13,14 @@ class AstConf extends Configuration {
1513
)
1614
}
1715

18-
override predicate isSink(Node sink) {
16+
predicate isSink(Node sink) {
1917
exists(Call c |
2018
c.getTarget().hasName("sink") and
2119
c.getAnArgument() = sink.asExpr()
2220
)
2321
}
2422

25-
override predicate isAdditionalFlowStep(Node a, Node b) {
23+
predicate isAdditionalFlowStep(Node a, Node b) {
2624
b.asPartialDefinition() =
2725
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
2826
.getQualifier()
@@ -31,5 +29,4 @@ class AstConf extends Configuration {
3129
}
3230
}
3331

34-
/** DEPRECATED: Alias for AstConf */
35-
deprecated class ASTConf = AstConf;
32+
module AstFlow = Global<AstConfig>;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
private import semmle.code.cpp.ir.dataflow.DataFlow
22
private import DataFlow
33

4-
class IRConf extends Configuration {
5-
IRConf() { this = "IRFieldFlowConf" }
6-
7-
override predicate isSource(Node src) {
4+
module IRConfig implements ConfigSig {
5+
predicate isSource(Node src) {
86
src.asExpr() instanceof NewExpr
97
or
108
src.asExpr().(Call).getTarget().hasName("user_input")
@@ -15,18 +13,20 @@ class IRConf extends Configuration {
1513
)
1614
}
1715

18-
override predicate isSink(Node sink) {
16+
predicate isSink(Node sink) {
1917
exists(Call c |
2018
c.getTarget().hasName("sink") and
2119
c.getAnArgument() = [sink.asExpr(), sink.asIndirectExpr(), sink.asConvertedExpr()]
2220
)
2321
}
2422

25-
override predicate isAdditionalFlowStep(Node a, Node b) {
23+
predicate isAdditionalFlowStep(Node a, Node b) {
2624
b.asPartialDefinition() =
2725
any(Call c | c.getTarget().hasName("insert") and c.getAnArgument() = a.asExpr())
2826
.getQualifier()
2927
or
3028
b.asExpr().(AddressOfExpr).getOperand() = a.asExpr()
3129
}
3230
}
31+
32+
module IRFlow = Global<IRConfig>;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import TestUtilities.dataflow.FlowTestCommon
22

33
module AstTest {
4-
private import ASTConfiguration
4+
import ASTConfiguration
55
}
66

77
module IRTest {
8-
private import IRConfiguration
8+
import IRConfiguration
99
}
10+
11+
import MakeTest<MergeTests<AstFlowTest<AstTest::AstFlow>, IRFlowTest<IRTest::IRFlow>>>

cpp/ql/test/library-tests/dataflow/fields/ir-path-flow.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import semmle.code.cpp.ir.dataflow.DataFlow
66
import IRConfiguration
7-
import DataFlow::PathGraph
7+
import IRFlow::PathGraph
88

9-
from DataFlow::PathNode src, DataFlow::PathNode sink, IRConf conf
10-
where conf.hasFlowPath(src, sink)
9+
from IRFlow::PathNode src, IRFlow::PathNode sink
10+
where IRFlow::flowPath(src, sink)
1111
select sink, src, sink, sink + " flows from $@", src, src.toString()

cpp/ql/test/library-tests/dataflow/fields/path-flow.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import semmle.code.cpp.dataflow.DataFlow
66
import ASTConfiguration
7-
import DataFlow::PathGraph
7+
import AstFlow::PathGraph
88

9-
from DataFlow::PathNode src, DataFlow::PathNode sink, AstConf conf
10-
where conf.hasFlowPath(src, sink)
9+
from AstFlow::PathNode src, AstFlow::PathNode sink
10+
where AstFlow::flowPath(src, sink)
1111
select sink, src, sink, sink + " flows from $@", src, src.toString()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures

0 commit comments

Comments
 (0)