0

I already have some analyzer and code fix providers defined and looking to find dead code in Visual Basic to clean up our source code. I'm currently struggling to find commented code and determining if they are commented code (dead code) rather than usual comments.

Typical function commented (it could be a multiline comment too, but rarely seen in our code base):

'''' <summary>
'''' This function is unused
'''' </summary>
'''' <returns></returns>
'Public Function IsUnusedFunction() As Boolean

'   ' This function always return true
'   Return True

'End Function

Does anyone achieve that with Roslyn? I wish I could provide our developers with some analyzers and code fix for that.

EDIT

Here's my DiagnosticAnalyzer code bits that I'm now:

Public Overrides Sub Initialize(pContext As AnalysisContext)

    pContext.RegisterSyntaxTreeAction(AddressOf AnalyzeSyntaxTree)

End Sub

Private Async Sub AnalyzeSyntaxTree(pContext As SyntaxTreeAnalysisContext)

    Dim lRoot As SyntaxNode = Await pContext.Tree.GetRootAsync(pContext.CancellationToken)
    Dim lCommentNodes = lRoot.DescendantTrivia().Where(Function(t) t.IsKind(SyntaxKind.CommentTrivia))

    'TODO: Do some logic here to find consecutives comments (ignoring empty line) to perform action

End Sub
Gabriel Mongeon
  • 7,251
  • 3
  • 32
  • 35
  • I think the important question here is: how exactly do you differentiate code in comments from normal comments? That code in comments can be parsed as VB without syntax errors? – svick Oct 28 '15 at 01:23
  • This is not an easy task... I guess that if it can be parsed as VB even with some syntax errors that could be considered as dead code. Also comments block (with 3+ lines commented) could also be potential dead code, since we know we don't comment that much. – Gabriel Mongeon Oct 28 '15 at 10:36
  • Well, that's a problem. any string can be parsed as VB with *some* syntax errors. – svick Oct 28 '15 at 12:42
  • That is true, I guess that assuming our dead code has a good syntax should be a good start. – Gabriel Mongeon Oct 28 '15 at 13:33
  • 2
    This question is a bit broad: Roslyn gives you the pieces you could use to build this, but you'll have to build it. You can walk a syntax tree, strip off comment characters and reparse that. So do you have a specific question that prevents you from writing that code? – Jason Malinowski Oct 28 '15 at 17:03
  • @JasonMalinowski Thanks for the quick reply. I've updated my question with some code bits. I'm still playing around with Roslyn trying to grasp the concepts and way of doing but the hardest parts is to discover the commented lines and determining if it's a commented code or not. – Gabriel Mongeon Oct 28 '15 at 18:19
  • @GabrielMongeon It won't help now, but in future you might want to mark unused methods with ``, which should make them easier to find: [ObsoleteAttribute Class](https://msdn.microsoft.com/en-us/library/system.obsoleteattribute%28v=vs.110%29.aspx). – Andrew Morton Oct 28 '15 at 18:28
  • @AndrewMorton Rest assured that I know that attribute and I also don't comment code. I rather delete them and the history is saved in the source control. But we do have a large code base to maintain and not all developers have the same level of coding skills. – Gabriel Mongeon Oct 28 '15 at 18:53
  • @GabrielMongeon: so still, what's the question? You're right, the TODO there you should go do what the TODO says. :-) Is there a particular step you need help with? I'm trying to avoid this question turning into "write 100 lines of code for you." – Jason Malinowski Oct 30 '15 at 01:02
  • @JasonMalinowski You won't do it for me? Just kidding, the question should have been: "Is it possible using strictly Roslyn to find commented dead code?". You answered correctly by your yesterday's comment: "You can walk a syntax tree, strip off comment characters and reparse that." If you repost that as an answer I will make select it or I will answer myself when I'll fill the TODO at some point in the future. – Gabriel Mongeon Oct 30 '15 at 16:55

0 Answers0