AnalyzerDriver.CompilationData.cs 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Microsoft.CodeAnalysis.Diagnostics
{
    internal abstract partial class AnalyzerDriver : IDisposable
    {
13
        private static readonly WeakReference<CompilationData> s_cachedCompilationData = new WeakReference<CompilationData>(null);
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

        internal class CompilationData
        {
            /// <summary>
            /// Cached semantic model for the compilation trees.
            /// PERF: This cache enables us to re-use semantic model's bound node cache across analyzer execution and diagnostic queries.
            /// </summary>
            private readonly Dictionary<SyntaxTree, SemanticModel> _semanticModelsMap;

            /// <summary>
            /// Cached syntax references for a symbol for the lifetime of symbol declared event.
            /// PERF: This cache reduces allocations for computing declaring syntax references for a symbol.
            /// </summary>
            private readonly Dictionary<ISymbol, ImmutableArray<SyntaxReference>> _symbolDeclarationsMap;

            public CompilationData(Compilation comp)
            {
31
                this.Compilation = comp;
32 33 34 35 36 37
                _semanticModelsMap = new Dictionary<SyntaxTree, SemanticModel>();
                _symbolDeclarationsMap = new Dictionary<ISymbol, ImmutableArray<SyntaxReference>>();
                this.SuppressMessageAttributeState = new SuppressMessageAttributeState(comp);
                this.DeclarationAnalysisDataMap = new Dictionary<SyntaxReference, DeclarationAnalysisData>();
            }

38 39 40 41 42
            /// <summary>
            /// Compilation whose data is cached.
            /// </summary>
            public readonly Compilation Compilation;

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
            public SuppressMessageAttributeState SuppressMessageAttributeState { get; }
            public Dictionary<SyntaxReference, DeclarationAnalysisData> DeclarationAnalysisDataMap { get; }

            public SemanticModel GetOrCreateCachedSemanticModel(SyntaxTree tree, Compilation compilation, CancellationToken cancellationToken)
            {
                SemanticModel model;
                lock (_semanticModelsMap)
                {
                    if (_semanticModelsMap.TryGetValue(tree, out model))
                    {
                        return model;
                    }
                }


                model = compilation.GetSemanticModel(tree);

                // Invoke GetDiagnostics to populate the compilation's CompilationEvent queue.
                model.GetDiagnostics(null, cancellationToken);

                lock (_semanticModelsMap)
                {
                    _semanticModelsMap[tree] = model;
                }

                return model;
            }

            public bool RemoveCachedSemanticModel(SyntaxTree tree)
            {
                lock (_semanticModelsMap)
                {
                    return _semanticModelsMap.Remove(tree);
                }
            }

            public bool TryGetCachedDeclaringReferences(ISymbol symbol, out ImmutableArray<SyntaxReference> declaringReferences)
            {
                lock (_symbolDeclarationsMap)
                {
                    if (!_symbolDeclarationsMap.TryGetValue(symbol, out declaringReferences))
                    {
                        declaringReferences = default(ImmutableArray<SyntaxReference>);
                        return false;
                    }

                    return true;
                }
            }

            public void CacheDeclaringReferences(ISymbol symbol, ImmutableArray<SyntaxReference> declaringReferences)
            {
                lock (_symbolDeclarationsMap)
                {
                    _symbolDeclarationsMap[symbol] = declaringReferences;
                }
            }

            public bool RemoveCachedDeclaringReferences(ISymbol symbol)
            {
                lock (_symbolDeclarationsMap)
                {
                    return _symbolDeclarationsMap.Remove(symbol);
                }
            }
        }

        internal class DeclarationAnalysisData
        {
            /// <summary>
            /// GetSyntax() for the given SyntaxReference.
            /// </summary>
            public SyntaxNode DeclaringReferenceSyntax { get; set; }

            /// <summary>
            /// Topmost declaration node for analysis.
            /// </summary>
            public SyntaxNode TopmostNodeForAnalysis { get; set; }

            /// <summary>
            /// All member declarations within the declaration.
            /// </summary>
            public List<DeclarationInfo> DeclarationsInNode { get; }

            /// <summary>
            /// All descendant nodes for syntax node actions.
            /// </summary>
            public List<SyntaxNode> DescendantNodesToAnalyze { get; }

            /// <summary>
            /// Flag indicating if this is a partial analysis.
            /// </summary>
            public bool IsPartialAnalysis { get; set; }

            public DeclarationAnalysisData()
            {
                this.DeclarationsInNode = new List<DeclarationInfo>();
                this.DescendantNodesToAnalyze = new List<SyntaxNode>();
            }

            public void Free()
            {
                DeclaringReferenceSyntax = null;
                TopmostNodeForAnalysis = null;
                DeclarationsInNode.Clear();
                DescendantNodesToAnalyze.Clear();
                IsPartialAnalysis = false;
            }
        }

153
        internal static CompilationData GetOrCreateCachedCompilationData(Compilation compilation)
154
        {
155 156 157 158 159 160 161 162 163 164 165 166 167 168
            CompilationData data;
            if (!s_cachedCompilationData.TryGetTarget(out data) || data == null || data.Compilation != compilation)
            {
                data = new CompilationData(compilation);
                s_cachedCompilationData.SetTarget(data);
            }

            return data;
        }

        internal static bool TryGetCachedCompilationData(Compilation compilation, out CompilationData compilationData)
        {
            return s_cachedCompilationData.TryGetTarget(out compilationData) &&
                compilationData?.Compilation == compilation;
169 170
        }

171
        internal static void RemoveCachedCompilationData(Compilation compilation)
172
        {
173 174 175 176 177
            CompilationData compilationData;
            if (TryGetCachedCompilationData(compilation, out compilationData))
            {
                s_cachedCompilationData.SetTarget(null);
            }
178 179 180 181
        }

        public static SemanticModel GetOrCreateCachedSemanticModel(SyntaxTree tree, Compilation compilation, CancellationToken cancellationToken)
        {
182
            var compilationData = GetOrCreateCachedCompilationData(compilation);
183 184 185
            return compilationData.GetOrCreateCachedSemanticModel(tree, compilation, cancellationToken);
        }

186
        public static void RemoveCachedSemanticModel(SyntaxTree tree, Compilation compilation)
187 188
        {
            CompilationData compilationData;
189 190
            if (TryGetCachedCompilationData(compilation, out compilationData))
            {
191
                compilationData.RemoveCachedSemanticModel(tree);
192
            }
193 194 195 196
        }

        public static bool TryGetCachedDeclaringReferences(ISymbol symbol, Compilation compilation, out ImmutableArray<SyntaxReference> declaringReferences)
        {
197
            var compilationData = GetOrCreateCachedCompilationData(compilation);
198 199 200 201 202
            return compilationData.TryGetCachedDeclaringReferences(symbol, out declaringReferences);
        }

        public static void CacheDeclaringReferences(ISymbol symbol, Compilation compilation, ImmutableArray<SyntaxReference> declaringReferences)
        {
203
            var compilationData = GetOrCreateCachedCompilationData(compilation);
204 205 206
            compilationData.CacheDeclaringReferences(symbol, declaringReferences);
        }

207
        public static void RemoveCachedDeclaringReferences(ISymbol symbol, Compilation compilation)
208 209
        {
            CompilationData compilationData;
210 211
            if (TryGetCachedCompilationData(compilation, out compilationData))
            {
212
                compilationData.RemoveCachedDeclaringReferences(symbol);
213
            }
214 215 216
        }
    }
}