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