When creating a Visual Studio analyzer, you may ask yourself the question, how can I analyze comments? I say to you good question. Let's start by trying the obvious:

 context.RegisterSyntaxNodeAction(CommentMethod, SyntaxKind.SingleLineCommentTrivia)

You would think that this would work, as I did the first time I tried this. Unfortunately, comment trivia is handled differently and your CommentMethod will never be called. To properly handle comment trivia, you need to register for the entire syntax tree and then parse out the nodes that you want.

Fortunately, this is not difficult. First, in your initialize method, you want to register a SyntaxTreeAction

public override void Initialize(AnalysisContext context)
{
    context.RegisterSyntaxTreeAction(this.HandleSyntaxTree);
}

Then in your syntax tree action, you can parse out the nodes that you care about. For example, below I am looking for empty comments, so I parse out the SingleLineCommentTrivia and MultiLineCommentTrivia :

private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
{
    SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
    var commentNodes = from node in root.DescendantTrivia() where node.IsKind(SyntaxKind.MultiLineCommentTrivia) || node.IsKind(SyntaxKind.SingleLineCommentTrivia) select node;

    if (!commentNodes.Any())
    {
        return;
    }
    foreach (var node in commentNodes)
    {
        string commentText = "";
        switch (node.Kind())
        {
            case SyntaxKind.SingleLineCommentTrivia:
                commentText = node.ToString().TrimStart('/');
                break;
            case SyntaxKind.MultiLineCommentTrivia:
                var nodeText = node.ToString();

                commentText = nodeText.Substring(2, nodeText.Length-4);
                break;
        }

        if (String.IsNullOrWhiteSpace(commentText))
        {
            var diagnostic = Diagnostic.Create(Rule, node.GetLocation());
            context.ReportDiagnostic(diagnostic);
        }
    }

}

This method is pretty straightforward. I use the DescentdantTrivia from the root node to get all of the nodes and then filter out anything except the comment nodes that I wish to parse.

After that it is just removing the comment characters and parsing the text of the comments to ensure they are not empty.

As you can see, parsing comments using the Roslyn code analysis engine is easy once you know the correct way to access the syntax trivia nodes.