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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
//
// GatherVisitorBase.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <http://xamarin.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using ICSharpCode.NRefactory.CSharp.Refactoring;
using ICSharpCode.NRefactory.TypeSystem;
using Mono.CSharp;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// A base class for writing issue provider visitor implementations.
/// </summary>
class GatherVisitorBase<T> : DepthFirstAstVisitor where T : ICodeIssueProvider
{
/// <summary>
/// The issue provider. May be <c>null</c> if none was specified.
/// </summary>
protected readonly T QualifierDirectiveEvidentIssueProvider;
protected readonly BaseRefactoringContext ctx;
bool isDisabled;
bool isDisabledOnce;
bool isGloballySuppressed;
bool isPragmaDisabled;
List<DomRegion> suppressedRegions =new List<DomRegion> ();
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
static string disableString;
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
static string disableOnceString;
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
static string restoreString;
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
static string suppressMessageCategory;
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
static string suppressMessageCheckId;
[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
static int pragmaWarning;
static void SetDisableKeyword(string disableKeyword)
{
disableString = "disable " + disableKeyword;
disableOnceString = "disable once " + disableKeyword;
restoreString = "restore " + disableKeyword;
}
public readonly List<CodeIssue> FoundIssues = new List<CodeIssue> ();
static GatherVisitorBase()
{
var attr = (IssueDescriptionAttribute)typeof(T).GetCustomAttributes(false).FirstOrDefault(a => a is IssueDescriptionAttribute);
if (attr == null)
return;
if (attr.ResharperDisableKeyword != null)
SetDisableKeyword(attr.ResharperDisableKeyword);
suppressMessageCheckId = attr.SuppressMessageCheckId;
suppressMessageCategory = attr.SuppressMessageCategory;
pragmaWarning = attr.PragmaWarning;
}
/// <summary>
/// Initializes a new instance of the <see cref="GatherVisitorBase{T}"/> class.
/// </summary>
/// <param name='ctx'>
/// The refactoring context.
/// </param>
/// <param name='qualifierDirectiveEvidentIssueProvider'>
/// The issue provider.
/// </param>
public GatherVisitorBase (BaseRefactoringContext ctx, T qualifierDirectiveEvidentIssueProvider = default(T))
{
this.ctx = ctx;
this.QualifierDirectiveEvidentIssueProvider = qualifierDirectiveEvidentIssueProvider;
if (suppressMessageCheckId != null) {
foreach (var attr in this.ctx.Compilation.MainAssembly.AssemblyAttributes) {
if (attr.AttributeType.Name == "SuppressMessageAttribute" && attr.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") {
if (attr.PositionalArguments.Count < 2)
return;
var category = attr.PositionalArguments [0].ConstantValue;
if (category == null || category.ToString() != suppressMessageCategory)
continue;
var checkId = attr.PositionalArguments [1].ConstantValue;
if (checkId == null || checkId.ToString() != suppressMessageCheckId)
continue;
isGloballySuppressed = true;
}
}
}
}
/// <summary>
/// Gets all the issues using the context root node as base.
/// </summary>
/// <returns>
/// The issues.
/// </returns>
public IEnumerable<CodeIssue> GetIssues()
{
ctx.RootNode.AcceptVisitor(this);
return FoundIssues;
}
protected override void VisitChildren (AstNode node)
{
if (ctx.CancellationToken.IsCancellationRequested || isGloballySuppressed)
return;
base.VisitChildren (node);
}
public override void VisitComment(Comment comment)
{
if (comment.CommentType == CommentType.SingleLine && restoreString != null) {
var txt = comment.Content;
if (string.IsNullOrEmpty(txt))
return;
if (isDisabled) {
isDisabled &= txt.IndexOf(restoreString, StringComparison.InvariantCulture) < 0;
} else {
isDisabled |= txt.IndexOf(disableString, StringComparison.InvariantCulture) > 0;
isDisabledOnce |= txt.IndexOf(disableOnceString, StringComparison.InvariantCulture) > 0;
}
}
}
public override void VisitPreProcessorDirective(PreProcessorDirective preProcessorDirective)
{
if (pragmaWarning == 0)
return;
var warning = preProcessorDirective as PragmaWarningPreprocssorDirective;
if (warning == null)
return;
if (warning.WarningList.Contains(pragmaWarning))
isPragmaDisabled = warning.Disable;
}
public override void VisitAttribute(Attribute attribute)
{
base.VisitAttribute(attribute);
if (suppressMessageCheckId == null)
return;
var resolveResult = ctx.Resolve(attribute);
if (resolveResult.Type.Name == "SuppressMessageAttribute" && resolveResult.Type.Namespace == "System.Diagnostics.CodeAnalysis") {
if (attribute.Arguments.Count < 2)
return;
var category = attribute.Arguments.First () as PrimitiveExpression;
if (category == null || category.Value.ToString () != suppressMessageCategory)
return;
var checkId = attribute.Arguments.Skip (1).First () as PrimitiveExpression;
if (checkId == null || checkId.Value.ToString() != suppressMessageCheckId)
return;
suppressedRegions.Add (attribute.Parent.Parent.Region);
}
}
protected bool IsSuppressed(TextLocation location)
{
if (isDisabledOnce) {
isDisabledOnce = false;
return true;
}
return isDisabled || isGloballySuppressed || isPragmaDisabled || suppressedRegions.Any(r => r.IsInside(location));
}
protected void AddIssue(AstNode node, string title, System.Action<Script> fix = null)
{
if (IsSuppressed(node.StartLocation))
return;
FoundIssues.Add(new CodeIssue (title, node.StartLocation, node.EndLocation, fix != null ? new CodeAction (title, fix, node) : null));
}
protected void AddIssue(TextLocation start, TextLocation end, string title, System.Action<Script> fix = null)
{
if (IsSuppressed(start))
return;
FoundIssues.Add(new CodeIssue(title, start, end, fix != null ? new CodeAction(title, fix, start, end) : null));
}
protected void AddIssue(AstNode node, string title, CodeAction fix)
{
if (IsSuppressed(node.StartLocation))
return;
FoundIssues.Add(new CodeIssue(title, node.StartLocation, node.EndLocation, fix));
}
protected void AddIssue(TextLocation start, TextLocation end, string title, CodeAction fix)
{
if (IsSuppressed(start))
return;
FoundIssues.Add(new CodeIssue (title, start, end, fix));
}
protected void AddIssue(AstNode node, string title, IEnumerable<CodeAction> fixes)
{
if (IsSuppressed(node.StartLocation))
return;
FoundIssues.Add(new CodeIssue(title, node.StartLocation, node.EndLocation, fixes));
}
protected void AddIssue(TextLocation start, TextLocation end, string title, IEnumerable<CodeAction> fixes)
{
if (IsSuppressed(start))
return;
FoundIssues.Add(new CodeIssue (title, start, end, fixes));
}
}
}
|