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