|
| 1 | +using System.Linq; |
| 2 | +using System.Threading; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 | + |
| 7 | +namespace ManagedCode.Communication.Analyzers; |
| 8 | + |
| 9 | +internal static class ResultStatusFlow |
| 10 | +{ |
| 11 | + private const string AnnotationType = "System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute"; |
| 12 | + |
| 13 | + internal static bool ProvesMember(SyntaxNode node, MemberAccessExpressionSyntax member, SemanticModel model, CancellationToken token) |
| 14 | + { |
| 15 | + var receiver = model.GetSymbolInfo(member.Expression, token).Symbol; |
| 16 | + if (receiver is not (ILocalSymbol or IParameterSymbol)) |
| 17 | + { |
| 18 | + return false; |
| 19 | + } |
| 20 | + for (var current = node; current.Parent is not null; current = current.Parent) |
| 21 | + { |
| 22 | + if (current is AnonymousFunctionExpressionSyntax or LocalFunctionStatementSyntax || Writes(current, receiver, model, token)) |
| 23 | + { |
| 24 | + return false; |
| 25 | + } |
| 26 | + if (current.Parent is BinaryExpressionSyntax binary && current == binary.Right && |
| 27 | + (binary.IsKind(SyntaxKind.LogicalAndExpression) && Guarantees(binary.Left, true, member, receiver, model, token) || |
| 28 | + binary.IsKind(SyntaxKind.LogicalOrExpression) && Guarantees(binary.Left, false, member, receiver, model, token))) |
| 29 | + { |
| 30 | + return true; |
| 31 | + } |
| 32 | + if (current.Parent is ConditionalExpressionSyntax conditional && |
| 33 | + (current == conditional.WhenTrue && Guarantees(conditional.Condition, true, member, receiver, model, token) || |
| 34 | + current == conditional.WhenFalse && Guarantees(conditional.Condition, false, member, receiver, model, token))) |
| 35 | + { |
| 36 | + return true; |
| 37 | + } |
| 38 | + if (current.Parent is IfStatementSyntax parentIf && current == parentIf.Statement && |
| 39 | + Guarantees(parentIf.Condition, true, member, receiver, model, token) || |
| 40 | + current.Parent is ElseClauseSyntax { Parent: IfStatementSyntax elseIf } && |
| 41 | + Guarantees(elseIf.Condition, false, member, receiver, model, token)) |
| 42 | + { |
| 43 | + return true; |
| 44 | + } |
| 45 | + if (current is StatementSyntax statement && statement.Parent is BlockSyntax block) |
| 46 | + { |
| 47 | + for (int index = block.Statements.IndexOf(statement) - 1; index >= 0; index--) |
| 48 | + { |
| 49 | + var preceding = block.Statements[index]; |
| 50 | + if (Writes(preceding, receiver, model, token)) |
| 51 | + { |
| 52 | + return false; |
| 53 | + } |
| 54 | + if (preceding is IfStatementSyntax { Else: null } guard && |
| 55 | + model.AnalyzeControlFlow(guard.Statement) is { Succeeded: true, EndPointIsReachable: false } && |
| 56 | + Guarantees(guard.Condition, false, member, receiver, model, token)) |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + private static bool Writes(SyntaxNode node, ISymbol receiver, SemanticModel model, CancellationToken token) |
| 67 | + { |
| 68 | + return node.DescendantNodesAndSelf().Any(part => part switch |
| 69 | + { |
| 70 | + AssignmentExpressionSyntax assignment => References(assignment.Left, receiver, model, token), |
| 71 | + ArgumentSyntax argument when argument.RefKindKeyword.IsKind(SyntaxKind.RefKeyword) || argument.RefKindKeyword.IsKind(SyntaxKind.OutKeyword) => |
| 72 | + References(argument.Expression, receiver, model, token), |
| 73 | + RefExpressionSyntax reference => References(reference.Expression, receiver, model, token), |
| 74 | + PrefixUnaryExpressionSyntax prefix when prefix.IsKind(SyntaxKind.PreIncrementExpression) || prefix.IsKind(SyntaxKind.PreDecrementExpression) => |
| 75 | + References(prefix.Operand, receiver, model, token), |
| 76 | + PostfixUnaryExpressionSyntax postfix when postfix.IsKind(SyntaxKind.PostIncrementExpression) || postfix.IsKind(SyntaxKind.PostDecrementExpression) => |
| 77 | + References(postfix.Operand, receiver, model, token), |
| 78 | + VariableDeclaratorSyntax variable => SymbolEqualityComparer.Default.Equals(model.GetDeclaredSymbol(variable, token), receiver), |
| 79 | + _ => false |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private static bool References(ExpressionSyntax expression, ISymbol receiver, SemanticModel model, CancellationToken token) => |
| 84 | + expression.DescendantNodesAndSelf().OfType<ExpressionSyntax>() |
| 85 | + .Any(part => SymbolEqualityComparer.Default.Equals(model.GetSymbolInfo(part, token).Symbol, receiver)); |
| 86 | + |
| 87 | + private static bool Guarantees(ExpressionSyntax condition, bool outcome, MemberAccessExpressionSyntax member, |
| 88 | + ISymbol receiver, SemanticModel model, CancellationToken token) |
| 89 | + { |
| 90 | + if (Writes(condition, receiver, model, token)) |
| 91 | + { |
| 92 | + return false; |
| 93 | + } |
| 94 | + if (condition is ParenthesizedExpressionSyntax parentheses) |
| 95 | + { |
| 96 | + return Guarantees(parentheses.Expression, outcome, member, receiver, model, token); |
| 97 | + } |
| 98 | + if (condition is PrefixUnaryExpressionSyntax negation && negation.IsKind(SyntaxKind.LogicalNotExpression)) |
| 99 | + { |
| 100 | + return Guarantees(negation.Operand, !outcome, member, receiver, model, token); |
| 101 | + } |
| 102 | + if (condition is BinaryExpressionSyntax binary && |
| 103 | + (outcome && binary.IsKind(SyntaxKind.LogicalAndExpression) || !outcome && binary.IsKind(SyntaxKind.LogicalOrExpression))) |
| 104 | + { |
| 105 | + return Guarantees(binary.Left, outcome, member, receiver, model, token) || |
| 106 | + Guarantees(binary.Right, outcome, member, receiver, model, token); |
| 107 | + } |
| 108 | + if (condition is not MemberAccessExpressionSyntax status || !ResultUsageAnalyzer.IsResultProperty(status, model, token) || |
| 109 | + !SymbolEqualityComparer.Default.Equals(model.GetSymbolInfo(status.Expression, token).Symbol, receiver) || |
| 110 | + model.GetSymbolInfo(status, token).Symbol is not IPropertySymbol property) |
| 111 | + { |
| 112 | + return false; |
| 113 | + } |
| 114 | + return property.GetAttributes().Any(attribute => attribute.AttributeClass?.ToDisplayString() == AnnotationType && |
| 115 | + attribute.ConstructorArguments.Length == 2 && attribute.ConstructorArguments[0].Value is bool expected && expected == outcome && |
| 116 | + (attribute.ConstructorArguments[1].Value is string name && name == member.Name.Identifier.ValueText || |
| 117 | + attribute.ConstructorArguments[1].Kind == TypedConstantKind.Array && |
| 118 | + attribute.ConstructorArguments[1].Values.Any(value => value.Value is string item && item == member.Name.Identifier.ValueText))); |
| 119 | + } |
| 120 | +} |
0 commit comments