~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/AccessToClosureIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// 
 
2
// AccessToClosureIssue.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System.Collections.Generic;
 
28
using System.Linq;
 
29
using ICSharpCode.NRefactory.CSharp.Analysis;
 
30
using ICSharpCode.NRefactory.Semantics;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
 
 
33
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
34
{
 
35
        public abstract class AccessToClosureIssue : ICodeIssueProvider
 
36
        {
 
37
                ControlFlowGraphBuilder cfgBuilder = new ControlFlowGraphBuilder ();
 
38
 
 
39
                public string Title
 
40
                { get; private set; }
 
41
 
 
42
                protected AccessToClosureIssue (string title)
 
43
                {
 
44
                        Title = title;
 
45
                }
 
46
 
 
47
                public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
 
48
                {
 
49
                        var unit = context.RootNode as SyntaxTree;
 
50
                        if (unit == null)
 
51
                                return Enumerable.Empty<CodeIssue> ();
 
52
                        return new GatherVisitor (context, unit, this).GetIssues ();
 
53
                }
 
54
 
 
55
                protected virtual bool IsTargetVariable (IVariable variable)
 
56
                {
 
57
                        return true;
 
58
                }
 
59
 
 
60
                protected abstract NodeKind GetNodeKind (AstNode node);
 
61
 
 
62
                protected virtual bool CanReachModification (ControlFlowNode node, Statement start,
 
63
                                                                                                     IDictionary<Statement, IList<Node>> modifications)
 
64
                {
 
65
                        return node.NextStatement != null && node.NextStatement != start &&
 
66
                                   modifications.ContainsKey (node.NextStatement);
 
67
                }
 
68
 
 
69
                protected abstract IEnumerable<CodeAction> GetFixes (BaseRefactoringContext context, Node env, 
 
70
                                                                                                                         string variableName);
 
71
 
 
72
                #region GatherVisitor
 
73
 
 
74
                class GatherVisitor : GatherVisitorBase<AccessToClosureIssue>
 
75
                {
 
76
                        string title;
 
77
 
 
78
                        public GatherVisitor (BaseRefactoringContext context, SyntaxTree unit,
 
79
                                                                  AccessToClosureIssue issueProvider)
 
80
                                : base (context, issueProvider)
 
81
                        {
 
82
                                this.title = context.TranslateString (issueProvider.Title);
 
83
                        }
 
84
 
 
85
                        public override void VisitVariableInitializer (VariableInitializer variableInitializer)
 
86
                        {
 
87
                                var variableDecl = variableInitializer.Parent as VariableDeclarationStatement;
 
88
                                if (variableDecl != null)
 
89
                                        CheckVariable (((LocalResolveResult)ctx.Resolve (variableInitializer)).Variable, 
 
90
                                                                   variableDecl.GetParent<Statement> ());
 
91
                                base.VisitVariableInitializer (variableInitializer);
 
92
                        }
 
93
 
 
94
                        public override void VisitForeachStatement (ForeachStatement foreachStatement)
 
95
                        {
 
96
                                CheckVariable (((LocalResolveResult)ctx.Resolve (foreachStatement.VariableNameToken)).Variable,
 
97
                                                           foreachStatement);
 
98
                                base.VisitForeachStatement (foreachStatement);
 
99
                        }
 
100
 
 
101
                        public override void VisitParameterDeclaration (ParameterDeclaration parameterDeclaration)
 
102
                        {
 
103
                                var parent = parameterDeclaration.Parent;
 
104
                                Statement body = null;
 
105
                                if (parent is MethodDeclaration) {
 
106
                                        body = ((MethodDeclaration)parent).Body;
 
107
                                } else if (parent is AnonymousMethodExpression) {
 
108
                                        body = ((AnonymousMethodExpression)parent).Body;
 
109
                                } else if (parent is LambdaExpression) {
 
110
                                        body = ((LambdaExpression)parent).Body as Statement;
 
111
                                } else if (parent is ConstructorDeclaration) {
 
112
                                        body = ((ConstructorDeclaration)parent).Body;
 
113
                                } else if (parent is OperatorDeclaration) {
 
114
                                        body = ((OperatorDeclaration)parent).Body;
 
115
                                }
 
116
                                if (body != null) {
 
117
                                        var lrr = ctx.Resolve (parameterDeclaration) as LocalResolveResult;
 
118
                                        if (lrr != null)
 
119
                                                CheckVariable (lrr.Variable, body);
 
120
                                }
 
121
                                base.VisitParameterDeclaration (parameterDeclaration);
 
122
                        }
 
123
 
 
124
                        void CheckVariable(IVariable variable, Statement env)
 
125
                        {
 
126
                                if (!IssueProvider.IsTargetVariable(variable))
 
127
                                        return;
 
128
 
 
129
                                var root = new Environment (env, env);
 
130
                                var envLookup = new Dictionary<AstNode, Environment> ();
 
131
                                envLookup [env] = root;
 
132
 
 
133
                                foreach (var result in ctx.FindReferences(env, variable)) {
 
134
                                        AddNode(envLookup, new Node(result.Node, IssueProvider.GetNodeKind(result.Node)));
 
135
                                }
 
136
 
 
137
                                root.SortChildren ();
 
138
                                CollectIssues (root, variable.Name);
 
139
                        }
 
140
 
 
141
                        void CollectIssues (Environment env, string variableName)
 
142
                        {
 
143
                                IList<ControlFlowNode> cfg = null;
 
144
                                IDictionary<Statement, IList<Node>> modifications = null;
 
145
 
 
146
                                if (env.Body != null) {
 
147
                                        cfg = IssueProvider.cfgBuilder.BuildControlFlowGraph (env.Body);
 
148
                                        modifications = new Dictionary<Statement, IList<Node>> ();
 
149
                                        foreach (var node in env.Children) {
 
150
                                                if (node.Kind == NodeKind.Modification || node.Kind == NodeKind.ReferenceAndModification) {
 
151
                                                        IList<Node> nodes;
 
152
                                                        if (!modifications.TryGetValue (node.ContainingStatement, out nodes))
 
153
                                                                modifications [node.ContainingStatement] = nodes = new List<Node> ();
 
154
                                                        nodes.Add (node);
 
155
                                                }
 
156
                                        }
 
157
                                }
 
158
 
 
159
                                foreach (var child in env.GetChildEnvironments ()) {
 
160
                                        if (!child.IssueCollected && cfg != null && 
 
161
                                                CanReachModification (cfg, child, modifications))
 
162
                                                CollectAllIssues (child, variableName);
 
163
 
 
164
                                        CollectIssues (child, variableName);
 
165
                                }
 
166
                        }
 
167
 
 
168
                        void CollectAllIssues (Environment env, string variableName)
 
169
                        {
 
170
                                var fixes = IssueProvider.GetFixes (ctx, env, variableName).ToArray ();
 
171
                                env.IssueCollected = true;
 
172
 
 
173
                                foreach (var child in env.Children) {
 
174
                                        if (child is Environment) {
 
175
                                                CollectAllIssues ((Environment)child, variableName);
 
176
                                        } else {
 
177
                                                if (child.Kind != NodeKind.Modification)
 
178
                                                        AddIssue (child.AstNode, title, fixes);
 
179
                                                // stop marking references after the variable is modified in current environment
 
180
                                                if (child.Kind != NodeKind.Reference)
 
181
                                                        break;
 
182
                                        }
 
183
                                }
 
184
                        }
 
185
 
 
186
                        void AddNode (IDictionary<AstNode, Environment> envLookup, Node node)
 
187
                        {
 
188
                                var astParent = node.AstNode.Parent;
 
189
                                var path = new List<AstNode> ();
 
190
                                while (astParent != null) {
 
191
                                        Environment parent;
 
192
                                        if (envLookup.TryGetValue (astParent, out parent)) {
 
193
                                                parent.Children.Add (node);
 
194
                                                return;
 
195
                                        }
 
196
 
 
197
                                        if (astParent is LambdaExpression) {
 
198
                                                parent = new Environment (astParent, ((LambdaExpression)astParent).Body as Statement);
 
199
                                        } else if (astParent is AnonymousMethodExpression) {
 
200
                                                parent = new Environment (astParent, ((AnonymousMethodExpression)astParent).Body);
 
201
                                        }
 
202
 
 
203
                                        path.Add (astParent);
 
204
                                        if (parent != null) {
 
205
                                                foreach (var astNode in path)
 
206
                                                        envLookup [astNode] = parent;
 
207
                                                path.Clear ();
 
208
 
 
209
                                                parent.Children.Add (node);
 
210
                                                node = parent;
 
211
                                        }
 
212
                                        astParent = astParent.Parent;
 
213
                                }
 
214
                        }
 
215
 
 
216
                        bool CanReachModification (IEnumerable<ControlFlowNode> cfg, Environment env,
 
217
                                                                           IDictionary<Statement, IList<Node>> modifications)
 
218
                        {
 
219
                                if (modifications.Count == 0)
 
220
                                        return false;
 
221
 
 
222
                                var start = env.ContainingStatement;
 
223
                                if (modifications.ContainsKey (start) &&
 
224
                                        modifications [start].Any (v => v.AstNode.StartLocation > env.AstNode.EndLocation))
 
225
                                        return true;
 
226
 
 
227
                                var stack = new Stack<ControlFlowNode> (cfg.Where (node => node.NextStatement == start));
 
228
                                var visitedNodes = new HashSet<ControlFlowNode> (stack);
 
229
                                while (stack.Count > 0) {
 
230
                                        var node = stack.Pop ();
 
231
                                        if (IssueProvider.CanReachModification (node, start, modifications))
 
232
                                                return true;
 
233
                                        foreach (var edge in node.Outgoing) {
 
234
                                                if (visitedNodes.Add (edge.To))
 
235
                                                        stack.Push (edge.To);
 
236
                                        }
 
237
                                }
 
238
                                return false;
 
239
                        }
 
240
 
 
241
                }
 
242
 
 
243
                #endregion
 
244
 
 
245
                #region Node
 
246
 
 
247
                protected enum NodeKind
 
248
                {
 
249
                        Reference,
 
250
                        Modification,
 
251
                        ReferenceAndModification,
 
252
                        Environment,
 
253
                }
 
254
 
 
255
                protected class Node
 
256
                {
 
257
                        public AstNode AstNode
 
258
                        { get; private set; }
 
259
 
 
260
                        public NodeKind Kind
 
261
                        { get; private set; }
 
262
 
 
263
                        public Statement ContainingStatement
 
264
                        { get; private set; }
 
265
 
 
266
                        public Node (AstNode astNode, NodeKind kind)
 
267
                        {
 
268
                                AstNode = astNode;
 
269
                                Kind = kind;
 
270
                                ContainingStatement = astNode.GetParent<Statement> ();
 
271
                        }
 
272
 
 
273
                        public virtual IEnumerable<Node> GetAllReferences ()
 
274
                        {
 
275
                                yield return this;
 
276
                        }
 
277
                }
 
278
 
 
279
                protected class Environment : Node
 
280
                {
 
281
                        public Statement Body
 
282
                        { get; private set; }
 
283
 
 
284
                        public bool IssueCollected
 
285
                        { get; set; }
 
286
 
 
287
                        public List<Node> Children
 
288
                        { get; private set; }
 
289
 
 
290
                        public Environment (AstNode astNode, Statement body)
 
291
                                : base (astNode, NodeKind.Environment)
 
292
                        {
 
293
                                Body = body;
 
294
                                Children = new List<Node> ();
 
295
                        }
 
296
 
 
297
                        public override IEnumerable<Node> GetAllReferences ()
 
298
                        {
 
299
                                return Children.SelectMany (child => child.GetAllReferences ());
 
300
                        }
 
301
 
 
302
                        public IEnumerable<Environment> GetChildEnvironments ()
 
303
                        {
 
304
                                return from child in Children
 
305
                                           where child is Environment
 
306
                                           select (Environment)child;
 
307
                        }
 
308
 
 
309
                        public void SortChildren ()
 
310
                        {
 
311
                                Children.Sort ((x, y) => x.AstNode.StartLocation.CompareTo(y.AstNode.StartLocation));
 
312
                                foreach (var env in GetChildEnvironments ())
 
313
                                        env.SortChildren ();
 
314
                        }
 
315
                }
 
316
 
 
317
                #endregion
 
318
        }
 
319
}