Skip to content

Commit d1fe542

Browse files
authored
Merge pull request github#5131 from tamasvajk/feature/refactor
C# Cleanup and refactoring
2 parents bb95b8a + 2de7fbe commit d1fe542

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+590
-675
lines changed

csharp/autobuilder/Semmle.Autobuild.Shared/Solution.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal class Solution : ProjectOrSolution, ISolution
4545
private readonly SolutionFile? solution;
4646

4747
private readonly IEnumerable<Project> includedProjects;
48+
4849
public override IEnumerable<IProjectOrSolution> IncludedProjects => includedProjects;
4950

5051
public IEnumerable<SolutionConfigurationInSolution> Configurations =>
@@ -84,8 +85,12 @@ public Solution(Autobuilder builder, string path, bool allowProject) : base(buil
8485
.ToArray();
8586
}
8687

87-
private IEnumerable<Version> ToolsVersions => includedProjects.Where(p => p.ValidToolsVersion).Select(p => p.ToolsVersion);
88+
private IEnumerable<Version> ToolsVersions => includedProjects
89+
.Where(p => p.ValidToolsVersion)
90+
.Select(p => p.ToolsVersion);
8891

89-
public Version ToolsVersion => ToolsVersions.Any() ? ToolsVersions.Max() : new Version();
92+
public Version ToolsVersion => ToolsVersions.Any()
93+
? ToolsVersions.Max()
94+
: new Version();
9095
}
9196
}

csharp/extractor/Semmle.Extraction.CIL.Driver/ExtractorOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void ResolveReferences()
163163
}
164164

165165
private readonly HashSet<string> filesAnalyzed = new HashSet<string>();
166-
public HashSet<AssemblyName> MissingReferences {get;} = new HashSet<AssemblyName>();
166+
public HashSet<AssemblyName> MissingReferences { get; } = new HashSet<AssemblyName>();
167167
}
168168

169169
/// <summary>

csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static void ExtractCIL(Layout layout, string assemblyPath, ILogger logger
136136
trapFile = trapWriter.TrapFile;
137137
if (nocache || !System.IO.File.Exists(trapFile))
138138
{
139-
var cx = extractor.CreateContext(null, trapWriter, null, false);
139+
var cx = new Extraction.Context(extractor, trapWriter);
140140
ExtractCIL(cx, assemblyPath, extractPdbs);
141141
extracted = true;
142142
}

csharp/extractor/Semmle.Extraction.CIL/PDB/MdProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Microsoft.DiaSymReader;
33
using System.Reflection;
44

5+
#pragma warning disable IDE0060, CA1822
6+
57
namespace Semmle.Extraction.PDB
68
{
79
/// <summary>
@@ -31,3 +33,5 @@ public bool TryGetTypeReferenceInfo(int typeReferenceToken, out string namespace
3133
throw new NotImplementedException();
3234
}
3335
}
36+
37+
#pragma warning restore

csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Semmle.Extraction.CSharp
1717
/// </summary>
1818
public sealed class Analyser : IDisposable
1919
{
20-
private IExtractor extractor;
20+
private Extraction.Extractor extractor;
2121
private CSharpCompilation compilation;
2222
private Layout layout;
2323
private bool init;
@@ -230,7 +230,7 @@ private void DoAnalyseCompilation()
230230
var projectLayout = layout.LookupProjectOrDefault(transformedAssemblyPath);
231231
var trapWriter = projectLayout.CreateTrapWriter(Logger, transformedAssemblyPath, options.TrapCompression, discardDuplicates: false);
232232
compilationTrapFile = trapWriter; // Dispose later
233-
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);
233+
var cx = new Context(extractor, compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);
234234

235235
compilationEntity = Entities.Compilation.Create(cx);
236236
}
@@ -285,7 +285,7 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)
285285

286286
if (c.GetAssemblyOrModuleSymbol(r) is IAssemblySymbol assembly)
287287
{
288-
var cx = extractor.CreateContext(c, trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);
288+
var cx = new Context(extractor, c, trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);
289289

290290
foreach (var module in assembly.Modules)
291291
{
@@ -371,7 +371,7 @@ private void DoExtractTree(SyntaxTree tree)
371371

372372
if (!upToDate)
373373
{
374-
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new SourceScope(tree), AddAssemblyTrapPrefix);
374+
var cx = new Context(extractor, compilation.Clone(), trapWriter, new SourceScope(tree), AddAssemblyTrapPrefix);
375375
// Ensure that the file itself is populated in case the source file is totally empty
376376
var root = tree.GetRoot();
377377
Extraction.Entities.File.Create(cx, root.SyntaxTree.FilePath);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private Expression CreateExpressionFromArgument(TypedConstant constant, Expressi
118118

119119
private Semmle.Extraction.Entities.Location location;
120120
private Semmle.Extraction.Entities.Location Location =>
121-
location ?? (location = Semmle.Extraction.Entities.Location.Create(Context, attributeSyntax is null ? entity.ReportingLocation : attributeSyntax.Name.GetLocation()));
121+
location ?? (location = Context.CreateLocation(attributeSyntax is null ? entity.ReportingLocation : attributeSyntax.Name.GetLocation()));
122122

123123
public override bool NeedsPopulation => true;
124124

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Semmle.Extraction.Kinds;
2+
3+
namespace Semmle.Extraction.CSharp.Entities
4+
{
5+
internal static class CallTypeExtensions
6+
{
7+
/// <summary>
8+
/// Adjust the expression kind <paramref name="k"/> to match this call type.
9+
/// </summary>
10+
public static ExprKind AdjustKind(this Expression.CallType ct, ExprKind k)
11+
{
12+
if (k == ExprKind.ADDRESS_OF)
13+
{
14+
return k;
15+
}
16+
17+
switch (ct)
18+
{
19+
case Expression.CallType.Dynamic:
20+
case Expression.CallType.UserOperator:
21+
return ExprKind.OPERATOR_INVOCATION;
22+
default:
23+
return k;
24+
}
25+
}
26+
}
27+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public override void Populate(TextWriter trapFile)
1313
{
1414
trapFile.commentblock(this);
1515
var child = 0;
16-
trapFile.commentblock_location(this, Context.Create(symbol.Location));
16+
trapFile.commentblock_location(this, Context.CreateLocation(symbol.Location));
1717
foreach (var l in symbol.CommentLines)
1818
{
1919
trapFile.commentblock_child(this, (CommentLine)l, child++);
@@ -24,7 +24,7 @@ public override void Populate(TextWriter trapFile)
2424

2525
public override void WriteId(TextWriter trapFile)
2626
{
27-
trapFile.WriteSubId(Context.Create(symbol.Location));
27+
trapFile.WriteSubId(Context.CreateLocation(symbol.Location));
2828
trapFile.Write(";commentblock");
2929
}
3030

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private CommentLine(Context cx, Microsoft.CodeAnalysis.Location loc, CommentLine
2323

2424
public override void Populate(TextWriter trapFile)
2525
{
26-
location = Context.Create(Location);
26+
location = Context.CreateLocation(Location);
2727
trapFile.commentline(this, Type == CommentLineType.MultilineContinuation ? CommentLineType.Multiline : Type, Text, RawText);
2828
trapFile.commentline_location(this, location);
2929
}
@@ -34,7 +34,7 @@ public override void Populate(TextWriter trapFile)
3434

3535
public override void WriteId(TextWriter trapFile)
3636
{
37-
trapFile.WriteSubId(Context.Create(Location));
37+
trapFile.WriteSubId(Context.CreateLocation(Location));
3838
trapFile.Write(";commentline");
3939
}
4040

csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Diagnostic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Diagnostic(Context cx, Microsoft.CodeAnalysis.Diagnostic diag) : base(cx)
1717
protected override void Populate(TextWriter trapFile)
1818
{
1919
trapFile.diagnostics(this, (int)diagnostic.Severity, diagnostic.Id, diagnostic.Descriptor.Title.ToString(),
20-
diagnostic.GetMessage(), Extraction.Entities.Location.Create(cx, diagnostic.Location));
20+
diagnostic.GetMessage(), cx.CreateLocation(diagnostic.Location));
2121
}
2222
}
2323
}

0 commit comments

Comments
 (0)