mirror of https://github.com/icsharpcode/ILSpy.git
9 changed files with 442 additions and 24 deletions
@ -0,0 +1,132 @@
@@ -0,0 +1,132 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using System.Linq; |
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Analysis |
||||
{ |
||||
[TestFixture] |
||||
public class DefiniteAssignmentTests |
||||
{ |
||||
[Test] |
||||
public void TryFinally() |
||||
{ |
||||
BlockStatement block = new BlockStatement { |
||||
new TryCatchStatement { |
||||
TryBlock = new BlockStatement { |
||||
new GotoStatement("LABEL"), |
||||
new AssignmentExpression(new IdentifierExpression("i"), new PrimitiveExpression(1)) |
||||
}, |
||||
CatchClauses = { |
||||
new CatchClause { |
||||
Body = new BlockStatement { |
||||
new AssignmentExpression(new IdentifierExpression("i"), new PrimitiveExpression(3)) |
||||
} |
||||
} |
||||
}, |
||||
FinallyBlock = new BlockStatement { |
||||
new AssignmentExpression(new IdentifierExpression("j"), new PrimitiveExpression(5)) |
||||
} |
||||
}, |
||||
new LabelStatement { Label = "LABEL" }, |
||||
new EmptyStatement() |
||||
}; |
||||
TryCatchStatement tryCatchStatement = (TryCatchStatement)block.Statements.First(); |
||||
Statement stmt1 = tryCatchStatement.TryBlock.Statements.ElementAt(1); |
||||
Statement stmt3 = tryCatchStatement.CatchClauses.Single().Body.Statements.Single(); |
||||
Statement stmt5 = tryCatchStatement.FinallyBlock.Statements.Single(); |
||||
LabelStatement label = (LabelStatement)block.Statements.ElementAt(1); |
||||
|
||||
DefiniteAssignmentAnalysis da = new DefiniteAssignmentAnalysis(block); |
||||
da.Analyze("i"); |
||||
Assert.AreEqual(0, da.UnassignedVariableUses.Count); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(tryCatchStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.CodeUnreachable, da.GetStatusBefore(stmt1)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.CodeUnreachable, da.GetStatusAfter(stmt1)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(stmt3)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusAfter(stmt3)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(stmt5)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusAfter(stmt5)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusAfter(tryCatchStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusBefore(label)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusAfter(label)); |
||||
|
||||
da.Analyze("j"); |
||||
Assert.AreEqual(0, da.UnassignedVariableUses.Count); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(tryCatchStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.CodeUnreachable, da.GetStatusBefore(stmt1)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.CodeUnreachable, da.GetStatusAfter(stmt1)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(stmt3)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusAfter(stmt3)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(stmt5)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusAfter(stmt5)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusAfter(tryCatchStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusBefore(label)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusAfter(label)); |
||||
} |
||||
|
||||
[Test] |
||||
public void ConditionalAnd() |
||||
{ |
||||
IfElseStatement ifStmt = new IfElseStatement { |
||||
Condition = new BinaryOperatorExpression { |
||||
Left = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.GreaterThan, new PrimitiveExpression(0)), |
||||
Operator = BinaryOperatorType.ConditionalAnd, |
||||
Right = new BinaryOperatorExpression { |
||||
Left = new ParenthesizedExpression { |
||||
Expression = new AssignmentExpression { |
||||
Left = new IdentifierExpression("i"), |
||||
Operator = AssignmentOperatorType.Assign, |
||||
Right = new IdentifierExpression("y") |
||||
} |
||||
}, |
||||
Operator = BinaryOperatorType.GreaterThanOrEqual, |
||||
Right = new PrimitiveExpression(0) |
||||
} |
||||
}, |
||||
TrueStatement = new BlockStatement(), |
||||
FalseStatement = new BlockStatement() |
||||
}; |
||||
DefiniteAssignmentAnalysis da = new DefiniteAssignmentAnalysis(ifStmt); |
||||
da.Analyze("i"); |
||||
Assert.AreEqual(0, da.UnassignedVariableUses.Count); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(ifStmt)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusBefore(ifStmt.TrueStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(ifStmt.FalseStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusAfter(ifStmt)); |
||||
} |
||||
|
||||
[Test] |
||||
public void ConditionalOr() |
||||
{ |
||||
IfElseStatement ifStmt = new IfElseStatement { |
||||
Condition = new BinaryOperatorExpression { |
||||
Left = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.GreaterThan, new PrimitiveExpression(0)), |
||||
Operator = BinaryOperatorType.ConditionalOr, |
||||
Right = new BinaryOperatorExpression { |
||||
Left = new ParenthesizedExpression { |
||||
Expression = new AssignmentExpression { |
||||
Left = new IdentifierExpression("i"), |
||||
Operator = AssignmentOperatorType.Assign, |
||||
Right = new IdentifierExpression("y") |
||||
} |
||||
}, |
||||
Operator = BinaryOperatorType.GreaterThanOrEqual, |
||||
Right = new PrimitiveExpression(0) |
||||
} |
||||
}, |
||||
TrueStatement = new BlockStatement(), |
||||
FalseStatement = new BlockStatement() |
||||
}; |
||||
DefiniteAssignmentAnalysis da = new DefiniteAssignmentAnalysis(ifStmt); |
||||
da.Analyze("i"); |
||||
Assert.AreEqual(0, da.UnassignedVariableUses.Count); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(ifStmt)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusBefore(ifStmt.TrueStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.DefinitelyAssigned, da.GetStatusBefore(ifStmt.FalseStatement)); |
||||
Assert.AreEqual(DefiniteAssignmentStatus.PotentiallyAssigned, da.GetStatusAfter(ifStmt)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,204 @@
@@ -0,0 +1,204 @@
|
||||
// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Diagnostics; |
||||
using System.Globalization; |
||||
using System.IO; |
||||
using System.Text.RegularExpressions; |
||||
|
||||
namespace ICSharpCode.NRefactory.Utils |
||||
{ |
||||
/// <summary>
|
||||
/// GraphViz graph.
|
||||
/// </summary>
|
||||
public sealed class GraphVizGraph |
||||
{ |
||||
List<GraphVizNode> nodes = new List<GraphVizNode>(); |
||||
List<GraphVizEdge> edges = new List<GraphVizEdge>(); |
||||
|
||||
public string rankdir; |
||||
public string Title; |
||||
|
||||
public void AddEdge(GraphVizEdge edge) |
||||
{ |
||||
edges.Add(edge); |
||||
} |
||||
|
||||
public void AddNode(GraphVizNode node) |
||||
{ |
||||
nodes.Add(node); |
||||
} |
||||
|
||||
public void Save(string fileName) |
||||
{ |
||||
using (StreamWriter writer = new StreamWriter(fileName)) |
||||
Save(writer); |
||||
} |
||||
|
||||
public void Show() |
||||
{ |
||||
Show(null); |
||||
} |
||||
|
||||
public void Show(string name) |
||||
{ |
||||
if (name == null) |
||||
name = Title; |
||||
if (name != null) |
||||
foreach (char c in Path.GetInvalidFileNameChars()) |
||||
name = name.Replace(c, '-'); |
||||
string fileName = name != null ? Path.Combine(Path.GetTempPath(), name) : Path.GetTempFileName(); |
||||
Save(fileName + ".gv"); |
||||
Process.Start("dot", "\"" + fileName + ".gv\" -Tpng -o \"" + fileName + ".png\"").WaitForExit(); |
||||
Process.Start(fileName + ".png"); |
||||
} |
||||
|
||||
static string Escape(string text) |
||||
{ |
||||
if (Regex.IsMatch(text, @"^[\w\d]+$")) { |
||||
return text; |
||||
} else { |
||||
return "\"" + text.Replace("\\", "\\\\").Replace("\r", "").Replace("\n", "\\n").Replace("\"", "\\\"") + "\""; |
||||
} |
||||
} |
||||
|
||||
static void WriteGraphAttribute(TextWriter writer, string name, string value) |
||||
{ |
||||
if (value != null) |
||||
writer.WriteLine("{0}={1};", name, Escape(value)); |
||||
} |
||||
|
||||
internal static void WriteAttribute(TextWriter writer, string name, double? value, ref bool isFirst) |
||||
{ |
||||
if (value != null) { |
||||
WriteAttribute(writer, name, value.Value.ToString(CultureInfo.InvariantCulture), ref isFirst); |
||||
} |
||||
} |
||||
|
||||
internal static void WriteAttribute(TextWriter writer, string name, bool? value, ref bool isFirst) |
||||
{ |
||||
if (value != null) { |
||||
WriteAttribute(writer, name, value.Value ? "true" : "false", ref isFirst); |
||||
} |
||||
} |
||||
|
||||
internal static void WriteAttribute(TextWriter writer, string name, string value, ref bool isFirst) |
||||
{ |
||||
if (value != null) { |
||||
if (isFirst) |
||||
isFirst = false; |
||||
else |
||||
writer.Write(','); |
||||
writer.Write("{0}={1}", name, Escape(value)); |
||||
} |
||||
} |
||||
|
||||
public void Save(TextWriter writer) |
||||
{ |
||||
if (writer == null) |
||||
throw new ArgumentNullException("writer"); |
||||
writer.WriteLine("digraph G {"); |
||||
writer.WriteLine("node [fontsize = 16];"); |
||||
WriteGraphAttribute(writer, "rankdir", rankdir); |
||||
foreach (GraphVizNode node in nodes) { |
||||
node.Save(writer); |
||||
} |
||||
foreach (GraphVizEdge edge in edges) { |
||||
edge.Save(writer); |
||||
} |
||||
writer.WriteLine("}"); |
||||
} |
||||
} |
||||
|
||||
public sealed class GraphVizEdge |
||||
{ |
||||
public readonly string Source, Target; |
||||
|
||||
/// <summary>edge stroke color</summary>
|
||||
public string color; |
||||
/// <summary>use edge to affect node ranking</summary>
|
||||
public bool? constraint; |
||||
|
||||
public string label; |
||||
|
||||
public string style; |
||||
|
||||
/// <summary>point size of label</summary>
|
||||
public int? fontsize; |
||||
|
||||
public GraphVizEdge(string source, string target) |
||||
{ |
||||
if (source == null) |
||||
throw new ArgumentNullException("source"); |
||||
if (target == null) |
||||
throw new ArgumentNullException("target"); |
||||
this.Source = source; |
||||
this.Target = target; |
||||
} |
||||
|
||||
public GraphVizEdge(int source, int target) |
||||
{ |
||||
this.Source = source.ToString(CultureInfo.InvariantCulture); |
||||
this.Target = target.ToString(CultureInfo.InvariantCulture); |
||||
} |
||||
|
||||
public void Save(TextWriter writer) |
||||
{ |
||||
writer.Write("{0} -> {1} [", Source, Target); |
||||
bool isFirst = true; |
||||
GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "style", style, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "color", color, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "constraint", constraint, ref isFirst); |
||||
writer.WriteLine("];"); |
||||
} |
||||
} |
||||
|
||||
public sealed class GraphVizNode |
||||
{ |
||||
public readonly string ID; |
||||
public string label; |
||||
|
||||
public string labelloc; |
||||
|
||||
/// <summary>point size of label</summary>
|
||||
public int? fontsize; |
||||
|
||||
/// <summary>minimum height in inches</summary>
|
||||
public double? height; |
||||
|
||||
/// <summary>space around label</summary>
|
||||
public string margin; |
||||
|
||||
/// <summary>node shape</summary>
|
||||
public string shape; |
||||
|
||||
public GraphVizNode(string id) |
||||
{ |
||||
if (id == null) |
||||
throw new ArgumentNullException("id"); |
||||
this.ID = id; |
||||
} |
||||
|
||||
public GraphVizNode(int id) |
||||
{ |
||||
this.ID = id.ToString(CultureInfo.InvariantCulture); |
||||
} |
||||
|
||||
public void Save(TextWriter writer) |
||||
{ |
||||
writer.Write(ID); |
||||
writer.Write(" ["); |
||||
bool isFirst = true; |
||||
GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "labelloc", labelloc, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "margin", margin, ref isFirst); |
||||
GraphVizGraph.WriteAttribute(writer, "shape", shape, ref isFirst); |
||||
writer.WriteLine("];"); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue