提交 a23ff109 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge branch 'dev15-rc2' into valueTuple4

......@@ -4,11 +4,13 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
using static Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers;
namespace Microsoft.CodeAnalysis.UnitTests.Diagnostics
{
......@@ -45,5 +47,22 @@ public void GetEffectiveDiagnostics()
AssertEx.Equal(new[] { d1 }, filtered);
}
[Fact]
public void GetAnalyzerTelemetry()
{
var compilation = CSharpCompilation.Create("c", options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
DiagnosticAnalyzer analyzer = new AnalyzerWithDisabledRules();
var analyzers = ImmutableArray.Create(analyzer);
var analyzerOptions = new AnalyzerOptions(ImmutableArray<AdditionalText>.Empty);
var compWithAnalyzers = new CompilationWithAnalyzers(compilation, analyzers, analyzerOptions, CancellationToken.None);
var analysisResult = compWithAnalyzers.GetAnalysisResultAsync(CancellationToken.None).Result;
Assert.Empty(analysisResult.CompilationDiagnostics);
// Even though the analyzer registers a symbol action, it should never be invoked because all of its rules are disabled.
var analyzerTelemetry = compWithAnalyzers.GetAnalyzerTelemetryInfoAsync(analyzer, CancellationToken.None).Result;
Assert.Equal(0, analyzerTelemetry.SymbolActionsCount);
}
}
}
......@@ -51,6 +51,7 @@ internal partial class AnalysisState
private readonly HashSet<SyntaxTree> _treesWithGeneratedSourceEvents;
private readonly HashSet<ISymbol> _partialSymbolsWithGeneratedSourceEvents;
private readonly CompilationData _compilationData;
private readonly CompilationOptions _compilationOptions;
private bool _compilationStartGenerated;
private bool _compilationEndGenerated;
......@@ -63,11 +64,12 @@ internal partial class AnalysisState
private readonly ObjectPool<HashSet<CompilationEvent>> _compilationEventsPool;
private readonly HashSet<CompilationEvent> _pooledEventsWithAnyActionsSet;
public AnalysisState(ImmutableArray<DiagnosticAnalyzer> analyzers, CompilationData compilationData)
public AnalysisState(ImmutableArray<DiagnosticAnalyzer> analyzers, CompilationData compilationData, CompilationOptions compilationOptions)
{
_gate = new object();
_analyzerStateMap = CreateAnalyzerStateMap(analyzers, out _analyzerStates);
_compilationData = compilationData;
_compilationOptions = compilationOptions;
_pendingSourceEvents = new Dictionary<SyntaxTree, HashSet<CompilationEvent>>();
_pendingNonSourceEvents = new HashSet<CompilationEvent>();
_lazyAnalyzerActionCountsMap = null;
......@@ -402,7 +404,7 @@ private async Task EnsureAnalyzerActionCountsInitializedAsync(AnalyzerDriver dri
var builder = ImmutableDictionary.CreateBuilder<DiagnosticAnalyzer, AnalyzerActionCounts>();
foreach (var analyzer in _analyzerStateMap.Keys)
{
var actionCounts = await driver.GetAnalyzerActionCountsAsync(analyzer, cancellationToken).ConfigureAwait(false);
var actionCounts = await driver.GetAnalyzerActionCountsAsync(analyzer, _compilationOptions, cancellationToken).ConfigureAwait(false);
builder.Add(analyzer, actionCounts);
}
......
......@@ -7,6 +7,8 @@ namespace Microsoft.CodeAnalysis.Diagnostics.Telemetry
/// </summary>
internal class AnalyzerActionCounts
{
internal static readonly AnalyzerActionCounts Empty = new AnalyzerActionCounts(null);
internal AnalyzerActionCounts(AnalyzerActions analyzerActions) :
this(
analyzerActions?.CompilationStartActionsCount ?? 0,
......
......@@ -1298,9 +1298,14 @@ protected bool IsGeneratedCode(SyntaxTree tree)
protected bool DoNotAnalyzeGeneratedCode => _doNotAnalyzeGeneratedCode;
internal async Task<AnalyzerActionCounts> GetAnalyzerActionCountsAsync(DiagnosticAnalyzer analyzer, CancellationToken cancellationToken)
internal async Task<AnalyzerActionCounts> GetAnalyzerActionCountsAsync(DiagnosticAnalyzer analyzer, CompilationOptions compilationOptions, CancellationToken cancellationToken)
{
var executor = analyzerExecutor.WithCancellationToken(cancellationToken);
if (IsDiagnosticAnalyzerSuppressed(analyzer, compilationOptions, analyzerManager, executor))
{
return AnalyzerActionCounts.Empty;
}
var analyzerActions = await analyzerManager.GetAnalyzerActionsAsync(analyzer, executor).ConfigureAwait(false);
return new AnalyzerActionCounts(analyzerActions);
}
......
......@@ -129,7 +129,7 @@ private CompilationWithAnalyzers(Compilation compilation, ImmutableArray<Diagnos
_cancellationToken = cancellationToken;
_compilationData = new CompilationData(_compilation);
_analysisState = new AnalysisState(analyzers, _compilationData);
_analysisState = new AnalysisState(analyzers, _compilationData, _compilation.Options);
_analysisResultBuilder = new AnalysisResultBuilder(analysisOptions.LogAnalyzerExecutionTime, analyzers);
_driverPool = new ObjectPool<AnalyzerDriver>(() => _compilation.AnalyzerForLanguage(analyzers, AnalyzerManager.Instance));
_executingConcurrentTreeTasksOpt = analysisOptions.ConcurrentAnalysis ? new Dictionary<SyntaxTree, Tuple<Task, CancellationTokenSource>>() : null;
......@@ -412,7 +412,7 @@ private async Task ComputeAnalyzerDiagnosticsWithoutStateTrackingAsync(Cancellat
var analyzerActionCounts = new Dictionary<DiagnosticAnalyzer, AnalyzerActionCounts>(analyzers.Length);
foreach (var analyzer in analyzers)
{
var actionCounts = await driver.GetAnalyzerActionCountsAsync(analyzer, cancellationToken).ConfigureAwait(false);
var actionCounts = await driver.GetAnalyzerActionCountsAsync(analyzer, compilation.Options, cancellationToken).ConfigureAwait(false);
analyzerActionCounts.Add(analyzer, actionCounts);
}
Func<DiagnosticAnalyzer, AnalyzerActionCounts> getAnalyzerActionCounts = analyzer => analyzerActionCounts[analyzer];
......
......@@ -384,6 +384,24 @@ public sealed class AnalyzerWithNoActions : DiagnosticAnalyzer
public override void Initialize(AnalysisContext context) { }
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class AnalyzerWithDisabledRules : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
"ID1",
"Title1",
"Message1",
"Category1",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: false);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(_ => { }, SymbolKind.NamedType);
}
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class EnsureNoMergedNamespaceSymbolAnalyzer : DiagnosticAnalyzer
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册