in a Roslyn Analyzer project, I want to flag all symbols marked with a particular attribute. For example, if the symbol is a method, then I want this method (or rather, its definition, although this distinction is not so relevant because GetAttributes
gives me what I need anyways) to be checked for this attribute anywhere it is invoked or even simply used without any direct invocation (such as in a method group). Likewise, I would want any references (variable declaration, cast, type parameter, return type, etc.) to a certain (named) type to be checked to see if the type is decorated with the corresponding attribute.
Now, I thought I could get away with simply registering a symbol action using RegisterSymbolAction
on AnalysisContext
, but the thing is that, while I have the ability to break on symbols directly (without doing any syntax manipulation), I do not have a SemanticModel
to interpret the symbols I find, as it is not on the SymbolAnalysisContext
type. This means that I cannot even check if the attribute is of the right type, let alone do any other related comparative operation.
Now, from what I have gathered from the source code, the semantic model is not guaranteed to be valid when a call to the handler method that is provided to RegisterSymbolAction
is invoked for a particular symbol (since it might not even have finished building yet). That being said, is there a way to actually be provided with a symbol (or at least a collection of encountered symbols) and a corresponding valid semantic model at the same time ? What I am trying to avoid, if I can, is to be forced to go through a whole syntax tree (most probably obtained from a SemanticModelAnalysisContext
) and to interpret each and every node to its potential symbol equivalent.
I am not saying that it is not a valid solution, I am just looking for a potential alternative that I do not know about. I was maybe thinking of something along the lines of CompilationAnalysisContext
or CodeBlockAnalysisContext
, but so far, I've had no luck.
If I understand correctly, you're trying to get access to a
SemanticModel
fromSymbolAnalysisContext
?On the
Compilation
property you can useGetSemanticModel()
and pass in the syntax tree of the symbol you're looking at.