Skip to content

Commit 5470de0

Browse files
committed
C#: Add QL implementation for the new operators.
1 parent 517a8cb commit 5470de0

File tree

1 file changed

+253
-2
lines changed

1 file changed

+253
-2
lines changed

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

Lines changed: 253 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ class Destructor extends Callable, Member, Attributable, @destructor {
496496
* A user-defined operator.
497497
*
498498
* Either a unary operator (`UnaryOperator`), a binary operator
499-
* (`BinaryOperator`), or a conversion operator (`ConversionOperator`).
499+
* (`BinaryOperator`), a conversion operator (`ConversionOperator`), or
500+
* a (`CompoundAssignmentOperator`).
500501
*/
501502
class Operator extends Callable, Member, Attributable, Overridable, @operator {
502503
override string getName() { operators(this, _, result, _, _, _) }
@@ -584,7 +585,8 @@ class ExtensionOperator extends ExtensionCallableImpl, Operator {
584585
class UnaryOperator extends Operator {
585586
UnaryOperator() {
586587
this.getNumberOfParameters() = 1 and
587-
not this instanceof ConversionOperator
588+
not this instanceof ConversionOperator and
589+
not this instanceof CompoundAssignmentOperator
588590
}
589591
}
590592

@@ -1157,6 +1159,255 @@ class CheckedExplicitConversionOperator extends ConversionOperator {
11571159
override string getAPrimaryQlClass() { result = "CheckedExplicitConversionOperator" }
11581160
}
11591161

1162+
/**
1163+
* A user-defined compound assignment operator.
1164+
*
1165+
* Either an addition operator (`AddCompoundAssignmentOperator`), a checked addition operator
1166+
* (`CheckedAddCompoundAssignmentOperator`) a subtraction operator (`SubCompoundAssignmentOperator`), a checked
1167+
* subtraction operator (`CheckedSubCompoundAssignmentOperator`), a multiplication operator
1168+
* (`MulCompoundAssignmentOperator`), a checked multiplication operator (`CheckedMulCompoundAssignmentOperator`),
1169+
* a division operator (`DivCompoundAssignmentOperator`), a checked division operator
1170+
* (`CheckedDivCompoundAssignmentOperator`), a remainder operator (`RemCompoundAssignmentOperator`), an and
1171+
* operator (`AndCompoundAssignmentOperator`), an or operator (`OrCompoundAssignmentOperator`), an xor
1172+
* operator (`XorCompoundAssignmentOperator`), a left shift operator (`LeftShiftCompoundAssignmentOperator`),
1173+
* a right shift operator (`RightShiftCompoundAssignmentOperator`), or an unsigned right shift
1174+
* operator(`UnsignedRightShiftCompoundAssignmentOperator`).
1175+
*/
1176+
class CompoundAssignmentOperator extends Operator {
1177+
CompoundAssignmentOperator() {
1178+
this.getNumberOfParameters() = 1 and
1179+
not this.isStatic() and
1180+
this.getName().matches("%=")
1181+
}
1182+
1183+
override string getAPrimaryQlClass() { result = "CompoundAssignmentOperator" }
1184+
}
1185+
1186+
/**
1187+
* A user-defined compound assignment addition operator (`+=`), for example
1188+
*
1189+
* ```csharp
1190+
* public void operator checked +=(Widget w) {
1191+
* ...
1192+
* }
1193+
* ```
1194+
*/
1195+
class AddCompoundAssignmentOperator extends CompoundAssignmentOperator {
1196+
AddCompoundAssignmentOperator() { this.getName() = "+=" }
1197+
1198+
override string getAPrimaryQlClass() { result = "AddCompoundAssignmentOperator" }
1199+
}
1200+
1201+
/**
1202+
* A user-defined checked compound assignment addition operator (`checked +=`), for example
1203+
*
1204+
* ```csharp
1205+
* public void operator checked +=(Widget w) {
1206+
* ...
1207+
* }
1208+
* ```
1209+
*/
1210+
class CheckedAddCompoundAssignmentOperator extends CompoundAssignmentOperator {
1211+
CheckedAddCompoundAssignmentOperator() { this.getName() = "checked +=" }
1212+
1213+
override string getAPrimaryQlClass() { result = "CheckedAddCompoundAssignmentOperator" }
1214+
}
1215+
1216+
/**
1217+
* A user-defined compound assignment subtraction operator (`-=`), for example
1218+
*
1219+
* ```csharp
1220+
* public void operator -=(Widget w) {
1221+
* ...
1222+
* }
1223+
* ```
1224+
*/
1225+
class SubCompoundAssignmentOperator extends CompoundAssignmentOperator {
1226+
SubCompoundAssignmentOperator() { this.getName() = "-=" }
1227+
1228+
override string getAPrimaryQlClass() { result = "SubCompoundAssignmentOperator" }
1229+
}
1230+
1231+
/**
1232+
* A user-defined checked compound assignment subtraction operator (`checked -=`), for example
1233+
*
1234+
* ```csharp
1235+
* public void operator checked -=(Widget w) {
1236+
* ...
1237+
* }
1238+
* ```
1239+
*/
1240+
class CheckedSubCompoundAssignmentOperator extends CompoundAssignmentOperator {
1241+
CheckedSubCompoundAssignmentOperator() { this.getName() = "checked -=" }
1242+
1243+
override string getAPrimaryQlClass() { result = "CheckedSubCompoundAssignmentOperator" }
1244+
}
1245+
1246+
/**
1247+
* A user-defined compound assignment multiplication operator (`*=`), for example
1248+
*
1249+
* ```csharp
1250+
* public void operator *=(Widget w) {
1251+
* ...
1252+
* }
1253+
* ```
1254+
*/
1255+
class MulCompoundAssignmentOperator extends CompoundAssignmentOperator {
1256+
MulCompoundAssignmentOperator() { this.getName() = "*=" }
1257+
1258+
override string getAPrimaryQlClass() { result = "MulCompoundAssignmentOperator" }
1259+
}
1260+
1261+
/**
1262+
* A user-defined checked compound assignment multiplication operator (`checked *=`), for example
1263+
*
1264+
* ```csharp
1265+
* public void operator checked *=(Widget w) {
1266+
* ...
1267+
* }
1268+
* ```
1269+
*/
1270+
class CheckedMulCompoundAssignmentOperator extends CompoundAssignmentOperator {
1271+
CheckedMulCompoundAssignmentOperator() { this.getName() = "checked *=" }
1272+
1273+
override string getAPrimaryQlClass() { result = "CheckedMulCompoundAssignmentOperator" }
1274+
}
1275+
1276+
/**
1277+
* A user-defined compound assignment division operator (`/=`), for example
1278+
*
1279+
* ```csharp
1280+
* public void operator /=(Widget w) {
1281+
* ...
1282+
* }
1283+
* ```
1284+
*/
1285+
class DivCompoundAssignmentOperator extends CompoundAssignmentOperator {
1286+
DivCompoundAssignmentOperator() { this.getName() = "/=" }
1287+
1288+
override string getAPrimaryQlClass() { result = "DivCompoundAssignmentOperator" }
1289+
}
1290+
1291+
/**
1292+
* A user-defined checked compound assignment division operator (`checked /=`), for example
1293+
*
1294+
* ```csharp
1295+
* public void operator checked /=(Widget w) {
1296+
* ...
1297+
* }
1298+
* ```
1299+
*/
1300+
class CheckedDivCompoundAssignmentOperator extends CompoundAssignmentOperator {
1301+
CheckedDivCompoundAssignmentOperator() { this.getName() = "checked /=" }
1302+
1303+
override string getAPrimaryQlClass() { result = "CheckedDivCompoundAssignmentOperator" }
1304+
}
1305+
1306+
/**
1307+
* A user-defined compound assignment remainder operator (`%=`), for example
1308+
*
1309+
* ```csharp
1310+
* public void operator %=(Widget w) {
1311+
* ...
1312+
* }
1313+
* ```
1314+
*/
1315+
class RemCompoundAssignmentOperator extends CompoundAssignmentOperator {
1316+
RemCompoundAssignmentOperator() { this.getName() = "%=" }
1317+
1318+
override string getAPrimaryQlClass() { result = "RemCompoundAssignmentOperator" }
1319+
}
1320+
1321+
/**
1322+
* A user-defined compound assignment and operator (`&=`), for example
1323+
*
1324+
* ```csharp
1325+
* public void operator &=(Widget w) {
1326+
* ...
1327+
* }
1328+
* ```
1329+
*/
1330+
class AndCompoundAssignmentOperator extends CompoundAssignmentOperator {
1331+
AndCompoundAssignmentOperator() { this.getName() = "&=" }
1332+
1333+
override string getAPrimaryQlClass() { result = "AndCompoundAssignmentOperator" }
1334+
}
1335+
1336+
/**
1337+
* A user-defined compound assignment or operator (`|=`), for example
1338+
*
1339+
* ```csharp
1340+
* public void operator |=(Widget w) {
1341+
* ...
1342+
* }
1343+
* ```
1344+
*/
1345+
class OrCompoundAssignmentOperator extends CompoundAssignmentOperator {
1346+
OrCompoundAssignmentOperator() { this.getName() = "|=" }
1347+
1348+
override string getAPrimaryQlClass() { result = "OrCompoundAssignmentOperator" }
1349+
}
1350+
1351+
/**
1352+
* A user-defined compound assignment xor operator (`^=`), for example
1353+
*
1354+
* ```csharp
1355+
* public void operator ^=(Widget w) {
1356+
* ...
1357+
* }
1358+
* ```
1359+
*/
1360+
class XorCompoundAssignmentOperator extends CompoundAssignmentOperator {
1361+
XorCompoundAssignmentOperator() { this.getName() = "^=" }
1362+
1363+
override string getAPrimaryQlClass() { result = "XorCompoundAssignmentOperator" }
1364+
}
1365+
1366+
/**
1367+
* A user-defined compound assignment left shift operator (`<<=`), for example
1368+
*
1369+
* ```csharp
1370+
* public void operator <<=(Widget w) {
1371+
* ...
1372+
* }
1373+
* ```
1374+
*/
1375+
class LeftShiftCompoundAssignmentOperator extends CompoundAssignmentOperator {
1376+
LeftShiftCompoundAssignmentOperator() { this.getName() = "<<=" }
1377+
1378+
override string getAPrimaryQlClass() { result = "LeftShiftCompoundAssignmentOperator" }
1379+
}
1380+
1381+
/**
1382+
* A user-defined compound assignment right shift operator (`>>=`), for example
1383+
*
1384+
* ```csharp
1385+
* public void operator >>=(Widget w) {
1386+
* ...
1387+
* }
1388+
* ```
1389+
*/
1390+
class RightShiftCompoundAssignmentOperator extends CompoundAssignmentOperator {
1391+
RightShiftCompoundAssignmentOperator() { this.getName() = ">>=" }
1392+
1393+
override string getAPrimaryQlClass() { result = "RightShiftCompoundAssignmentOperator" }
1394+
}
1395+
1396+
/**
1397+
* A user-defined compound assignment unsigned right shift operator (`>>>=`), for example
1398+
*
1399+
* ```csharp
1400+
* public void operator >>>=(Widget w) {
1401+
* ...
1402+
* }
1403+
* ```
1404+
*/
1405+
class UnsignedRightShiftCompoundAssignmentOperator extends CompoundAssignmentOperator {
1406+
UnsignedRightShiftCompoundAssignmentOperator() { this.getName() = ">>>=" }
1407+
1408+
override string getAPrimaryQlClass() { result = "UnsignedRightShiftCompoundAssignmentOperator" }
1409+
}
1410+
11601411
/**
11611412
* A local function, defined within the scope of another callable.
11621413
* For example, `Fac` on lines 2--4 in

0 commit comments

Comments
 (0)