未验证 提交 7cf994c6 编写于 作者: M Manish Vasani 提交者: GitHub

Merge pull request #39394 from mavasani/ReuseSemanticModel

Re-use semantic model when attempting to compute suppress message att…
......@@ -341,7 +341,10 @@ public SuppressionInfo GetSuppressionInfo(Compilation compilation)
AttributeData attribute;
var suppressMessageState = new SuppressMessageAttributeState(compilation);
if (!suppressMessageState.IsDiagnosticSuppressed(this, out attribute))
if (!suppressMessageState.IsDiagnosticSuppressed(
this,
getSemanticModel: (compilation, tree) => compilation.GetSemanticModel(tree),
out attribute))
{
attribute = null;
}
......
......@@ -309,7 +309,7 @@ private void Initialize(AnalyzerExecutor analyzerExecutor, DiagnosticQueue diagn
var analyzerExecutor = AnalyzerExecutor.Create(
compilation, analysisOptions.Options ?? AnalyzerOptions.Empty, addNotCategorizedDiagnosticOpt, newOnAnalyzerException, analysisOptions.AnalyzerExceptionFilter,
IsCompilerAnalyzer, AnalyzerManager, ShouldSkipAnalysisOnGeneratedCode, ShouldSuppressGeneratedCodeDiagnostic, IsGeneratedOrHiddenCodeLocation, IsAnalyzerSuppressedForTree, GetAnalyzerGate,
getSemanticModel: tree => CurrentCompilationData.GetOrCreateCachedSemanticModel(tree, compilation, cancellationToken),
getSemanticModel: GetOrCreateSemanticModel,
analysisOptions.LogAnalyzerExecutionTime, addCategorizedLocalDiagnosticOpt, addCategorizedNonLocalDiagnosticOpt, s => _programmaticSuppressions.Add(s), cancellationToken);
Initialize(analyzerExecutor, diagnosticQueue, compilationData, cancellationToken);
......@@ -649,7 +649,7 @@ public async Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(Compilation co
Diagnostic d;
while (DiagnosticQueue.TryDequeue(out d))
{
d = suppressMessageState.ApplySourceSuppressions(d);
d = suppressMessageState.ApplySourceSuppressions(d, GetOrCreateSemanticModel);
if (reportSuppressedDiagnostics || !d.IsSuppressed)
{
allDiagnostics.Add(d);
......@@ -659,6 +659,12 @@ public async Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(Compilation co
return allDiagnostics.ToReadOnlyAndFree();
}
private SemanticModel GetOrCreateSemanticModel(SyntaxTree tree)
=> GetOrCreateSemanticModel(AnalyzerExecutor.Compilation, tree);
private SemanticModel GetOrCreateSemanticModel(Compilation compilation, SyntaxTree tree)
=> CurrentCompilationData.GetOrCreateCachedSemanticModel(tree, compilation, AnalyzerExecutor.CancellationToken);
public void ApplyProgrammaticSuppressions(DiagnosticBag reportedDiagnostics, Compilation compilation)
{
Debug.Assert(!reportedDiagnostics.IsEmptyWithoutResolution);
......@@ -817,11 +823,15 @@ public ImmutableArray<Diagnostic> DequeueNonLocalDiagnosticsAndApplySuppressions
private ImmutableArray<Diagnostic> FilterDiagnosticsSuppressedInSourceOrByAnalyzers(ImmutableArray<Diagnostic> diagnostics, Compilation compilation)
{
diagnostics = FilterDiagnosticsSuppressedInSource(diagnostics, compilation, CurrentCompilationData.SuppressMessageAttributeState);
diagnostics = FilterDiagnosticsSuppressedInSource(diagnostics, compilation, CurrentCompilationData.SuppressMessageAttributeState, GetOrCreateSemanticModel);
return ApplyProgrammaticSuppressions(diagnostics, compilation);
}
private static ImmutableArray<Diagnostic> FilterDiagnosticsSuppressedInSource(ImmutableArray<Diagnostic> diagnostics, Compilation compilation, SuppressMessageAttributeState suppressMessageState)
private static ImmutableArray<Diagnostic> FilterDiagnosticsSuppressedInSource(
ImmutableArray<Diagnostic> diagnostics,
Compilation compilation,
SuppressMessageAttributeState suppressMessageState,
Func<Compilation, SyntaxTree, SemanticModel> getSemanticModel)
{
if (diagnostics.IsEmpty)
{
......@@ -837,7 +847,7 @@ private static ImmutableArray<Diagnostic> FilterDiagnosticsSuppressedInSource(Im
DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationsInCompilation(diagnostics[i], compilation);
#endif
var diagnostic = suppressMessageState.ApplySourceSuppressions(diagnostics[i]);
var diagnostic = suppressMessageState.ApplySourceSuppressions(diagnostics[i], getSemanticModel);
if (!reportSuppressedDiagnostics && diagnostic.IsSuppressed)
{
// Diagnostic suppressed in source.
......
// 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.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
......@@ -1149,7 +1150,13 @@ public static IEnumerable<Diagnostic> GetEffectiveDiagnostics(ImmutableArray<Dia
private static IEnumerable<Diagnostic> GetEffectiveDiagnosticsImpl(ImmutableArray<Diagnostic> diagnostics, Compilation compilation)
{
if (diagnostics.IsEmpty)
{
yield break;
}
var suppressMessageState = new SuppressMessageAttributeState(compilation);
var semanticModelsByTree = new Dictionary<SyntaxTree, SemanticModel>();
foreach (var diagnostic in diagnostics)
{
if (diagnostic != null)
......@@ -1157,10 +1164,21 @@ private static IEnumerable<Diagnostic> GetEffectiveDiagnosticsImpl(ImmutableArra
var effectiveDiagnostic = compilation.Options.FilterDiagnostic(diagnostic);
if (effectiveDiagnostic != null)
{
yield return suppressMessageState.ApplySourceSuppressions(effectiveDiagnostic);
yield return suppressMessageState.ApplySourceSuppressions(effectiveDiagnostic, getSemanticModel);
}
}
}
SemanticModel getSemanticModel(Compilation compilation, SyntaxTree tree)
{
if (!semanticModelsByTree.TryGetValue(tree, out var model))
{
model = compilation.GetSemanticModel(tree);
semanticModelsByTree.Add(tree, model);
}
return model;
}
}
/// <summary>
......
......@@ -99,7 +99,7 @@ internal SuppressMessageAttributeState(Compilation compilation)
_localSuppressionsBySymbol = new ConcurrentDictionary<ISymbol, ImmutableDictionary<string, SuppressMessageInfo>>();
}
public Diagnostic ApplySourceSuppressions(Diagnostic diagnostic, ISymbol symbolOpt = null)
public Diagnostic ApplySourceSuppressions(Diagnostic diagnostic, Func<Compilation, SyntaxTree, SemanticModel> getSemanticModel, ISymbol symbolOpt = null)
{
if (diagnostic.IsSuppressed)
{
......@@ -108,7 +108,7 @@ public Diagnostic ApplySourceSuppressions(Diagnostic diagnostic, ISymbol symbolO
}
SuppressMessageInfo info;
if (IsDiagnosticSuppressed(diagnostic, out info))
if (IsDiagnosticSuppressed(diagnostic, getSemanticModel, out info))
{
// Attach the suppression info to the diagnostic.
diagnostic = diagnostic.WithIsSuppressed(true);
......@@ -117,10 +117,10 @@ public Diagnostic ApplySourceSuppressions(Diagnostic diagnostic, ISymbol symbolO
return diagnostic;
}
public bool IsDiagnosticSuppressed(Diagnostic diagnostic, out AttributeData suppressingAttribute)
public bool IsDiagnosticSuppressed(Diagnostic diagnostic, Func<Compilation, SyntaxTree, SemanticModel> getSemanticModel, out AttributeData suppressingAttribute)
{
SuppressMessageInfo info;
if (IsDiagnosticSuppressed(diagnostic, out info))
if (IsDiagnosticSuppressed(diagnostic, getSemanticModel, out info))
{
suppressingAttribute = info.Attribute;
return true;
......@@ -130,10 +130,10 @@ public bool IsDiagnosticSuppressed(Diagnostic diagnostic, out AttributeData supp
return false;
}
private bool IsDiagnosticSuppressed(Diagnostic diagnostic, out SuppressMessageInfo info)
=> IsDiagnosticSuppressed(diagnostic.Id, diagnostic.Location, out info);
private bool IsDiagnosticSuppressed(Diagnostic diagnostic, Func<Compilation, SyntaxTree, SemanticModel> getSemanticModel, out SuppressMessageInfo info)
=> IsDiagnosticSuppressed(diagnostic.Id, diagnostic.Location, getSemanticModel, out info);
private bool IsDiagnosticSuppressed(string id, Location location, out SuppressMessageInfo info)
private bool IsDiagnosticSuppressed(string id, Location location, Func<Compilation, SyntaxTree, SemanticModel> getSemanticModel, out SuppressMessageInfo info)
{
Debug.Assert(id != null);
Debug.Assert(location != null);
......@@ -148,7 +148,7 @@ private bool IsDiagnosticSuppressed(string id, Location location, out SuppressMe
// Walk up the syntax tree checking for suppression by any declared symbols encountered
if (location.IsInSource)
{
var model = _compilation.GetSemanticModel(location.SourceTree);
var model = getSemanticModel(_compilation, location.SourceTree);
bool inImmediatelyContainingSymbol = true;
for (var node = location.SourceTree.GetRoot().FindNode(location.SourceSpan, getInnermostNodeForTie: true);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册