Browse Source

Added support for Cyclomatic Complexity metric and for the number of variables inside of a method.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6435 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Tomáš Linhart 15 years ago
parent
commit
b7dd8b62f3
  1. 15
      src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml.cs
  2. 15
      src/AddIns/Analysis/CodeQuality/Src/Method.cs
  3. 7
      src/AddIns/Analysis/CodeQuality/Src/MetricsReader.cs

15
src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml.cs

@ -204,6 +204,8 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -204,6 +204,8 @@ namespace ICSharpCode.CodeQualityAnalysis
} else if (content == "Method") {
cbxMetrics.Items.Add(new ComboBoxItem { Content = "IL instructions" });
cbxMetrics.Items.Add(new ComboBoxItem { Content = "Cyclomatic Complexity" });
cbxMetrics.Items.Add(new ComboBoxItem { Content = "Variables" });
}
}
@ -219,6 +221,8 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -219,6 +221,8 @@ namespace ICSharpCode.CodeQualityAnalysis
var metric = metricItem.Content.ToString();
// TODO: Redone this with enums or some smarter way
if (level == "Assembly") {
//cbxMetrics.Items.Add(new ComboBoxItem { Content = });
} else if (level == "Namespace") {
@ -228,12 +232,19 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -228,12 +232,19 @@ namespace ICSharpCode.CodeQualityAnalysis
} else if (level == "Field") {
} else if (level == "Method") {
if (metric == "IL instructions") {
treemap.ItemsSource = from ns in MetricsReader.MainModule.Namespaces
from type in ns.Types
from method in type.Methods
select method;
}
if (metric == "IL instructions")
treemap.ItemDefinition.ValuePath = "Instructions.Count";
if (metric == "Cyclomatic Complexity")
treemap.ItemDefinition.ValuePath = "CyclomaticComplexity";
if (metric == "Variables")
treemap.ItemDefinition.ValuePath = "Variables";
}
}
}

15
src/AddIns/Analysis/CodeQuality/Src/Method.cs

@ -111,6 +111,16 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -111,6 +111,16 @@ namespace ICSharpCode.CodeQualityAnalysis
/// </summary>
public bool IsReturnTypeGenericInstance { get; set; }
/// <summary>
/// Cyclomatic Complexity of the method
/// </summary>
public int CyclomaticComplexity { get; set; }
/// <summary>
/// The number of variables declared in the body of the method
/// </summary>
public int Variables { get; set; }
public Method()
{
Parameters = new HashSet<MethodParameter>();
@ -127,6 +137,9 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -127,6 +137,9 @@ namespace ICSharpCode.CodeQualityAnalysis
IsReturnTypeGenericInstance = false;
Dependency = null;
CyclomaticComplexity = 0;
Variables = 0;
}
public Relationship GetRelationship(INode node)
@ -149,6 +162,8 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -149,6 +162,8 @@ namespace ICSharpCode.CodeQualityAnalysis
builder.Append(Environment.NewLine);
builder.AppendLine(String.Format("Name: {0}", Name));
builder.AppendLine(String.Format("Parameters: {0}", Parameters.Count));
builder.AppendLine(String.Format("Cyclomatic Complexity: {0}", CyclomaticComplexity));
builder.AppendLine(String.Format("Variables: {0}", Variables));
// more to come
builder.Append(Environment.NewLine);

7
src/AddIns/Analysis/CodeQuality/Src/MetricsReader.cs

@ -336,7 +336,8 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -336,7 +336,8 @@ namespace ICSharpCode.CodeQualityAnalysis
IsAbstract = methodDefinition.IsAbstract,
IsSetter = methodDefinition.IsSetter,
IsGetter = methodDefinition.IsGetter,
IsVirtual = methodDefinition.IsVirtual
IsVirtual = methodDefinition.IsVirtual,
Variables = methodDefinition.Body != null ? methodDefinition.Body.Variables.Count : 0
};
var returnType =
@ -418,6 +419,10 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -418,6 +419,10 @@ namespace ICSharpCode.CodeQualityAnalysis
// Operand = instruction.Operand.ToString() // for now operand as string should be enough
});
// IL cyclomatic complexity
if (instruction.OpCode.FlowControl == FlowControl.Cond_Branch)
method.CyclomaticComplexity++;
var operand = ReadOperand(instruction);
if (operand is MethodDefinition)

Loading…
Cancel
Save