Skip to content

Commit deb1c46

Browse files
committed
Add full analyzer for loggable source generator
1 parent e699dbe commit deb1c46

File tree

9 files changed

+391
-302
lines changed

9 files changed

+391
-302
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Stereologue.SourceGenerator;
2+
3+
public enum FailureMode
4+
{
5+
None,
6+
AttributeUnknownMemberType,
7+
ProtobufArray,
8+
UnknownTypeNonArray,
9+
UnknownTypeArray,
10+
MethodReturnsVoid,
11+
MethodHasParameters,
12+
UnknownTypeToLog,
13+
NullableStructArray,
14+
}
Lines changed: 17 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Collections.Immutable;
22
using Microsoft.CodeAnalysis;
3-
using Microsoft.CodeAnalysis.CSharp;
4-
using Microsoft.CodeAnalysis.CSharp.Syntax;
53
using Microsoft.CodeAnalysis.Diagnostics;
64

75
namespace Stereologue.SourceGenerator;
@@ -10,48 +8,24 @@ namespace Stereologue.SourceGenerator;
108
public sealed class GenerateLogAnalyzer : DiagnosticAnalyzer
119
{
1210
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
1420
]);
1521

1622
public override void Initialize(AnalysisContext context)
1723
{
1824
context.EnableConcurrentExecution();
1925
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-
2826
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
2927
}
3028

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-
5529
private static void AnalyzeSymbol(SymbolAnalysisContext context)
5630
{
5731
var token = context.CancellationToken;
@@ -62,52 +36,17 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
6236
return;
6337
}
6438

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);
6643
token.ThrowIfCancellationRequested();
6744

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);
7946

80-
}
81-
}
47+
foreach (var failure in failures)
48+
{
49+
context.ReportDiagnostic(failure.Item1, failure.Item2);
8250
}
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-
11251
}
11352
}

sourcegeneration/StereologueSourceGenerator/LogGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1818
{
1919
return null;
2020
}
21-
return classSymbol.GetLoggableType(token);
21+
return classSymbol.GetLoggableType(token, null, null);
2222
})
2323
.Where(static m => m is not null);
2424

0 commit comments

Comments
 (0)