| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using Microsoft.CodeAnalysis; |
| | | 3 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 4 | | using Microsoft.CodeAnalysis.Operations; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.Analyzers; |
| | | 7 | | |
| | | 8 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 9 | | public sealed class GovernanceArtifactPersistenceAnalyzer : DiagnosticAnalyzer |
| | | 10 | | { |
| | | 11 | | public const string DiagnosticId = "ASIB001"; |
| | | 12 | | |
| | 2 | 13 | | private static readonly DiagnosticDescriptor Rule = new( |
| | 2 | 14 | | DiagnosticId, |
| | 2 | 15 | | "Persist or continue AsiBackbone governance artifact", |
| | 2 | 16 | | "AsiBackbone governance artifact '{0}' is created or returned and then discarded; persist audit/outbox residue o |
| | 2 | 17 | | "AsiBackbone.GovernanceSafety", |
| | 2 | 18 | | DiagnosticSeverity.Warning, |
| | 2 | 19 | | isEnabledByDefault: true, |
| | 2 | 20 | | description: "Governance decisions, audit residue, capability grants, handshake outcomes, and outbox artifacts s |
| | | 21 | | |
| | 2 | 22 | | private static readonly ImmutableHashSet<string> GovernanceArtifactTypeNames = ImmutableHashSet.Create( |
| | 2 | 23 | | StringComparer.Ordinal, |
| | 2 | 24 | | "AsiBackbone.Core.Audit.AuditLedgerRecord", |
| | 2 | 25 | | "AsiBackbone.Core.Audit.AuditResidue", |
| | 2 | 26 | | "AsiBackbone.Core.CapabilityTokens.CapabilityGrantUseResult", |
| | 2 | 27 | | "AsiBackbone.Core.CapabilityTokens.CapabilityGrantValidationResult", |
| | 2 | 28 | | "AsiBackbone.Core.CapabilityTokens.CapabilityTokenGrant", |
| | 2 | 29 | | "AsiBackbone.Core.Decisions.GovernanceDecision", |
| | 2 | 30 | | "AsiBackbone.Core.Emissions.GovernanceEmissionEnvelope", |
| | 2 | 31 | | "AsiBackbone.Core.Emissions.GovernanceEmissionResult", |
| | 2 | 32 | | "AsiBackbone.Core.Handshakes.LiabilityHandshakeAcknowledgment", |
| | 2 | 33 | | "AsiBackbone.Core.Outbox.GovernanceOutboxEntry"); |
| | | 34 | | |
| | 14 | 35 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule]; |
| | | 36 | | |
| | | 37 | | public override void Initialize(AnalysisContext context) |
| | | 38 | | { |
| | 14 | 39 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 14 | 40 | | context.EnableConcurrentExecution(); |
| | 14 | 41 | | context.RegisterOperationAction(AnalyzeExpressionStatement, OperationKind.ExpressionStatement); |
| | 14 | 42 | | context.RegisterOperationAction(AnalyzeDiscardAssignment, OperationKind.SimpleAssignment); |
| | 14 | 43 | | } |
| | | 44 | | |
| | | 45 | | private static void AnalyzeExpressionStatement(OperationAnalysisContext context) |
| | | 46 | | { |
| | 12 | 47 | | var expressionStatement = (IExpressionStatementOperation)context.Operation; |
| | | 48 | | |
| | 12 | 49 | | if (IsSuppressedByHostMarker(context.ContainingSymbol) || expressionStatement.Operation is ISimpleAssignmentOper |
| | | 50 | | { |
| | 6 | 51 | | return; |
| | | 52 | | } |
| | | 53 | | |
| | 6 | 54 | | IOperation expression = UnwrapAwait(expressionStatement.Operation); |
| | 6 | 55 | | ITypeSymbol? artifactType = GetGovernanceArtifactType(expression.Type); |
| | 6 | 56 | | if (artifactType is null || !IsArtifactProducerOperation(expression)) |
| | | 57 | | { |
| | 0 | 58 | | return; |
| | | 59 | | } |
| | | 60 | | |
| | 6 | 61 | | context.ReportDiagnostic( |
| | 6 | 62 | | Diagnostic.Create( |
| | 6 | 63 | | Rule, |
| | 6 | 64 | | expression.Syntax.GetLocation(), |
| | 6 | 65 | | artifactType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))); |
| | 6 | 66 | | } |
| | | 67 | | |
| | | 68 | | private static void AnalyzeDiscardAssignment(OperationAnalysisContext context) |
| | | 69 | | { |
| | 4 | 70 | | var assignment = (ISimpleAssignmentOperation)context.Operation; |
| | | 71 | | |
| | 4 | 72 | | if (IsSuppressedByHostMarker(context.ContainingSymbol) || assignment.Target is not IDiscardOperation) |
| | | 73 | | { |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | 4 | 77 | | IOperation value = UnwrapAwait(assignment.Value); |
| | 4 | 78 | | ITypeSymbol? artifactType = GetGovernanceArtifactType(value.Type); |
| | 4 | 79 | | if (artifactType is null || !IsArtifactProducerOperation(value)) |
| | | 80 | | { |
| | 2 | 81 | | return; |
| | | 82 | | } |
| | | 83 | | |
| | 2 | 84 | | context.ReportDiagnostic( |
| | 2 | 85 | | Diagnostic.Create( |
| | 2 | 86 | | Rule, |
| | 2 | 87 | | assignment.Syntax.GetLocation(), |
| | 2 | 88 | | artifactType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))); |
| | 2 | 89 | | } |
| | | 90 | | |
| | | 91 | | private static IOperation UnwrapAwait(IOperation operation) |
| | | 92 | | { |
| | 20 | 93 | | return operation is IAwaitOperation awaitOperation |
| | 20 | 94 | | ? awaitOperation.Operation |
| | 20 | 95 | | : operation; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | private static bool IsArtifactProducerOperation(IOperation? operation) |
| | | 99 | | { |
| | 10 | 100 | | if (operation is null) |
| | | 101 | | { |
| | 0 | 102 | | return false; |
| | | 103 | | } |
| | | 104 | | |
| | 10 | 105 | | operation = UnwrapAwait(operation); |
| | | 106 | | |
| | 10 | 107 | | while (operation is IConversionOperation conversionOperation) |
| | | 108 | | { |
| | 0 | 109 | | operation = conversionOperation.Operand; |
| | 0 | 110 | | } |
| | | 111 | | |
| | 10 | 112 | | return operation switch |
| | 10 | 113 | | { |
| | 8 | 114 | | IInvocationOperation => true, |
| | 0 | 115 | | IObjectCreationOperation => true, |
| | 0 | 116 | | IPropertyReferenceOperation => true, |
| | 0 | 117 | | IConditionalOperation conditionalOperation => IsArtifactProducerOperation(conditionalOperation.WhenTrue) |
| | 0 | 118 | | || IsArtifactProducerOperation(conditionalOperation.WhenFalse), |
| | 0 | 119 | | ICoalesceOperation coalesceOperation => IsArtifactProducerOperation(coalesceOperation.Value) |
| | 0 | 120 | | || IsArtifactProducerOperation(coalesceOperation.WhenNull), |
| | 2 | 121 | | _ => false |
| | 10 | 122 | | }; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | private static ITypeSymbol? GetGovernanceArtifactType(ITypeSymbol? type) |
| | | 126 | | { |
| | 10 | 127 | | ITypeSymbol? candidate = UnwrapKnownWrapper(type); |
| | 10 | 128 | | if (candidate is null) |
| | | 129 | | { |
| | 0 | 130 | | return null; |
| | | 131 | | } |
| | | 132 | | |
| | 10 | 133 | | string candidateName = candidate.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat); |
| | 10 | 134 | | return GovernanceArtifactTypeNames.Contains(candidateName) ? candidate : null; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | private static ITypeSymbol? UnwrapKnownWrapper(ITypeSymbol? type) |
| | | 138 | | { |
| | 10 | 139 | | if (type is not INamedTypeSymbol namedType || !namedType.IsGenericType || namedType.TypeArguments.Length != 1) |
| | | 140 | | { |
| | 6 | 141 | | return type; |
| | | 142 | | } |
| | | 143 | | |
| | 4 | 144 | | string namespaceName = namedType.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageForm |
| | | 145 | | |
| | 4 | 146 | | return namespaceName == "System.Threading.Tasks" && (namedType.Name == "Task" || namedType.Name == "ValueTask") |
| | 4 | 147 | | ? namedType.TypeArguments[0] |
| | 4 | 148 | | : namespaceName == "AsiBackbone.Core.Results" && namedType.Name == "OperationResult" |
| | 4 | 149 | | ? namedType.TypeArguments[0] |
| | 4 | 150 | | : type; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private static bool IsSuppressedByHostMarker(ISymbol? symbol) |
| | | 154 | | { |
| | 60 | 155 | | for (ISymbol? current = symbol; current is not null; current = current.ContainingSymbol) |
| | | 156 | | { |
| | 62 | 157 | | foreach (AttributeData attribute in current.GetAttributes()) |
| | | 158 | | { |
| | 2 | 159 | | string? attributeName = attribute.AttributeClass?.Name; |
| | 2 | 160 | | if (attributeName is "AsiBackbonePersistenceHandled" or "AsiBackbonePersistenceHandledAttribute") |
| | | 161 | | { |
| | 2 | 162 | | return true; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | 28 | 166 | | if (current is INamedTypeSymbol) |
| | | 167 | | { |
| | | 168 | | break; |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | |
| | 14 | 172 | | return false; |
| | | 173 | | } |
| | | 174 | | } |