forked from dotnet/roslyn-analyzers
/
DiagnosticAnalyzerCorrectnessAnalyzer.DiagnosticAnalyzerSymbolAnalyzer.cs
52 lines (44 loc) · 2.02 KB
/
DiagnosticAnalyzerCorrectnessAnalyzer.DiagnosticAnalyzerSymbolAnalyzer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Microsoft.CodeAnalysis.Analyzers.MetaAnalyzers
{
public abstract partial class DiagnosticAnalyzerCorrectnessAnalyzer : DiagnosticAnalyzer
{
protected abstract class DiagnosticAnalyzerSymbolAnalyzer
{
protected DiagnosticAnalyzerSymbolAnalyzer(INamedTypeSymbol diagnosticAnalyzer, INamedTypeSymbol diagnosticAnalyzerAttribute)
{
DiagnosticAnalyzer = diagnosticAnalyzer;
DiagnosticAnalyzerAttribute = diagnosticAnalyzerAttribute;
}
protected INamedTypeSymbol DiagnosticAnalyzer { get; }
protected INamedTypeSymbol DiagnosticAnalyzerAttribute { get; }
protected bool IsDiagnosticAnalyzer(INamedTypeSymbol type)
{
return type.Equals(DiagnosticAnalyzer);
}
internal void AnalyzeSymbol(SymbolAnalysisContext symbolContext)
{
var namedType = (INamedTypeSymbol)symbolContext.Symbol;
if (namedType.GetBaseTypes().Any(IsDiagnosticAnalyzer))
{
AnalyzeDiagnosticAnalyzer(symbolContext);
}
}
protected abstract void AnalyzeDiagnosticAnalyzer(SymbolAnalysisContext symbolContext);
protected bool HasDiagnosticAnalyzerAttribute(INamedTypeSymbol namedType, INamedTypeSymbol? attributeUsageAttribute)
{
foreach (AttributeData attribute in namedType.GetApplicableAttributes(attributeUsageAttribute))
{
if (attribute.AttributeClass.DerivesFrom(DiagnosticAnalyzerAttribute))
{
return true;
}
}
return false;
}
}
}
}