DiagnosticLogAggregator.cs 3.8 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
2 3 4 5 6 7

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Internal.Log;

8
namespace Microsoft.CodeAnalysis.Diagnostics.Log
9 10 11 12 13
{
    internal class DiagnosticLogAggregator : LogAggregator
    {
        public static readonly string[] AnalyzerTypes =
        {
14
            "Analyzer.CodeBlock",
15 16
            "Analyzer.CodeBlockEnd",
            "Analyzer.CodeBlockStart",
17
            "Analyzer.Compilation",
18 19 20 21 22 23 24 25
            "Analyzer.CompilationEnd",
            "Analyzer.CompilationStart",
            "Analyzer.SemanticModel",
            "Analyzer.Symbol",
            "Analyzer.SyntaxNode",
            "Analyzer.SyntaxTree"
        };

26 27
        private readonly DiagnosticAnalyzerService _owner;
        private ImmutableDictionary<Type, AnalyzerInfo> _analyzerInfoMap;
28 29 30

        public DiagnosticLogAggregator(DiagnosticAnalyzerService owner)
        {
31 32
            _owner = owner;
            _analyzerInfoMap = ImmutableDictionary<Type, AnalyzerInfo>.Empty;
33 34 35 36
        }

        public IEnumerable<KeyValuePair<Type, AnalyzerInfo>> AnalyzerInfoMap
        {
37
            get { return _analyzerInfoMap; }
38 39 40 41
        }

        public void UpdateAnalyzerTypeCount(DiagnosticAnalyzer analyzer, AnalyzerActions analyzerActions)
        {
42
            var telemetry = DiagnosticAnalyzerLogger.AllowsTelemetry(_owner, analyzer);
43 44

            ImmutableInterlocked.AddOrUpdate(
45
                ref _analyzerInfoMap,
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
                analyzer.GetType(),
                addValue: new AnalyzerInfo(analyzer, analyzerActions, telemetry),
                updateValueFactory: (k, ai) =>
                {
                    ai.SetAnalyzerTypeCount(analyzerActions);
                    return ai;
                });
        }

        public class AnalyzerInfo
        {
            public Type CLRType;
            public bool Telemetry;
            public int[] Counts = new int[DiagnosticLogAggregator.AnalyzerTypes.Length];

            public AnalyzerInfo(DiagnosticAnalyzer analyzer, AnalyzerActions analyzerActions, bool telemetry)
            {
                CLRType = analyzer.GetType();
                Telemetry = telemetry;

66 67 68 69 70 71 72 73 74 75
                Counts[0] = analyzerActions.CodeBlockActionsCount;
                Counts[1] = analyzerActions.CodeBlockEndActionsCount;
                Counts[2] = analyzerActions.CodeBlockStartActionsCount;
                Counts[3] = analyzerActions.CompilationActionsCount;
                Counts[4] = analyzerActions.CompilationEndActionsCount;
                Counts[5] = analyzerActions.CompilationStartActionsCount;
                Counts[6] = analyzerActions.SemanticModelActionsCount;
                Counts[7] = analyzerActions.SymbolActionsCount;
                Counts[8] = analyzerActions.SyntaxNodeActionsCount;
                Counts[9] = analyzerActions.SyntaxTreeActionsCount;
76 77 78 79
            }

            public void SetAnalyzerTypeCount(AnalyzerActions analyzerActions)
            {
80 81 82 83 84 85 86 87 88 89
                Counts[0] = analyzerActions.CodeBlockActionsCount;
                Counts[1] = analyzerActions.CodeBlockEndActionsCount;
                Counts[2] = analyzerActions.CodeBlockStartActionsCount;
                Counts[3] = analyzerActions.CompilationActionsCount;
                Counts[4] = analyzerActions.CompilationEndActionsCount;
                Counts[5] = analyzerActions.CompilationStartActionsCount;
                Counts[6] = analyzerActions.SemanticModelActionsCount;
                Counts[7] = analyzerActions.SymbolActionsCount;
                Counts[8] = analyzerActions.SyntaxNodeActionsCount;
                Counts[9] = analyzerActions.SyntaxTreeActionsCount;
90 91 92 93
            }
        }
    }
}