-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMethods.cs
More file actions
64 lines (54 loc) · 2.47 KB
/
Methods.cs
File metadata and controls
64 lines (54 loc) · 2.47 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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Semmle.Util;
namespace Semmle.Extraction.CSharp.Populators
{
public static class MethodExtensions
{
class AstLineCounter : CSharpSyntaxVisitor<LineCounts>
{
public override LineCounts DefaultVisit(SyntaxNode node)
{
string text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString();
return Semmle.Util.LineCounter.ComputeLineCounts(text);
}
public override LineCounts VisitMethodDeclaration(MethodDeclarationSyntax method)
{
return Visit(method.Identifier, method.Body ?? (SyntaxNode)method.ExpressionBody);
}
public LineCounts Visit(SyntaxToken identifier, SyntaxNode body)
{
int start = identifier.GetLocation().SourceSpan.Start;
int end = body.GetLocation().SourceSpan.End - 1;
var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start);
string text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n";
return Semmle.Util.LineCounter.ComputeLineCounts(text);
}
public override LineCounts VisitConstructorDeclaration(ConstructorDeclarationSyntax method)
{
return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody);
}
public override LineCounts VisitDestructorDeclaration(DestructorDeclarationSyntax method)
{
return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody);
}
public override LineCounts VisitOperatorDeclaration(OperatorDeclarationSyntax node)
{
return Visit(node.OperatorToken, node.Body ?? (SyntaxNode)node.ExpressionBody);
}
}
public static void NumberOfLines(this Context cx, ISymbol symbol, IEntity callable)
{
foreach (var decl in symbol.DeclaringSyntaxReferences)
{
cx.NumberOfLines((CSharpSyntaxNode)decl.GetSyntax(), callable);
}
}
public static void NumberOfLines(this Context cx, CSharpSyntaxNode node, IEntity callable)
{
var lineCounts = node.Accept(new AstLineCounter());
cx.Emit(Tuples.numlines(callable, lineCounts));
}
}
}