Ejemplo n.º 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);
            _symbolActions = _symbolActions.Add(analyzerAction);
        }
Ejemplo n.º 2
0
        public void RegisterSymbolAction(DiagnosticAnalyzer analyzer, Action <SymbolAnalysisContext> action, ImmutableArray <SymbolKind> symbolKinds)
        {
            SymbolAnalyzerAction analyzerAction = new SymbolAnalyzerAction(action, symbolKinds, analyzer);

            this.GetOrCreateAnalyzerActions(analyzer).AddSymbolAction(analyzerAction);
            _symbolActions = _symbolActions.Add(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:
                        INamedTypeSymbol namedType         = (INamedTypeSymbol)context.Symbol;
                        IMethodSymbol delegateInvokeMethod = namedType.DelegateInvokeMethod;
                        parameters = delegateInvokeMethod?.Parameters ?? ImmutableArray.Create <IParameterSymbol>();
                        break;

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

                    foreach (IParameterSymbol 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));
            }
        }
Ejemplo n.º 3
0
 internal void AddSymbolAction(SymbolAnalyzerAction action)
 {
     _symbolActions = _symbolActions.Add(action);
 }
Ejemplo n.º 4
0
 internal void AddSymbolAction(SymbolAnalyzerAction action)
 {
     _symbolActions = _symbolActions.Add(action);
     IsEmpty        = false;
 }