Skip to content
This repository was archived by the owner on Oct 2, 2022. It is now read-only.

Commit e6b3a80

Browse files
bkoelmanbkoelman
authored andcommitted
Enable concurrent execution / skip generated code; if available
We are skipping generated code anyway, but the builtin check should cause less callbacks, probably making analysis more efficient.
1 parent ff16e1f commit e6b3a80

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

src/CodeContractNullability/CodeContractNullability/BaseAnalyzer.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using System.Collections.Immutable;
1+
using System;
2+
using System.Collections.Immutable;
23
using System.Diagnostics.CodeAnalysis;
4+
using System.Reflection;
5+
using System.Threading;
36
using CodeContractNullability.ExternalAnnotations;
47
using CodeContractNullability.NullabilityAttributes;
58
using CodeContractNullability.SymbolAnalysis;
@@ -19,6 +22,19 @@ public abstract class BaseAnalyzer : DiagnosticAnalyzer
1922
public const string DisableReportOnNullableValueTypesDiagnosticId = "XNUL";
2023

2124
protected const string Category = "Nullability";
25+
26+
[NotNull]
27+
[ItemCanBeNull]
28+
private static readonly Lazy<MethodInfo> LazyEnableConcurrentExecutionMethod =
29+
new Lazy<MethodInfo>(() => typeof(AnalysisContext).GetMethod("EnableConcurrentExecution"),
30+
LazyThreadSafetyMode.PublicationOnly);
31+
32+
[NotNull]
33+
[ItemCanBeNull]
34+
private static readonly Lazy<MethodInfo> LazyConfigureGeneratedCodeAnalysisMethod =
35+
new Lazy<MethodInfo>(() => typeof(AnalysisContext).GetMethod("ConfigureGeneratedCodeAnalysis"),
36+
LazyThreadSafetyMode.PublicationOnly);
37+
2238
private readonly bool appliesToItem;
2339

2440
[NotNull]
@@ -89,9 +105,30 @@ public override void Initialize([NotNull] AnalysisContext context)
89105
{
90106
Guard.NotNull(context, nameof(context));
91107

108+
TryEnableConcurrentExecution(context);
109+
TrySkipGeneratedCode(context);
110+
92111
context.RegisterCompilationStartAction(StartAnalyzeCompilation);
93112
}
94113

114+
private void TryEnableConcurrentExecution([NotNull] AnalysisContext context)
115+
{
116+
MethodInfo method = LazyEnableConcurrentExecutionMethod.Value;
117+
if (method != null)
118+
{
119+
method.Invoke(context, new object[0]);
120+
}
121+
}
122+
123+
private void TrySkipGeneratedCode([NotNull] AnalysisContext context)
124+
{
125+
MethodInfo method = LazyConfigureGeneratedCodeAnalysisMethod.Value;
126+
if (method != null)
127+
{
128+
method.Invoke(context, new object[] { 0 });
129+
}
130+
}
131+
95132
private void StartAnalyzeCompilation([NotNull] CompilationStartAnalysisContext context)
96133
{
97134
Guard.NotNull(context, nameof(context));

0 commit comments

Comments
 (0)