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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/GatherVisitorBase.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
// GatherVisitorBase.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin <http://xamarin.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
using System;
 
27
using System.Collections.Generic;
 
28
using System.Diagnostics.CodeAnalysis;
 
29
using System.Linq;
 
30
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
 
 
33
namespace ICSharpCode.NRefactory.CSharp
 
34
{
 
35
        /// <summary>
 
36
        /// A base class for writing issue provider visitor implementations.
 
37
        /// </summary>
 
38
        class GatherVisitorBase<T> : DepthFirstAstVisitor where T : ICodeIssueProvider
 
39
        {
 
40
                /// <summary>
 
41
                /// The issue provider. May be <c>null</c> if none was specified.
 
42
                /// </summary>
 
43
                protected readonly T IssueProvider;
 
44
 
 
45
                protected readonly BaseRefactoringContext ctx;
 
46
                bool isDisabled;
 
47
                bool isGloballySuppressed;
 
48
                List<DomRegion> suppressedRegions =new List<DomRegion> ();
 
49
 
 
50
                [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
 
51
                static string disableString;
 
52
 
 
53
                [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
 
54
                static string restoreString;
 
55
 
 
56
                [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
 
57
                static string suppressMessageCategory;
 
58
 
 
59
                [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
 
60
                static string suppressMessageCheckId;
 
61
 
 
62
                static void SetDisableKeyword(string disableKeyword)
 
63
                {
 
64
                        disableString = "disable " + disableKeyword;
 
65
                        restoreString = "restore " + disableKeyword;
 
66
                }
 
67
 
 
68
                public readonly List<CodeIssue> FoundIssues = new List<CodeIssue> ();
 
69
 
 
70
                static GatherVisitorBase()
 
71
                {
 
72
                        var attr = (IssueDescriptionAttribute)typeof(T).GetCustomAttributes(false).FirstOrDefault(a => a is IssueDescriptionAttribute);
 
73
                        if (attr == null)
 
74
                                return;
 
75
                        if (attr.ResharperDisableKeyword != null) 
 
76
                                SetDisableKeyword(attr.ResharperDisableKeyword);
 
77
                        suppressMessageCheckId  = attr.SuppressMessageCheckId;
 
78
                        suppressMessageCategory = attr.SuppressMessageCategory;
 
79
                }
 
80
 
 
81
                /// <summary>
 
82
                /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.GatherVisitorBase"/> class.
 
83
                /// </summary>
 
84
                /// <param name='ctx'>
 
85
                /// The refactoring context.
 
86
                /// </param>
 
87
                /// <param name='issueProvider'>
 
88
                /// The issue provider.
 
89
                /// </param>
 
90
                public GatherVisitorBase (BaseRefactoringContext ctx, T issueProvider = default(T))
 
91
                {
 
92
                        this.ctx = ctx;
 
93
                        this.IssueProvider = issueProvider;
 
94
                        if (suppressMessageCheckId != null) {
 
95
                                foreach (var attr in this.ctx.Compilation.MainAssembly.AssemblyAttributes) {
 
96
                                        if (attr.AttributeType.Name == "SuppressMessageAttribute" && attr.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") {
 
97
                                                if (attr.PositionalArguments.Count < 2)
 
98
                                                        return;
 
99
                                                var category = attr.PositionalArguments [0].ConstantValue;
 
100
                                                if (category == null || category.ToString() != suppressMessageCategory)
 
101
                                                        continue;
 
102
                                                var checkId = attr.PositionalArguments [1].ConstantValue;
 
103
                                                if (checkId == null || checkId.ToString() != suppressMessageCheckId) 
 
104
                                                        continue;
 
105
                                                isGloballySuppressed = true;
 
106
                                        }
 
107
                                }
 
108
                        }
 
109
                }
 
110
 
 
111
                /// <summary>
 
112
                /// Gets all the issues using the context root node as base.
 
113
                /// </summary>
 
114
                /// <returns>
 
115
                /// The issues.
 
116
                /// </returns>
 
117
                public IEnumerable<CodeIssue> GetIssues()
 
118
                {
 
119
                        ctx.RootNode.AcceptVisitor(this);
 
120
                        return FoundIssues;
 
121
                }
 
122
 
 
123
                protected override void VisitChildren (AstNode node)
 
124
                {
 
125
                        if (ctx.CancellationToken.IsCancellationRequested || isGloballySuppressed)
 
126
                                return;
 
127
                        base.VisitChildren (node);
 
128
                }
 
129
 
 
130
                public override void VisitComment(Comment comment)
 
131
                {
 
132
                        if (comment.CommentType == CommentType.SingleLine && restoreString != null) {
 
133
                                var txt = comment.Content;
 
134
                                if (string.IsNullOrEmpty(txt))
 
135
                                        return;
 
136
                                if (isDisabled) {
 
137
                                        isDisabled &= txt.IndexOf(restoreString, StringComparison.InvariantCulture) < 0;
 
138
                                } else {
 
139
                                        isDisabled |= txt.IndexOf(disableString, StringComparison.InvariantCulture) > 0;
 
140
                                }
 
141
                        }
 
142
                }
 
143
 
 
144
                public override void VisitAttribute(Attribute attribute)
 
145
                {
 
146
                        base.VisitAttribute(attribute);
 
147
                        if (suppressMessageCheckId == null)
 
148
                                return;
 
149
                        var resolveResult = ctx.Resolve(attribute);
 
150
                        if (resolveResult.Type.Name == "SuppressMessageAttribute" && resolveResult.Type.Namespace == "System.Diagnostics.CodeAnalysis") {
 
151
                                if (attribute.Arguments.Count < 2)
 
152
                                        return;
 
153
                                var category = attribute.Arguments.First () as PrimitiveExpression;
 
154
                                if (category == null || category.Value.ToString () != suppressMessageCategory)
 
155
                                        return;
 
156
                                var checkId = attribute.Arguments.Skip (1).First () as PrimitiveExpression;
 
157
                                if (checkId == null || checkId.Value.ToString() != suppressMessageCheckId) 
 
158
                                        return;
 
159
                                suppressedRegions.Add (attribute.Parent.Parent.Region);
 
160
                        }
 
161
                }
 
162
 
 
163
                protected bool IsSuppressed(TextLocation location)
 
164
                {
 
165
                        return isDisabled || isGloballySuppressed || suppressedRegions.Any(r => r.IsInside(location));
 
166
                }
 
167
 
 
168
                protected void AddIssue(AstNode node, string title, System.Action<Script> fix = null)
 
169
                {
 
170
                        if (IsSuppressed(node.StartLocation))
 
171
                                return;
 
172
                        FoundIssues.Add(new CodeIssue (title, node.StartLocation, node.EndLocation, fix != null ? new CodeAction (title, fix, node) : null));
 
173
                }
 
174
 
 
175
                protected void AddIssue(TextLocation start, TextLocation end, string title, System.Action<Script> fix = null)
 
176
                {
 
177
                        if (IsSuppressed(start))
 
178
                                return;
 
179
                        FoundIssues.Add(new CodeIssue(title, start, end, fix != null ? new CodeAction(title, fix, start, end) : null));
 
180
                }
 
181
 
 
182
                protected void AddIssue(AstNode node, string title, CodeAction fix)
 
183
                {
 
184
                        if (IsSuppressed(node.StartLocation))
 
185
                                return;
 
186
                        FoundIssues.Add(new CodeIssue(title, node.StartLocation, node.EndLocation, fix));
 
187
                }
 
188
 
 
189
                protected void AddIssue(TextLocation start, TextLocation end, string title, CodeAction fix)
 
190
                {
 
191
                        if (IsSuppressed(start))
 
192
                                return;
 
193
                        FoundIssues.Add(new CodeIssue (title, start, end, fix));
 
194
                }
 
195
                
 
196
                protected void AddIssue(AstNode node, string title, IEnumerable<CodeAction> fixes)
 
197
                {
 
198
                        if (IsSuppressed(node.StartLocation))
 
199
                                return;
 
200
                        FoundIssues.Add(new CodeIssue(title, node.StartLocation, node.EndLocation, fixes));
 
201
                }
 
202
 
 
203
                protected void AddIssue(TextLocation start, TextLocation end, string title, IEnumerable<CodeAction> fixes)
 
204
                {
 
205
                        if (IsSuppressed(start))
 
206
                                return;
 
207
                        FoundIssues.Add(new CodeIssue (title, start, end, fixes));
 
208
                }
 
209
        }
 
210
}