Skip to content

Commit ee3e38f

Browse files
committed
Simplify test interface in FlowSummaryImpl.qll
1 parent 70c6744 commit ee3e38f

File tree

4 files changed

+51
-106
lines changed

4 files changed

+51
-106
lines changed

csharp/ql/test/library-tests/dataflow/library/FlowSummaries.ql

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,20 @@ import shared.FlowSummaries
22
import semmle.code.csharp.dataflow.internal.ExternalFlow
33
import External
44

5-
module TestSummaryInput implements TestSummaryInputSig {
6-
class RelevantSummarizedCallable = IncludeSummarizedCallable;
7-
}
8-
9-
module TestNeutralInput implements TestNeutralInputSig {
10-
class RelevantNeutralCallable instanceof NeutralCallable {
11-
final string getCallableCsv() { result = asPartialNeutralModel(this) }
5+
final private class NeutralCallableFinal = NeutralCallable;
126

13-
string toString() { result = super.toString() }
14-
}
7+
class RelevantNeutralCallable extends NeutralCallableFinal {
8+
final string getCallableCsv() { result = asPartialNeutralModel(this) }
159
}
1610

17-
module TestSourceSinkInput implements TestSourceSinkInputSig {
18-
class RelevantSourceCallable instanceof SourceCallable {
19-
string getCallableCsv() { result = asPartialModel(this) }
20-
21-
string toString() { result = super.toString() }
22-
}
23-
24-
class RelevantSinkCallable instanceof SinkCallable {
25-
string getCallableCsv() { result = asPartialModel(this) }
11+
class RelevantSourceCallable extends SourceCallable {
12+
string getCallableCsv() { result = asPartialModel(this) }
13+
}
2614

27-
string toString() { result = super.toString() }
28-
}
15+
class RelevantSinkCallable extends SinkCallable {
16+
string getCallableCsv() { result = asPartialModel(this) }
2917
}
3018

31-
import TestSummaryOutput<TestSummaryInput>
32-
import TestNeutralOutput<TestNeutralInput>
33-
import TestSourceSinkOutput<TestSourceSinkInput>
19+
import TestSummaryOutput<IncludeSummarizedCallable>
20+
import TestNeutralOutput<RelevantNeutralCallable>
21+
import TestSourceSinkOutput<RelevantSourceCallable, RelevantSinkCallable>
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import shared.FlowSummaries
22
private import semmle.code.csharp.dataflow.internal.ExternalFlow
33

4-
module TestSummaryInput implements TestSummaryInputSig {
5-
class RelevantSummarizedCallable = IncludeSummarizedCallable;
6-
}
7-
8-
import TestSummaryOutput<TestSummaryInput>
9-
10-
class IncludeFilteredSummarizedCallable extends RelevantSummarizedCallable {
4+
class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable {
115
/**
126
* Holds if flow is propagated between `input` and `output` and
137
* if there is no summary for a callable in a `base` class or interface
@@ -16,11 +10,13 @@ class IncludeFilteredSummarizedCallable extends RelevantSummarizedCallable {
1610
override predicate relevantSummary(
1711
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
1812
) {
19-
this.(SummarizedCallableImpl).propagatesFlow(input, output, preservesValue) and
13+
this.propagatesFlow(input, output, preservesValue) and
2014
not exists(IncludeSummarizedCallable rsc |
2115
isBaseCallableOrPrototype(rsc) and
2216
rsc.(SummarizedCallableImpl).propagatesFlow(input, output, preservesValue) and
2317
this.(UnboundCallable).overridesOrImplementsUnbound(rsc)
2418
)
2519
}
2620
}
21+
22+
import TestSummaryOutput<IncludeFilteredSummarizedCallable>

csharp/ql/test/shared/FlowSummaries.qll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Private
22
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Public
33
private import semmle.code.csharp.dataflow.internal.ExternalFlow
44

5-
class IncludeSummarizedCallable instanceof SummarizedCallableImpl {
5+
final private class SummarizedCallableImplFinal = SummarizedCallableImpl;
6+
7+
class IncludeSummarizedCallable extends SummarizedCallableImplFinal {
68
IncludeSummarizedCallable() {
79
[this.(Modifiable), this.(Accessor).getDeclaration()].isEffectivelyPublic()
810
}
911

1012
/** Gets a string representing the callable in semi-colon separated format for use in flow summaries. */
1113
final string getCallableCsv() { result = asPartialModel(this) }
1214

15+
predicate relevantSummary(
16+
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
17+
) {
18+
this.propagatesFlow(input, output, preservesValue)
19+
}
20+
1321
string toString() { result = super.toString() }
1422
}

shared/dataflow/codeql/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 27 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,39 +1692,27 @@ module Make<DF::InputSig DataFlowLang, InputSig<DataFlowLang> Input> {
16921692
)
16931693
}
16941694

1695-
signature module TestSourceSinkInputSig {
1696-
/**
1697-
* A class or source elements relevant for testing.
1698-
*/
1699-
class RelevantSourceCallable instanceof SourceOrSinkElement {
1700-
/** Gets the string representation of this callable used by `source/1`. */
1701-
string getCallableCsv();
1702-
}
1703-
1704-
/**
1705-
* A class or sink elements relevant for testing.
1706-
*/
1707-
class RelevantSinkCallable instanceof SourceOrSinkElement {
1708-
/** Gets the string representation of this callable used by `source/1`. */
1709-
string getCallableCsv();
1710-
}
1695+
/** A source or sink relevant for testing. */
1696+
signature class RelevantSourceOrSinkElementSig extends SourceOrSinkElement {
1697+
/** Gets the string representation of this callable used by `source/1` or `sink/1`. */
1698+
string getCallableCsv();
17111699
}
17121700

17131701
/** Provides query predicates for outputting a set of relevant sources and sinks. */
1714-
module TestSourceSinkOutput<TestSourceSinkInputSig TestSourceSinkInput> {
1715-
private import TestSourceSinkInput
1716-
1702+
module TestSourceSinkOutput<
1703+
RelevantSourceOrSinkElementSig RelevantSource, RelevantSourceOrSinkElementSig RelevantSink>
1704+
{
17171705
/**
17181706
* Holds if there exists a relevant source callable with information roughly corresponding to `csv`.
17191707
* Used for testing.
17201708
* The syntax is: "namespace;type;overrides;name;signature;ext;outputspec;kind;provenance",
17211709
* ext is hardcoded to empty.
17221710
*/
17231711
query predicate source(string csv) {
1724-
exists(RelevantSourceCallable c, string output, string kind, Provenance provenance |
1725-
sourceElement(c, output, kind, provenance) and
1712+
exists(RelevantSource s, string output, string kind, Provenance provenance |
1713+
sourceElement(s, output, kind, provenance) and
17261714
csv =
1727-
c.getCallableCsv() // Callable information
1715+
s.getCallableCsv() // Callable information
17281716
+ output + ";" // output
17291717
+ kind + ";" // kind
17301718
+ provenance // provenance
@@ -1738,10 +1726,10 @@ module Make<DF::InputSig DataFlowLang, InputSig<DataFlowLang> Input> {
17381726
* ext is hardcoded to empty.
17391727
*/
17401728
query predicate sink(string csv) {
1741-
exists(RelevantSinkCallable c, string input, string kind, Provenance provenance |
1742-
sinkElement(c, input, kind, provenance) and
1729+
exists(RelevantSink s, string input, string kind, Provenance provenance |
1730+
sinkElement(s, input, kind, provenance) and
17431731
csv =
1744-
c.getCallableCsv() // Callable information
1732+
s.getCallableCsv() // Callable information
17451733
+ input + ";" // input
17461734
+ kind + ";" // kind
17471735
+ provenance // provenance
@@ -1751,35 +1739,18 @@ module Make<DF::InputSig DataFlowLang, InputSig<DataFlowLang> Input> {
17511739
}
17521740
}
17531741

1754-
signature module TestSummaryInputSig {
1755-
/**
1756-
* A class of callables where the flow summary should be included
1757-
* in the `summary/1` query predicate.
1758-
*/
1759-
class RelevantSummarizedCallable instanceof SummarizedCallableImpl {
1760-
/** Gets the string representation of this callable used by `summary/1`. */
1761-
string getCallableCsv();
1762-
}
1742+
/** A summarized callable relevant for testing. */
1743+
signature class RelevantSummarizedCallableSig extends SummarizedCallableImpl {
1744+
/** Gets the string representation of this callable used by `summary/1`. */
1745+
string getCallableCsv();
1746+
1747+
predicate relevantSummary(
1748+
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
1749+
);
17631750
}
17641751

17651752
/** Provides a query predicate for outputting a set of relevant flow summaries. */
1766-
module TestSummaryOutput<TestSummaryInputSig TestInput> {
1767-
private import TestInput
1768-
1769-
final class RelevantSummarizedCallableFinal = TestInput::RelevantSummarizedCallable;
1770-
1771-
class RelevantSummarizedCallable extends RelevantSummarizedCallableFinal instanceof SummarizedCallableImpl
1772-
{
1773-
/** Holds if flow is propagated between `input` and `output`. */
1774-
predicate relevantSummary(
1775-
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
1776-
) {
1777-
super.propagatesFlow(input, output, preservesValue)
1778-
}
1779-
1780-
string toString() { result = super.toString() }
1781-
}
1782-
1753+
module TestSummaryOutput<RelevantSummarizedCallableSig RelevantSummarizedCallable> {
17831754
/** Render the kind in the format used in flow summaries. */
17841755
private string renderKind(boolean preservesValue) {
17851756
preservesValue = true and result = "value"
@@ -1816,31 +1787,13 @@ module Make<DF::InputSig DataFlowLang, InputSig<DataFlowLang> Input> {
18161787
}
18171788
}
18181789

1819-
signature module TestNeutralInputSig {
1820-
/**
1821-
* A class of callables where the neutral model should be included
1822-
* in the `neutral/1` query predicate.
1823-
*/
1824-
class RelevantNeutralCallable instanceof NeutralCallable {
1825-
/** Gets the string representation of this callable used by `neutral/1`. */
1826-
string getCallableCsv();
1827-
}
1790+
/** A summarized callable relevant for testing. */
1791+
signature class RelevantNeutralCallableSig extends NeutralCallable {
1792+
/** Gets the string representation of this callable used by `neutral/1`. */
1793+
string getCallableCsv();
18281794
}
18291795

1830-
module TestNeutralOutput<TestNeutralInputSig TestInput> {
1831-
private import TestInput
1832-
1833-
final class RelevantNeutralCallableFinal = TestInput::RelevantNeutralCallable;
1834-
1835-
class RelevantNeutralCallable extends RelevantNeutralCallableFinal instanceof NeutralCallable {
1836-
/**
1837-
* Gets the kind of the neutral.
1838-
*/
1839-
string getKind() { result = super.getKind() }
1840-
1841-
string toString() { result = super.toString() }
1842-
}
1843-
1796+
module TestNeutralOutput<RelevantNeutralCallableSig RelevantNeutralCallable> {
18441797
private string renderProvenance(NeutralCallable c) {
18451798
exists(Provenance p | p.isManual() and c.hasProvenance(p) and result = p.toString())
18461799
or

0 commit comments

Comments
 (0)