Skip to content

Commit

Permalink
Merge branch 'next' of https://github.com/rubberduck-vba/Rubberduck i…
Browse files Browse the repository at this point in the history
…nto CleanComSafeLeakRound2
  • Loading branch information
bclothier committed Nov 13, 2018
2 parents 06ae0be + cf1fdbd commit 71034ff
Show file tree
Hide file tree
Showing 62 changed files with 1,844 additions and 877 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Inspections.CodePathAnalysis.Extensions
{
Expand Down Expand Up @@ -50,5 +51,34 @@ public static INode GetFirstNode(this INode node, IEnumerable<Type> excludedType

return GetFirstNode(node.Children[0], excludedTypes);
}

public static List<IdentifierReference> GetIdentifierReferences(this INode node)
{
var nodes = new List<IdentifierReference>();

var blockNodes = node.GetNodes(new[] { typeof(BlockNode) });
foreach (var block in blockNodes)
{
INode lastNode = default;
foreach (var flattenedNode in block.GetFlattenedNodes(new[] { typeof(GenericNode), typeof(BlockNode) }))
{
if (flattenedNode is AssignmentNode &&
lastNode is AssignmentNode)
{
nodes.Add(lastNode.Reference);
}

lastNode = flattenedNode;
}

if (lastNode is AssignmentNode &&
block.Children[0].GetFirstNode(new[] { typeof(GenericNode) }) is DeclarationNode)
{
nodes.Add(lastNode.Reference);
}
}

return nodes;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Antlr4.Runtime.Tree;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public abstract class NodeBase : INode
{
protected NodeBase(IParseTree tree)
{
Children = new List<INode>().ToImmutableList();
ParseTree = tree;
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }
public IParseTree ParseTree { get; }
public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
}
}
3 changes: 2 additions & 1 deletion Rubberduck.CodeAnalysis/CodePathAnalysis/Nodes/INode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Rubberduck.Parsing.Symbols;
using System.Collections.Immutable;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
Expand All @@ -8,7 +9,7 @@ public interface INode
int SortOrder { get; set; }
ImmutableList<INode> Children { get; set; }
INode Parent { get; set; }

IParseTree ParseTree { get; }
Declaration Declaration { get; set; }
IdentifierReference Reference { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Rubberduck.Parsing.Symbols;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public class AssignmentNode : INode
public class AssignmentNode : NodeBase
{
public AssignmentNode()
{
Children = new List<INode>().ToImmutableList();
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }

public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
public AssignmentNode(IParseTree tree) : base(tree) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Rubberduck.Parsing.Symbols;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public class BlockNode : INode
public class BlockNode : NodeBase
{
public BlockNode()
{
Children = new List<INode>().ToImmutableList();
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }

public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
public BlockNode(IParseTree tree) : base(tree) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Rubberduck.Parsing.Symbols;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public class BranchNode : IBranchNode
public class BranchNode : NodeBase, IBranchNode
{
public BranchNode()
{
Children = new List<INode>().ToImmutableList();
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }

public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
public BranchNode(IParseTree tree) : base(tree) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Rubberduck.Parsing.Symbols;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public class DeclarationNode : INode
public class DeclarationNode : NodeBase
{
public DeclarationNode()
{
Children = new List<INode>().ToImmutableList();
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }

public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
public DeclarationNode(IParseTree tree) : base(tree) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Rubberduck.Parsing.Symbols;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public class GenericNode : INode
public class GenericNode : NodeBase
{
public GenericNode()
{
Children = new List<INode>().ToImmutableList();
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }

public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
public GenericNode(IParseTree tree) : base(tree) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
using Rubberduck.Parsing.Symbols;
using System.Collections.Generic;
using System.Collections.Immutable;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public class LoopNode : ILoopNode
public class LoopNode : NodeBase, ILoopNode
{
public LoopNode()
{
Children = new List<INode>().ToImmutableList();
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }

public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
public LoopNode(IParseTree tree) : base(tree) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Rubberduck.Parsing.Symbols;
using Antlr4.Runtime.Tree;

namespace Rubberduck.Inspections.CodePathAnalysis.Nodes
{
public class ReferenceNode : INode
public class ReferenceNode : NodeBase
{
public ReferenceNode()
{
Children = new List<INode>().ToImmutableList();
}

public int SortOrder { get; set; }
public ImmutableList<INode> Children { get; set; }
public INode Parent { get; set; }

public Declaration Declaration { get; set; }
public IdentifierReference Reference { get; set; }
public ReferenceNode(IParseTree tree) : base(tree) { }
}
}
15 changes: 8 additions & 7 deletions Rubberduck.CodeAnalysis/CodePathAnalysis/Walker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Antlr4.Runtime;

namespace Rubberduck.Inspections.CodePathAnalysis
{
Expand All @@ -19,7 +20,7 @@ public INode GenerateTree(IParseTree tree, Declaration declaration)
case VBAParser.ForEachStmtContext _:
case VBAParser.WhileWendStmtContext _:
case VBAParser.DoLoopStmtContext _:
node = new LoopNode();
node = new LoopNode(tree);
break;
case VBAParser.IfStmtContext _:
case VBAParser.ElseBlockContext _:
Expand All @@ -28,16 +29,16 @@ public INode GenerateTree(IParseTree tree, Declaration declaration)
case VBAParser.SingleLineElseClauseContext _:
case VBAParser.CaseClauseContext _:
case VBAParser.CaseElseClauseContext _:
node = new BranchNode();
node = new BranchNode(tree);
break;
case VBAParser.BlockContext _:
node = new BlockNode();
node = new BlockNode(tree);
break;
}

if (declaration.Context == tree)
{
node = new DeclarationNode
node = new DeclarationNode(tree)
{
Declaration = declaration
};
Expand All @@ -48,14 +49,14 @@ public INode GenerateTree(IParseTree tree, Declaration declaration)
{
if (reference.IsAssignment)
{
node = new AssignmentNode
node = new AssignmentNode(tree)
{
Reference = reference
};
}
else
{
node = new ReferenceNode
node = new ReferenceNode(tree)
{
Reference = reference
};
Expand All @@ -64,7 +65,7 @@ public INode GenerateTree(IParseTree tree, Declaration declaration)

if (node == null)
{
node = new GenericNode();
node = new GenericNode(tree);
}

var children = new List<INode>();
Expand Down
Loading

0 comments on commit 71034ff

Please sign in to comment.