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
|
//
// UnreachableCodeIssue.cs
//
// Author:
// Mansheng Yang <lightyang0@gmail.com>
//
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.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 ICSharpCode.NRefactory.CSharp.Analysis;
using System.Linq;
using ICSharpCode.NRefactory.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[IssueDescription ("Code is unreachable",
Description = "Code is unreachable.",
Category = IssueCategories.Redundancies,
Severity = Severity.Warning,
IssueMarker = IssueMarker.GrayOut)]
public class UnreachableCodeIssue : ICodeIssueProvider
{
public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
{
return new GatherVisitor (context).GetIssues ();
}
class GatherVisitor : GatherVisitorBase<UnreachableCodeIssue>
{
HashSet<AstNode> unreachableNodes;
public GatherVisitor (BaseRefactoringContext ctx)
: base (ctx)
{
unreachableNodes = new HashSet<AstNode> ();
}
public override void VisitMethodDeclaration (MethodDeclaration methodDeclaration)
{
StatementIssueCollector.Collect (this, methodDeclaration.Body);
base.VisitMethodDeclaration (methodDeclaration);
}
public override void VisitLambdaExpression (LambdaExpression lambdaExpression)
{
var blockStatement = lambdaExpression.Body as BlockStatement;
if (blockStatement != null)
StatementIssueCollector.Collect (this, blockStatement);
base.VisitLambdaExpression (lambdaExpression);
}
public override void VisitAnonymousMethodExpression (AnonymousMethodExpression anonymousMethodExpression)
{
StatementIssueCollector.Collect (this, anonymousMethodExpression.Body);
base.VisitAnonymousMethodExpression (anonymousMethodExpression);
}
public override void VisitConditionalExpression (ConditionalExpression conditionalExpression)
{
var resolveResult = ctx.Resolve (conditionalExpression.Condition);
if (resolveResult.ConstantValue is bool) {
var condition = (bool)resolveResult.ConstantValue;
Expression resultExpr, unreachableExpr;
if (condition) {
resultExpr = conditionalExpression.TrueExpression;
unreachableExpr = conditionalExpression.FalseExpression;
} else {
resultExpr = conditionalExpression.FalseExpression;
unreachableExpr = conditionalExpression.TrueExpression;
}
unreachableNodes.Add (unreachableExpr);
AddIssue (unreachableExpr, ctx.TranslateString ("Remove unreachable code"),
script => script.Replace (conditionalExpression, resultExpr.Clone ()));
}
base.VisitConditionalExpression (conditionalExpression);
}
protected override void VisitChildren (AstNode node)
{
// skip unreachable nodes
if (!unreachableNodes.Contains (node))
base.VisitChildren (node);
}
class StatementIssueCollector : DepthFirstAstVisitor
{
GatherVisitor visitor;
ReachabilityAnalysis reachability;
HashSet<Statement> collectedStatements;
private StatementIssueCollector (GatherVisitor visitor, ReachabilityAnalysis reachability)
{
collectedStatements = new HashSet<Statement> ();
this.visitor = visitor;
this.reachability = reachability;
}
public static void Collect (GatherVisitor visitor, BlockStatement body)
{
if (body.IsNull)
return;
var reachability = visitor.ctx.CreateReachabilityAnalysis (body);
var collector = new StatementIssueCollector (visitor, reachability);
collector.VisitBlockStatement (body);
}
protected override void VisitChildren (AstNode node)
{
var statement = node as Statement;
if (statement == null || !AddStatement (statement))
base.VisitChildren (node);
}
bool AddStatement(Statement statement)
{
if (reachability.IsReachable (statement))
return false;
if (collectedStatements.Contains (statement))
return true;
var prevEnd = statement.GetPrevNode (n => !(n is NewLineNode)).EndLocation;
// group multiple continuous statements into one issue
var start = statement.StartLocation;
collectedStatements.Add (statement);
visitor.unreachableNodes.Add (statement);
while (statement.GetNextSibling (s => s is Statement) != null) {
statement = (Statement)statement.GetNextSibling (s => s is Statement);
collectedStatements.Add (statement);
visitor.unreachableNodes.Add (statement);
}
var end = statement.EndLocation;
var removeAction = new CodeAction (visitor.ctx.TranslateString ("Remove unreachable code"),
script =>
{
var startOffset = script.GetCurrentOffset (prevEnd);
var endOffset = script.GetCurrentOffset (end);
script.RemoveText (startOffset, endOffset - startOffset);
}, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation);
var commentAction = new CodeAction(visitor.ctx.TranslateString("Comment unreachable code"),
script =>
{
var startOffset = script.GetCurrentOffset(prevEnd);
script.InsertText(startOffset, Environment.NewLine + "/*");
var endOffset = script.GetCurrentOffset(end);
script.InsertText(endOffset, Environment.NewLine + "*/");
}, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation);
var actions = new [] { removeAction, commentAction };
visitor.AddIssue (start, end, visitor.ctx.TranslateString ("Code is unreachable"), actions);
return true;
}
// skip lambda and anonymous method
public override void VisitLambdaExpression (LambdaExpression lambdaExpression)
{
}
public override void VisitAnonymousMethodExpression (
AnonymousMethodExpression anonymousMethodExpression)
{
}
}
}
}
}
|