Skip to content

Commit 8d2c5c2

Browse files
committed
C#: Use a dictionary for translating operator methods to operator symbols.
1 parent 219ea28 commit 8d2c5c2

File tree

1 file changed

+42
-97
lines changed

1 file changed

+42
-97
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Util/SymbolExtensions.cs

Lines changed: 42 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Text.RegularExpressions;
23
using Microsoft.CodeAnalysis;
34

@@ -18,111 +19,55 @@ public static string GetName(this ISymbol symbol, bool useMetadataName = false)
1819
return symbol.CanBeReferencedByName ? name : name.Substring(symbol.Name.LastIndexOf('.') + 1);
1920
}
2021

22+
private static readonly Dictionary<string, string> methodToOperator = new Dictionary<string, string>
23+
{
24+
{ "op_LogicalNot", "!" },
25+
{ "op_BitwiseAnd", "&" },
26+
{ "op_Equality", "==" },
27+
{ "op_Inequality", "!=" },
28+
{ "op_UnaryPlus", "+" },
29+
{ "op_Addition", "+" },
30+
{ "op_UnaryNegation", "-" },
31+
{ "op_Subtraction", "-" },
32+
{ "op_Multiply", "*" },
33+
{ "op_Division", "/" },
34+
{ "op_Modulus", "%" },
35+
{ "op_GreaterThan", ">" },
36+
{ "op_GreaterThanOrEqual", ">=" },
37+
{ "op_LessThan", "<" },
38+
{ "op_LessThanOrEqual", "<=" },
39+
{ "op_Decrement", "--" },
40+
{ "op_Increment", "++" },
41+
{ "op_Implicit", "implicit conversion" },
42+
{ "op_Explicit", "explicit conversion" },
43+
{ "op_OnesComplement", "~" },
44+
{ "op_RightShift", ">>" },
45+
{ "op_UnsignedRightShift", ">>>" },
46+
{ "op_LeftShift", "<<" },
47+
{ "op_BitwiseOr", "|" },
48+
{ "op_ExclusiveOr", "^" },
49+
{ "op_True", "true" },
50+
{ "op_False", "false" }
51+
};
52+
2153
/// <summary>
2254
/// Convert an operator method name in to a symbolic name.
2355
/// A return value indicates whether the conversion succeeded.
2456
/// </summary>
2557
public static bool TryGetOperatorSymbol(this ISymbol symbol, out string operatorName)
2658
{
27-
static bool TryGetOperatorSymbolFromName(string methodName, out string operatorName)
59+
var methodName = symbol.GetName(useMetadataName: false);
60+
61+
if (methodToOperator.TryGetValue(methodName, out operatorName!))
62+
return true;
63+
64+
var match = CheckedRegex().Match(methodName);
65+
if (match.Success && methodToOperator.TryGetValue($"op_{match.Groups[1]}", out var uncheckedName))
2866
{
29-
var success = true;
30-
switch (methodName)
31-
{
32-
case "op_LogicalNot":
33-
operatorName = "!";
34-
break;
35-
case "op_BitwiseAnd":
36-
operatorName = "&";
37-
break;
38-
case "op_Equality":
39-
operatorName = "==";
40-
break;
41-
case "op_Inequality":
42-
operatorName = "!=";
43-
break;
44-
case "op_UnaryPlus":
45-
case "op_Addition":
46-
operatorName = "+";
47-
break;
48-
case "op_UnaryNegation":
49-
case "op_Subtraction":
50-
operatorName = "-";
51-
break;
52-
case "op_Multiply":
53-
operatorName = "*";
54-
break;
55-
case "op_Division":
56-
operatorName = "/";
57-
break;
58-
case "op_Modulus":
59-
operatorName = "%";
60-
break;
61-
case "op_GreaterThan":
62-
operatorName = ">";
63-
break;
64-
case "op_GreaterThanOrEqual":
65-
operatorName = ">=";
66-
break;
67-
case "op_LessThan":
68-
operatorName = "<";
69-
break;
70-
case "op_LessThanOrEqual":
71-
operatorName = "<=";
72-
break;
73-
case "op_Decrement":
74-
operatorName = "--";
75-
break;
76-
case "op_Increment":
77-
operatorName = "++";
78-
break;
79-
case "op_Implicit":
80-
operatorName = "implicit conversion";
81-
break;
82-
case "op_Explicit":
83-
operatorName = "explicit conversion";
84-
break;
85-
case "op_OnesComplement":
86-
operatorName = "~";
87-
break;
88-
case "op_RightShift":
89-
operatorName = ">>";
90-
break;
91-
case "op_UnsignedRightShift":
92-
operatorName = ">>>";
93-
break;
94-
case "op_LeftShift":
95-
operatorName = "<<";
96-
break;
97-
case "op_BitwiseOr":
98-
operatorName = "|";
99-
break;
100-
case "op_ExclusiveOr":
101-
operatorName = "^";
102-
break;
103-
case "op_True":
104-
operatorName = "true";
105-
break;
106-
case "op_False":
107-
operatorName = "false";
108-
break;
109-
default:
110-
var match = CheckedRegex().Match(methodName);
111-
if (match.Success)
112-
{
113-
TryGetOperatorSymbolFromName($"op_{match.Groups[1]}", out var uncheckedName);
114-
operatorName = $"checked {uncheckedName}";
115-
break;
116-
}
117-
operatorName = methodName;
118-
success = false;
119-
break;
120-
}
121-
return success;
67+
operatorName = $"checked {uncheckedName}";
68+
return true;
12269
}
123-
124-
var methodName = symbol.GetName(useMetadataName: false);
125-
return TryGetOperatorSymbolFromName(methodName, out operatorName);
70+
return false;
12671
}
12772

12873
[GeneratedRegex("^op_Checked(.*)$")]

0 commit comments

Comments
 (0)