Skip to content

Commit

Permalink
GH-153 - clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Sep 11, 2022
1 parent cc80645 commit 8f04b5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,21 @@ private IEnumerable<IOperation> GetOtherSubstitutionsForSymbol(Compilation compi

private static IEnumerable<IOperation> GetConstructorOperations(Compilation compilation, ISymbol fieldReferenceOperation)
{
// TODO naming
foreach (var location in fieldReferenceOperation.ContainingType.GetMembers().OfType<IMethodSymbol>()
.Where(methodSymbol => methodSymbol.MethodKind is MethodKind.Constructor or MethodKind.StaticConstructor && methodSymbol.Locations.Length > 0)
SemanticModel semanticModel = null;
foreach (var constructorLocation in fieldReferenceOperation.ContainingType.Constructors
.Where(methodSymbol => methodSymbol.Locations.Length > 0)
.SelectMany(x => x.Locations)
.Where(location => location.IsInSource))
{
var root = location.SourceTree.GetRoot();
var relatedNode = root.FindNode(location.SourceSpan);
var root = constructorLocation.SourceTree.GetRoot();
var relatedNode = root.FindNode(constructorLocation.SourceSpan);

// perf - take original semantic model whenever possible
// but keep in mind that we might traverse outside of the original one https://github.com/nsubstitute/NSubstitute.Analyzers/issues/56
semanticModel = semanticModel == null || semanticModel.SyntaxTree != constructorLocation.SourceTree
? compilation.GetSemanticModel(constructorLocation.SourceTree)
: semanticModel;

// TODO reuse semantic model
var semanticModel = compilation.GetSemanticModel(location.SourceTree);
var operation = semanticModel.GetOperation(relatedNode) ?? semanticModel.GetOperation(relatedNode.Parent);

if (operation is not null)
Expand Down Expand Up @@ -140,7 +144,7 @@ private class ReEntrantCallVisitor : OperationWalker
private readonly Compilation _compilation;
private readonly HashSet<IOperation> _visitedOperations = new();
private readonly List<IInvocationOperation> _invocationOperation = new();
private readonly Dictionary<SyntaxTree, SemanticModel> _semanticModelCache = new(1);
private SemanticModel _semanticModel;

public ImmutableList<IInvocationOperation> InvocationOperations => _invocationOperation.ToImmutableList();

Expand Down Expand Up @@ -184,15 +188,15 @@ private void VisitRelatedSymbols(IInvocationOperation invocationOperation)
private SemanticModel GetSemanticModel(SyntaxNode syntaxNode)
{
var syntaxTree = syntaxNode.SyntaxTree;
if (_semanticModelCache.TryGetValue(syntaxTree, out var semanticModel))

// perf - take original semantic model whenever possible
// but keep in mind that we might traverse outside of the original one https://github.com/nsubstitute/NSubstitute.Analyzers/issues/56
if (_semanticModel == null || _semanticModel.SyntaxTree != syntaxTree)
{
return semanticModel;
_semanticModel = _compilation.GetSemanticModel(syntaxTree);
}

semanticModel = _compilation.GetSemanticModel(syntaxTree);
_semanticModelCache[syntaxTree] = semanticModel;

return semanticModel;
return _semanticModel;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private class WhenVisitor : OperationWalker
private readonly bool _includeAll;
private readonly HashSet<IOperation> _operations = new();

private readonly Dictionary<SyntaxTree, SemanticModel> _semanticModelCache = new(1);
private SemanticModel _semanticModel;

public WhenVisitor(
Compilation compilation,
Expand All @@ -150,7 +150,6 @@ public override void VisitMethodReference(IMethodReferenceOperation operation)
{
foreach (var methodDeclaringSyntaxReference in operation.Method.DeclaringSyntaxReferences)
{
// TODO async?
var syntaxNode = methodDeclaringSyntaxReference.GetSyntax();
var semanticModel = GetSemanticModel(syntaxNode.Parent);
var referencedOperation = semanticModel.GetOperation(syntaxNode) ??
Expand Down Expand Up @@ -196,15 +195,15 @@ private void TryAdd(IOperation operation)
private SemanticModel GetSemanticModel(SyntaxNode syntaxNode)
{
var syntaxTree = syntaxNode.SyntaxTree;
if (_semanticModelCache.TryGetValue(syntaxTree, out var semanticModel))

// perf - take original semantic model whenever possible
// but keep in mind that we might traverse outside of the original one https://github.com/nsubstitute/NSubstitute.Analyzers/issues/56
if (_semanticModel == null || _semanticModel.SyntaxTree != syntaxTree)
{
return semanticModel;
_semanticModel = _compilation.GetSemanticModel(syntaxTree);
}

semanticModel = _compilation.GetSemanticModel(syntaxTree);
_semanticModelCache[syntaxTree] = semanticModel;

return semanticModel;
return _semanticModel;
}
}
}

0 comments on commit 8f04b5a

Please sign in to comment.