提交 af178ee2 编写于 作者: C Cyrus Najmabadi

Reduce Location allocations while running analyzers.

上级 32bc937e
......@@ -8,9 +8,10 @@
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Diagnostics.Telemetry;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Diagnostics
......@@ -613,7 +614,7 @@ private bool IsInGeneratedCode(Location location, Compilation compilation, Cance
}
// Check if this is a generated code location.
if (IsGeneratedOrHiddenCodeLocation(location))
if (IsGeneratedOrHiddenCodeLocation(location.SourceTree, location.SourceSpan))
{
return true;
}
......@@ -1275,7 +1276,7 @@ private bool IsGeneratedCodeSymbol(ISymbol symbol)
foreach (var declaringRef in symbol.DeclaringSyntaxReferences)
{
if (!IsGeneratedOrHiddenCodeLocation(declaringRef.GetLocation()))
if (!IsGeneratedOrHiddenCodeLocation(declaringRef.SyntaxTree, declaringRef.Span))
{
return false;
}
......@@ -1309,13 +1310,12 @@ protected bool IsGeneratedCode(SyntaxTree tree)
protected bool DoNotAnalyzeGeneratedCode => _doNotAnalyzeGeneratedCode;
// Location is in generated code if either the containing tree is a generated code file OR if it is a hidden source location.
protected bool IsGeneratedOrHiddenCodeLocation(Location location) =>
IsGeneratedCode(location.SourceTree) || IsHiddenSourceLocation(location);
protected bool IsGeneratedOrHiddenCodeLocation(SyntaxTree syntaxTree, TextSpan span)
=> IsGeneratedCode(syntaxTree) || IsHiddenSourceLocation(syntaxTree, span);
protected bool IsHiddenSourceLocation(Location location) =>
location.IsInSource &&
HasHiddenRegions(location.SourceTree) &&
location.SourceTree.IsHiddenPosition(location.SourceSpan.Start);
protected bool IsHiddenSourceLocation(SyntaxTree syntaxTree, TextSpan span)
=> HasHiddenRegions(syntaxTree) &&
syntaxTree.IsHiddenPosition(span.Start);
private bool HasHiddenRegions(SyntaxTree tree)
{
......@@ -1630,7 +1630,7 @@ private bool ShouldExecuteOperationBlockActions(AnalysisScope analysisScope, ISy
continue;
}
var isInGeneratedCode = isGeneratedCodeSymbol || IsGeneratedOrHiddenCodeLocation(decl.GetLocation());
var isInGeneratedCode = isGeneratedCodeSymbol || IsGeneratedOrHiddenCodeLocation(decl.SyntaxTree, decl.Span);
if (isInGeneratedCode && DoNotAnalyzeGeneratedCode)
{
analysisStateOpt?.MarkDeclarationComplete(symbol, i, analysisScope.Analyzers);
......
......@@ -42,7 +42,7 @@ internal class AnalyzerExecutor
private readonly Func<DiagnosticAnalyzer, object> _getAnalyzerGateOpt;
private readonly Func<DiagnosticAnalyzer, bool> _shouldSkipAnalysisOnGeneratedCode;
private readonly Func<Diagnostic, DiagnosticAnalyzer, Compilation, CancellationToken, bool> _shouldSuppressGeneratedCodeDiagnostic;
private readonly Func<Location, bool> _isGeneratedCodeLocation;
private readonly Func<SyntaxTree, TextSpan, bool> _isGeneratedCodeLocation;
private readonly ConcurrentDictionary<DiagnosticAnalyzer, TimeSpan> _analyzerExecutionTimeMapOpt;
private readonly CompilationAnalysisValueProviderFactory _compilationAnalysisValueProviderFactory;
private readonly CancellationToken _cancellationToken;
......@@ -86,7 +86,7 @@ internal class AnalyzerExecutor
AnalyzerManager analyzerManager,
Func<DiagnosticAnalyzer, bool> shouldSkipAnalysisOnGeneratedCode,
Func<Diagnostic, DiagnosticAnalyzer, Compilation, CancellationToken, bool> shouldSuppressGeneratedCodeDiagnostic,
Func<Location, bool> isGeneratedCodeLocation,
Func<SyntaxTree, TextSpan, bool> isGeneratedCodeLocation,
Func<DiagnosticAnalyzer, object> getAnalyzerGate,
bool logExecutionTime = false,
Action<Diagnostic, DiagnosticAnalyzer, bool> addCategorizedLocalDiagnosticOpt = null,
......@@ -125,7 +125,7 @@ internal class AnalyzerExecutor
isCompilerAnalyzer: null,
shouldSkipAnalysisOnGeneratedCode: _ => false,
shouldSuppressGeneratedCodeDiagnostic: (diagnostic, analyzer, compilation, ct) => false,
isGeneratedCodeLocation: _ => false,
isGeneratedCodeLocation: (_1, _2) => false,
getAnalyzerGateOpt: null,
onAnalyzerException: onAnalyzerException,
analyzerExceptionFilter: null,
......@@ -146,7 +146,7 @@ internal class AnalyzerExecutor
AnalyzerManager analyzerManager,
Func<DiagnosticAnalyzer, bool> shouldSkipAnalysisOnGeneratedCode,
Func<Diagnostic, DiagnosticAnalyzer, Compilation, CancellationToken, bool> shouldSuppressGeneratedCodeDiagnostic,
Func<Location, bool> isGeneratedCodeLocation,
Func<SyntaxTree, TextSpan, bool> isGeneratedCodeLocation,
Func<DiagnosticAnalyzer, object> getAnalyzerGateOpt,
ConcurrentDictionary<DiagnosticAnalyzer, TimeSpan> analyzerExecutionTimeMapOpt,
Action<Diagnostic, DiagnosticAnalyzer, bool> addCategorizedLocalDiagnosticOpt,
......@@ -1488,7 +1488,8 @@ private bool ShouldExecuteNode(SyntaxNodeAnalyzerStateData analyzerStateOpt, Syn
}
// Check if the node is generated code that must be skipped.
if (_shouldSkipAnalysisOnGeneratedCode(analyzer) && _isGeneratedCodeLocation(node.GetLocation()))
if (_shouldSkipAnalysisOnGeneratedCode(analyzer) &&
_isGeneratedCodeLocation(node.SyntaxTree, node.Span))
{
return false;
}
......@@ -1505,7 +1506,8 @@ private bool ShouldExecuteOperation(OperationAnalyzerStateData analyzerStateOpt,
}
// Check if the operation syntax is generated code that must be skipped.
if (operation.Syntax != null && _shouldSkipAnalysisOnGeneratedCode(analyzer) && _isGeneratedCodeLocation(operation.Syntax.GetLocation()))
if (operation.Syntax != null && _shouldSkipAnalysisOnGeneratedCode(analyzer) &&
_isGeneratedCodeLocation(operation.Syntax.SyntaxTree, operation.Syntax.Span))
{
return false;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册