public void DiagnosticEquivalence()
        {
#if DEBUG
            var source =
                @"class C
{
    static int F(string s) { return 1; }
    static int x = F(new { });
    static int y = F(new { A = 1 });
}";
            var tree        = SyntaxFactory.ParseSyntaxTree(source);
            var options     = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, concurrentBuild: false);
            var compilation = CSharpCompilation.Create(GetUniqueName(), new[] { tree }, new[] { MscorlibRef }, options);
            var model       = compilation.GetSemanticModel(tree);
            // Each call to GetDiagnostics will bind field initializers
            // (see https://github.com/dotnet/roslyn/issues/6264).
            var diagnostics1 = model.GetDiagnostics().ToArray();
            var diagnostics2 = model.GetDiagnostics().ToArray();
            diagnostics1.Verify(
                // (4,22): error CS1503: Argument 1: cannot convert from '<empty anonymous type>' to 'string'
                //     static int x = F(new { });
                Diagnostic(1503, "new { }").WithArguments("1", "<empty anonymous type>", "string").WithLocation(4, 22),
                // (5,22): error CS1503: Argument 1: cannot convert from '<anonymous type: int A>' to 'string'
                //     static int y = F(new { A = 1 });
                Diagnostic(1503, "new { A = 1 }").WithArguments("1", "<anonymous type: int A>", "string").WithLocation(5, 22));
            Assert.NotEqual(diagnostics1, diagnostics2);
            Assert.True(DiagnosticIncrementalAnalyzer.AreEquivalent(diagnostics1, diagnostics2));
            // Verify that not all collections are treated as equivalent.
            diagnostics1 = new[] { diagnostics1[0] };
            diagnostics2 = new[] { diagnostics2[1] };
            Assert.NotEqual(diagnostics1, diagnostics2);
            Assert.False(DiagnosticIncrementalAnalyzer.AreEquivalent(diagnostics1, diagnostics2));
#endif
        }
                public DiagnosticAnalyzersAndStates(DiagnosticIncrementalAnalyzer owner, Workspace workspace, AnalyzerManager analyzerManager)
                {
                    _owner = owner;
                    _sharedAnalyzersAndStates     = new WorkspaceAnalyzersAndStates(analyzerManager);
                    _projectAnalyzersAndStatesMap = new ConcurrentDictionary <ProjectId, ProjectAnalyzersAndStates>();

                    this.Workspace = workspace;
                }
Exemple #3
0
                public DiagnosticAnalyzersAndStates(DiagnosticIncrementalAnalyzer owner, Workspace workspace, ImmutableArray <AnalyzerReference> workspaceAnalyzers)
                {
                    this.owner = owner;
                    this.sharedAnalyzersAndStates     = new WorkspaceAnalyzersAndStates(workspaceAnalyzers);
                    this.projectAnalyzersAndStatesMap = new ConcurrentDictionary <ProjectId, ProjectAnalyzersAndStates>();

                    this.Workspace = workspace;
                }
                public DiagnosticAnalyzersAndStates(DiagnosticIncrementalAnalyzer owner, Workspace workspace, AnalyzerManager analyzerManager)
                {
                    _owner = owner;
                    _sharedAnalyzersAndStates = new WorkspaceAnalyzersAndStates(analyzerManager);
                    _projectAnalyzersAndStatesMap = new ConcurrentDictionary<ProjectId, ProjectAnalyzersAndStates>();

                    this.Workspace = workspace;
                }
        private async Task <DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult> > GetCompilerAnalysisResultAsync(Stream stream, Dictionary <string, DiagnosticAnalyzer> analyzerMap, Project project, CancellationToken cancellationToken)
        {
            // handling of cancellation and exception
            var version = await DiagnosticIncrementalAnalyzer.GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);

            using (var reader = new ObjectReader(stream))
            {
                return(DiagnosticResultSerializer.Deserialize(reader, analyzerMap, project, version, cancellationToken));
            }
        }
Exemple #6
0
        private async Task <DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult> > GetCompilerAnalysisResultAsync(Stream stream, Dictionary <string, DiagnosticAnalyzer> analyzerMap, Project project, CancellationToken cancellationToken)
        {
            // handling of cancellation and exception
            var version = await DiagnosticIncrementalAnalyzer.GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);

            using (var reader = StreamObjectReader.TryGetReader(stream))
            {
                Debug.Assert(reader != null,
                             @"We only ge a reader for data transmitted between live processes.
This data should always be correct as we're never persisting the data between sessions.");
                return(DiagnosticResultSerializer.Deserialize(reader, analyzerMap, project, version, cancellationToken));
            }
        }
        private static async Task <DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult> > ReadCompilerAnalysisResultAsync(
            Stream stream,
            Dictionary <string, DiagnosticAnalyzer> analyzerMap,
            DocumentAnalysisScope?documentAnalysisScope,
            Project project,
            CancellationToken cancellationToken)
        {
            // handling of cancellation and exception
            var version = await DiagnosticIncrementalAnalyzer.GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);

            using var reader = ObjectReader.TryGetReader(stream, leaveOpen: true, cancellationToken);

            // We only get a reader for data transmitted between live processes.
            // This data should always be correct as we're never persisting the data between sessions.
            Contract.ThrowIfNull(reader);

            return(DiagnosticResultSerializer.ReadDiagnosticAnalysisResults(reader, analyzerMap, documentAnalysisScope, project, version, cancellationToken));
        }
Exemple #8
0
        private async Task <DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult> > AnalyzeInProcAsync(
            DocumentAnalysisScope?documentAnalysisScope,
            Project project,
            CompilationWithAnalyzers compilationWithAnalyzers,
            RemoteHostClient?client,
            bool logPerformanceInfo,
            bool getTelemetryInfo,
            CancellationToken cancellationToken)
        {
            var version = await DiagnosticIncrementalAnalyzer.GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);

            var(analysisResult, additionalPragmaSuppressionDiagnostics) = await compilationWithAnalyzers.GetAnalysisResultAsync(
                documentAnalysisScope, project, AnalyzerInfoCache, cancellationToken).ConfigureAwait(false);

            if (logPerformanceInfo)
            {
                // if remote host is there, report performance data
                var asyncToken = _asyncOperationListener.BeginAsyncOperation(nameof(AnalyzeInProcAsync));
                var _          = FireAndForgetReportAnalyzerPerformanceAsync(documentAnalysisScope, project, client, analysisResult, cancellationToken).CompletesAsyncOperation(asyncToken);
            }

            var analyzers            = documentAnalysisScope?.Analyzers ?? compilationWithAnalyzers.Analyzers;
            var skippedAnalyzersInfo = project.GetSkippedAnalyzersInfo(AnalyzerInfoCache);

            // get compiler result builder map
            var builderMap = await analysisResult.ToResultBuilderMapAsync(
                additionalPragmaSuppressionDiagnostics, documentAnalysisScope, project, version,
                compilationWithAnalyzers.Compilation, analyzers, skippedAnalyzersInfo,
                compilationWithAnalyzers.AnalysisOptions.ReportSuppressedDiagnostics, cancellationToken).ConfigureAwait(false);

            var result    = builderMap.ToImmutableDictionary(kv => kv.Key, kv => DiagnosticAnalysisResult.CreateFromBuilder(kv.Value));
            var telemetry = getTelemetryInfo
                ? analysisResult.AnalyzerTelemetryInfo
                : ImmutableDictionary <DiagnosticAnalyzer, AnalyzerTelemetryInfo> .Empty;

            return(DiagnosticAnalysisResultMap.Create(result, telemetry));
        }
Exemple #9
0
 public IDELatestDiagnosticGetter(DiagnosticIncrementalAnalyzer owner) : this(owner, null)
 {
 }
 public IDELatestDiagnosticGetter(DiagnosticIncrementalAnalyzer owner) : this(owner, null)
 {
 }
 public IDELatestDiagnosticGetter(DiagnosticIncrementalAnalyzer owner, ImmutableHashSet<string> diagnosticIds) : base(owner, diagnosticIds)
 {
 }
 public AnalyzerExecutor(DiagnosticIncrementalAnalyzer owner)
 {
     _owner = owner;
 }
 public LatestDiagnosticsGetter(DiagnosticIncrementalAnalyzer owner, ImmutableHashSet<string> diagnosticIds) : base(owner)
 {
     this.DiagnosticIds = diagnosticIds;
 }
Exemple #14
0
 public DiagnosticsGetter(DiagnosticIncrementalAnalyzer owner)
 {
     this.Owner = owner;
 }
            > AnalyzeOutOfProcAsync(
            DocumentAnalysisScope?documentAnalysisScope,
            Project project,
            CompilationWithAnalyzers compilationWithAnalyzers,
            RemoteHostClient client,
            bool forceExecuteAllAnalyzers,
            bool logPerformanceInfo,
            bool getTelemetryInfo,
            CancellationToken cancellationToken
            )
        {
            var solution = project.Solution;

            using var pooledObject = SharedPools
                                     .Default <Dictionary <string, DiagnosticAnalyzer> >()
                                     .GetPooledObject();
            var analyzerMap = pooledObject.Object;

            var analyzers =
                documentAnalysisScope?.Analyzers
                ?? compilationWithAnalyzers.Analyzers.Where(
                    a => forceExecuteAllAnalyzers || !a.IsOpenFileOnly(solution.Options)
                    );

            analyzerMap.AppendAnalyzerMap(analyzers);

            if (analyzerMap.Count == 0)
            {
                return(DiagnosticAnalysisResultMap <
                           DiagnosticAnalyzer,
                           DiagnosticAnalysisResult
                           > .Empty);
            }

            var argument = new DiagnosticArguments(
                compilationWithAnalyzers.AnalysisOptions.ReportSuppressedDiagnostics,
                logPerformanceInfo,
                getTelemetryInfo,
                documentAnalysisScope?.TextDocument.Id,
                documentAnalysisScope?.Span,
                documentAnalysisScope?.Kind,
                project.Id,
                analyzerMap.Keys.ToArray()
                );

            var result = await client
                         .TryInvokeAsync <
                IRemoteDiagnosticAnalyzerService,
                SerializableDiagnosticAnalysisResults
                >(
                solution,
                invocation : (service, solutionInfo, cancellationToken) =>
                service.CalculateDiagnosticsAsync(
                    solutionInfo,
                    argument,
                    cancellationToken
                    ),
                cancellationToken
                )
                         .ConfigureAwait(false);

            if (!result.HasValue)
            {
                return(DiagnosticAnalysisResultMap <
                           DiagnosticAnalyzer,
                           DiagnosticAnalysisResult
                           > .Empty);
            }

            // handling of cancellation and exception
            var version = await DiagnosticIncrementalAnalyzer
                          .GetDiagnosticVersionAsync(project, cancellationToken)
                          .ConfigureAwait(false);

            var documentIds =
                (documentAnalysisScope != null)
                    ? ImmutableHashSet.Create(documentAnalysisScope.TextDocument.Id)
                    : null;

            return(new DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult>(
                       result.Value.Diagnostics.ToImmutableDictionary(
                           entry => analyzerMap[entry.analyzerId],
                           entry =>
                           DiagnosticAnalysisResult.Create(
                               project,
                               version,
                               syntaxLocalMap: Hydrate(entry.diagnosticMap.Syntax, project),
                               semanticLocalMap: Hydrate(entry.diagnosticMap.Semantic, project),
                               nonLocalMap: Hydrate(entry.diagnosticMap.NonLocal, project),
                               others: entry.diagnosticMap.Other,
                               documentIds
                               )
                           ),
                       result.Value.Telemetry.ToImmutableDictionary(
                           entry => analyzerMap[entry.analyzerId],
                           entry => entry.telemetry
                           )
                       ));
        }
Exemple #16
0
 public LatestDiagnosticsGetter(DiagnosticIncrementalAnalyzer owner, ImmutableHashSet <string> diagnosticIds) : base(owner)
 {
     this.DiagnosticIds = diagnosticIds;
 }
Exemple #17
0
 public IDECachedDiagnosticGetter(DiagnosticIncrementalAnalyzer owner) : base(owner)
 {
 }
 public DiagnosticsGetter(DiagnosticIncrementalAnalyzer owner)
 {
     this.Owner = owner;
 }
 public AnalyzerExecutor(DiagnosticIncrementalAnalyzer owner)
 {
     _owner = owner;
 }
Exemple #20
0
 public IDELatestDiagnosticGetter(DiagnosticIncrementalAnalyzer owner, ImmutableHashSet <string> diagnosticIds) : base(owner, diagnosticIds)
 {
 }
 public IDECachedDiagnosticGetter(DiagnosticIncrementalAnalyzer owner) : base(owner)
 {
 }