What is difference between DaemonStage and ElementProblemAnalyzer?

113 views Asked by At

I'm developing Resharper plugin and I don't realize between Deamon Stages and Element Problem Analyzers?

When I need use one or another? If they both provide code analysis.

1

There are 1 answers

0
citizenmatt On BEST ANSWER

An ElementProblemAnalyzer<T> will only be called for specific nodes in the abstract syntax tree, while a daemon stage gets to process the whole file. The nodes you're interested in are registered in the ElementProblemAnalyzerAttribute constructor, and the T parameter of the base class is the common node interface. If you're interested in just one node type, it's the interface for that node, if you're interested in several, it would be the most common base type, perhaps ITreeNode or ICSharpTreeNode.

[ElementProblemAnalyzer(typeof(ICSharpArgument),…)]
public class MyAnalyzer : ElementProblemAnalyzer<ICSharpArgument>
{
  // ...
}

You'd use an element problem analyser if you only need to check a particular node, without looking at the rest of the file. You can still navigate from the node you're at (for example, given a method call, you could have an analyser for the argument, in which you navigate from the argument node up to the method call node, and look to see if the argument is the same as the default value, meaning it's redundant code).

You would use a daemon stage if you need more context within the file, for example, a list of all of the methods in a class, or more control of how the abstract syntax tree is walked - you can skip child nodes of a method declaration if you're not interested in the statements or expressions within.

If it helps, element problem analysers are actually implemented as daemon stages. They're only supported by C#, VB, JS and XML. Each language has a daemon stage that walks the AST for error checking, and as it does so, calls Run for each analyser that's interested in each node type of the tree.