Skip to content

Commit 93eb9f2

Browse files
Cache lambdas in analyzer driver (#80759)
Fixes #37532
1 parent 87b83db commit 93eb9f2

File tree

1 file changed

+45
-59
lines changed

1 file changed

+45
-59
lines changed

src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,17 @@ public void ExecuteSymbolActions(
458458

459459
var symbol = symbolDeclaredEvent.Symbol;
460460
var analyzerOptions = this.GetAnalyzerSpecificOptions(analyzer);
461-
var addDiagnostic = GetAddDiagnostic(
462-
symbol, symbolDeclaredEvent.DeclaringSyntaxReferences, analyzer, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken);
463461

464-
using var _ = PooledDelegates.GetPooledFunction(
462+
using var _1 = PooledDelegates.GetPooledAction(
463+
static (diagnostic, tuple) =>
464+
{
465+
var (self, analyzer, symbolDeclaredEvent, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken) = tuple;
466+
tuple.self.AddSymbolDiagnostic(symbolDeclaredEvent, diagnostic, analyzer, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken);
467+
},
468+
(self: this, analyzer, symbolDeclaredEvent, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken),
469+
out Action<Diagnostic> addDiagnostic);
470+
471+
using var _2 = PooledDelegates.GetPooledFunction(
465472
static (d, ct, arg) => arg.self.IsSupportedDiagnostic(arg.analyzer, d, ct),
466473
(self: this, analyzer),
467474
out Func<Diagnostic, CancellationToken, bool> isSupportedDiagnostic);
@@ -570,9 +577,17 @@ private void ExecuteSymbolEndActionsCore(
570577

571578
var symbol = symbolDeclaredEvent.Symbol;
572579
var analyzerOptions = this.GetAnalyzerSpecificOptions(analyzer);
573-
var addDiagnostic = GetAddDiagnostic(symbol, symbolDeclaredEvent.DeclaringSyntaxReferences, analyzer, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken);
574580

575-
using var _ = PooledDelegates.GetPooledFunction(
581+
using var _1 = PooledDelegates.GetPooledAction(
582+
static (diagnostic, tuple) =>
583+
{
584+
var (self, analyzer, symbolDeclaredEvent, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken) = tuple;
585+
tuple.self.AddSymbolDiagnostic(symbolDeclaredEvent, diagnostic, analyzer, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken);
586+
},
587+
(self: this, analyzer, symbolDeclaredEvent, analyzerOptions, getTopMostNodeForAnalysis, cancellationToken),
588+
out Action<Diagnostic> addDiagnostic);
589+
590+
using var _2 = PooledDelegates.GetPooledFunction(
576591
static (d, ct, arg) => arg.self.IsSupportedDiagnostic(arg.analyzer, d, ct),
577592
(self: this, analyzer),
578593
out Func<Diagnostic, CancellationToken, bool> isSupportedDiagnostic);
@@ -1507,76 +1522,47 @@ private bool IsSupportedDiagnostic(DiagnosticAnalyzer analyzer, Diagnostic diagn
15071522
return _analyzerManager.IsSupportedDiagnostic(analyzer, diagnostic, _isCompilerAnalyzer, this, cancellationToken);
15081523
}
15091524

1510-
private Action<Diagnostic> GetAddDiagnostic(
1511-
ISymbol contextSymbol,
1512-
ImmutableArray<SyntaxReference> cachedDeclaringReferences,
1525+
private void AddSymbolDiagnostic(
1526+
SymbolDeclaredCompilationEvent symbolDeclaredEvent,
1527+
Diagnostic diagnostic,
15131528
DiagnosticAnalyzer analyzer,
15141529
AnalyzerOptions options,
15151530
Func<ISymbol, SyntaxReference, Compilation, CancellationToken, SyntaxNode> getTopMostNodeForAnalysis,
15161531
CancellationToken cancellationToken)
15171532
{
1518-
return GetAddDiagnostic(
1519-
contextSymbol,
1520-
cachedDeclaringReferences,
1521-
Compilation,
1522-
analyzer,
1523-
options,
1524-
_addNonCategorizedDiagnostic,
1525-
_addCategorizedLocalDiagnostic,
1526-
_addCategorizedNonLocalDiagnostic,
1527-
getTopMostNodeForAnalysis,
1528-
_shouldSuppressGeneratedCodeDiagnostic,
1529-
cancellationToken);
1530-
}
1531-
1532-
private static Action<Diagnostic> GetAddDiagnostic(
1533-
ISymbol contextSymbol,
1534-
ImmutableArray<SyntaxReference> cachedDeclaringReferences,
1535-
Compilation compilation,
1536-
DiagnosticAnalyzer analyzer,
1537-
AnalyzerOptions options,
1538-
Action<Diagnostic, AnalyzerOptions, CancellationToken>? addNonCategorizedDiagnostic,
1539-
Action<Diagnostic, DiagnosticAnalyzer, AnalyzerOptions, bool, CancellationToken>? addCategorizedLocalDiagnostic,
1540-
Action<Diagnostic, DiagnosticAnalyzer, AnalyzerOptions, CancellationToken>? addCategorizedNonLocalDiagnostic,
1541-
Func<ISymbol, SyntaxReference, Compilation, CancellationToken, SyntaxNode> getTopMostNodeForAnalysis,
1542-
Func<Diagnostic, DiagnosticAnalyzer, Compilation, CancellationToken, bool> shouldSuppressGeneratedCodeDiagnostic,
1543-
CancellationToken cancellationToken)
1544-
{
1545-
return diagnostic =>
1533+
if (_shouldSuppressGeneratedCodeDiagnostic(diagnostic, analyzer, this.Compilation, cancellationToken))
15461534
{
1547-
if (shouldSuppressGeneratedCodeDiagnostic(diagnostic, analyzer, compilation, cancellationToken))
1548-
{
1549-
return;
1550-
}
1535+
return;
1536+
}
15511537

1552-
if (addCategorizedLocalDiagnostic == null)
1553-
{
1554-
Debug.Assert(addNonCategorizedDiagnostic != null);
1555-
addNonCategorizedDiagnostic(diagnostic, options, cancellationToken);
1556-
return;
1557-
}
1538+
if (_addCategorizedLocalDiagnostic == null)
1539+
{
1540+
Debug.Assert(_addNonCategorizedDiagnostic != null);
1541+
_addNonCategorizedDiagnostic(diagnostic, options, cancellationToken);
1542+
return;
1543+
}
15581544

1559-
Debug.Assert(addNonCategorizedDiagnostic == null);
1560-
Debug.Assert(addCategorizedNonLocalDiagnostic != null);
1545+
Debug.Assert(_addNonCategorizedDiagnostic == null);
1546+
Debug.Assert(_addCategorizedNonLocalDiagnostic != null);
15611547

1562-
if (diagnostic.Location.IsInSource)
1548+
if (diagnostic.Location.IsInSource)
1549+
{
1550+
var symbol = symbolDeclaredEvent.Symbol;
1551+
foreach (var syntaxRef in symbolDeclaredEvent.DeclaringSyntaxReferences)
15631552
{
1564-
foreach (var syntaxRef in cachedDeclaringReferences)
1553+
if (syntaxRef.SyntaxTree == diagnostic.Location.SourceTree)
15651554
{
1566-
if (syntaxRef.SyntaxTree == diagnostic.Location.SourceTree)
1555+
var syntax = getTopMostNodeForAnalysis(symbol, syntaxRef, this.Compilation, cancellationToken);
1556+
if (diagnostic.Location.SourceSpan.IntersectsWith(syntax.FullSpan))
15671557
{
1568-
var syntax = getTopMostNodeForAnalysis(contextSymbol, syntaxRef, compilation, cancellationToken);
1569-
if (diagnostic.Location.SourceSpan.IntersectsWith(syntax.FullSpan))
1570-
{
1571-
addCategorizedLocalDiagnostic(diagnostic, analyzer, options, false, cancellationToken);
1572-
return;
1573-
}
1558+
_addCategorizedLocalDiagnostic(diagnostic, analyzer, options, false, cancellationToken);
1559+
return;
15741560
}
15751561
}
15761562
}
1563+
}
15771564

1578-
addCategorizedNonLocalDiagnostic(diagnostic, analyzer, options, cancellationToken);
1579-
};
1565+
_addCategorizedNonLocalDiagnostic(diagnostic, analyzer, options, cancellationToken);
15801566
}
15811567

15821568
private Action<Diagnostic> GetAddCompilationDiagnostic(

0 commit comments

Comments
 (0)