Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 32 additions & 43 deletions Rubberduck.Parsing/Symbols/DeclarationFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ public class DeclarationFinder
private readonly Lazy<ConcurrentDictionary<Declaration, Declaration[]>> _handlersByWithEventsField;
private readonly Lazy<ConcurrentDictionary<VBAParser.ImplementsStmtContext, Declaration[]>> _membersByImplementsContext;
private readonly Lazy<ConcurrentDictionary<Declaration, Declaration[]>> _interfaceMembers;
private Lazy<List<Declaration>> _nonBaseAsType;
private readonly Lazy<ConcurrentBag<Declaration>> _eventHandlers;
private readonly Lazy<ConcurrentBag<Declaration>> _classes;

private static readonly object ThreadLock = new object();
private readonly object threadLock = new object();

public DeclarationFinder(IReadOnlyList<Declaration> declarations, IEnumerable<IAnnotation> annotations, IReadOnlyList<UnboundMemberDeclaration> unresolvedMemberDeclarations, IHostApplication hostApp = null)
{
Expand Down Expand Up @@ -133,8 +136,16 @@ public DeclarationFinder(IReadOnlyList<Declaration> declarations, IEnumerable<IA
});

_membersByImplementsContext = new Lazy<ConcurrentDictionary<VBAParser.ImplementsStmtContext, Declaration[]>>(() =>
new ConcurrentDictionary<VBAParser.ImplementsStmtContext, Declaration[]>(
implementableMembers.ToDictionary(item => item.Context, item => item.Members)), true);
new ConcurrentDictionary<VBAParser.ImplementsStmtContext, Declaration[]>(
implementableMembers.ToDictionary(item => item.Context, item => item.Members)), true);

_nonBaseAsType = new Lazy<List<Declaration>>(() =>
_declarations.AllValues().Where(d =>
!string.IsNullOrWhiteSpace(d.AsTypeName)
&& !d.AsTypeIsBaseType
&& d.DeclarationType != DeclarationType.Project
&& d.DeclarationType != DeclarationType.ProceduralModule).ToList()
,true);
}

public IEnumerable<Declaration> FreshUndeclared
Expand All @@ -152,38 +163,22 @@ public IEnumerable<Declaration> Members(QualifiedModuleName module)
return _declarations[module];
}

private IEnumerable<Declaration> _nonBaseAsType;
public IEnumerable<Declaration> FindDeclarationsWithNonBaseAsType()
{
lock (ThreadLock)
{
return _nonBaseAsType ?? (_nonBaseAsType = _declarations.AllValues().Where(d =>
!string.IsNullOrWhiteSpace(d.AsTypeName)
&& !d.AsTypeIsBaseType
&& d.DeclarationType != DeclarationType.Project
&& d.DeclarationType != DeclarationType.ProceduralModule).ToList());
}
}
return _nonBaseAsType.Value;

private readonly Lazy<ConcurrentBag<Declaration>> _eventHandlers;
}

public IEnumerable<Declaration> FindEventHandlers()
{
lock (ThreadLock)
{
return _eventHandlers.Value;
}
return _eventHandlers.Value;
}

private readonly Lazy<ConcurrentBag<Declaration>> _classes;

public IEnumerable<Declaration> Classes
{
get
{
lock (ThreadLock)
{
return _classes.Value;
}
return _classes.Value;
}
}

Expand All @@ -193,10 +188,7 @@ public IEnumerable<Declaration> Projects
{
get
{
lock (ThreadLock)
{
return _projects.Value;
}
return _projects.Value;
}
}

Expand All @@ -214,10 +206,7 @@ public IEnumerable<Declaration> UserDeclarations(DeclarationType type)

public IEnumerable<UnboundMemberDeclaration> FreshUnresolvedMemberDeclarations()
{
lock (ThreadLock)
{
return _newUnresolved.ToArray();
}
return _newUnresolved.ToArray(); //This does not need a lock because enumerators over a ConcurrentBag uses a snapshot.
}

public IEnumerable<UnboundMemberDeclaration> UnresolvedMemberDeclarations()
Expand Down Expand Up @@ -262,19 +251,18 @@ public Declaration FindParameter(Declaration procedure, string parameterName)
public IEnumerable<Declaration> FindMemberMatches(Declaration parent, string memberName)
{
ConcurrentBag<Declaration> children;
if (_declarations.TryGetValue(parent.QualifiedName.QualifiedModuleName, out children))
{
return children.Where(item => item.DeclarationType.HasFlag(DeclarationType.Member)
&& item.IdentifierName == memberName).ToList();
}

return Enumerable.Empty<Declaration>();
return _declarations.TryGetValue(parent.QualifiedName.QualifiedModuleName, out children)
? children.Where(item => item.DeclarationType.HasFlag(DeclarationType.Member)
&& item.IdentifierName == memberName).ToList()
: Enumerable.Empty<Declaration>();
}

public IEnumerable<IAnnotation> FindAnnotations(QualifiedModuleName module)
{
ConcurrentBag<IAnnotation> result;
return _annotations.TryGetValue(module, out result) ? result : Enumerable.Empty<IAnnotation>();
return _annotations.TryGetValue(module, out result)
? result
: Enumerable.Empty<IAnnotation>();
}

public bool IsMatch(string declarationName, string potentialMatchName)
Expand Down Expand Up @@ -329,7 +317,8 @@ public Declaration FindProject(string name, Declaration currentScope = null)
Declaration result = null;
try
{
result = MatchName(name).SingleOrDefault(project => project.DeclarationType.HasFlag(DeclarationType.Project)
result = MatchName(name).SingleOrDefault(project =>
project.DeclarationType.HasFlag(DeclarationType.Project)
&& (currentScope == null || project.ProjectId == currentScope.ProjectId));
}
catch (InvalidOperationException exception)
Expand All @@ -340,7 +329,7 @@ public Declaration FindProject(string name, Declaration currentScope = null)
return result;
}

public Declaration FindStdModule(string name, Declaration parent = null, bool includeBuiltIn = false)
public Declaration FindStdModule(string name, Declaration parent, bool includeBuiltIn = false)
{
Debug.Assert(parent != null);
Declaration result = null;
Expand All @@ -359,7 +348,7 @@ public Declaration FindStdModule(string name, Declaration parent = null, bool in
return result;
}

public Declaration FindClassModule(string name, Declaration parent = null, bool includeBuiltIn = false)
public Declaration FindClassModule(string name, Declaration parent, bool includeBuiltIn = false)
{
Debug.Assert(parent != null);
Declaration result = null;
Expand Down
7 changes: 4 additions & 3 deletions Rubberduck.Parsing/VBA/RubberduckParserState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,12 +1207,13 @@ public HashSet<QualifiedModuleName> ModulesReferencedBy(QualifiedModuleName refe

public HashSet<QualifiedModuleName> ModulesReferencedBy(IEnumerable<QualifiedModuleName> referencingModules)
{
var referencedModules = new HashSet<QualifiedModuleName>();
var toModules = new HashSet<QualifiedModuleName>();

foreach (var referencingModule in referencingModules)
{
referencedModules.UnionWith(ModulesReferencedBy(referencingModule));
toModules.UnionWith(ModulesReferencedBy(referencingModule));
}
return referencedModules;
return toModules;
}

public HashSet<QualifiedModuleName> ModulesReferencing(QualifiedModuleName referencedModule)
Expand Down
1 change: 1 addition & 0 deletions RubberduckTests/RubberduckTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
<Compile Include="SourceControl\UnsyncedCommitsViewModelTests.cs" />
<Compile Include="StringExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Symbols\DeclarationFinderTests.cs" />
<Compile Include="TodoExplorer\TodoExplorerTests.cs" />
<Compile Include="UnitTesting\AssertTests.cs" />
<Compile Include="UnitTesting\EngineTests.cs" />
Expand Down
71 changes: 71 additions & 0 deletions RubberduckTests/Symbols/DeclarationFinderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.Grammar;
using Rubberduck.VBEditor;
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;

namespace RubberduckTests.Symbols
{
[TestClass]
public class DeclarationFinderTests
{
[TestCategory("Resolver")]
[TestMethod]
public void DeclarationFinderCanCopeWithMultipleModulesImplementingTheSameInterface()
{
var project = GetTestProject("testProject");
var interf = GetTestClassModule(project, "interface");
var member = GetTestFunction(interf, "testMember", Accessibility.Public);
var implementingClass1 = GetTestClassModule(project, "implementingClass1");
var implementingClass2 = GetTestClassModule(project, "implementingClass2");
var implementsContext1 = new VBAParser.ImplementsStmtContext(null, 0);
var implementsContext2 = new VBAParser.ImplementsStmtContext(null, 0);
AddReference(interf, implementingClass1, implementsContext1);
AddReference(interf, implementingClass1, implementsContext2);
var declarations = new List<Declaration> {interf, member, implementingClass1, implementingClass2};

DeclarationFinder finder = new DeclarationFinder(declarations, new List<Rubberduck.Parsing.Annotations.IAnnotation>(), new List<UnboundMemberDeclaration>());
var interfaceDeclarations = finder.FindAllInterfaceMembers().ToList();

Assert.AreEqual(1, interfaceDeclarations.Count());
}

private static ClassModuleDeclaration GetTestClassModule(Declaration projectDeclatation, string name, bool isExposed = false)
{
var qualifiedClassModuleMemberName = new QualifiedMemberName(StubQualifiedModuleName(name), name);
var classModuleAttributes = new Rubberduck.Parsing.VBA.Attributes();
if (isExposed)
{
classModuleAttributes.AddExposedClassAttribute();
}
return new ClassModuleDeclaration(qualifiedClassModuleMemberName, projectDeclatation, name, false, null, classModuleAttributes);
}

private static ProjectDeclaration GetTestProject(string name)
{
var qualifiedProjectName = new QualifiedMemberName(StubQualifiedModuleName("proj"), name);
return new ProjectDeclaration(qualifiedProjectName, name, false, null);
}

private static QualifiedModuleName StubQualifiedModuleName(string name)
{
return new QualifiedModuleName("dummy", "dummy", name);
}

private static FunctionDeclaration GetTestFunction(Declaration moduleDeclatation, string name, Accessibility functionAccessibility)
{
var qualifiedFunctionMemberName = new QualifiedMemberName(moduleDeclatation.QualifiedName.QualifiedModuleName, name);
return new FunctionDeclaration(qualifiedFunctionMemberName, moduleDeclatation, moduleDeclatation, "test", null, "test", functionAccessibility, null, Selection.Home, false, false, null, null);
}

private static void AddReference(Declaration toDeclaration, Declaration fromModuleDeclaration, ParserRuleContext context = null)
{
toDeclaration.AddReference(toDeclaration.QualifiedName.QualifiedModuleName, fromModuleDeclaration, fromModuleDeclaration, context, toDeclaration.IdentifierName, toDeclaration, Selection.Home, new List<Rubberduck.Parsing.Annotations.IAnnotation>());
}

}
}