Skip to content

Commit 71e0dc0

Browse files
committed
C#: General code tidy.
1 parent 8797033 commit 71e0dc0

File tree

13 files changed

+87
-899
lines changed

13 files changed

+87
-899
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Runtime.InteropServices;
6-
using Semmle.Util;
75
using Semmle.Extraction.CSharp.Standalone;
86
using System.Threading.Tasks;
9-
using System.Collections.Concurrent;
107

118
namespace Semmle.BuildAnalyser
129
{
@@ -47,18 +44,18 @@ interface IBuildAnalysis
4744
/// </summary>
4845
class BuildAnalysis : IBuildAnalysis
4946
{
50-
readonly AssemblyCache assemblyCache;
51-
readonly NugetPackages nuget;
52-
readonly IProgressMonitor progressMonitor;
53-
HashSet<string> usedReferences = new HashSet<string>();
54-
readonly HashSet<string> usedSources = new HashSet<string>();
55-
readonly HashSet<string> missingSources = new HashSet<string>();
56-
readonly Dictionary<string, string> unresolvedReferences = new Dictionary<string, string>();
57-
readonly DirectoryInfo sourceDir;
58-
int failedProjects, succeededProjects;
59-
readonly string[] allSources;
60-
int conflictedReferences = 0;
61-
object mutex = new object();
47+
private readonly AssemblyCache assemblyCache;
48+
private readonly NugetPackages nuget;
49+
private readonly IProgressMonitor progressMonitor;
50+
private HashSet<string> usedReferences = new HashSet<string>();
51+
private readonly HashSet<string> usedSources = new HashSet<string>();
52+
private readonly HashSet<string> missingSources = new HashSet<string>();
53+
private readonly Dictionary<string, string> unresolvedReferences = new Dictionary<string, string>();
54+
private readonly DirectoryInfo sourceDir;
55+
private int failedProjects, succeededProjects;
56+
private readonly string[] allSources;
57+
private int conflictedReferences = 0;
58+
private readonly object mutex = new object();
6259

6360
/// <summary>
6461
/// Performs a C# build analysis.
@@ -80,7 +77,7 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
8077
ToArray();
8178

8279
var dllDirNames = options.DllDirs.Select(Path.GetFullPath).ToList();
83-
PackageDirectory = TemporaryDirectory.CreateTempDirectory(sourceDir.FullName, progressMonitor);
80+
PackageDirectory = TemporaryDirectory.CreateTempDirectory(sourceDir.FullName);
8481

8582
if (options.UseNuGet)
8683
{
@@ -102,6 +99,7 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
10299
}
103100

104101
{
102+
// These files can sometimes prevent `dotnet restore` from working correctly.
105103
using var renamer1 = new FileRenamer(sourceDir.GetFiles("global.json", SearchOption.AllDirectories));
106104
using var renamer2 = new FileRenamer(sourceDir.GetFiles("Directory.Build.props", SearchOption.AllDirectories));
107105

@@ -118,12 +116,6 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
118116
usedReferences = new HashSet<string>(assemblyCache.AllAssemblies.Select(a => a.Filename));
119117
}
120118

121-
if (!options.AnalyseCsProjFiles)
122-
{
123-
usedReferences = new HashSet<string>(assemblyCache.AllAssemblies.Select(a => a.Filename));
124-
}
125-
126-
127119
ResolveConflicts();
128120

129121
if (options.UseMscorlib)
@@ -150,9 +142,8 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
150142
UnresolvedReferences.Count(),
151143
conflictedReferences,
152144
succeededProjects + failedProjects,
153-
failedProjects);
154-
155-
Console.WriteLine($"Build analysis completed in {DateTime.Now - startTime}");
145+
failedProjects,
146+
DateTime.Now - startTime);
156147
}
157148

158149
/// <summary>
@@ -285,7 +276,7 @@ void AnalyseProject(FileInfo project)
285276

286277
try
287278
{
288-
var csProj = new CsProjFile(project);
279+
IProjectFile csProj = new CsProjFile(project);
289280

290281
foreach (var @ref in csProj.References)
291282
{
@@ -329,8 +320,16 @@ public void Cleanup()
329320
void Restore(string projectOrSolution)
330321
{
331322
int exit = DotNet.RestoreToDirectory(projectOrSolution, PackageDirectory.DirInfo.FullName);
332-
if (exit != 0)
333-
progressMonitor.CommandFailed("dotnet", $"restore \"{projectOrSolution}\"", exit);
323+
switch(exit)
324+
{
325+
case 0:
326+
case 1:
327+
// No errors
328+
break;
329+
default:
330+
progressMonitor.CommandFailed("dotnet", $"restore \"{projectOrSolution}\"", exit);
331+
break;
332+
}
334333
}
335334

336335
public void RestoreSolutions(IEnumerable<string> solutions)

csharp/extractor/Semmle.Extraction.CSharp.Standalone/CsProjFile.cs

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,84 +5,17 @@
55

66
namespace Semmle.BuildAnalyser
77
{
8-
/// <summary>
9-
/// A reference to a particular version of a particular package.
10-
/// </summary>
11-
class PackageReference
12-
{
13-
public PackageReference(string include, string version) {
14-
Include = include;
15-
Version = version;
16-
}
17-
public string Include, Version;
18-
19-
public override string ToString() => $"Include={Include}, Version={Version}";
20-
}
21-
22-
enum ProjectFileType
23-
{
24-
MsBuildProject,
25-
DotNetProject,
26-
OtherProject
27-
}
28-
298
interface IProjectFile
309
{
31-
IEnumerable<IProjectFile> ProjectReferences { get; }
32-
33-
IEnumerable<PackageReference> Packages { get; }
34-
3510
IEnumerable<string> References { get; }
3611

37-
IEnumerable<FileInfo> Sources { get; }
38-
39-
IEnumerable<string> TargetFrameworks { get; }
40-
}
41-
42-
class NetCoreProjectFile : IProjectFile
43-
{
44-
FileInfo path;
45-
XmlDocument doc;
46-
XmlElement root;
47-
48-
public NetCoreProjectFile(FileInfo path)
49-
{
50-
this.path = path;
51-
doc = new XmlDocument();
52-
doc.Load(path.FullName);
53-
root = doc.DocumentElement;
54-
}
55-
56-
public IEnumerable<IProjectFile> ProjectReferences => throw new System.NotImplementedException();
57-
58-
public IEnumerable<PackageReference> Packages
59-
{
60-
get
61-
{
62-
var packages = root.SelectNodes("/Project/ItemGroup/PackageReference");
63-
return packages.NodeList().
64-
Select(r =>
65-
new PackageReference(r.Attributes.GetNamedItem("Include").Value, r.Attributes.GetNamedItem("Version").Value));
66-
}
67-
}
68-
69-
public IEnumerable<string> References => throw new System.NotImplementedException();
70-
71-
public IEnumerable<FileInfo> Sources
72-
{
73-
get
74-
{
75-
return path.Directory.GetFiles("*.cs", SearchOption.AllDirectories);
76-
}
77-
}
78-
79-
public IEnumerable<string> TargetFrameworks => throw new System.NotImplementedException();
12+
IEnumerable<string> Sources { get; }
8013
}
8114

8215
/// <summary>
8316
/// Represents a .csproj file and reads information from it.
8417
/// </summary>
85-
class CsProjFile
18+
class CsProjFile : IProjectFile
8619
{
8720
public string Filename { get; }
8821

@@ -136,8 +69,6 @@ public void ReadMsBuildProject(FileInfo filename)
13669
.ToArray();
13770
}
13871

139-
string[] targetFrameworks = new string[0];
140-
14172
/// <summary>
14273
/// Reads the .csproj file directly as XML.
14374
/// This doesn't handle variables etc, and should only used as a
@@ -162,8 +93,6 @@ public void ReadProjectFileAsXml(FileInfo filename)
16293
var frameworksNode = root.SelectNodes("/Project/PropertyGroup/TargetFrameworks").NodeList().Concat(
16394
root.SelectNodes("/Project/PropertyGroup/TargetFramework").NodeList()).Select(node => node.InnerText);
16495

165-
targetFrameworks = frameworksNode.SelectMany(node => node.Split(";")).ToArray();
166-
16796
var relativeCsIncludes2 =
16897
root.SelectNodes("/Project/ItemGroup/Compile/@Include", mgr).
16998
NodeList().
@@ -208,8 +137,6 @@ public void ReadProjectFileAsXml(FileInfo filename)
208137
/// </summary>
209138
public IEnumerable<string> References => references;
210139

211-
public IEnumerable<string> TargetFrameworks => targetFrameworks;
212-
213140
/// <summary>
214141
/// The list of C# source files in full path format.
215142
/// </summary>

csharp/extractor/Semmle.Extraction.CSharp.Standalone/DotNet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static int RestoreToDirectory(string projectOrSolutionFile, string packag
2222
/// <summary>
2323
/// Utility to temporarily rename a set of files.
2424
/// </summary>
25-
class FileRenamer : IDisposable
25+
sealed class FileRenamer : IDisposable
2626
{
2727
string[] files;
2828
const string suffix = ".codeqlhidden";

csharp/extractor/Semmle.Extraction.CSharp.Standalone/DotNetRuntimeInfo.cs

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)