/
ParameterCanBeByValInspection.cs
154 lines (131 loc) · 7.81 KB
/
ParameterCanBeByValInspection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Rubberduck.Common;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Results;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Resources.Inspections;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
namespace Rubberduck.Inspections.Concrete
{
public sealed class ParameterCanBeByValInspection : InspectionBase
{
public ParameterCanBeByValInspection(RubberduckParserState state)
: base(state) { }
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
{
var declarations = UserDeclarations.ToArray();
var issues = new List<IInspectionResult>();
var interfaceDeclarationMembers = State.DeclarationFinder.FindAllInterfaceMembers().ToArray();
var interfaceScopes = State.DeclarationFinder.FindAllInterfaceImplementingMembers().Concat(interfaceDeclarationMembers).Select(s => s.Scope).ToArray();
issues.AddRange(GetResults(declarations, interfaceDeclarationMembers));
var eventMembers = declarations.Where(item => item.DeclarationType == DeclarationType.Event).ToArray();
var formEventHandlerScopes = State.FindFormEventHandlers().Select(handler => handler.Scope).ToArray();
var eventHandlerScopes = State.DeclarationFinder.FindEventHandlers().Concat(declarations.FindUserEventHandlers()).Select(e => e.Scope).ToArray();
var eventScopes = eventMembers.Select(s => s.Scope)
.Concat(formEventHandlerScopes)
.Concat(eventHandlerScopes)
.ToArray();
issues.AddRange(GetResults(declarations, eventMembers));
var declareScopes = declarations.Where(item =>
item.DeclarationType == DeclarationType.LibraryFunction
|| item.DeclarationType == DeclarationType.LibraryProcedure)
.Select(e => e.Scope)
.ToArray();
issues.AddRange(declarations.OfType<ParameterDeclaration>()
.Where(declaration => IsIssue(declaration, declarations, declareScopes, eventScopes, interfaceScopes))
.Select(issue => new DeclarationInspectionResult(this, string.Format(InspectionResults.ParameterCanBeByValInspection, issue.IdentifierName), issue)));
return issues;
}
private bool IsIssue(ParameterDeclaration declaration, Declaration[] userDeclarations, string[] declareScopes, string[] eventScopes, string[] interfaceScopes)
{
var isIssue =
!declaration.IsArray
&& !declaration.IsParamArray
&& (declaration.IsByRef || declaration.IsImplicitByRef)
&& (declaration.AsTypeDeclaration == null || declaration.AsTypeDeclaration.DeclarationType != DeclarationType.ClassModule && declaration.AsTypeDeclaration.DeclarationType != DeclarationType.UserDefinedType && declaration.AsTypeDeclaration.DeclarationType != DeclarationType.Enumeration)
&& !declareScopes.Contains(declaration.ParentScope)
&& !eventScopes.Contains(declaration.ParentScope)
&& !interfaceScopes.Contains(declaration.ParentScope)
&& !IsUsedAsByRefParam(userDeclarations, declaration)
&& (!declaration.References.Any() || !declaration.References.Any(reference => reference.IsAssignment));
return isIssue;
}
private IEnumerable<IInspectionResult> GetResults(Declaration[] declarations, Declaration[] declarationMembers)
{
foreach (var declaration in declarationMembers)
{
var declarationParameters = declarations.OfType<ParameterDeclaration>()
.Where(d => Equals(d.ParentDeclaration, declaration))
.OrderBy(o => o.Selection.StartLine)
.ThenBy(t => t.Selection.StartColumn)
.ToList();
if (!declarationParameters.Any()) { continue; }
var parametersAreByRef = declarationParameters.Select(s => true).ToList();
var members = declarationMembers.Any(a => a.DeclarationType == DeclarationType.Event)
? declarations.FindHandlersForEvent(declaration).Select(s => s.Item2).ToList()
: State.DeclarationFinder.FindInterfaceImplementationMembers(declaration).Cast<Declaration>().ToList();
foreach (var member in members)
{
var parameters = declarations.OfType<ParameterDeclaration>()
.Where(d => Equals(d.ParentDeclaration, member))
.OrderBy(o => o.Selection.StartLine)
.ThenBy(t => t.Selection.StartColumn)
.ToList();
//If you hit this assert, reopen https://github.com/rubberduck-vba/Rubberduck/issues/3906
Debug.Assert(parametersAreByRef.Count == parameters.Count);
for (var i = 0; i < parameters.Count; i++)
{
parametersAreByRef[i] = parametersAreByRef[i] &&
!IsUsedAsByRefParam(declarations, parameters[i]) &&
((VBAParser.ArgContext) parameters[i].Context).BYVAL() == null &&
!parameters[i].References.Any(reference => reference.IsAssignment);
}
}
for (var i = 0; i < declarationParameters.Count; i++)
{
if (parametersAreByRef[i])
{
yield return new DeclarationInspectionResult(this,
string.Format(InspectionResults.ParameterCanBeByValInspection, declarationParameters[i].IdentifierName),
declarationParameters[i]);
}
}
}
}
private static bool IsUsedAsByRefParam(IEnumerable<Declaration> declarations, Declaration parameter)
{
// find the procedure calls in the procedure of the parameter.
// note: works harder than it needs to when procedure has more than a single procedure call...
// ...but caching [declarations] would be a memory leak
var items = declarations as List<Declaration> ?? declarations.ToList();
var procedureCalls = items.Where(item => item.DeclarationType.HasFlag(DeclarationType.Member))
.SelectMany(member => member.References.Where(reference => reference.ParentScoping.Equals(parameter.ParentScopeDeclaration)))
.GroupBy(call => call.Declaration)
.ToList(); // only check a procedure once. its declaration doesn't change if it's called 20 times anyway.
foreach (var item in procedureCalls)
{
var calledProcedureArgs = items
.Where(arg => arg.DeclarationType == DeclarationType.Parameter && arg.ParentScope == item.Key.Scope)
.OrderBy(arg => arg.Selection.StartLine)
.ThenBy(arg => arg.Selection.StartColumn)
.ToArray();
foreach (var declaration in calledProcedureArgs)
{
if (((VBAParser.ArgContext) declaration.Context).BYVAL() != null)
{
continue;
}
if (declaration.References.Any(reference => reference.IsAssignment))
{
return true;
}
}
}
return false;
}
}
}