|
13 | 13 | using Microsoft.CodeAnalysis.Diagnostics; |
14 | 14 |
|
15 | 15 | using System; |
| 16 | +using System.Collections.Concurrent; |
16 | 17 | using System.Collections.Immutable; |
| 18 | +using System.Reflection; |
17 | 19 |
|
18 | 20 | namespace EppNet.Source.Analysis |
19 | 21 | { |
20 | 22 | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
21 | 23 | public class NetworkObjectAnalysis : DiagnosticAnalyzer |
22 | 24 | { |
23 | 25 |
|
| 26 | + public static ConcurrentDictionary<string, string> Resolvers = new(); |
| 27 | + public static ConcurrentDictionary<string, NetworkObjectModel?> Objects = new(); |
| 28 | + |
24 | 29 | public override void Initialize(AnalysisContext context) |
25 | 30 | { |
26 | | - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics); |
| 31 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
27 | 32 | context.EnableConcurrentExecution(); |
28 | 33 |
|
29 | | - context.RegisterSyntaxNodeAction((snac) => |
| 34 | + |
| 35 | + context.RegisterCompilationStartAction(compContext => |
30 | 36 | { |
31 | 37 |
|
32 | | - ClassDeclarationSyntax classNode = snac.Node as ClassDeclarationSyntax; |
| 38 | + compContext.RegisterSyntaxNodeAction(snac => |
| 39 | + { |
| 40 | + |
| 41 | + ClassDeclarationSyntax classNode = snac.Node as ClassDeclarationSyntax; |
| 42 | + |
| 43 | + if (!Globals.HasAttribute(classNode, Globals.NetTypeResolverAttr)) |
| 44 | + return; |
33 | 45 |
|
34 | | - if (!Globals.HasAttribute(classNode, Globals.NetObjectAttr)) |
35 | | - return; |
| 46 | + (ResolverModel?, ResolverAnalysisError) data = Globals.LocateResolver(snac.Node, snac.SemanticModel, snac.CancellationToken); |
| 47 | + ResolverAnalysisError results = data.Item2; |
36 | 48 |
|
37 | | - NetworkObjectAnalysisError results = Globals.LocateNetObject(snac.Node, snac.SemanticModel, snac.CancellationToken).Item2; |
| 49 | + foreach (ResolverAnalysisError error in Enum.GetValues(typeof(ResolverAnalysisError))) |
| 50 | + { |
| 51 | + if (error == ResolverAnalysisError.None) |
| 52 | + continue; |
38 | 53 |
|
39 | | - foreach (NetworkObjectAnalysisError error in Enum.GetValues(typeof(NetworkObjectAnalysisError))) |
| 54 | + if (results.HasFlag(error)) |
| 55 | + { |
| 56 | + string message = error switch |
| 57 | + { |
| 58 | + ResolverAnalysisError.LacksSingleton => $"{classNode.Identifier.Text}: Resolvers must define a public static singleton field or property \"Instance\"", |
| 59 | + ResolverAnalysisError.LacksInheritance => $"{classNode.Identifier.Text}: Resolvers must inherit from {Globals.TypeResolverFullGenericName}", |
| 60 | + ResolverAnalysisError.NotClass => $"{classNode.Identifier.Text}: Resolvers must be a class!", |
| 61 | + _ => ToString(), |
| 62 | + }; |
| 63 | + snac.ReportDiagnostic(Diagnostic.Create(Globals.DescTypeResolverError, |
| 64 | + classNode.Identifier.GetLocation(), message)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (results == ResolverAnalysisError.None && data.Item1.HasValue) |
| 69 | + { |
| 70 | + Resolvers[data.Item1.Value.ResolvedTypeFullName] = data.Item1.Value.Name; |
| 71 | + } |
| 72 | + |
| 73 | + }, SyntaxKind.ClassDeclaration); |
| 74 | + |
| 75 | + compContext.RegisterSyntaxNodeAction(snac => |
40 | 76 | { |
41 | | - if (error == NetworkObjectAnalysisError.None) |
42 | | - continue; |
| 77 | + ClassDeclarationSyntax classNode = snac.Node as ClassDeclarationSyntax; |
| 78 | + |
| 79 | + if (!Globals.HasAttribute(classNode, Globals.NetObjectAttr)) |
| 80 | + return; |
| 81 | + |
| 82 | + var data = Globals.LocateNetObject( |
| 83 | + classNode, snac.SemanticModel, snac.CancellationToken); |
| 84 | + |
| 85 | + NetworkObjectModel? netModel = data.Item1; |
| 86 | + NetworkObjectAnalysisError results = data.Item2; |
43 | 87 |
|
44 | | - if (results.HasFlag(error)) |
| 88 | + foreach (NetworkObjectAnalysisError error in Enum.GetValues(typeof(NetworkObjectAnalysisError))) |
45 | 89 | { |
46 | | - string message = error switch |
| 90 | + if (error == NetworkObjectAnalysisError.None) |
| 91 | + continue; |
| 92 | + |
| 93 | + if (results.HasFlag(error)) |
47 | 94 | { |
48 | | - NetworkObjectAnalysisError.NotPartial => $"{classNode.Identifier.Text}: Network Object definitions must be partial!", |
49 | | - NetworkObjectAnalysisError.LacksInheritance => $"{classNode.Identifier.Text}: Network Object definitions must inherit from {Globals.NetworkObjectInterfaceName}", |
50 | | - NetworkObjectAnalysisError.NotClass => $"{classNode.Identifier.Text}: Network Object definitions must be a class!", |
51 | | - _ => ToString(), |
52 | | - }; |
53 | | - snac.ReportDiagnostic(Diagnostic.Create(Globals.DescTypeResolverError, |
54 | | - classNode.Identifier.GetLocation(), message)); |
| 95 | + string message = error switch |
| 96 | + { |
| 97 | + NetworkObjectAnalysisError.NotPartial => $"{classNode.Identifier.Text}: Network Object definitions must be partial! Num Resolvers: {Resolvers.Count}", |
| 98 | + NetworkObjectAnalysisError.LacksInheritance => $"{classNode.Identifier.Text}: Network Object definitions must inherit from {Globals.NetworkObjectInterfaceName}", |
| 99 | + NetworkObjectAnalysisError.NotClass => $"{classNode.Identifier.Text}: Network Object definitions must be a class!", |
| 100 | + _ => ToString(), |
| 101 | + }; |
| 102 | + |
| 103 | + snac.ReportDiagnostic(Diagnostic.Create(Globals.DescNetObjError, |
| 104 | + classNode.Identifier.GetLocation(), message)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (results == NetworkObjectAnalysisError.None && data.Item1.HasValue) |
| 109 | + { |
| 110 | + Objects[data.Item1.Value.FullyQualifiedName] = data.Item1; |
55 | 111 | } |
56 | | - } |
57 | 112 |
|
58 | | - }, SyntaxKind.ClassDeclaration); |
| 113 | + }, SyntaxKind.ClassDeclaration); |
| 114 | + |
| 115 | + compContext.RegisterSyntaxNodeAction(snac => |
| 116 | + { |
| 117 | + |
| 118 | + MethodDeclarationSyntax methodNode = snac.Node as MethodDeclarationSyntax; |
| 119 | + |
| 120 | + if (!Globals.HasAttribute(methodNode, Globals.NetMethodAttr)) |
| 121 | + return; |
| 122 | + |
| 123 | + var data = Globals.LocateNetMethod(methodNode, snac.SemanticModel, Resolvers, Objects); |
| 124 | + NetworkMethodAnalysisError results = data.Item2; |
| 125 | + |
| 126 | + foreach (NetworkMethodAnalysisError error in Enum.GetValues(typeof(NetworkMethodAnalysisError))) |
| 127 | + { |
| 128 | + if (error == NetworkMethodAnalysisError.None) |
| 129 | + continue; |
| 130 | + |
| 131 | + if (results.HasFlag(error)) |
| 132 | + { |
| 133 | + |
| 134 | + if (error == NetworkMethodAnalysisError.MissingResolver) |
| 135 | + { |
| 136 | + ITypeSymbol typeSymbol = snac.SemanticModel.GetTypeInfo(data.Item4).Type; |
| 137 | + |
| 138 | + if (typeSymbol == null) |
| 139 | + continue; |
| 140 | + |
| 141 | + string fullTypeName = $"{typeSymbol.ContainingNamespace.ToDisplayString()}.{typeSymbol.Name}"; |
| 142 | + |
| 143 | + snac.ReportDiagnostic(Diagnostic.Create(Globals.DescNetMethodError, |
| 144 | + data.Item3.GetLocation(), [data.Item4.GetLocation()], $"{methodNode.Identifier.Text}: Type {fullTypeName} does not have a registered Resolver!")); |
| 145 | + |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + string message = error switch |
| 150 | + { |
| 151 | + NetworkMethodAnalysisError.NotNetworkObjectClass => $"{methodNode.Identifier.Text}: Network method must be within a class decorated with the NetworkObject attribute!", |
| 152 | + NetworkMethodAnalysisError.Inaccessible => $"{methodNode.Identifier.Text}: Network method must be public!", |
| 153 | + _ => $"{ToString()}", |
| 154 | + }; |
| 155 | + |
| 156 | + snac.ReportDiagnostic(Diagnostic.Create(Globals.DescNetMethodError, |
| 157 | + methodNode.Identifier.GetLocation(), message)); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + }, SyntaxKind.MethodDeclaration); |
| 162 | + }); |
| 163 | + |
| 164 | + |
59 | 165 | } |
60 | 166 |
|
61 | | - public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Globals.DescTypeResolverError); |
| 167 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| 168 | + ImmutableArray.Create(Globals.DescTypeResolverError, Globals.DescNetObjError, Globals.DescNetMethodError); |
62 | 169 |
|
63 | 170 | } |
64 | 171 |
|
|
0 commit comments