Skip to content

Commit

Permalink
replaced IdentifierUsageInspector with parseResult.Declarations queries
Browse files Browse the repository at this point in the history
  • Loading branch information
retailcoder committed Mar 23, 2015
1 parent 256aadd commit 1545bc1
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 562 deletions.
3 changes: 3 additions & 0 deletions RetailCoder.VBE/Inspections/Inspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public async Task<IList<ICodeInspectionResult>> FindIssuesAsync(VBProject projec
RaiseResetEvent();

var code = new VBProjectParseResult(_parser.Parse(project));
code.IdentifySymbols();
code.IdentifySymbolUsages(); // surely a bottleneck

var allIssues = new ConcurrentBag<ICodeInspectionResult>();

var inspections = _inspections.Where(inspection => inspection.Severity != CodeInspectionSeverity.DoNotShow)
Expand Down
13 changes: 10 additions & 3 deletions RetailCoder.VBE/Inspections/ParameterCanBeByValInspection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Inspections
{
Expand All @@ -16,11 +19,15 @@ public ParameterCanBeByValInspection()

public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
{
var issues = parseResult.IdentifierUsageInspector.UnassignedByRefParameters();
var declarations =
parseResult.Declarations.Items.Where(declaration =>
declaration.DeclarationType == DeclarationType.Parameter
&& ((VBAParser.ArgContext) declaration.Context).BYVAL() == null
&& !declaration.References.Any(reference => reference.IsAssignment));

foreach (var issue in issues)
foreach (var issue in declarations)
{
yield return new ParameterCanBeByValInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.MemberName);
yield return new ParameterCanBeByValInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.QualifiedName);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions RetailCoder.VBE/Inspections/ParameterNotUsedInspection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Inspections
{
Expand All @@ -16,10 +18,13 @@ public ParameterNotUsedInspection()

public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
{
var issues = parseResult.IdentifierUsageInspector.UnusedParameters();
foreach (var issue in issues)
var declarations = parseResult.Declarations.Items.Where(declaration =>
declaration.DeclarationType == DeclarationType.Parameter
&& !declaration.References.Any());

foreach (var issue in declarations)
{
yield return new ParameterNotUsedInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.MemberName);
yield return new ParameterNotUsedInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.QualifiedName);
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions RetailCoder.VBE/Inspections/UnassignedVariableUsageInspection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Inspections
{
Expand All @@ -16,11 +18,18 @@ public UnassignedVariableUsageInspection()

public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
{
var issues = parseResult.IdentifierUsageInspector.AllUnassignedVariableUsages();
foreach (var issue in issues)
var usages = parseResult.Declarations.Items.Where(declaration =>
declaration.DeclarationType == DeclarationType.Variable
&& !declaration.References.Any(reference => reference.IsAssignment))
.SelectMany(declaration => declaration.References);

foreach (var issue in usages)
{
yield return new UnassignedVariableUsageInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.QualifiedName);
//todo: add context to IdentifierReference
//yield return new UnassignedVariableUsageInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.QualifiedName);
}

return null;
}
}
}
15 changes: 12 additions & 3 deletions RetailCoder.VBE/Inspections/VariableNotAssignedInspection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Inspections
{
Expand All @@ -16,10 +18,17 @@ public VariableNotAssignedInspection()

public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
{
var issues = parseResult.IdentifierUsageInspector.AllUnassignedVariables();
foreach (var issue in issues)
var declarations = parseResult.Declarations.Items.Where(declaration =>
declaration.DeclarationType == DeclarationType.Variable
&& !parseResult.Declarations.Items.Any(item =>
item.IdentifierName == declaration.AsTypeName
&& item.DeclarationType == DeclarationType.UserDefinedType) // UDT variables don't need to be assigned
&& !declaration.IsSelfAssigned
&& !declaration.References.Any(reference => reference.IsAssignment));

foreach (var issue in declarations)
{
yield return new VariableNotAssignedInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.QualifiedName);
yield return new VariableNotAssignedInspectionResult(string.Format(Name, issue.IdentifierName), Severity, issue.Context, issue.QualifiedName.ModuleScope);
}
}
}
Expand Down
26 changes: 0 additions & 26 deletions RetailCoder.VBE/Inspections/VariableNotDeclaredInspection.cs

This file was deleted.

11 changes: 8 additions & 3 deletions RetailCoder.VBE/Inspections/VariableNotUsedInspection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Inspections
{
Expand All @@ -16,10 +18,13 @@ public VariableNotUsedInspection()

public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
{
var issues = parseResult.IdentifierUsageInspector.AllUnusedVariables();
foreach (var issue in issues)
var declarations = parseResult.Declarations.Items.Where(declaration =>
(declaration.DeclarationType == DeclarationType.Variable)
&& !declaration.References.Any(reference => !reference.IsAssignment));

foreach (var issue in declarations)
{
yield return new VariableNotUsedInspectionResult(string.Format(Name, issue.Context.GetText()), Severity, issue.Context, issue.QualifiedName);
yield return new VariableNotUsedInspectionResult(string.Format(Name, issue.IdentifierName), Severity, issue.Context, issue.QualifiedName.ModuleScope);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion RetailCoder.VBE/Rubberduck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@
<Compile Include="Inspections\ParameterNotUsedInspectionResult.cs" />
<Compile Include="Inspections\UnassignedVariableUsageInspection.cs" />
<Compile Include="Inspections\UnassignedVariableUsageInspectionResult.cs" />
<Compile Include="Inspections\VariableNotDeclaredInspection.cs" />
<Compile Include="Inspections\VariableNotDeclaredInspectionResult.cs" />
<Compile Include="Inspections\VariableNotUsedInspection.cs" />
<Compile Include="Inspections\VariableNotUsedInspectionResult.cs" />
Expand Down
37 changes: 25 additions & 12 deletions RetailCoder.VBE/VBA/RubberduckParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Concurrent;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
Expand Down Expand Up @@ -38,25 +40,36 @@ public IEnumerable<VBComponentParseResult> Parse(VBProject project)
var modules = project.VBComponents.Cast<VBComponent>();
foreach(var module in modules)
{
yield return Parse(module);
var result = Parse(module);
if (result != null)
{
yield return result;
}
}
}

public VBComponentParseResult Parse(VBComponent component)
{
VBComponentParseResult cachedValue;
var name = component.QualifiedName();
if (ParseResultCache.TryGetValue(name, out cachedValue))
try
{
return cachedValue;
}
VBComponentParseResult cachedValue;
var name = component.QualifiedName();
if (ParseResultCache.TryGetValue(name, out cachedValue))
{
return cachedValue;
}

var parseTree = Parse(CodeModuleExtensions.Lines(component.CodeModule));
var comments = ParseComments(component);
var result = new VBComponentParseResult(component, parseTree, comments);
var parseTree = Parse(CodeModuleExtensions.Lines(component.CodeModule));
var comments = ParseComments(component);
var result = new VBComponentParseResult(component, parseTree, comments);

ParseResultCache.AddOrUpdate(name, module => result, (qName, module) => result);
return result;
ParseResultCache.AddOrUpdate(name, module => result, (qName, module) => result);
return result;
}
catch (COMException exception)
{
return null;
}
}

public IEnumerable<CommentNode> ParseComments(VBComponent component)
Expand Down
1 change: 0 additions & 1 deletion Rubberduck.Parsing/Rubberduck.Parsing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<Compile Include="Grammar\VBAVisitor.cs" />
<Compile Include="Symbols\IdentifierReference.cs" />
<Compile Include="Symbols\IdentifierReferenceListener.cs" />
<Compile Include="Symbols\IdentifierUsageInspector.cs" />
<Compile Include="VBComponentExtensions.cs" />
<Compile Include="VBComponentParseResult.cs" />
<Compile Include="VBProjectParseResult.cs" />
Expand Down
23 changes: 14 additions & 9 deletions Rubberduck.Parsing/Symbols/Declaration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using Antlr4.Runtime;

namespace Rubberduck.Parsing.Symbols
{
Expand All @@ -8,22 +8,27 @@ namespace Rubberduck.Parsing.Symbols
/// </summary>
public class Declaration
{
public Declaration(int projectHashCode, string parentScope,
string projectName, string componentName, string identifierName, string asTypeName, bool isSelfAssigned,
Accessibility accessibility, DeclarationType declarationType, Selection selection)
public Declaration(QualifiedMemberName qualifiedName, string parentScope,
string identifierName, string asTypeName, bool isSelfAssigned,
Accessibility accessibility, DeclarationType declarationType, ParserRuleContext context)
{
_projectHashCode = projectHashCode;
_qualifiedName = qualifiedName;
_parentScope = parentScope;
_projectName = projectName;
_componentName = componentName;
_identifierName = identifierName;
_asTypeName = asTypeName;
_isSelfAssigned = isSelfAssigned;
_accessibility = accessibility;
_declarationType = declarationType;
_selection = selection;
_selection = context.GetSelection();
_context = context;
}

private readonly QualifiedMemberName _qualifiedName;
public QualifiedMemberName QualifiedName { get { return _qualifiedName; } }

private readonly ParserRuleContext _context;
public ParserRuleContext Context { get { return _context; } }

private readonly IList<IdentifierReference> _references = new List<IdentifierReference>();
public IEnumerable<IdentifierReference> References { get { return _references; } }

Expand Down

0 comments on commit 1545bc1

Please sign in to comment.