示例#1
0
        public void RegisterSymbolAction(DiagnosticAnalyzer analyzer, Action <SymbolAnalysisContext> action, ImmutableArray <SymbolKind> symbolKinds)
        {
            SymbolAnalyzerAction analyzerAction = new SymbolAnalyzerAction(action, symbolKinds, analyzer);

            this.GetOrCreateAnalyzerActions(analyzer).AddSymbolAction(analyzerAction);

            // The SymbolAnalyzerAction does not handle SymbolKind.Parameter because the compiler
            // does not make CompilationEvents for them. As a workaround, handle them specially by
            // registering further SymbolActions (for Methods) and utilize the results to construct
            // the necessary SymbolAnalysisContexts.

            if (symbolKinds.Contains(SymbolKind.Parameter))
            {
                RegisterSymbolAction(
                    analyzer,
                    context =>
                {
                    ImmutableArray <IParameterSymbol> parameters;

                    switch (context.Symbol.Kind)
                    {
                    case SymbolKind.Method:
                        parameters = ((IMethodSymbol)context.Symbol).Parameters;
                        break;

                    case SymbolKind.Property:
                        parameters = ((IPropertySymbol)context.Symbol).Parameters;
                        break;

                    case SymbolKind.NamedType:
                        var namedType            = (INamedTypeSymbol)context.Symbol;
                        var delegateInvokeMethod = namedType.DelegateInvokeMethod;
                        parameters = delegateInvokeMethod?.Parameters ?? ImmutableArray.Create <IParameterSymbol>();
                        break;

                    default:
                        throw new ArgumentException($"{context.Symbol.Kind} is not supported.", nameof(context));
                    }

                    foreach (var parameter in parameters)
                    {
                        if (!parameter.IsImplicitlyDeclared)
                        {
                            action(new SymbolAnalysisContext(
                                       parameter,
                                       context.Compilation,
                                       context.Options,
                                       context.ReportDiagnostic,
                                       context.IsSupportedDiagnostic,
                                       context.CancellationToken));
                        }
                    }
                },
                    ImmutableArray.Create(SymbolKind.Method, SymbolKind.Property, SymbolKind.NamedType));
            }
        }
示例#2
0
 internal void AddSymbolAction(SymbolAnalyzerAction action)
 {
     _symbolActions = _symbolActions.Add(action);
     IsEmpty        = false;
 }