Skip to content

Commit 6021d00

Browse files
committed
C#: Move some methods into newly created Semmle.Extraction.CSharp.Util project
1 parent 2429a53 commit 6021d00

File tree

17 files changed

+177
-136
lines changed

17 files changed

+177
-136
lines changed

csharp/CSharp.sln

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
32
# Visual Studio 15
43
VisualStudioVersion = 15.0.27130.2036
54
MinimumVisualStudioVersion = 10.0.40219.1
@@ -15,6 +14,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp.De
1514
EndProject
1615
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp.Standalone", "extractor\Semmle.Extraction.CSharp.Standalone\Semmle.Extraction.CSharp.Standalone.csproj", "{D00E7D25-0FA0-48EC-B048-CD60CE1B30D8}"
1716
EndProject
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp.Util", "extractor\Semmle.Extraction.CSharp.Util\Semmle.Extraction.CSharp.Util.csproj", "{998A0D4C-8BFC-4513-A28D-4816AFB89882}"
18+
EndProject
1819
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CIL.Driver", "extractor\Semmle.Extraction.CIL.Driver\Semmle.Extraction.CIL.Driver.csproj", "{EFA400B3-C1CE-446F-A4E2-8B44E61EF47C}"
1920
EndProject
2021
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp.Driver", "extractor\Semmle.Extraction.CSharp.Driver\Semmle.Extraction.CSharp.Driver.csproj", "{C36453BF-0C82-448A-B15D-26947503A2D3}"
@@ -85,6 +86,10 @@ Global
8586
{34256E8F-866A-46C1-800E-3DF69FD1DCB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
8687
{34256E8F-866A-46C1-800E-3DF69FD1DCB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
8788
{34256E8F-866A-46C1-800E-3DF69FD1DCB7}.Release|Any CPU.Build.0 = Release|Any CPU
89+
{B7C9FD47-A78C-4C20-AC29-B0AE638ADE9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
90+
{B7C9FD47-A78C-4C20-AC29-B0AE638ADE9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
91+
{B7C9FD47-A78C-4C20-AC29-B0AE638ADE9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
92+
{B7C9FD47-A78C-4C20-AC29-B0AE638ADE9D}.Release|Any CPU.Build.0 = Release|Any CPU
8893
EndGlobalSection
8994
GlobalSection(SolutionProperties) = preSolution
9095
HideSolutionNode = FALSE
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net7.0</TargetFramework>
4+
<AssemblyName>Semmle.Extraction.CSharp.Util</AssemblyName>
5+
<RootNamespace>Semmle.Extraction.CSharp.Util</RootNamespace>
6+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
9+
<Nullable>enable</Nullable>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System.Text.RegularExpressions;
2+
using Microsoft.CodeAnalysis;
3+
4+
namespace Semmle.Extraction.CSharp.Util
5+
{
6+
public static partial class SymbolExtensions
7+
{
8+
/// <summary>
9+
/// Gets the name of this symbol.
10+
///
11+
/// If the symbol implements an explicit interface, only the
12+
/// name of the member being implemented is included, not the
13+
/// explicit prefix.
14+
/// </summary>
15+
public static string GetName(this ISymbol symbol, bool useMetadataName = false)
16+
{
17+
var name = useMetadataName ? symbol.MetadataName : symbol.Name;
18+
return symbol.CanBeReferencedByName ? name : name.Substring(symbol.Name.LastIndexOf('.') + 1);
19+
}
20+
21+
/// <summary>
22+
/// Convert an operator method name in to a symbolic name.
23+
/// A return value indicates whether the conversion succeeded.
24+
/// </summary>
25+
public static bool TryGetOperatorSymbol(this ISymbol symbol, out string operatorName)
26+
{
27+
static bool TryGetOperatorSymbolFromName(string methodName, out string operatorName)
28+
{
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;
122+
}
123+
124+
var methodName = symbol.GetName(useMetadataName: false);
125+
return TryGetOperatorSymbolFromName(methodName, out operatorName);
126+
}
127+
128+
[GeneratedRegex("^op_Checked(.*)$")]
129+
private static partial Regex CheckedRegex();
130+
}
131+
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Event.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Semmle.Extraction.CSharp.Util;
56

67
namespace Semmle.Extraction.CSharp.Entities
78
{

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.CodeAnalysis.CSharp;
77
using Microsoft.CodeAnalysis.CSharp.Syntax;
88
using Semmle.Extraction.CSharp.Entities.Expressions;
9+
using Semmle.Extraction.CSharp.Util;
910
using Semmle.Extraction.Kinds;
1011

1112
namespace Semmle.Extraction.CSharp.Entities
@@ -205,7 +206,7 @@ private static bool ContainsPattern(SyntaxNode node) =>
205206
{
206207
// this can happen in VB.NET
207208
cx.ExtractionError($"Extracting default argument value 'object {parameter.Name} = default' instead of 'object {parameter.Name} = {defaultValue}'. The latter is not supported in C#.",
208-
null, null, severity: Util.Logging.Severity.Warning);
209+
null, null, severity: Semmle.Util.Logging.Severity.Warning);
209210

210211
// we're generating a default expression:
211212
return Default.CreateGenerated(cx, parent, childIndex, location, ValueAsString(null));
@@ -250,7 +251,7 @@ public void OperatorCall(TextWriter trapFile, ExpressionSyntax node)
250251
var callType = GetCallType(Context, node);
251252
if (callType == CallType.Dynamic)
252253
{
253-
UserOperator.TryGetOperatorSymbol(method.Name, out var operatorName);
254+
method.TryGetOperatorSymbol(out var operatorName);
254255
trapFile.dynamic_member_name(this, operatorName);
255256
return;
256257
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using Microsoft.CodeAnalysis;
3+
using Semmle.Extraction.CSharp.Util;
34
using Semmle.Extraction.Kinds;
45

56
namespace Semmle.Extraction.CSharp.Entities.Expressions

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/DateTimeObjectCreation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.IO;
22
using System.Linq;
33
using Microsoft.CodeAnalysis;
4+
using Semmle.Extraction.CSharp.Util;
45
using Semmle.Extraction.Kinds;
56

67
namespace Semmle.Extraction.CSharp.Entities.Expressions

csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Semmle.Extraction.CSharp.Util;
56

67
namespace Semmle.Extraction.CSharp.Entities
78
{

csharp/extractor/Semmle.Extraction.CSharp/Entities/OrdinaryMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.CSharp.Syntax;
55
using Semmle.Extraction.CSharp.Populators;
6-
6+
using Semmle.Extraction.CSharp.Util;
77

88
namespace Semmle.Extraction.CSharp.Entities
99
{
@@ -51,7 +51,7 @@ public override void Populate(TextWriter trapFile)
5151
{
5252
if (method.MethodKind == MethodKind.ReducedExtension)
5353
{
54-
cx.Extractor.Logger.Log(Util.Logging.Severity.Warning, "Reduced extension method symbols should not be directly extracted.");
54+
cx.Extractor.Logger.Log(Semmle.Util.Logging.Severity.Warning, "Reduced extension method symbols should not be directly extracted.");
5555
}
5656

5757
return OrdinaryMethodFactory.Instance.CreateEntityFromSymbol(cx, method);

csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.CodeAnalysis.CSharp.Syntax;
66
using Semmle.Extraction.CSharp.Entities.Expressions;
77
using Semmle.Extraction.Kinds;
8+
using Semmle.Extraction.CSharp.Util;
89

910
namespace Semmle.Extraction.CSharp.Entities
1011
{

0 commit comments

Comments
 (0)