How do I get a IMethodSymbol or the syntax node of

2020-08-05 11:07发布

问题:

In the implementation of my analyzer I am in the AnalyzeSymbol method:

public override void Initialize(AnalysisContext context)
{
  context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Method);
}

private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
  // here I look through the syntax of the method body in order to find invocations.
  var methodSymbol = context.Symbol as IMethodSymbol;
  var location = methodSymbol.Locations.FirstOrDefault();
  var methodNode = location.SourceTree.GetCompilationUnitRoot().FindNode(locati‌​on.SourceSpan);
  var blockNode = methodNode.ChildNodes().FirstOrDefault(s => s is BlockSyntax);
  var invocations = blockNode.DescendantNodes()
    .Select(s => s as InvocationExpressionSyntax)
    .Where(s => s != null)

  foreach(InvocationExpressionSyntax i in invocations i)
  {
    // look at the signature of the method which is invoked
    // in order to figure out if the arguments are attributed
  }
}

For each InvocationExpressionSyntax node I find, I need to check with which attributes the arguments are declared in the signature of the declaring method the invocation calls (namely my [ReadOnly] attribute see here). In my understanding I need to get somehow from the InvocationExpressionSyntax object to the corresponding IMethodSymbol, but I cannot figure out how. Unlike FieldDeclarationSyntax which has a property Declarations by which I get an IFieldSymbol like posted here, InvocationExpressionSyntax doesn't and the following code I tried yields null for the symbol:

var model = context.Compilation.GetSemanticModel(ies.SyntaxTree);
var symbol = model.GetDeclaredSymbol(ies.Expression);

What am I doing wrong?

Example code of what my analyzer should analyze is:

void Foo()
{
  int i = 42;

  // coming from this InvocationExpressionSyntax node I want to find out that i is an parameter for an argument which has an [ReadOnly] attribute
  Bar(i);

  // coming from this InvocationExpressionSyntax node I want to find out that i is an parameter for an argument which has NOT an [ReadOnly] attribute
  Baz(i);
}

void Bar([ReadOnly] int i)
{
}

void Baz(int i)
{
}

回答1:

You can get declaration symbol for InvocationExpressionSyntax using SymbolFinder

Workspace workspace;
Workspace.TryGetWorkspace(location.SourceTree.GetText().Container, out workspace);
if (workspace != null)
{
    var invocMethodSymbol = model.GetSymbolInfo(ies.Expression).Symbol;
    // Await this task if you can
    var declarationMethodSymbol = SymbolFinder.FindSourceDefinitionAsync(invocMethodSymbol, workspace.CurrentSolution).Result;
}