Skip to content

Commit 238a70f

Browse files
committed
C#: Add library support for checked operators.
1 parent b7123aa commit 238a70f

File tree

1 file changed

+133
-8
lines changed

1 file changed

+133
-8
lines changed

csharp/ql/lib/semmle/code/csharp/Callable.qll

Lines changed: 133 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,11 @@ class RecordCloneMethod extends Method, DotNet::RecordCloneCallable {
485485
* A user-defined unary operator - an operator taking one operand.
486486
*
487487
* Either a plus operator (`PlusOperator`), minus operator (`MinusOperator`),
488-
* not operator (`NotOperator`), complement operator (`ComplementOperator`),
489-
* true operator (`TrueOperator`), false operator (`FalseOperator`),
490-
* increment operator (`IncrementOperator`), or decrement operator
491-
* (`DecrementOperator`).
488+
* checked minus operator (`CheckedMinusOperator`), not operator (`NotOperator`),
489+
* complement operator (`ComplementOperator`), true operator (`TrueOperator`),
490+
* false operator (`FalseOperator`), increment operator (`IncrementOperator`),
491+
* checked increment operator (`CheckedIncrementOperator`), decrement operator
492+
* (`DecrementOperator`) or checked decrement operator (`CheckedDecrementOperator`).
492493
*/
493494
class UnaryOperator extends Operator {
494495
UnaryOperator() {
@@ -527,6 +528,21 @@ class MinusOperator extends UnaryOperator {
527528
override string getAPrimaryQlClass() { result = "MinusOperator" }
528529
}
529530

531+
/**
532+
* A user-defined checked minus operator (`-`), for example
533+
*
534+
* ```csharp
535+
* public static Widget operator checked -(Widget w) {
536+
* ...
537+
* }
538+
* ```
539+
*/
540+
class CheckedMinusOperator extends UnaryOperator {
541+
CheckedMinusOperator() { this.getName() = "checked -" }
542+
543+
override string getAPrimaryQlClass() { result = "CheckedMinusOperator" }
544+
}
545+
530546
/**
531547
* A user-defined not operator (`!`), for example
532548
*
@@ -572,6 +588,21 @@ class IncrementOperator extends UnaryOperator {
572588
override string getAPrimaryQlClass() { result = "IncrementOperator" }
573589
}
574590

591+
/**
592+
* A user-defined checked increment operator (`++`), for example
593+
*
594+
* ```csharp
595+
* public static Widget operator checked ++(Widget w) {
596+
* ...
597+
* }
598+
* ```
599+
*/
600+
class CheckedIncrementOperator extends UnaryOperator {
601+
CheckedIncrementOperator() { this.getName() = "checked ++" }
602+
603+
override string getAPrimaryQlClass() { result = "CheckedIncrementOperator" }
604+
}
605+
575606
/**
576607
* A user-defined decrement operator (`--`), for example
577608
*
@@ -587,6 +618,21 @@ class DecrementOperator extends UnaryOperator {
587618
override string getAPrimaryQlClass() { result = "DecrementOperator" }
588619
}
589620

621+
/**
622+
* A user-defined checked decrement operator (`--`), for example
623+
*
624+
* ```csharp
625+
* public static Widget operator checked --(Widget w) {
626+
* ...
627+
* }
628+
* ```
629+
*/
630+
class CheckedDecrementOperator extends UnaryOperator {
631+
CheckedDecrementOperator() { this.getName() = "checked --" }
632+
633+
override string getAPrimaryQlClass() { result = "CheckedDecrementOperator" }
634+
}
635+
590636
/**
591637
* A user-defined false operator (`false`), for example
592638
*
@@ -620,9 +666,12 @@ class TrueOperator extends UnaryOperator {
620666
/**
621667
* A user-defined binary operator.
622668
*
623-
* Either an addition operator (`AddOperator`), a subtraction operator
624-
* (`SubOperator`), a multiplication operator (`MulOperator`), a division
625-
* operator (`DivOperator`), a remainder operator (`RemOperator`), an and
669+
* Either an addition operator (`AddOperator`), a checked addition operator
670+
* (`CheckedAddOperator`) a subtraction operator (`SubOperator`), a checked
671+
* substraction operator (`CheckedSubOperator`), a multiplication operator
672+
* (`MulOperator`), a checked multiplication operator (`CheckedMulOperator`),
673+
* a division operator (`DivOperator`), a checked division operator
674+
* (`CheckedDivOperator`), a remainder operator (`RemOperator`), an and
626675
* operator (`AndOperator`), an or operator (`OrOperator`), an xor
627676
* operator (`XorOperator`), a left shift operator (`LeftShiftOperator`),
628677
* a right shift operator (`RightShiftOperator`), an unsigned right shift
@@ -650,6 +699,21 @@ class AddOperator extends BinaryOperator {
650699
override string getAPrimaryQlClass() { result = "AddOperator" }
651700
}
652701

702+
/**
703+
* A user-defined checked addition operator (`+`), for example
704+
*
705+
* ```csharp
706+
* public static Widget operator checked +(Widget lhs, Widget rhs) {
707+
* ...
708+
* }
709+
* ```
710+
*/
711+
class CheckedAddOperator extends BinaryOperator {
712+
CheckedAddOperator() { this.getName() = "checked +" }
713+
714+
override string getAPrimaryQlClass() { result = "CheckedAddOperator" }
715+
}
716+
653717
/**
654718
* A user-defined subtraction operator (`-`), for example
655719
*
@@ -665,6 +729,21 @@ class SubOperator extends BinaryOperator {
665729
override string getAPrimaryQlClass() { result = "SubOperator" }
666730
}
667731

732+
/**
733+
* A user-defined checked subtraction operator (`-`), for example
734+
*
735+
* ```csharp
736+
* public static Widget operator checked -(Widget lhs, Widget rhs) {
737+
* ...
738+
* }
739+
* ```
740+
*/
741+
class CheckedSubOperator extends BinaryOperator {
742+
CheckedSubOperator() { this.getName() = "checked -" }
743+
744+
override string getAPrimaryQlClass() { result = "CheckedSubOperator" }
745+
}
746+
668747
/**
669748
* A user-defined multiplication operator (`*`), for example
670749
*
@@ -680,6 +759,21 @@ class MulOperator extends BinaryOperator {
680759
override string getAPrimaryQlClass() { result = "MulOperator" }
681760
}
682761

762+
/**
763+
* A user-defined checked multiplication operator (`*`), for example
764+
*
765+
* ```csharp
766+
* public static Widget operator checked *(Widget lhs, Widget rhs) {
767+
* ...
768+
* }
769+
* ```
770+
*/
771+
class CheckedMulOperator extends BinaryOperator {
772+
CheckedMulOperator() { this.getName() = "checked *" }
773+
774+
override string getAPrimaryQlClass() { result = "CheckedMulOperator" }
775+
}
776+
683777
/**
684778
* A user-defined division operator (`/`), for example
685779
*
@@ -695,6 +789,21 @@ class DivOperator extends BinaryOperator {
695789
override string getAPrimaryQlClass() { result = "DivOperator" }
696790
}
697791

792+
/**
793+
* A user-defined checked division operator (`/`), for example
794+
*
795+
* ```csharp
796+
* public static Widget operator checked /(Widget lhs, Widget rhs) {
797+
* ...
798+
* }
799+
* ```
800+
*/
801+
class CheckedDivOperator extends BinaryOperator {
802+
CheckedDivOperator() { this.getName() = "checked /" }
803+
804+
override string getAPrimaryQlClass() { result = "CheckedDivOperator" }
805+
}
806+
698807
/**
699808
* A user-defined remainder operator (`%`), for example
700809
*
@@ -908,7 +1017,8 @@ class GEOperator extends BinaryOperator {
9081017
class ConversionOperator extends Operator {
9091018
ConversionOperator() {
9101019
this.getName() = "implicit conversion" or
911-
this.getName() = "explicit conversion"
1020+
this.getName() = "explicit conversion" or
1021+
this.getName() = "checked explicit conversion"
9121022
}
9131023

9141024
/** Gets the source type of the conversion. */
@@ -948,6 +1058,21 @@ class ExplicitConversionOperator extends ConversionOperator {
9481058
override string getAPrimaryQlClass() { result = "ExplicitConversionOperator" }
9491059
}
9501060

1061+
/**
1062+
* A user-defined checked explicit conversion operator, for example
1063+
*
1064+
* ```csharp
1065+
* public static explicit operator checked int(BigInteger i) {
1066+
* ...
1067+
* }
1068+
* ```
1069+
*/
1070+
class CheckedExplicitConversionOperator extends ConversionOperator {
1071+
CheckedExplicitConversionOperator() { this.getName() = "checked explicit conversion" }
1072+
1073+
override string getAPrimaryQlClass() { result = "CheckedExplicitConversionOperator" }
1074+
}
1075+
9511076
/**
9521077
* A local function, defined within the scope of another callable.
9531078
* For example, `Fac` on lines 2--4 in

0 commit comments

Comments
 (0)