UPDATE
I reflected Microsoft.Cci.dll and build my rule. It works fine. However, I am facing some problem which I put here with all the details. Source code is here. I didn't want to increase the length of this question by putting all the details.
I am trying to write a code analysis rule which would raise warnings for methods having more than 100 lines. I am following this article. However, I am unable to count the number of lines by following the API provided by CodeAnalysis. for example,
public override ProblemCollection Check(Member member)
{
Method method = member as Method;
if (method == null)
{
return null;
}
CheckForLOC(method);
return Problems;
}
Following is the CheckForLOC()
private void CheckForLOC(Method method)
{
int startLineForMethod = method.Body.SourceContext.StartLine;
int endLineForMethod = method.Body.SourceContext.EndLine;
if (endLineForMethod > startLineForMethod
&& ((endLineForMethod - startLineForMethod) > constMaximumLOCforAMethod))
{
Resolution resolution = GetResolution(method, constMaximumLOCforAMethod);
Problem problem = new Problem(resolution);
Problems.Add(problem);
}
}
In the above code, method.Body.SourceContext.StartLine and method.Body.SourceContext.EndLine return the same value. Not sure why.
I also tried using the StatementCollection :-
private void CheckForLOC(Method method)
{
int LOCPerMethod = 0;
if (method.Body.Statements.Count >= 1)
{
foreach (var statement in method.Body.Statements)
{
LOCPerMethod += GetNumberOfLinesPerStatement(statement);
}
}
if (LOCPerMethod > constMaximumLOCforAMethod)
{
Resolution resolution = GetResolution(method, constMaximumLOCforAMethod);
Problem problem = new Problem(resolution);
Problems.Add(problem);
}
}
private int GetNumberOfLinesPerStatement(Statement statement)
{
int LOCperStatement = 0;
if (statement.SourceContext.EndLine > statement.SourceContext.StartLine)
{
LOCperStatement = statement.SourceContext.EndLine - statement.SourceContext.StartLine;
}
return LOCperStatement;
}
Here also, Statement.SourceContext.StartLine and Statement.SourceContext.EndLine return the same value. I see that the StartLine for each statement is different and one needs to substract the StartLine value of the one statement from its previous one's. However, I see that result is erratic. For example, in the below snippet in a method, It gives me the line number of Statement1 as StartLineNumber whereas It should give the StartLineNumber of If(SomeCondition):-
if(SomeCondition)
{
Statement1
Statement2
Statement3
}
Could anybody provide some direction on this?