|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 4 | +using Semmle.Util; |
| 5 | + |
| 6 | +namespace Semmle.Extraction.CSharp.Populators |
| 7 | +{ |
| 8 | + internal class AstLineCounter : CSharpSyntaxVisitor<LineCounts> |
| 9 | + { |
| 10 | + public override LineCounts DefaultVisit(SyntaxNode node) |
| 11 | + { |
| 12 | + var text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString(); |
| 13 | + return LineCounter.ComputeLineCounts(text); |
| 14 | + } |
| 15 | + |
| 16 | + public override LineCounts VisitMethodDeclaration(MethodDeclarationSyntax method) |
| 17 | + { |
| 18 | + return Visit(method.Identifier, method.Body ?? (SyntaxNode)method.ExpressionBody); |
| 19 | + } |
| 20 | + |
| 21 | + public static LineCounts Visit(SyntaxToken identifier, SyntaxNode body) |
| 22 | + { |
| 23 | + var start = identifier.GetLocation().SourceSpan.Start; |
| 24 | + var end = body.GetLocation().SourceSpan.End - 1; |
| 25 | + |
| 26 | + var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start); |
| 27 | + |
| 28 | + var text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n"; |
| 29 | + return LineCounter.ComputeLineCounts(text); |
| 30 | + } |
| 31 | + |
| 32 | + public override LineCounts VisitConstructorDeclaration(ConstructorDeclarationSyntax method) |
| 33 | + { |
| 34 | + return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody); |
| 35 | + } |
| 36 | + |
| 37 | + public override LineCounts VisitDestructorDeclaration(DestructorDeclarationSyntax method) |
| 38 | + { |
| 39 | + return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody); |
| 40 | + } |
| 41 | + |
| 42 | + public override LineCounts VisitOperatorDeclaration(OperatorDeclarationSyntax node) |
| 43 | + { |
| 44 | + return Visit(node.OperatorToken, node.Body ?? (SyntaxNode)node.ExpressionBody); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments