|
| 1 | +using System.Collections.Immutable; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | + |
| 7 | +namespace BitMono.Analyzers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Flags <c>Context.Module.GetAllTypes()</c> inside a protection: it walks the whole module and |
| 11 | +/// bypasses the <c>[DoNotResolve]</c> filtering BitMono applies to <c>Context.Parameters.Members</c>. |
| 12 | +/// </summary> |
| 13 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 14 | +public sealed class IterateMembersNotModuleAnalyzer : DiagnosticAnalyzer |
| 15 | +{ |
| 16 | + public const string DiagnosticId = "BITM0001"; |
| 17 | + |
| 18 | + private static readonly DiagnosticDescriptor Rule = new( |
| 19 | + id: DiagnosticId, |
| 20 | + title: "Iterate Context.Parameters.Members, not the module", |
| 21 | + messageFormat: "'{0}' walks the whole module and bypasses BitMono's [DoNotResolve] filtering; iterate Context.Parameters.Members.OfType<…>() instead so excluded members are respected", |
| 22 | + category: "Usage", |
| 23 | + defaultSeverity: DiagnosticSeverity.Warning, |
| 24 | + isEnabledByDefault: true, |
| 25 | + description: "Inside a protection, Context.Module.GetAllTypes() returns every type and skips the reflection / special-runtime / model / BAML filtering BitMono applies to Context.Parameters.Members. Iterate the sorted member list instead. Suppress this if you genuinely need the raw module (e.g. collecting references to re-sync after renaming).", |
| 26 | + helpLinkUri: "https://bitmono.readthedocs.io/en/latest/developers/do-not-resolve-members.html"); |
| 27 | + |
| 28 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 29 | + |
| 30 | + public override void Initialize(AnalysisContext context) |
| 31 | + { |
| 32 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 33 | + context.EnableConcurrentExecution(); |
| 34 | + context.RegisterCompilationStartAction(static start => |
| 35 | + { |
| 36 | + // Only meaningful in a BitMono protection-authoring compilation. If IProtection isn't |
| 37 | + // referenced, this isn't one - do nothing. |
| 38 | + var protectionInterface = start.Compilation.GetTypeByMetadataName("BitMono.API.Protections.IProtection"); |
| 39 | + if (protectionInterface is null) |
| 40 | + { |
| 41 | + return; |
| 42 | + } |
| 43 | + start.RegisterSyntaxNodeAction(ctx => Analyze(ctx, protectionInterface), SyntaxKind.InvocationExpression); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + private static void Analyze(SyntaxNodeAnalysisContext context, INamedTypeSymbol protectionInterface) |
| 48 | + { |
| 49 | + var invocation = (InvocationExpressionSyntax)context.Node; |
| 50 | + if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + if (memberAccess.Name.Identifier.ValueText != "GetAllTypes") |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + // The receiver must be an AsmResolver ModuleDefinition (e.g. Context.Module), so we don't trip |
| 59 | + // on some unrelated GetAllTypes() the author happens to define. |
| 60 | + var receiverType = context.SemanticModel.GetTypeInfo(memberAccess.Expression, context.CancellationToken).Type; |
| 61 | + if (receiverType is not { Name: "ModuleDefinition" } || |
| 62 | + receiverType.ContainingNamespace?.ToDisplayString() != "AsmResolver.DotNet") |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + // Only inside a protection. Anchored on IProtection so it covers built-in protections and |
| 67 | + // external plugins alike, whatever namespace they live in. |
| 68 | + var enclosingType = context.SemanticModel |
| 69 | + .GetEnclosingSymbol(invocation.SpanStart, context.CancellationToken)?.ContainingType; |
| 70 | + if (enclosingType is null || !ImplementsProtection(enclosingType, protectionInterface)) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + context.ReportDiagnostic(Diagnostic.Create(Rule, memberAccess.GetLocation(), memberAccess.ToString())); |
| 75 | + } |
| 76 | + |
| 77 | + private static bool ImplementsProtection(INamedTypeSymbol type, INamedTypeSymbol protectionInterface) |
| 78 | + { |
| 79 | + foreach (var iface in type.AllInterfaces) |
| 80 | + { |
| 81 | + if (SymbolEqualityComparer.Default.Equals(iface, protectionInterface)) |
| 82 | + { |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
0 commit comments