Skip to content

Commit

Permalink
Merge pull request #91 from semdiffdotnet/remove_syntax_tree_bug
Browse files Browse the repository at this point in the history
Replace DataStore with simple dictionary
  • Loading branch information
MRose90 committed May 5, 2016
2 parents 8e4c9b2 + 13f0d4e commit e2db106
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 106 deletions.
93 changes: 0 additions & 93 deletions SemDiff.Core/DataStore.cs

This file was deleted.

27 changes: 27 additions & 0 deletions SemDiff.Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,32 @@ public static IEnumerable<Tuple<L, R>> Map<L, R>(this IEnumerable<L> sourcel, IE
{
return source;
}

internal static Dictionary<Repo, List<SyntaxTree>> GetRepoAndTrees(this Compilation comp, Func<SyntaxTree, Repo> getRepo)
{
if (getRepo == null)
throw new InvalidOperationException();

var dictionary = new Dictionary<Repo, List<SyntaxTree>>();
foreach (var t in comp.SyntaxTrees)
{
var repo = getRepo(t);
if (repo == null)
{
continue; //Not a part of a repo
}
if (dictionary.ContainsKey(repo))
{
dictionary[repo].Add(t);
}
else
{
var list = new List<SyntaxTree>();
list.Add(t);
dictionary[repo] = list;
}
}
return dictionary;
}
}
}
1 change: 0 additions & 1 deletion SemDiff.Core/SemDiff.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@
<ItemGroup>
<Compile Include="Analysis.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="DataStore.cs" />
<Compile Include="DetectedFalseNegative.cs" />
<Compile Include="DetectedFalsePositive.cs" />
<Compile Include="Diagnostics.cs" />
Expand Down
29 changes: 17 additions & 12 deletions SemDiff.Core/SemDiffAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using SemDiff.Core.Exceptions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
Expand All @@ -22,8 +23,9 @@ namespace SemDiff.Core
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class SemDiffAnalyzer : DiagnosticAnalyzer
{
private static readonly ConcurrentDictionary<string, Repo> _treePathLookup = new ConcurrentDictionary<string, Repo>();
private static int libGit2SharpNativePathSet;
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => Diagnostics.Supported;
internal static DataStore Store { get; } = new DataStore();

/// <summary>
/// Called once at session start to register actions in the analysis context.
Expand All @@ -40,9 +42,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterCompilationAction(OnCompilation);
}

private int libGit2SharpNativePathSet;

private void SetupLibGit2Sharp()
private static void SetupLibGit2Sharp()
{
// It is important to only set the native library path before they are accessed for the first time.
// Otherwise exceptions will be thrown. So this code is only executed once.
Expand Down Expand Up @@ -71,6 +71,13 @@ private void SetupLibGit2Sharp()
}
}

private static Repo GetRepo(SyntaxTree tree)
{
return _treePathLookup.AddOrUpdate(tree.FilePath,
p => Repo.GetRepoFor(p),
(p, o) => o ?? Repo.GetRepoFor(p)); //Always check again if null
}

private static void OnCompilation(CompilationAnalysisContext context)
{
var diags = OnCompilationAsync(context.Compilation).Result;
Expand All @@ -85,9 +92,8 @@ private async static Task<IEnumerable<Diagnostic>> OnCompilationAsync(Compilatio
Logger.Trace($"Entering {nameof(OnCompilationAsync)}: {comp.AssemblyName}");
try
{
var data = Store.InterlockedAddOrUpdate(comp.AssemblyName, comp.SyntaxTrees, GetRepo);
var repos = data.Repos;
foreach (var repo in repos)
var data = comp.GetRepoAndTrees(GetRepo);
foreach (var repo in data.Keys)
{
try
{
Expand Down Expand Up @@ -121,11 +127,12 @@ private async static Task<IEnumerable<Diagnostic>> OnCompilationAsync(Compilatio
}

var diagnostics = new List<Diagnostic>();
foreach (var r in repos)
foreach (var rt in data)
{
foreach (var t in data.GetTreesForRepo(r))
var repo = rt.Key;
foreach (var t in rt.Value) //Foreach tree
{
diagnostics.AddRange(Analyze(comp.GetSemanticModel(t), r));
diagnostics.AddRange(Analyze(comp.GetSemanticModel(t), repo));
}
}
return diagnostics;
Expand All @@ -141,8 +148,6 @@ private async static Task<IEnumerable<Diagnostic>> OnCompilationAsync(Compilatio
}
}

private static Repo GetRepo(SyntaxTree tree) => Repo.GetRepoFor(tree.FilePath);

private static IEnumerable<Diagnostic> Analyze(SemanticModel semanticModel, Repo repo)
{
Logger.Trace($"Entering {nameof(Analyze)}: {semanticModel?.SyntaxTree?.FilePath}");
Expand Down

0 comments on commit e2db106

Please sign in to comment.