Skip to content

Commit 34c6b24

Browse files
authored
Merge pull request github#12147 from michaelnebel/csharp/relaxedshift
C# 11: Test of relaxed shift operator requirements.
2 parents 457a2bb + db41463 commit 34c6b24

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,102 @@ PatternMatchSpan.cs:
343343
# 16| 2: [DefaultCase] default:
344344
# 16| 3: [BlockStmt] {...}
345345
# 16| 0: [BreakStmt] break;
346+
RelaxedShift.cs:
347+
# 1| [Interface] IShiftOperators<,,>
348+
#-----| 1: (Type parameters)
349+
# 1| 0: [TypeParameter] TSelf
350+
# 1| 1: [TypeParameter] TOther
351+
# 1| 2: [TypeParameter] TReturn
352+
# 3| 4: [LeftShiftOperator] <<
353+
# 3| -1: [TypeMention] TReturn
354+
#-----| 2: (Parameters)
355+
# 3| 0: [Parameter] value
356+
# 3| -1: [TypeMention] TSelf
357+
# 3| 1: [Parameter] shiftAmount
358+
# 3| -1: [TypeMention] TOther
359+
# 5| 5: [RightShiftOperator] >>
360+
# 5| -1: [TypeMention] TReturn
361+
#-----| 2: (Parameters)
362+
# 5| 0: [Parameter] value
363+
# 5| -1: [TypeMention] TSelf
364+
# 5| 1: [Parameter] shiftAmount
365+
# 5| -1: [TypeMention] TOther
366+
# 7| 6: [UnsignedRightShiftOperator] >>>
367+
# 7| -1: [TypeMention] TReturn
368+
#-----| 2: (Parameters)
369+
# 7| 0: [Parameter] value
370+
# 7| -1: [TypeMention] TSelf
371+
# 7| 1: [Parameter] shiftAmount
372+
# 7| -1: [TypeMention] TOther
373+
# 10| [Class] Number
374+
#-----| 3: (Base types)
375+
# 12| 5: [LeftShiftOperator] <<
376+
# 12| -1: [TypeMention] Number
377+
#-----| 2: (Parameters)
378+
# 12| 0: [Parameter] value
379+
# 12| -1: [TypeMention] Number
380+
# 12| 1: [Parameter] shiftAmount
381+
# 12| -1: [TypeMention] string
382+
# 12| 4: [ParameterAccess] access to parameter value
383+
# 14| 6: [RightShiftOperator] >>
384+
# 14| -1: [TypeMention] Number
385+
#-----| 2: (Parameters)
386+
# 14| 0: [Parameter] value
387+
# 14| -1: [TypeMention] Number
388+
# 14| 1: [Parameter] shiftAmount
389+
# 14| -1: [TypeMention] string
390+
# 14| 4: [ParameterAccess] access to parameter value
391+
# 16| 7: [UnsignedRightShiftOperator] >>>
392+
# 16| -1: [TypeMention] Number
393+
#-----| 2: (Parameters)
394+
# 16| 0: [Parameter] value
395+
# 16| -1: [TypeMention] Number
396+
# 16| 1: [Parameter] shiftAmount
397+
# 16| -1: [TypeMention] string
398+
# 16| 4: [ParameterAccess] access to parameter value
399+
# 19| [Class] TestRelaxedShift
400+
# 21| 5: [Method] M1
401+
# 21| -1: [TypeMention] Void
402+
# 22| 4: [BlockStmt] {...}
403+
# 23| 0: [LocalVariableDeclStmt] ... ...;
404+
# 23| 0: [LocalVariableDeclAndInitExpr] Number n11 = ...
405+
# 23| -1: [TypeMention] Number
406+
# 23| 0: [LocalVariableAccess] access to local variable n11
407+
# 23| 1: [ObjectCreation] object creation of type Number
408+
# 23| 0: [TypeMention] Number
409+
# 24| 1: [LocalVariableDeclStmt] ... ...;
410+
# 24| 0: [LocalVariableDeclAndInitExpr] Number n12 = ...
411+
# 24| -1: [TypeMention] Number
412+
# 24| 0: [LocalVariableAccess] access to local variable n12
413+
# 24| 1: [OperatorCall] call to operator <<
414+
# 24| 0: [LocalVariableAccess] access to local variable n11
415+
# 24| 1: [StringLiteralUtf16] "1"
416+
# 26| 2: [LocalVariableDeclStmt] ... ...;
417+
# 26| 0: [LocalVariableDeclAndInitExpr] Number n21 = ...
418+
# 26| -1: [TypeMention] Number
419+
# 26| 0: [LocalVariableAccess] access to local variable n21
420+
# 26| 1: [ObjectCreation] object creation of type Number
421+
# 26| 0: [TypeMention] Number
422+
# 27| 3: [LocalVariableDeclStmt] ... ...;
423+
# 27| 0: [LocalVariableDeclAndInitExpr] Number n22 = ...
424+
# 27| -1: [TypeMention] Number
425+
# 27| 0: [LocalVariableAccess] access to local variable n22
426+
# 27| 1: [OperatorCall] call to operator >>
427+
# 27| 0: [LocalVariableAccess] access to local variable n21
428+
# 27| 1: [StringLiteralUtf16] "2"
429+
# 29| 4: [LocalVariableDeclStmt] ... ...;
430+
# 29| 0: [LocalVariableDeclAndInitExpr] Number n31 = ...
431+
# 29| -1: [TypeMention] Number
432+
# 29| 0: [LocalVariableAccess] access to local variable n31
433+
# 29| 1: [ObjectCreation] object creation of type Number
434+
# 29| 0: [TypeMention] Number
435+
# 30| 5: [LocalVariableDeclStmt] ... ...;
436+
# 30| 0: [LocalVariableDeclAndInitExpr] Number n32 = ...
437+
# 30| -1: [TypeMention] Number
438+
# 30| 0: [LocalVariableAccess] access to local variable n32
439+
# 30| 1: [OperatorCall] call to operator >>>
440+
# 30| 0: [LocalVariableAccess] access to local variable n31
441+
# 30| 1: [StringLiteralUtf16] "3"
346442
Scoped.cs:
347443
# 1| [Struct] S1
348444
# 2| [Struct] S2
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public interface IShiftOperators<TSelf, TOther, TReturn> where TSelf : IShiftOperators<TSelf, TOther, TReturn>
2+
{
3+
public static abstract TReturn operator <<(TSelf value, TOther shiftAmount);
4+
5+
public static abstract TReturn operator >>(TSelf value, TOther shiftAmount);
6+
7+
public static abstract TReturn operator >>>(TSelf value, TOther shiftAmount);
8+
}
9+
10+
public class Number : IShiftOperators<Number, string, Number>
11+
{
12+
public static Number operator <<(Number value, string shiftAmount) => value;
13+
14+
public static Number operator >>(Number value, string shiftAmount) => value;
15+
16+
public static Number operator >>>(Number value, string shiftAmount) => value;
17+
}
18+
19+
public class TestRelaxedShift
20+
{
21+
public void M1()
22+
{
23+
var n11 = new Number();
24+
var n12 = n11 << "1";
25+
26+
var n21 = new Number();
27+
var n22 = n21 >> "2";
28+
29+
var n31 = new Number();
30+
var n32 = n31 >>> "3";
31+
}
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
userdefinedoperators
2+
| RelaxedShift.cs:3:45:3:46 | << | LeftShiftOperator | RelaxedShift.cs:1:18:1:56 | IShiftOperators<Number,String,Number> |
3+
| RelaxedShift.cs:5:45:5:46 | >> | RightShiftOperator | RelaxedShift.cs:1:18:1:56 | IShiftOperators<Number,String,Number> |
4+
| RelaxedShift.cs:7:45:7:47 | >>> | UnsignedRightShiftOperator | RelaxedShift.cs:1:18:1:56 | IShiftOperators<Number,String,Number> |
5+
binaryoperatorcalls
6+
| RelaxedShift.cs:24:19:24:28 | call to operator << | RelaxedShift.cs:12:35:12:36 | << | RelaxedShift.cs:24:19:24:21 | access to local variable n11 | RelaxedShift.cs:24:26:24:28 | "1" |
7+
| RelaxedShift.cs:27:19:27:28 | call to operator >> | RelaxedShift.cs:14:35:14:36 | >> | RelaxedShift.cs:27:19:27:21 | access to local variable n21 | RelaxedShift.cs:27:26:27:28 | "2" |
8+
| RelaxedShift.cs:30:19:30:29 | call to operator >>> | RelaxedShift.cs:16:35:16:37 | >>> | RelaxedShift.cs:30:19:30:21 | access to local variable n31 | RelaxedShift.cs:30:27:30:29 | "3" |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import csharp
2+
3+
query predicate userdefinedoperators(BinaryOperator op, string qlclass, Type t) {
4+
op.getFile().getStem() = "RelaxedShift" and
5+
qlclass = op.getAPrimaryQlClass() and
6+
t = op.getDeclaringType() and
7+
op != op.getUnboundDeclaration()
8+
}
9+
10+
query predicate binaryoperatorcalls(OperatorCall oc, BinaryOperator o, Expr left, Expr right) {
11+
oc.getFile().getStem() = "RelaxedShift" and
12+
o = oc.getTarget() and
13+
left = oc.getArgument(0) and
14+
right = oc.getArgument(1)
15+
}

0 commit comments

Comments
 (0)