-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStatementCollector.cs
More file actions
75 lines (63 loc) · 2.15 KB
/
StatementCollector.cs
File metadata and controls
75 lines (63 loc) · 2.15 KB
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
using System.Collections.Generic;
using System.Linq;
using Microsoft.Cci;
using Microsoft.Cci.Analysis;
using Microsoft.Cci.ILToCodeModel;
namespace andrena.Usus.net.Core.Metrics.Methods
{
internal class StatementCollector : CodeTraverser
{
bool requireLocations;
List<IStatement> statements;
public int ResultCount
{
get { return statements.Count; }
}
public StatementCollector(PdbReader pdb)
{
statements = new List<IStatement>();
requireLocations = pdb != null;
}
public override void TraverseChildren(IStatement statement)
{
if (statement is IEmptyStatement) return;
if (statement is IReturnStatement && (statement as IReturnStatement).Expression == null) return;
RememberStatement(statement);
base.TraverseChildren(statement);
}
private void RememberStatement(IStatement statement)
{
if (requireLocations)
RememberStatementWithLocation(statement);
else
RememberStatementWithoutLocation(statement);
}
private void RememberStatementWithLocation(IStatement statement)
{
if (HasLocation(statement) || IsConditional(statement) || IsDeclaration(statement))
statements.Add(statement);
}
private void RememberStatementWithoutLocation(IStatement statement)
{
if (IsNotBlock(statement))
statements.Add(statement);
}
private bool IsNotBlock(IStatement statement)
{
return !(statement is IBlockStatement);
}
private bool HasLocation(IStatement statement)
{
return statement.Locations.Any();
}
private bool IsConditional(IStatement statement)
{
return statement is IConditionalStatement;
}
private bool IsDeclaration(IStatement statement)
{
var declaration = statement as ILocalDeclarationStatement;
return declaration != null && declaration.InitialValue != null;
}
}
}