| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 8 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 9 | | |
| | | 10 | | using NexusLabs.Needlr.Generators.Models; |
| | | 11 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 12 | | |
| | | 13 | | namespace NexusLabs.Needlr.Generators; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Validates record constructor-overload markers, participating properties, |
| | | 17 | | /// constructor guards, and proposed constructor signatures. |
| | | 18 | | /// </summary> |
| | | 19 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 20 | | public sealed class RecordConstructorOverloadAnalyzer : DiagnosticAnalyzer |
| | | 21 | | { |
| | | 22 | | private const string ConstructorGuardAttributeName = |
| | | 23 | | "ConstructorGuardAttribute"; |
| | | 24 | | |
| | | 25 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 972 | 26 | | ImmutableArray.Create( |
| | 972 | 27 | | DiagnosticDescriptors.RecordConstructorOverloadRequiresPartialType, |
| | 972 | 28 | | DiagnosticDescriptors.RecordConstructorOverloadUnsupportedTypeShape, |
| | 972 | 29 | | DiagnosticDescriptors.RecordConstructorOverloadPropertyUnsupported, |
| | 972 | 30 | | DiagnosticDescriptors.ConstructorGuardOnNonparticipatingProperty, |
| | 972 | 31 | | DiagnosticDescriptors.RecordConstructorOverloadConflictsWithGeneratedConstructor, |
| | 972 | 32 | | DiagnosticDescriptors.RecordConstructorOverloadSignatureCollision, |
| | 972 | 33 | | DiagnosticDescriptors.InvalidConstructorGuardEnumValue, |
| | 972 | 34 | | DiagnosticDescriptors.ConstructorGuardIncompatibleWithFieldType, |
| | 972 | 35 | | DiagnosticDescriptors.ConstructorGuardTypeInvalid, |
| | 972 | 36 | | DiagnosticDescriptors.ConstructorGuardMethodNameInvalid, |
| | 972 | 37 | | DiagnosticDescriptors.ConstructorGuardMethodInvalid, |
| | 972 | 38 | | DiagnosticDescriptors.ConstructorGuardMethodAmbiguous, |
| | 972 | 39 | | DiagnosticDescriptors.ConstructorGuardAliasUsageArgumentUnsupported, |
| | 972 | 40 | | DiagnosticDescriptors.ConstructorGuardForwardedArgumentIncompatible); |
| | | 41 | | |
| | | 42 | | public override void Initialize(AnalysisContext context) |
| | | 43 | | { |
| | 66 | 44 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 66 | 45 | | context.EnableConcurrentExecution(); |
| | 66 | 46 | | context.RegisterSyntaxNodeAction( |
| | 66 | 47 | | AnalyzeTypeDeclaration, |
| | 66 | 48 | | SyntaxKind.ClassDeclaration, |
| | 66 | 49 | | SyntaxKind.StructDeclaration, |
| | 66 | 50 | | SyntaxKind.InterfaceDeclaration, |
| | 66 | 51 | | SyntaxKind.RecordDeclaration, |
| | 66 | 52 | | SyntaxKind.RecordStructDeclaration); |
| | 66 | 53 | | } |
| | | 54 | | |
| | | 55 | | private static void AnalyzeTypeDeclaration( |
| | | 56 | | SyntaxNodeAnalysisContext context) |
| | | 57 | | { |
| | 832 | 58 | | var typeDeclaration = (TypeDeclarationSyntax)context.Node; |
| | 832 | 59 | | if (context.SemanticModel.GetDeclaredSymbol(typeDeclaration) is not |
| | 832 | 60 | | INamedTypeSymbol typeSymbol || |
| | 832 | 61 | | !GeneratedConstructorEligibility.IsCanonicalDeclaration( |
| | 832 | 62 | | typeSymbol, |
| | 832 | 63 | | typeDeclaration)) |
| | | 64 | | { |
| | 0 | 65 | | return; |
| | | 66 | | } |
| | | 67 | | |
| | 832 | 68 | | var properties = typeSymbol.GetMembers() |
| | 832 | 69 | | .OfType<IPropertySymbol>() |
| | 832 | 70 | | .ToArray(); |
| | 832 | 71 | | ReportNonparticipatingPropertyGuards(context, typeSymbol, properties); |
| | | 72 | | |
| | 832 | 73 | | var markedProperties = properties |
| | 832 | 74 | | .Where( |
| | 832 | 75 | | RecordConstructorOverloadDiscoveryHelper.HasMarker) |
| | 832 | 76 | | .OrderBy( |
| | 832 | 77 | | property => |
| | 0 | 78 | | property.Locations.FirstOrDefault()?.SourceTree?.FilePath ?? |
| | 0 | 79 | | string.Empty, |
| | 832 | 80 | | System.StringComparer.Ordinal) |
| | 832 | 81 | | .ThenBy( |
| | 832 | 82 | | property => |
| | 0 | 83 | | property.Locations.FirstOrDefault()?.SourceSpan.Start ?? 0) |
| | 832 | 84 | | .ToArray(); |
| | 832 | 85 | | if (markedProperties.Length == 0) |
| | 791 | 86 | | return; |
| | | 87 | | |
| | 41 | 88 | | var typeLocation = typeDeclaration.Identifier.GetLocation(); |
| | 41 | 89 | | if (RecordConstructorOverloadDiscoveryHelper |
| | 41 | 90 | | .HasFieldBasedGeneratedConstructorTrigger(typeSymbol)) |
| | | 91 | | { |
| | 4 | 92 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 93 | | DiagnosticDescriptors.RecordConstructorOverloadConflictsWithGeneratedConstructor, |
| | 4 | 94 | | typeLocation, |
| | 4 | 95 | | typeSymbol.Name)); |
| | 4 | 96 | | return; |
| | | 97 | | } |
| | | 98 | | |
| | 37 | 99 | | var typeReason = |
| | 37 | 100 | | RecordConstructorOverloadDiscoveryHelper |
| | 37 | 101 | | .GetTypeIneligibilityReason(typeSymbol); |
| | 37 | 102 | | if (typeReason is not null) |
| | | 103 | | { |
| | 12 | 104 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 12 | 105 | | DiagnosticDescriptors.RecordConstructorOverloadUnsupportedTypeShape, |
| | 12 | 106 | | typeLocation, |
| | 12 | 107 | | typeSymbol.Name, |
| | 12 | 108 | | typeReason)); |
| | 12 | 109 | | return; |
| | | 110 | | } |
| | | 111 | | |
| | 25 | 112 | | if (!GeneratedConstructorEligibility.IsDeclaredPartial(typeSymbol)) |
| | | 113 | | { |
| | 2 | 114 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 115 | | DiagnosticDescriptors.RecordConstructorOverloadRequiresPartialType, |
| | 2 | 116 | | typeLocation, |
| | 2 | 117 | | typeSymbol.Name)); |
| | 2 | 118 | | return; |
| | | 119 | | } |
| | | 120 | | |
| | 23 | 121 | | var primaryDeclaration = |
| | 23 | 122 | | RecordConstructorOverloadDiscoveryHelper |
| | 23 | 123 | | .GetPrimaryRecordDeclaration(typeSymbol); |
| | 23 | 124 | | if (primaryDeclaration is null) |
| | 0 | 125 | | return; |
| | | 126 | | |
| | 23 | 127 | | var hasInvalidProperty = false; |
| | 92 | 128 | | foreach (var property in markedProperties) |
| | | 129 | | { |
| | 23 | 130 | | var propertyReason = |
| | 23 | 131 | | RecordConstructorOverloadDiscoveryHelper |
| | 23 | 132 | | .GetPropertyIneligibilityReason( |
| | 23 | 133 | | typeSymbol, |
| | 23 | 134 | | property, |
| | 23 | 135 | | primaryDeclaration); |
| | 23 | 136 | | if (propertyReason is not null) |
| | | 137 | | { |
| | 12 | 138 | | var marker = |
| | 12 | 139 | | RecordConstructorOverloadDiscoveryHelper |
| | 12 | 140 | | .GetMarkerAttribute(property); |
| | 12 | 141 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 12 | 142 | | DiagnosticDescriptors.RecordConstructorOverloadPropertyUnsupported, |
| | 12 | 143 | | marker is null |
| | 12 | 144 | | ? property.Locations.FirstOrDefault() ?? Location.None |
| | 12 | 145 | | : ConstructorGuardAnalysisHelper.GetAttributeLocation( |
| | 12 | 146 | | context, |
| | 12 | 147 | | marker), |
| | 12 | 148 | | property.Name, |
| | 12 | 149 | | propertyReason)); |
| | 12 | 150 | | hasInvalidProperty = true; |
| | 12 | 151 | | continue; |
| | | 152 | | } |
| | | 153 | | |
| | 11 | 154 | | AnalyzeParticipatingPropertyGuards( |
| | 11 | 155 | | context, |
| | 11 | 156 | | typeSymbol, |
| | 11 | 157 | | property); |
| | | 158 | | } |
| | | 159 | | |
| | 23 | 160 | | if (hasInvalidProperty) |
| | 12 | 161 | | return; |
| | | 162 | | |
| | 11 | 163 | | if (RecordConstructorOverloadDiscoveryHelper.TryGetSignatureCollision( |
| | 11 | 164 | | typeSymbol, |
| | 11 | 165 | | context.Compilation, |
| | 11 | 166 | | out var collisionDisplay)) |
| | | 167 | | { |
| | 6 | 168 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 6 | 169 | | DiagnosticDescriptors.RecordConstructorOverloadSignatureCollision, |
| | 6 | 170 | | typeLocation, |
| | 6 | 171 | | typeSymbol.Name, |
| | 6 | 172 | | collisionDisplay)); |
| | | 173 | | } |
| | 11 | 174 | | } |
| | | 175 | | |
| | | 176 | | private static void ReportNonparticipatingPropertyGuards( |
| | | 177 | | SyntaxNodeAnalysisContext context, |
| | | 178 | | INamedTypeSymbol typeSymbol, |
| | | 179 | | IReadOnlyList<IPropertySymbol> properties) |
| | | 180 | | { |
| | 3790 | 181 | | foreach (var property in properties) |
| | | 182 | | { |
| | 1063 | 183 | | if (RecordConstructorOverloadDiscoveryHelper.HasMarker(property)) |
| | | 184 | | continue; |
| | | 185 | | |
| | 2048 | 186 | | foreach (var attribute in property.GetAttributes()) |
| | | 187 | | { |
| | 2 | 188 | | if (!IsConstructorGuardOrAlias(attribute)) |
| | | 189 | | continue; |
| | | 190 | | |
| | 2 | 191 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 192 | | DiagnosticDescriptors.ConstructorGuardOnNonparticipatingProperty, |
| | 2 | 193 | | ConstructorGuardAnalysisHelper.GetAttributeLocation( |
| | 2 | 194 | | context, |
| | 2 | 195 | | attribute), |
| | 2 | 196 | | property.Name)); |
| | | 197 | | } |
| | | 198 | | } |
| | 832 | 199 | | } |
| | | 200 | | |
| | | 201 | | private static void AnalyzeParticipatingPropertyGuards( |
| | | 202 | | SyntaxNodeAnalysisContext context, |
| | | 203 | | INamedTypeSymbol containingType, |
| | | 204 | | IPropertySymbol property) |
| | | 205 | | { |
| | 52 | 206 | | foreach (var attribute in property.GetAttributes()) |
| | | 207 | | { |
| | 15 | 208 | | if (attribute.AttributeClass is not { } attributeClass) |
| | | 209 | | continue; |
| | | 210 | | |
| | | 211 | | ConstructorGuardOccurrence occurrence; |
| | 15 | 212 | | if (GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 15 | 213 | | attributeClass, |
| | 15 | 214 | | ConstructorGuardAttributeName)) |
| | | 215 | | { |
| | 3 | 216 | | occurrence = |
| | 3 | 217 | | ConstructorGuardAnalysisHelper.BuildDirectGuardOccurrence( |
| | 3 | 218 | | property, |
| | 3 | 219 | | property.Type, |
| | 3 | 220 | | "property", |
| | 3 | 221 | | attribute, |
| | 3 | 222 | | null); |
| | | 223 | | } |
| | 12 | 224 | | else if (ConstructorGuardAnalysisHelper.TryGetGuardDefinition( |
| | 12 | 225 | | attributeClass, |
| | 12 | 226 | | out var guardType, |
| | 12 | 227 | | out var methodName, |
| | 12 | 228 | | out var methodNameExplicit)) |
| | | 229 | | { |
| | 1 | 230 | | occurrence = |
| | 1 | 231 | | ConstructorGuardAnalysisHelper.BuildAliasOccurrence( |
| | 1 | 232 | | property, |
| | 1 | 233 | | property.Type, |
| | 1 | 234 | | "property", |
| | 1 | 235 | | attribute, |
| | 1 | 236 | | null, |
| | 1 | 237 | | guardType, |
| | 1 | 238 | | methodName, |
| | 1 | 239 | | methodNameExplicit, |
| | 1 | 240 | | attributeClass.DeclaringSyntaxReferences.Length > 0); |
| | | 241 | | } |
| | | 242 | | else |
| | | 243 | | { |
| | | 244 | | continue; |
| | | 245 | | } |
| | | 246 | | |
| | 4 | 247 | | ConstructorGuardAnalysisHelper.AnalyzePositiveGuardOccurrence( |
| | 4 | 248 | | context, |
| | 4 | 249 | | containingType, |
| | 4 | 250 | | occurrence, |
| | 4 | 251 | | ConstructorGuardAnalysisHelper.GetAttributeLocation( |
| | 4 | 252 | | context, |
| | 4 | 253 | | attribute)); |
| | | 254 | | } |
| | 11 | 255 | | } |
| | | 256 | | |
| | | 257 | | private static bool IsConstructorGuardOrAlias(AttributeData attribute) |
| | | 258 | | { |
| | 2 | 259 | | if (attribute.AttributeClass is not { } attributeClass) |
| | 0 | 260 | | return false; |
| | | 261 | | |
| | 2 | 262 | | return GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 2 | 263 | | attributeClass, |
| | 2 | 264 | | ConstructorGuardAttributeName) || |
| | 2 | 265 | | ConstructorGuardAnalysisHelper.TryGetGuardDefinition( |
| | 2 | 266 | | attributeClass, |
| | 2 | 267 | | out _, |
| | 2 | 268 | | out _, |
| | 2 | 269 | | out _); |
| | | 270 | | } |
| | | 271 | | } |