Skip to content

Commit 0d387ef

Browse files
committed
C#: Add static call graph tests
1 parent 774030a commit 0d387ef

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Out of scope:
2+
// - overloading
3+
// - accessibility
4+
// - other callables than methods
5+
6+
namespace N1
7+
{
8+
public class C1
9+
{
10+
public void M1() { }
11+
12+
public void M2(C1 c1)
13+
{
14+
M1(); // $ StaticTarget=N1.C1.M1
15+
this.M1(); // $ StaticTarget=N1.C1.M1
16+
17+
c1.M1(); // $ StaticTarget=N1.C1.M1
18+
19+
C1 c2 = null;
20+
c2.M1(); // $ StaticTarget=N1.C1.M1
21+
22+
var c3 = new C1();
23+
c3.M1(); // $ StaticTarget=N1.C1.M1
24+
25+
M3(). // $ StaticTarget=N1.C1.M3
26+
M1(); // $ StaticTarget=N1.C1.M1
27+
28+
var c4 = M3(); // $ StaticTarget=N1.C1.M3
29+
c4.M1(); // $ StaticTarget=N1.C1.M1
30+
}
31+
32+
public C1 M3() => throw null;
33+
}
34+
}
35+
36+
namespace N2
37+
{
38+
using N1;
39+
40+
public class C1
41+
{
42+
public void M1(C1 c1 = null, N1.C1 c2 = null)
43+
{
44+
c1.M1(); // $ StaticTarget=N2.C1.M1
45+
c2.M1(); // $ StaticTarget=N1.C1.M1
46+
}
47+
}
48+
49+
public class C2 : C1
50+
{
51+
public void M2()
52+
{
53+
M1(); // $ StaticTarget=N2.C1.M1
54+
this.M1(); // $ StaticTarget=N2.C1.M1
55+
base.M1(); // $ StaticTarget=N2.C1.M1
56+
57+
C1 c1 = new C2();
58+
c1.M1(); // $ StaticTarget=N2.C1.M1
59+
}
60+
}
61+
62+
public class C3 : C2
63+
{
64+
public new void M2()
65+
{
66+
M2(); // $ StaticTarget=N2.C3.M2
67+
C3 c3 = null;
68+
c3.M2(); // $ StaticTarget=N2.C3.M2
69+
C2 c2 = c3;
70+
c2.M2(); // $ StaticTarget=N2.C2.M2
71+
}
72+
}
73+
}
74+
75+
namespace N3
76+
{
77+
using N1;
78+
79+
public class C2
80+
{
81+
C1 Field1;
82+
N1.C1 Field2;
83+
C2 Field3;
84+
85+
C1 Prop1 => null;
86+
N1.C1 Prop2 => null;
87+
C2 Prop3 => null;
88+
89+
public void M1(C1 c1, N1.C1 c2)
90+
{
91+
c1.M1(); // $ StaticTarget=N1.C1.M1
92+
c2.M1(); // $ StaticTarget=N1.C1.M1
93+
94+
Field1.M1(); // $ StaticTarget=N1.C1.M1
95+
Field2.M1(); // $ StaticTarget=N1.C1.M1
96+
Field3.M1(null, null); // $ StaticTarget=N3.C2.M1
97+
{
98+
C2 Field1 = null; // shadows outer field
99+
Field1.M1(null, null); // $ StaticTarget=N3.C2.M1
100+
this.Field1.M1(); // $ StaticTarget=N1.C1.M1
101+
}
102+
103+
Prop1.M1(); // $ StaticTarget=N1.C1.M1
104+
Prop2.M1(); // $ StaticTarget=N1.C1.M1
105+
Prop3.M1(null, null); // $ StaticTarget=N3.C2.M1
106+
{
107+
N2.C2 Prop1 = null; // shadows outer property
108+
Prop1.M1(); // $ StaticTarget=N2.C1.M1
109+
this.Prop1.M1(); // $ StaticTarget=N1.C1.M1
110+
}
111+
}
112+
}
113+
}
114+
115+
namespace N4
116+
{
117+
public class C1
118+
{
119+
public virtual void M1()
120+
{
121+
this.M1(); // $ StaticTarget=N4.C1.M1
122+
}
123+
}
124+
125+
public class C2 : C1
126+
{
127+
public override void M1()
128+
{
129+
this.M1(); // $ StaticTarget=N4.C2.M1
130+
base.M1(); // $ StaticTarget=N4.C1.M1
131+
}
132+
}
133+
}
134+
135+
namespace N5
136+
{
137+
public class C1 : N4.C1
138+
{
139+
public void M2()
140+
{
141+
this.M1(); // $ StaticTarget=N4.C1.M1
142+
}
143+
}
144+
145+
public class C2 : N4.C1
146+
{
147+
public override void M1()
148+
{
149+
this.M1(); // $ StaticTarget=N5.C2.M1
150+
base.M1(); // $ StaticTarget=N4.C1.M1
151+
}
152+
}
153+
}
154+
155+
namespace N6
156+
{
157+
using alias = N5;
158+
159+
public class C1
160+
{
161+
public void M1(alias::C2 c2)
162+
{
163+
c2.M1(); // $ StaticTarget=N5.C2.M1
164+
}
165+
}
166+
}

csharp/ql/test/library-tests/call-graph/CallGraph.expected

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import csharp
2+
import TestUtilities.InlineExpectationsTest
3+
4+
class CallGraphTest extends InlineExpectationsTest {
5+
CallGraphTest() { this = "CallGraphTest" }
6+
7+
override string getARelevantTag() { result = "StaticTarget" }
8+
9+
override predicate hasActualResult(Location location, string element, string tag, string value) {
10+
exists(MethodCall mc |
11+
element = mc.toString() and
12+
tag = "StaticTarget" and
13+
value = mc.getTarget().getQualifiedName() and
14+
mc.getLocation() = location
15+
)
16+
}
17+
}

0 commit comments

Comments
 (0)