1
1
using System . Collections . Immutable ;
2
2
using Microsoft . CodeAnalysis ;
3
- using Microsoft . CodeAnalysis . CSharp ;
4
- using Microsoft . CodeAnalysis . CSharp . Syntax ;
5
3
using Microsoft . CodeAnalysis . Diagnostics ;
6
4
7
5
namespace Stereologue . SourceGenerator ;
@@ -10,48 +8,24 @@ namespace Stereologue.SourceGenerator;
10
8
public sealed class GenerateLogAnalyzer : DiagnosticAnalyzer
11
9
{
12
10
public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics { get ; } = ImmutableArray . Create ( [
13
- LoggerDiagnostics . GeneratedTypeNotPartial
11
+ LoggerDiagnostics . UnknownMemberType ,
12
+ LoggerDiagnostics . ProtobufIsArray ,
13
+ LoggerDiagnostics . UnknownSpecialTypeArray ,
14
+ LoggerDiagnostics . LoggedMethodReturnsVoid ,
15
+ LoggerDiagnostics . LoggedMethodTakeParameters ,
16
+ LoggerDiagnostics . LoggedHasUnknownType ,
17
+ LoggerDiagnostics . UnknownFailureMode ,
18
+ LoggerDiagnostics . NullableStructArray ,
19
+ LoggerDiagnostics . UnknownSpecialTypeIntArray
14
20
] ) ;
15
21
16
22
public override void Initialize ( AnalysisContext context )
17
23
{
18
24
context . EnableConcurrentExecution ( ) ;
19
25
context . ConfigureGeneratedCodeAnalysis ( GeneratedCodeAnalysisFlags . None ) ;
20
-
21
- // context.RegisterSyntaxNodeAction(AnalyzeSyntax,
22
- // SyntaxKind.ClassDeclaration,
23
- // SyntaxKind.StructDeclaration,
24
- // SyntaxKind.RecordDeclaration,
25
- // SyntaxKind.RecordStructDeclaration,
26
- // SyntaxKind.InterfaceDeclaration);
27
-
28
26
context . RegisterSymbolAction ( AnalyzeSymbol , SymbolKind . NamedType ) ;
29
27
}
30
28
31
- // private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
32
- // {
33
- // if (context.SemanticModel.GetDeclaredSymbol(context.Node) is not INamedTypeSymbol namedTypeSymbol)
34
- // {
35
- // return;
36
- // }
37
-
38
- // if (!namedTypeSymbol.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == Strings.GenerateLogAttributeName).Any())
39
- // {
40
- // return;
41
- // }
42
-
43
- // if (context.Node is TypeDeclarationSyntax typeSyntax)
44
- // {
45
- // // Ensure type is partial.
46
- // if (!typeSyntax.IsInPartialContext(out var nonPartialIdentifier))
47
- // {
48
- // context.ReportDiagnostic(
49
- // Diagnostic.Create(LoggerDiagnostics.GeneratedTypeNotPartial, typeSyntax.GetLocation(), namedTypeSymbol.Name)
50
- // );
51
- // }
52
- // }
53
- // }
54
-
55
29
private static void AnalyzeSymbol ( SymbolAnalysisContext context )
56
30
{
57
31
var token = context . CancellationToken ;
@@ -62,52 +36,17 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
62
36
return ;
63
37
}
64
38
65
- var model = namedTypeSymbol . GetLoggableType ( token ) ;
39
+ List < ( FailureMode , ISymbol ) > failures = [ ] ;
40
+ Dictionary < LoggableMember , ISymbol > symbolMap = [ ] ;
41
+
42
+ var model = namedTypeSymbol . GetLoggableType ( token , failures , symbolMap ) ;
66
43
token . ThrowIfCancellationRequested ( ) ;
67
44
68
- foreach ( var member in model . LoggableMembers )
69
- {
70
- if ( member . MemberDeclaration . LoggedType == DeclarationType . Protobuf )
71
- {
72
- if ( member . MemberDeclaration . LoggedKind != DeclarationKind . None && member . MemberDeclaration . LoggedKind != DeclarationKind . NullableValueType && member . MemberDeclaration . LoggedKind != DeclarationKind . NullableReferenceType )
73
- {
74
- foreach ( var location in namedTypeSymbol . Locations )
75
- {
76
- context . ReportDiagnostic (
77
- Diagnostic . Create ( LoggerDiagnostics . GeneratedTypeNotPartial , location , namedTypeSymbol . Name ) ) ;
78
- }
45
+ model . ExecuteAnalysis ( context , symbolMap ) ;
79
46
80
- }
81
- }
47
+ foreach ( var failure in failures )
48
+ {
49
+ context . ReportDiagnostic ( failure . Item1 , failure . Item2 ) ;
82
50
}
83
-
84
-
85
- // // Find our attributes
86
-
87
- // if (!namedTypeSymbol.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == Strings.GenerateLogAttributeName).Any())
88
- // {
89
- // return;
90
- // }
91
-
92
- // token.ThrowIfCancellationRequested();
93
-
94
- // var syntaxNodes = namedTypeSymbol.DeclaringSyntaxReferences.Select(x => x.GetSyntax());
95
-
96
- // foreach (var node in syntaxNodes)
97
- // {
98
-
99
- // if (node is TypeDeclarationSyntax typeSyntax)
100
- // {
101
- // // Ensure type is partial.
102
- // if (!typeSyntax.IsInPartialContext(out var nonPartialIdentifier))
103
- // {
104
- // context.ReportDiagnostic(
105
- // Diagnostic.Create(LoggerDiagnostics.GeneratedTypeNotPartial, typeSyntax.GetLocation(), namedTypeSymbol.Name)
106
- // );
107
- // }
108
- // }
109
-
110
- // }
111
-
112
51
}
113
52
}
0 commit comments