| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Collections.Immutable; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | using Microsoft.CodeAnalysis; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 8 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 9 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 10 | | |
| | | 11 | | using NexusLabs.Needlr.Generators.Models; |
| | | 12 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 13 | | |
| | | 14 | | namespace NexusLabs.Needlr.Generators; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Suggests Needlr-generated constructors only when an authored constructor is |
| | | 18 | | /// mechanically equivalent to the generator's supported parameter, guard, and |
| | | 19 | | /// field-assignment model. |
| | | 20 | | /// </summary> |
| | | 21 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 22 | | public sealed class GenerateConstructorSuggestionAnalyzer : DiagnosticAnalyzer |
| | | 23 | | { |
| | | 24 | | private const string ConstructorGuardAttributeName = "ConstructorGuardAttribute"; |
| | | 25 | | private const string ConstructorGuardDefinitionAttributeName = "ConstructorGuardDefinitionAttribute"; |
| | | 26 | | private const string ConstructorIgnoreAttributeName = "ConstructorIgnoreAttribute"; |
| | | 27 | | private const string DeferToContainerAttributeName = "DeferToContainerAttribute"; |
| | | 28 | | private const string RecordConstructorOverloadParameterAttributeName = "RecordConstructorOverloadParameterAttribute" |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the diagnostics produced by this analyzer. |
| | | 32 | | /// </summary> |
| | | 33 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 414 | 34 | | ImmutableArray.Create(DiagnosticDescriptors.GenerateConstructorSuggested); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Registers symbol analysis for named types. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="context">The analyzer initialization context.</param> |
| | | 40 | | public override void Initialize(AnalysisContext context) |
| | | 41 | | { |
| | 66 | 42 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 66 | 43 | | context.EnableConcurrentExecution(); |
| | 66 | 44 | | context.RegisterSyntaxNodeAction( |
| | 66 | 45 | | AnalyzeClassDeclaration, |
| | 66 | 46 | | SyntaxKind.ClassDeclaration); |
| | 66 | 47 | | } |
| | | 48 | | |
| | | 49 | | private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context) |
| | | 50 | | { |
| | 616 | 51 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | 616 | 52 | | if (context.SemanticModel.GetDeclaredSymbol( |
| | 616 | 53 | | classDeclaration, |
| | 616 | 54 | | context.CancellationToken) is not |
| | 616 | 55 | | INamedTypeSymbol typeSymbol) |
| | | 56 | | { |
| | 0 | 57 | | return; |
| | | 58 | | } |
| | | 59 | | |
| | 616 | 60 | | if (!IsSupportedType(typeSymbol) || |
| | 616 | 61 | | HasExistingConstructorGenerationConfiguration(typeSymbol) || |
| | 616 | 62 | | HasDeferToContainerAttribute(typeSymbol) || |
| | 616 | 63 | | RequiresParameterlessActivation(typeSymbol)) |
| | | 64 | | { |
| | 5 | 65 | | return; |
| | | 66 | | } |
| | | 67 | | |
| | 611 | 68 | | if (typeSymbol.BaseType is not null && |
| | 611 | 69 | | typeSymbol.BaseType.SpecialType != SpecialType.System_Object && |
| | 611 | 70 | | !GeneratedConstructorEligibility.HasAccessibleParameterlessConstructor(typeSymbol.BaseType)) |
| | | 71 | | { |
| | 1 | 72 | | return; |
| | | 73 | | } |
| | | 74 | | |
| | 610 | 75 | | var constructors = typeSymbol.InstanceConstructors |
| | 719 | 76 | | .Where(constructor => !constructor.IsImplicitlyDeclared) |
| | 610 | 77 | | .ToArray(); |
| | 610 | 78 | | if (constructors.Length != 1) |
| | 473 | 79 | | return; |
| | | 80 | | |
| | 137 | 81 | | var constructor = constructors[0]; |
| | 137 | 82 | | if (constructor.DeclaredAccessibility != Accessibility.Public || |
| | 137 | 83 | | constructor.GetAttributes().Length != 0 || |
| | 137 | 84 | | constructor.Parameters.Any(IsUnsupportedParameter)) |
| | | 85 | | { |
| | 7 | 86 | | return; |
| | | 87 | | } |
| | | 88 | | |
| | 130 | 89 | | var constructorSyntax = classDeclaration.Members |
| | 130 | 90 | | .OfType<ConstructorDeclarationSyntax>() |
| | 130 | 91 | | .SingleOrDefault(declaration => |
| | 251 | 92 | | SymbolEqualityComparer.Default.Equals( |
| | 251 | 93 | | context.SemanticModel.GetDeclaredSymbol( |
| | 251 | 94 | | declaration, |
| | 251 | 95 | | context.CancellationToken), |
| | 251 | 96 | | constructor)); |
| | 130 | 97 | | if (constructorSyntax is not null) |
| | | 98 | | { |
| | 121 | 99 | | if (IsGeneratedSyntax(constructorSyntax) || |
| | 121 | 100 | | !MatchesOrdinaryConstructor( |
| | 121 | 101 | | context, |
| | 121 | 102 | | typeSymbol, |
| | 121 | 103 | | constructor, |
| | 121 | 104 | | constructorSyntax)) |
| | | 105 | | { |
| | 113 | 106 | | return; |
| | | 107 | | } |
| | | 108 | | |
| | 8 | 109 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 8 | 110 | | DiagnosticDescriptors.GenerateConstructorSuggested, |
| | 8 | 111 | | constructorSyntax.Identifier.GetLocation(), |
| | 8 | 112 | | typeSymbol.Name)); |
| | 8 | 113 | | return; |
| | | 114 | | } |
| | | 115 | | |
| | 9 | 116 | | if (classDeclaration.ParameterList is null || |
| | 9 | 117 | | typeSymbol.DeclaringSyntaxReferences.Length != 1 || |
| | 9 | 118 | | IsGeneratedSyntax(classDeclaration) || |
| | 9 | 119 | | !MatchesPrimaryConstructor( |
| | 9 | 120 | | context, |
| | 9 | 121 | | typeSymbol, |
| | 9 | 122 | | constructor, |
| | 9 | 123 | | classDeclaration)) |
| | | 124 | | { |
| | 5 | 125 | | return; |
| | | 126 | | } |
| | | 127 | | |
| | 4 | 128 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 129 | | DiagnosticDescriptors.GenerateConstructorSuggested, |
| | 4 | 130 | | classDeclaration.Identifier.GetLocation(), |
| | 4 | 131 | | typeSymbol.Name)); |
| | 4 | 132 | | } |
| | | 133 | | |
| | | 134 | | private static bool IsSupportedType(INamedTypeSymbol typeSymbol) |
| | | 135 | | { |
| | 616 | 136 | | return typeSymbol.TypeKind == TypeKind.Class && |
| | 616 | 137 | | !typeSymbol.IsRecord && |
| | 616 | 138 | | !typeSymbol.IsFileLocal && |
| | 616 | 139 | | typeSymbol.ContainingType is null; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | private static bool HasExistingConstructorGenerationConfiguration( |
| | | 143 | | INamedTypeSymbol typeSymbol) |
| | | 144 | | { |
| | 615 | 145 | | if (GeneratedConstructorEligibility.HasGenerateConstructorAttribute(typeSymbol)) |
| | 1 | 146 | | return true; |
| | | 147 | | |
| | 6945 | 148 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 149 | | { |
| | 5721 | 150 | | foreach (var attribute in member.GetAttributes()) |
| | | 151 | | { |
| | 2 | 152 | | var attributeClass = attribute.AttributeClass; |
| | 2 | 153 | | if (attributeClass is null) |
| | | 154 | | continue; |
| | | 155 | | |
| | 2 | 156 | | if (GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 2 | 157 | | attributeClass, |
| | 2 | 158 | | ConstructorGuardAttributeName) || |
| | 2 | 159 | | GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 2 | 160 | | attributeClass, |
| | 2 | 161 | | ConstructorIgnoreAttributeName) || |
| | 2 | 162 | | GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 2 | 163 | | attributeClass, |
| | 2 | 164 | | RecordConstructorOverloadParameterAttributeName) || |
| | 2 | 165 | | IsConstructorGuardAlias(attributeClass)) |
| | | 166 | | { |
| | 1 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | |
| | 613 | 172 | | return false; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private static bool IsConstructorGuardAlias(INamedTypeSymbol attributeClass) |
| | | 176 | | { |
| | 1 | 177 | | return attributeClass.GetAttributes().Any(attribute => |
| | 2 | 178 | | attribute.AttributeClass is { } metaAttributeClass && |
| | 2 | 179 | | GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 2 | 180 | | metaAttributeClass, |
| | 2 | 181 | | ConstructorGuardDefinitionAttributeName)); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | private static bool HasDeferToContainerAttribute(INamedTypeSymbol typeSymbol) |
| | | 185 | | { |
| | 613 | 186 | | return typeSymbol.GetAttributes().Any(attribute => |
| | 1121 | 187 | | attribute.AttributeClass is { } attributeClass && |
| | 1121 | 188 | | attributeClass.Name == DeferToContainerAttributeName && |
| | 1121 | 189 | | attributeClass.ContainingNamespace?.ToDisplayString() == |
| | 1121 | 190 | | "NexusLabs.Needlr"); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | private static bool RequiresParameterlessActivation(INamedTypeSymbol typeSymbol) |
| | | 194 | | { |
| | 612 | 195 | | return typeSymbol.AllInterfaces.Any(interfaceSymbol => |
| | 612 | 196 | | { |
| | 37 | 197 | | var namespaceName = |
| | 37 | 198 | | interfaceSymbol.ContainingNamespace?.ToDisplayString(); |
| | 37 | 199 | | if (namespaceName == "NexusLabs.Needlr") |
| | 612 | 200 | | { |
| | 37 | 201 | | return interfaceSymbol.Name is |
| | 37 | 202 | | "IServiceCollectionPlugin" or |
| | 37 | 203 | | "IPostBuildServiceCollectionPlugin" or |
| | 37 | 204 | | "IWebApplicationBuilderPlugin" or |
| | 37 | 205 | | "IHostApplicationBuilderPlugin"; |
| | 612 | 206 | | } |
| | 612 | 207 | | |
| | 0 | 208 | | return namespaceName == "NexusLabs.Needlr.SignalR" && |
| | 0 | 209 | | interfaceSymbol.Name == "IHubRegistrationPlugin"; |
| | 612 | 210 | | }); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | private static bool IsUnsupportedParameter(IParameterSymbol parameter) |
| | | 214 | | { |
| | 251 | 215 | | return parameter.RefKind != RefKind.None || |
| | 251 | 216 | | parameter.IsParams || |
| | 251 | 217 | | parameter.HasExplicitDefaultValue || |
| | 251 | 218 | | parameter.GetAttributes().Length != 0 || |
| | 251 | 219 | | ContainsPointerType(parameter.Type); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | private static bool MatchesOrdinaryConstructor( |
| | | 223 | | SyntaxNodeAnalysisContext context, |
| | | 224 | | INamedTypeSymbol typeSymbol, |
| | | 225 | | IMethodSymbol constructor, |
| | | 226 | | ConstructorDeclarationSyntax constructorSyntax) |
| | | 227 | | { |
| | 121 | 228 | | if (constructorSyntax.Initializer is not null || |
| | 121 | 229 | | constructorSyntax.Body is null || |
| | 121 | 230 | | constructorSyntax.ExpressionBody is not null) |
| | | 231 | | { |
| | 72 | 232 | | return false; |
| | | 233 | | } |
| | | 234 | | |
| | 49 | 235 | | var fields = |
| | 49 | 236 | | GeneratedConstructorEligibility.GetEligibleConstructorFields( |
| | 49 | 237 | | typeSymbol); |
| | 49 | 238 | | if (fields.Count == 0 || fields.Count != constructor.Parameters.Length) |
| | 37 | 239 | | return false; |
| | | 240 | | |
| | 52 | 241 | | for (var i = 0; i < fields.Count; i++) |
| | | 242 | | { |
| | 15 | 243 | | if (!MatchesParameter(fields[i], constructor.Parameters[i])) |
| | 1 | 244 | | return false; |
| | | 245 | | } |
| | | 246 | | |
| | 11 | 247 | | var semanticModel = context.SemanticModel; |
| | 11 | 248 | | var statements = constructorSyntax.Body.Statements; |
| | 11 | 249 | | var guards = new GeneratedConstructorGuardKind[fields.Count]; |
| | 11 | 250 | | var statementIndex = 0; |
| | | 251 | | |
| | 50 | 252 | | for (var i = 0; i < fields.Count; i++) |
| | | 253 | | { |
| | 14 | 254 | | if (statementIndex >= statements.Count || |
| | 14 | 255 | | !TryMatchGuardStatement( |
| | 14 | 256 | | semanticModel.Compilation, |
| | 14 | 257 | | semanticModel, |
| | 14 | 258 | | statements[statementIndex], |
| | 14 | 259 | | constructor.Parameters[i], |
| | 14 | 260 | | fields[i].Type, |
| | 14 | 261 | | out var guard)) |
| | | 262 | | { |
| | | 263 | | continue; |
| | | 264 | | } |
| | | 265 | | |
| | 7 | 266 | | guards[i] = guard; |
| | 7 | 267 | | statementIndex++; |
| | | 268 | | } |
| | | 269 | | |
| | 44 | 270 | | for (var i = 0; i < fields.Count; i++) |
| | | 271 | | { |
| | 14 | 272 | | if (statementIndex >= statements.Count || |
| | 14 | 273 | | !MatchesAssignmentStatement( |
| | 14 | 274 | | semanticModel, |
| | 14 | 275 | | statements[statementIndex], |
| | 14 | 276 | | fields[i], |
| | 14 | 277 | | constructor.Parameters[i])) |
| | | 278 | | { |
| | 3 | 279 | | return false; |
| | | 280 | | } |
| | | 281 | | |
| | 11 | 282 | | statementIndex++; |
| | | 283 | | } |
| | | 284 | | |
| | 8 | 285 | | return statementIndex == statements.Count && |
| | 8 | 286 | | GuardsCanBeGenerated(fields, guards); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | private static bool MatchesPrimaryConstructor( |
| | | 290 | | SyntaxNodeAnalysisContext context, |
| | | 291 | | INamedTypeSymbol typeSymbol, |
| | | 292 | | IMethodSymbol constructor, |
| | | 293 | | ClassDeclarationSyntax primaryDeclaration) |
| | | 294 | | { |
| | 9 | 295 | | if (primaryDeclaration.ParameterList is null || |
| | 9 | 296 | | primaryDeclaration.BaseList?.Types |
| | 9 | 297 | | .OfType<PrimaryConstructorBaseTypeSyntax>() |
| | 0 | 298 | | .Any(baseType => baseType.ArgumentList.Arguments.Count > 0) == |
| | 9 | 299 | | true) |
| | | 300 | | { |
| | 0 | 301 | | return false; |
| | | 302 | | } |
| | | 303 | | |
| | 9 | 304 | | var fields = GeneratedConstructorEligibility |
| | 9 | 305 | | .GetOrderedInstanceFields(typeSymbol) |
| | 9 | 306 | | .Where(field => |
| | 14 | 307 | | field.DeclaredAccessibility == Accessibility.Private && |
| | 14 | 308 | | field.IsReadOnly) |
| | 9 | 309 | | .ToArray(); |
| | 9 | 310 | | if (fields.Length == 0 || fields.Length != constructor.Parameters.Length) |
| | 1 | 311 | | return false; |
| | | 312 | | |
| | 8 | 313 | | var initializers = new ExpressionSyntax[fields.Length]; |
| | 8 | 314 | | var guards = new GeneratedConstructorGuardKind[fields.Length]; |
| | 38 | 315 | | for (var i = 0; i < fields.Length; i++) |
| | | 316 | | { |
| | 12 | 317 | | if (!MatchesParameter(fields[i], constructor.Parameters[i]) || |
| | 12 | 318 | | !TryGetFieldInitializer( |
| | 12 | 319 | | fields[i], |
| | 12 | 320 | | context.CancellationToken, |
| | 12 | 321 | | out var initializer) || |
| | 12 | 322 | | initializer.SyntaxTree != primaryDeclaration.SyntaxTree || |
| | 12 | 323 | | !TryMatchPrimaryInitializer( |
| | 12 | 324 | | context.SemanticModel.Compilation, |
| | 12 | 325 | | context.SemanticModel, |
| | 12 | 326 | | initializer, |
| | 12 | 327 | | constructor.Parameters[i], |
| | 12 | 328 | | fields[i].Type, |
| | 12 | 329 | | out guards[i])) |
| | | 330 | | { |
| | 1 | 331 | | return false; |
| | | 332 | | } |
| | | 333 | | |
| | 11 | 334 | | initializers[i] = initializer; |
| | | 335 | | } |
| | | 336 | | |
| | 7 | 337 | | var guardedCount = guards.Count( |
| | 17 | 338 | | guard => guard != GeneratedConstructorGuardKind.None); |
| | 7 | 339 | | if (guardedCount > 0 && fields.Length > 1) |
| | 1 | 340 | | return false; |
| | | 341 | | |
| | 6 | 342 | | if (HasOtherInstanceInitializer( |
| | 6 | 343 | | context, |
| | 6 | 344 | | primaryDeclaration, |
| | 6 | 345 | | fields) || |
| | 6 | 346 | | HasPrimaryParameterReferenceOutsideInitializers( |
| | 6 | 347 | | context, |
| | 6 | 348 | | primaryDeclaration, |
| | 6 | 349 | | constructor.Parameters, |
| | 6 | 350 | | initializers)) |
| | | 351 | | { |
| | 2 | 352 | | return false; |
| | | 353 | | } |
| | | 354 | | |
| | 4 | 355 | | return GuardsCanBeGenerated(fields, guards); |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | private static bool MatchesParameter( |
| | | 359 | | IFieldSymbol field, |
| | | 360 | | IParameterSymbol parameter) |
| | | 361 | | { |
| | 27 | 362 | | var parameterName = |
| | 27 | 363 | | ConstructorGenerationDiscoveryHelper.GetParameterName(field.Name); |
| | 27 | 364 | | if (parameterName.Length > 0 && parameterName[0] == '@') |
| | 0 | 365 | | parameterName = parameterName.Substring(1); |
| | | 366 | | |
| | 27 | 367 | | return parameter.Name == parameterName && |
| | 27 | 368 | | field.Type.ToDisplayString( |
| | 27 | 369 | | ConstructorGenerationDiscoveryHelper.NullableAwareFormat) == |
| | 27 | 370 | | parameter.Type.ToDisplayString( |
| | 27 | 371 | | ConstructorGenerationDiscoveryHelper.NullableAwareFormat); |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | private static bool TryMatchGuardStatement( |
| | | 375 | | Compilation compilation, |
| | | 376 | | SemanticModel semanticModel, |
| | | 377 | | StatementSyntax statement, |
| | | 378 | | IParameterSymbol parameter, |
| | | 379 | | ITypeSymbol fieldType, |
| | | 380 | | out GeneratedConstructorGuardKind guard) |
| | | 381 | | { |
| | 14 | 382 | | guard = GeneratedConstructorGuardKind.None; |
| | 14 | 383 | | if (statement is not ExpressionStatementSyntax |
| | 14 | 384 | | { |
| | 14 | 385 | | Expression: InvocationExpressionSyntax invocation, |
| | 14 | 386 | | } || |
| | 14 | 387 | | invocation.ArgumentList.Arguments.Count != 1 || |
| | 14 | 388 | | !ReferencesSymbol( |
| | 14 | 389 | | semanticModel, |
| | 14 | 390 | | invocation.ArgumentList.Arguments[0].Expression, |
| | 14 | 391 | | parameter)) |
| | | 392 | | { |
| | 6 | 393 | | return false; |
| | | 394 | | } |
| | | 395 | | |
| | 8 | 396 | | if (semanticModel.GetSymbolInfo(invocation).Symbol is not |
| | 8 | 397 | | IMethodSymbol { IsStatic: true } method) |
| | | 398 | | { |
| | 0 | 399 | | return false; |
| | | 400 | | } |
| | | 401 | | |
| | 8 | 402 | | var argumentNullException = |
| | 8 | 403 | | compilation.GetTypeByMetadataName( |
| | 8 | 404 | | "System.ArgumentNullException"); |
| | 8 | 405 | | var argumentException = |
| | 8 | 406 | | compilation.GetTypeByMetadataName("System.ArgumentException"); |
| | | 407 | | |
| | 8 | 408 | | if (method.Name == "ThrowIfNull" && |
| | 8 | 409 | | SymbolEqualityComparer.Default.Equals( |
| | 8 | 410 | | method.ContainingType, |
| | 8 | 411 | | argumentNullException)) |
| | | 412 | | { |
| | 4 | 413 | | guard = GeneratedConstructorGuardKind.NotNull; |
| | | 414 | | } |
| | 4 | 415 | | else if (method.Name == "ThrowIfNullOrEmpty" && |
| | 4 | 416 | | SymbolEqualityComparer.Default.Equals( |
| | 4 | 417 | | method.ContainingType, |
| | 4 | 418 | | argumentException)) |
| | | 419 | | { |
| | 2 | 420 | | guard = GeneratedConstructorGuardKind.NotNullOrEmpty; |
| | | 421 | | } |
| | 2 | 422 | | else if (method.Name == "ThrowIfNullOrWhiteSpace" && |
| | 2 | 423 | | SymbolEqualityComparer.Default.Equals( |
| | 2 | 424 | | method.ContainingType, |
| | 2 | 425 | | argumentException)) |
| | | 426 | | { |
| | 2 | 427 | | guard = GeneratedConstructorGuardKind.NotNullOrWhiteSpace; |
| | | 428 | | } |
| | | 429 | | else |
| | | 430 | | { |
| | 0 | 431 | | return false; |
| | | 432 | | } |
| | | 433 | | |
| | 8 | 434 | | return IsGuardCompatible(fieldType, guard); |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | private static bool MatchesAssignmentStatement( |
| | | 438 | | SemanticModel semanticModel, |
| | | 439 | | StatementSyntax statement, |
| | | 440 | | IFieldSymbol field, |
| | | 441 | | IParameterSymbol parameter) |
| | | 442 | | { |
| | 14 | 443 | | return statement is ExpressionStatementSyntax |
| | 14 | 444 | | { |
| | 14 | 445 | | Expression: AssignmentExpressionSyntax assignment, |
| | 14 | 446 | | } && |
| | 14 | 447 | | assignment.IsKind(SyntaxKind.SimpleAssignmentExpression) && |
| | 14 | 448 | | ReferencesSymbol(semanticModel, assignment.Left, field) && |
| | 14 | 449 | | ReferencesSymbol(semanticModel, assignment.Right, parameter); |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | private static bool TryMatchPrimaryInitializer( |
| | | 453 | | Compilation compilation, |
| | | 454 | | SemanticModel semanticModel, |
| | | 455 | | ExpressionSyntax initializer, |
| | | 456 | | IParameterSymbol parameter, |
| | | 457 | | ITypeSymbol fieldType, |
| | | 458 | | out GeneratedConstructorGuardKind guard) |
| | | 459 | | { |
| | 12 | 460 | | guard = GeneratedConstructorGuardKind.None; |
| | 12 | 461 | | if (ReferencesSymbol(semanticModel, initializer, parameter)) |
| | 6 | 462 | | return true; |
| | | 463 | | |
| | 6 | 464 | | if (initializer is not BinaryExpressionSyntax |
| | 6 | 465 | | { |
| | 6 | 466 | | RawKind: (int)SyntaxKind.CoalesceExpression, |
| | 6 | 467 | | Right: ThrowExpressionSyntax |
| | 6 | 468 | | { |
| | 6 | 469 | | Expression: ObjectCreationExpressionSyntax creation, |
| | 6 | 470 | | }, |
| | 6 | 471 | | } coalesce || |
| | 6 | 472 | | !ReferencesSymbol(semanticModel, coalesce.Left, parameter) || |
| | 6 | 473 | | creation.Initializer is not null || |
| | 6 | 474 | | creation.ArgumentList?.Arguments.Count != 1) |
| | | 475 | | { |
| | 1 | 476 | | return false; |
| | | 477 | | } |
| | | 478 | | |
| | 5 | 479 | | var argumentNullException = |
| | 5 | 480 | | compilation.GetTypeByMetadataName( |
| | 5 | 481 | | "System.ArgumentNullException"); |
| | 5 | 482 | | if (!SymbolEqualityComparer.Default.Equals( |
| | 5 | 483 | | semanticModel.GetTypeInfo(creation).Type, |
| | 5 | 484 | | argumentNullException) || |
| | 5 | 485 | | creation.ArgumentList.Arguments[0].Expression is not |
| | 5 | 486 | | InvocationExpressionSyntax |
| | 5 | 487 | | { |
| | 5 | 488 | | Expression: IdentifierNameSyntax nameofIdentifier, |
| | 5 | 489 | | ArgumentList.Arguments.Count: 1, |
| | 5 | 490 | | } nameofInvocation || |
| | 5 | 491 | | nameofIdentifier.Identifier.ValueText != "nameof" || |
| | 5 | 492 | | !ReferencesSymbol( |
| | 5 | 493 | | semanticModel, |
| | 5 | 494 | | nameofInvocation.ArgumentList.Arguments[0].Expression, |
| | 5 | 495 | | parameter)) |
| | | 496 | | { |
| | 0 | 497 | | return false; |
| | | 498 | | } |
| | | 499 | | |
| | 5 | 500 | | guard = GeneratedConstructorGuardKind.NotNull; |
| | 5 | 501 | | return IsGuardCompatible(fieldType, guard); |
| | | 502 | | } |
| | | 503 | | |
| | | 504 | | private static bool HasOtherInstanceInitializer( |
| | | 505 | | SyntaxNodeAnalysisContext context, |
| | | 506 | | ClassDeclarationSyntax declaration, |
| | | 507 | | IReadOnlyList<IFieldSymbol> captureFields) |
| | | 508 | | { |
| | 6 | 509 | | var captures = new HashSet<IFieldSymbol>( |
| | 6 | 510 | | captureFields, |
| | 6 | 511 | | SymbolEqualityComparer.Default); |
| | | 512 | | |
| | 31 | 513 | | foreach (var member in declaration.Members) |
| | | 514 | | { |
| | 10 | 515 | | if (member is FieldDeclarationSyntax fieldDeclaration) |
| | | 516 | | { |
| | 32 | 517 | | foreach (var variable in fieldDeclaration.Declaration.Variables) |
| | | 518 | | { |
| | 8 | 519 | | if (variable.Initializer is null || |
| | 8 | 520 | | context.SemanticModel.GetDeclaredSymbol( |
| | 8 | 521 | | variable, |
| | 8 | 522 | | context.CancellationToken) is not |
| | 8 | 523 | | IFieldSymbol field || |
| | 8 | 524 | | field.IsStatic || |
| | 8 | 525 | | captures.Contains(field)) |
| | | 526 | | { |
| | | 527 | | continue; |
| | | 528 | | } |
| | | 529 | | |
| | 0 | 530 | | return true; |
| | | 531 | | } |
| | | 532 | | } |
| | 2 | 533 | | else if (member is PropertyDeclarationSyntax |
| | 2 | 534 | | { |
| | 2 | 535 | | Initializer: not null, |
| | 2 | 536 | | } propertyDeclaration && |
| | 2 | 537 | | context.SemanticModel.GetDeclaredSymbol( |
| | 2 | 538 | | propertyDeclaration, |
| | 2 | 539 | | context.CancellationToken) is |
| | 2 | 540 | | IPropertySymbol { IsStatic: false }) |
| | | 541 | | { |
| | 1 | 542 | | return true; |
| | | 543 | | } |
| | 1 | 544 | | else if (member is EventFieldDeclarationSyntax |
| | 1 | 545 | | eventFieldDeclaration) |
| | | 546 | | { |
| | 0 | 547 | | foreach (var variable in |
| | 0 | 548 | | eventFieldDeclaration.Declaration.Variables) |
| | | 549 | | { |
| | 0 | 550 | | if (variable.Initializer is not null && |
| | 0 | 551 | | context.SemanticModel.GetDeclaredSymbol( |
| | 0 | 552 | | variable, |
| | 0 | 553 | | context.CancellationToken) is |
| | 0 | 554 | | IEventSymbol { IsStatic: false }) |
| | | 555 | | { |
| | 0 | 556 | | return true; |
| | | 557 | | } |
| | | 558 | | } |
| | | 559 | | } |
| | | 560 | | } |
| | | 561 | | |
| | 5 | 562 | | return false; |
| | | 563 | | } |
| | | 564 | | |
| | | 565 | | private static bool HasPrimaryParameterReferenceOutsideInitializers( |
| | | 566 | | SyntaxNodeAnalysisContext context, |
| | | 567 | | ClassDeclarationSyntax declaration, |
| | | 568 | | ImmutableArray<IParameterSymbol> parameters, |
| | | 569 | | IReadOnlyList<ExpressionSyntax> initializers) |
| | | 570 | | { |
| | 66 | 571 | | foreach (var identifier in |
| | 5 | 572 | | declaration.DescendantNodes().OfType<IdentifierNameSyntax>()) |
| | | 573 | | { |
| | 31 | 574 | | if (context.SemanticModel.GetSymbolInfo( |
| | 31 | 575 | | identifier, |
| | 31 | 576 | | context.CancellationToken).Symbol is not |
| | 31 | 577 | | IParameterSymbol referencedParameter) |
| | | 578 | | { |
| | | 579 | | continue; |
| | | 580 | | } |
| | | 581 | | |
| | 10 | 582 | | var parameterIndex = IndexOfParameter( |
| | 10 | 583 | | parameters, |
| | 10 | 584 | | referencedParameter); |
| | 10 | 585 | | if (parameterIndex < 0) |
| | | 586 | | continue; |
| | | 587 | | |
| | 10 | 588 | | var allowedInitializer = initializers[parameterIndex]; |
| | 10 | 589 | | if (!allowedInitializer.Span.Contains(identifier.Span)) |
| | 1 | 590 | | return true; |
| | | 591 | | } |
| | | 592 | | |
| | 4 | 593 | | return false; |
| | 1 | 594 | | } |
| | | 595 | | |
| | | 596 | | private static int IndexOfParameter( |
| | | 597 | | ImmutableArray<IParameterSymbol> parameters, |
| | | 598 | | IParameterSymbol candidate) |
| | | 599 | | { |
| | 24 | 600 | | for (var i = 0; i < parameters.Length; i++) |
| | | 601 | | { |
| | 12 | 602 | | if (SymbolEqualityComparer.Default.Equals(parameters[i], candidate)) |
| | 10 | 603 | | return i; |
| | | 604 | | } |
| | | 605 | | |
| | 0 | 606 | | return -1; |
| | | 607 | | } |
| | | 608 | | |
| | | 609 | | private static bool TryGetFieldInitializer( |
| | | 610 | | IFieldSymbol field, |
| | | 611 | | System.Threading.CancellationToken cancellationToken, |
| | | 612 | | out ExpressionSyntax initializer) |
| | | 613 | | { |
| | 36 | 614 | | foreach (var syntaxReference in field.DeclaringSyntaxReferences) |
| | | 615 | | { |
| | 12 | 616 | | if (syntaxReference.GetSyntax(cancellationToken) is |
| | 12 | 617 | | VariableDeclaratorSyntax |
| | 12 | 618 | | { |
| | 12 | 619 | | Initializer.Value: { } value, |
| | 12 | 620 | | }) |
| | | 621 | | { |
| | 12 | 622 | | initializer = value; |
| | 12 | 623 | | return true; |
| | | 624 | | } |
| | | 625 | | } |
| | | 626 | | |
| | 0 | 627 | | initializer = null!; |
| | 0 | 628 | | return false; |
| | | 629 | | } |
| | | 630 | | |
| | | 631 | | private static bool GuardsCanBeGenerated( |
| | | 632 | | IReadOnlyList<IFieldSymbol> fields, |
| | | 633 | | IReadOnlyList<GeneratedConstructorGuardKind> guards) |
| | | 634 | | { |
| | 56 | 635 | | for (var i = 0; i < fields.Count; i++) |
| | | 636 | | { |
| | 16 | 637 | | if (!IsGuardCompatible(fields[i].Type, guards[i])) |
| | 0 | 638 | | return false; |
| | | 639 | | } |
| | | 640 | | |
| | 12 | 641 | | return true; |
| | | 642 | | } |
| | | 643 | | |
| | | 644 | | private static bool IsGuardCompatible( |
| | | 645 | | ITypeSymbol fieldType, |
| | | 646 | | GeneratedConstructorGuardKind guard) |
| | | 647 | | { |
| | 29 | 648 | | return guard switch |
| | 29 | 649 | | { |
| | 8 | 650 | | GeneratedConstructorGuardKind.None => true, |
| | 29 | 651 | | GeneratedConstructorGuardKind.NotNull => |
| | 13 | 652 | | ConstructorGuardAnalysisHelper.CanBeRuntimeNull(fieldType), |
| | 29 | 653 | | GeneratedConstructorGuardKind.NotNullOrEmpty or |
| | 29 | 654 | | GeneratedConstructorGuardKind.NotNullOrWhiteSpace => |
| | 8 | 655 | | fieldType.SpecialType == SpecialType.System_String, |
| | 0 | 656 | | _ => false, |
| | 29 | 657 | | }; |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | private static bool ReferencesSymbol( |
| | | 661 | | SemanticModel semanticModel, |
| | | 662 | | ExpressionSyntax expression, |
| | | 663 | | ISymbol symbol) |
| | | 664 | | { |
| | 54 | 665 | | return SymbolEqualityComparer.Default.Equals( |
| | 54 | 666 | | semanticModel.GetSymbolInfo(expression).Symbol, |
| | 54 | 667 | | symbol); |
| | | 668 | | } |
| | | 669 | | |
| | | 670 | | private static bool ContainsPointerType(ITypeSymbol type) |
| | | 671 | | { |
| | 283 | 672 | | return type switch |
| | 283 | 673 | | { |
| | 1 | 674 | | IPointerTypeSymbol => true, |
| | 0 | 675 | | IFunctionPointerTypeSymbol => true, |
| | 283 | 676 | | IArrayTypeSymbol arrayType => |
| | 36 | 677 | | ContainsPointerType(arrayType.ElementType), |
| | 246 | 678 | | _ => false, |
| | 283 | 679 | | }; |
| | | 680 | | } |
| | | 681 | | |
| | | 682 | | private static bool IsGeneratedSyntax(SyntaxNode syntax) |
| | | 683 | | { |
| | 130 | 684 | | var filePath = syntax.SyntaxTree.FilePath; |
| | 130 | 685 | | if (filePath.EndsWith(".g.cs", StringComparison.OrdinalIgnoreCase) || |
| | 130 | 686 | | filePath.EndsWith( |
| | 130 | 687 | | ".generated.cs", |
| | 130 | 688 | | StringComparison.OrdinalIgnoreCase) || |
| | 130 | 689 | | filePath.EndsWith( |
| | 130 | 690 | | ".designer.cs", |
| | 130 | 691 | | StringComparison.OrdinalIgnoreCase)) |
| | | 692 | | { |
| | 0 | 693 | | return true; |
| | | 694 | | } |
| | | 695 | | |
| | 130 | 696 | | var leadingText = syntax.SyntaxTree |
| | 130 | 697 | | .GetRoot() |
| | 130 | 698 | | .GetLeadingTrivia() |
| | 130 | 699 | | .ToFullString(); |
| | 130 | 700 | | return leadingText.IndexOf( |
| | 130 | 701 | | "<auto-generated", |
| | 130 | 702 | | StringComparison.OrdinalIgnoreCase) >= 0; |
| | | 703 | | } |
| | | 704 | | } |