Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public static string GetName(this ISymbol symbol, bool useMetadataName = false)
{ "op_False", "false" }
});

/// <summary>
/// The operatorname for user-defined increment and decrement operators are "op_IncrementAssignment" and
/// "op_DecrementAssignment" respectively.
/// Thus we need to handle this explicitly to avoid postfixing them with an "=".
/// </summary>
private static bool isIncrementOrDecrement(string operatorName) => operatorName == "++" || operatorName == "--";

/// <summary>
/// Convert an operator method name in to a symbolic name.
/// A return value indicates whether the conversion succeeded.
Expand All @@ -72,7 +79,7 @@ public static bool TryGetOperatorSymbol(this ISymbol symbol, out string operator
if (match.Success && methodToOperator.TryGetValue($"op_{match.Groups[2]}", out var rawOperatorName))
{
var prefix = match.Groups[1].Success ? "checked " : "";
var postfix = match.Groups[3].Success ? "=" : "";
var postfix = match.Groups[3].Success && !isIncrementOrDecrement(rawOperatorName) ? "=" : "";
operatorName = $"{prefix}{rawOperatorName}{postfix}";
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/Callable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ class UnaryOperator extends Operator {
this.getNumberOfParameters() = 1 and
not this instanceof ConversionOperator and
not this instanceof CompoundAssignmentOperator
or
// Instance increment and decrement operators don't have a parameter (only a qualifier).
this.getNumberOfParameters() = 0 and not this.isStatic()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| operators.cs:96:32:96:39 | implicit conversion |
| operators.cs:118:36:118:43 | implicit conversion |
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| operators.cs:101:32:101:39 | explicit conversion |
| operators.cs:123:36:123:43 | explicit conversion |
30 changes: 15 additions & 15 deletions csharp/ql/test/library-tests/operators/Operators5.expected
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
| operators.cs:23:30:23:31 | += | operators.cs:61:13:61:22 | ... += ... |
| operators.cs:31:38:31:39 | checked += | operators.cs:77:17:77:26 | ... += ... |
| operators.cs:33:38:33:39 | checked -= | operators.cs:78:17:78:26 | ... -= ... |
| operators.cs:34:30:34:31 | -= | operators.cs:64:13:64:22 | ... -= ... |
| operators.cs:36:38:36:39 | checked *= | operators.cs:79:17:79:26 | ... *= ... |
| operators.cs:37:30:37:31 | *= | operators.cs:65:13:65:22 | ... *= ... |
| operators.cs:39:38:39:39 | checked /= | operators.cs:80:17:80:26 | ... /= ... |
| operators.cs:40:30:40:31 | /= | operators.cs:66:13:66:22 | ... /= ... |
| operators.cs:42:30:42:31 | %= | operators.cs:67:13:67:22 | ... %= ... |
| operators.cs:43:30:43:31 | &= | operators.cs:68:13:68:22 | ... &= ... |
| operators.cs:44:30:44:31 | \|= | operators.cs:69:13:69:22 | ... \|= ... |
| operators.cs:45:30:45:31 | ^= | operators.cs:70:13:70:22 | ... ^= ... |
| operators.cs:46:30:46:32 | <<= | operators.cs:71:13:71:23 | ... <<= ... |
| operators.cs:47:30:47:32 | >>= | operators.cs:72:13:72:23 | ... >>= ... |
| operators.cs:48:30:48:33 | >>>= | operators.cs:73:13:73:24 | ... >>>= ... |
| operators.cs:23:30:23:31 | += | operators.cs:70:13:70:22 | ... += ... |
| operators.cs:31:38:31:39 | checked += | operators.cs:86:17:86:26 | ... += ... |
| operators.cs:33:38:33:39 | checked -= | operators.cs:87:17:87:26 | ... -= ... |
| operators.cs:34:30:34:31 | -= | operators.cs:73:13:73:22 | ... -= ... |
| operators.cs:36:38:36:39 | checked *= | operators.cs:88:17:88:26 | ... *= ... |
| operators.cs:37:30:37:31 | *= | operators.cs:74:13:74:22 | ... *= ... |
| operators.cs:39:38:39:39 | checked /= | operators.cs:89:17:89:26 | ... /= ... |
| operators.cs:40:30:40:31 | /= | operators.cs:75:13:75:22 | ... /= ... |
| operators.cs:42:30:42:31 | %= | operators.cs:76:13:76:22 | ... %= ... |
| operators.cs:43:30:43:31 | &= | operators.cs:77:13:77:22 | ... &= ... |
| operators.cs:44:30:44:31 | \|= | operators.cs:78:13:78:22 | ... \|= ... |
| operators.cs:45:30:45:31 | ^= | operators.cs:79:13:79:22 | ... ^= ... |
| operators.cs:46:30:46:32 | <<= | operators.cs:80:13:80:23 | ... <<= ... |
| operators.cs:47:30:47:32 | >>= | operators.cs:81:13:81:23 | ... >>= ... |
| operators.cs:48:30:48:33 | >>>= | operators.cs:82:13:82:24 | ... >>>= ... |
10 changes: 10 additions & 0 deletions csharp/ql/test/library-tests/operators/Operators6.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
| operators.cs:15:42:15:43 | ++ | operators.cs:66:19:66:23 | call to operator ++ |
| operators.cs:15:42:15:43 | ++ | operators.cs:67:19:67:23 | call to operator ++ |
| operators.cs:54:38:54:39 | checked ++ | operators.cs:100:17:100:19 | call to operator checked ++ |
| operators.cs:54:38:54:39 | checked ++ | operators.cs:101:17:101:19 | call to operator checked ++ |
| operators.cs:55:30:55:31 | ++ | operators.cs:93:13:93:15 | call to operator ++ |
| operators.cs:55:30:55:31 | ++ | operators.cs:94:13:94:15 | call to operator ++ |
| operators.cs:56:38:56:39 | checked -- | operators.cs:102:17:102:19 | call to operator checked -- |
| operators.cs:56:38:56:39 | checked -- | operators.cs:103:17:103:19 | call to operator checked -- |
| operators.cs:57:30:57:31 | -- | operators.cs:95:13:95:15 | call to operator -- |
| operators.cs:57:30:57:31 | -- | operators.cs:96:13:96:15 | call to operator -- |
17 changes: 17 additions & 0 deletions csharp/ql/test/library-tests/operators/Operators6.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @name Test for operators
*/

import csharp

from Operator op, OperatorCall call
where
op.fromSource() and
(
op instanceof IncrementOperator or
op instanceof CheckedIncrementOperator or
op instanceof DecrementOperator or
op instanceof CheckedDecrementOperator
) and
call.getTarget() = op
select op, call
Loading
Loading