Skip to content

Commit 54b4513

Browse files
committed
C#: Add example code including tests.
1 parent c00b089 commit 54b4513

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

csharp/ql/test/library-tests/csharp11/PrintAst.expected

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,87 @@ PatternMatchSpan.cs:
343343
# 16| 2: [DefaultCase] default:
344344
# 16| 3: [BlockStmt] {...}
345345
# 16| 0: [BreakStmt] break;
346+
Scoped.cs:
347+
# 1| [Struct] S1
348+
# 2| [Struct] S2
349+
# 7| [Class] ScopedModifierTest
350+
# 9| 5: [Method] M1
351+
# 9| -1: [TypeMention] int
352+
#-----| 2: (Parameters)
353+
# 9| 0: [Parameter] x1
354+
# 9| -1: [TypeMention] int
355+
# 9| 1: [Parameter] y1
356+
# 9| -1: [TypeMention] int
357+
# 10| 4: [BlockStmt] {...}
358+
# 13| 0: [ReturnStmt] return ...;
359+
# 13| 0: [RefExpr] ref ...
360+
# 13| 0: [ParameterAccess] access to parameter y1
361+
# 16| 6: [Method] M2
362+
# 16| -1: [TypeMention] int
363+
#-----| 2: (Parameters)
364+
# 16| 0: [Parameter] x2
365+
# 16| -1: [TypeMention] int
366+
# 16| 1: [Parameter] y2
367+
# 16| -1: [TypeMention] int
368+
# 17| 4: [BlockStmt] {...}
369+
# 18| 0: [ExprStmt] ...;
370+
# 18| 0: [AssignExpr] ... = ...
371+
# 18| 0: [ParameterAccess] access to parameter x2
372+
# 18| 1: [IntLiteral] 0
373+
# 21| 1: [ReturnStmt] return ...;
374+
# 21| 0: [RefExpr] ref ...
375+
# 21| 0: [ParameterAccess] access to parameter y2
376+
# 24| 7: [Method] M3
377+
# 24| -1: [TypeMention] int
378+
#-----| 2: (Parameters)
379+
# 24| 0: [Parameter] x3
380+
# 24| -1: [TypeMention] int
381+
# 25| 4: [BlockStmt] {...}
382+
# 27| 0: [ReturnStmt] return ...;
383+
# 27| 0: [ParameterAccess] access to parameter x3
384+
# 30| 8: [Method] M4
385+
# 30| -1: [TypeMention] S1
386+
#-----| 2: (Parameters)
387+
# 30| 0: [Parameter] x4
388+
# 30| -1: [TypeMention] S1
389+
# 31| 4: [BlockStmt] {...}
390+
# 33| 0: [ReturnStmt] return ...;
391+
# 33| 0: [ParameterAccess] access to parameter x4
392+
# 36| 9: [Method] M5
393+
# 36| -1: [TypeMention] S2
394+
#-----| 2: (Parameters)
395+
# 36| 0: [Parameter] x5
396+
# 36| -1: [TypeMention] S2
397+
# 37| 4: [BlockStmt] {...}
398+
# 40| 0: [ReturnStmt] return ...;
399+
# 40| 0: [ObjectCreation] object creation of type S2
400+
# 40| 0: [TypeMention] S2
401+
# 43| 10: [Method] M6
402+
# 43| -1: [TypeMention] S2
403+
#-----| 2: (Parameters)
404+
# 43| 0: [Parameter] x6
405+
# 43| -1: [TypeMention] S2
406+
# 44| 4: [BlockStmt] {...}
407+
# 47| 0: [ReturnStmt] return ...;
408+
# 47| 0: [ObjectCreation] object creation of type S2
409+
# 47| 0: [TypeMention] S2
410+
# 50| 11: [Method] Locals
411+
# 50| -1: [TypeMention] S2
412+
# 51| 4: [BlockStmt] {...}
413+
# 52| 0: [LocalVariableDeclStmt] ... ...;
414+
# 52| 0: [LocalVariableDeclAndInitExpr] S2 x7 = ...
415+
# 52| -1: [TypeMention] null
416+
# 52| 0: [LocalVariableAccess] access to local variable x7
417+
# 52| 1: [ObjectCreation] object creation of type S2
418+
# 52| 0: [TypeMention] S2
419+
# 55| 1: [LocalVariableDeclStmt] ... ...;
420+
# 55| 0: [LocalVariableDeclAndInitExpr] S2 y7 = ...
421+
# 55| -1: [TypeMention] S2
422+
# 55| 0: [LocalVariableAccess] access to local variable y7
423+
# 55| 1: [ObjectCreation] object creation of type S2
424+
# 55| 0: [TypeMention] S2
425+
# 56| 2: [ReturnStmt] return ...;
426+
# 56| 0: [LocalVariableAccess] access to local variable y7
346427
SignAnalysis.cs:
347428
# 1| [Class] MySignAnalysis
348429
# 4| 5: [Method] UnsignedRightShiftSign
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
public struct S1 { }
2+
public ref struct S2 { }
3+
4+
// The `scoped` modifier can be applied to parameters
5+
// or local variables. The type of the parameter or
6+
// local variable must be either a `ref` value or `ref struct` value.
7+
public class ScopedModifierTest
8+
{
9+
public ref int M1(scoped ref int x1, ref int y1)
10+
{
11+
// Not allowed.
12+
// return ref x1;
13+
return ref y1;
14+
}
15+
16+
public ref int M2(scoped out int x2, ref int y2)
17+
{
18+
x2 = 0;
19+
// Not allowed.
20+
// return ref x;
21+
return ref y2;
22+
}
23+
24+
public int M3(scoped ref int x3)
25+
{
26+
// Allowed is it is not returned by reference.
27+
return x3;
28+
}
29+
30+
public S1 M4(scoped ref S1 x4)
31+
{
32+
// Allowed as it is not returned by reference.
33+
return x4;
34+
}
35+
36+
public S2 M5(scoped S2 x5)
37+
{
38+
// Not allowed.
39+
// return x5;
40+
return new S2();
41+
}
42+
43+
public S2 M6(scoped ref S2 x6)
44+
{
45+
// Not allowed.
46+
// return x6;
47+
return new S2();
48+
}
49+
50+
public S2 Locals()
51+
{
52+
scoped S2 x7 = new S2();
53+
// Not allowed.
54+
// return x7;
55+
S2 y7 = new S2();
56+
return y7;
57+
}
58+
59+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| Scoped.cs:9:38:9:39 | x1 | Parameter |
2+
| Scoped.cs:16:38:16:39 | x2 | Parameter |
3+
| Scoped.cs:24:34:24:35 | x3 | Parameter |
4+
| Scoped.cs:30:32:30:33 | x4 | Parameter |
5+
| Scoped.cs:36:28:36:29 | x5 | Parameter |
6+
| Scoped.cs:43:32:43:33 | x6 | Parameter |
7+
| Scoped.cs:52:19:52:20 | x7 | LocalVariable |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import csharp
2+
3+
from LocalScopeVariable v
4+
where v.getFile().getStem() = "Scoped" and v.isScoped()
5+
select v, v.getAPrimaryQlClass()

0 commit comments

Comments
 (0)