public VariablesWalker(MethodDeclarationSyntax methodSyntax, SemanticModel semanticModel, PXContext pxContext, CancellationToken cancellationToken) { methodSyntax.ThrowOnNull(nameof(methodSyntax)); semanticModel.ThrowOnNull(nameof(semanticModel)); pxContext.ThrowOnNull(nameof(pxContext)); _semanticModel = semanticModel; _pxContext = pxContext; _cancellationToken = cancellationToken; _eventArgsRowWalker = new EventArgsRowWalker(semanticModel, pxContext); if (methodSyntax.Body != null || methodSyntax.ExpressionBody?.Expression != null) { var dataFlow = methodSyntax.Body != null ? semanticModel.AnalyzeDataFlow(methodSyntax.Body) : semanticModel.AnalyzeDataFlow(methodSyntax.ExpressionBody.Expression); if (dataFlow.Succeeded) { _variables = dataFlow.WrittenInside .Intersect(dataFlow.VariablesDeclared) .OfType <ILocalSymbol>() .ToImmutableHashSet(); } } }
public override void VisitInvocationExpression(InvocationExpressionSyntax node) { _context.CancellationToken.ThrowIfCancellationRequested(); var methodSymbol = _semanticModel.GetSymbolInfo(node).Symbol as IMethodSymbol; if (methodSymbol != null && IsMethodForbidden(methodSymbol)) { bool found = node.ArgumentList.Arguments .Where(arg => arg.Expression != null) .Select(arg => _semanticModel.GetSymbolInfo(arg.Expression).Symbol as ILocalSymbol) .Any(variable => variable != null && _rowVariables.Contains(variable)); if (!found) { var walker = new EventArgsRowWalker(_semanticModel, _pxContext); node.ArgumentList.Accept(walker); found = walker.Success; } if (found && _analysisMode == RowChangesAnalysisMode.ChangesForbiddenForRowFromEventArgs) { _context.ReportDiagnostic(Diagnostic.Create(Descriptors.PX1047_RowChangesInEventHandlersForbiddenForArgs, node.GetLocation(), _messageArgs)); } else if (!found && _analysisMode == RowChangesAnalysisMode.ChangesAllowedOnlyForRowFromEventArgs) { _context.ReportDiagnostic(Diagnostic.Create(Descriptors.PX1048_RowChangesInEventHandlersAllowedForArgsOnly, node.GetLocation(), _messageArgs)); } } }
public override void VisitAssignmentExpression(AssignmentExpressionSyntax node) { if (node.Left != null) { var walker = new EventArgsRowWalker(_semanticModel, _pxContext); node.Left.Accept(walker); bool found = walker.Success; if (!found) { _variableMemberAccessWalker.Reset(); node.Left.Accept(_variableMemberAccessWalker); found = _variableMemberAccessWalker.Success; } if (found && _analysisMode == RowChangesAnalysisMode.ChangesForbiddenForRowFromEventArgs) { _context.ReportDiagnostic(Diagnostic.Create(Descriptors.PX1047_RowChangesInEventHandlersForbiddenForArgs, node.GetLocation(), _messageArgs)); } else if (!found && _analysisMode == RowChangesAnalysisMode.ChangesAllowedOnlyForRowFromEventArgs) { _dacInstanceAccessWalker.Reset(); node.Left.Accept(_dacInstanceAccessWalker); if (_dacInstanceAccessWalker.Success) { _context.ReportDiagnostic(Diagnostic.Create(Descriptors.PX1048_RowChangesInEventHandlersAllowedForArgsOnly, node.GetLocation(), _messageArgs)); } } } }
public override void VisitAssignmentExpression(AssignmentExpressionSyntax node) { if (node.Left != null) { var walker = new EventArgsRowWalker(_semanticModel, _pxContext); node.Left.Accept(walker); bool found = walker.Success; if (!found) { _variableMemberAccessWalker.Reset(); node.Left.Accept(_variableMemberAccessWalker); found = _variableMemberAccessWalker.Success; } if (found) { _context.ReportDiagnostic(Diagnostic.Create(Descriptors.PX1047_RowChangesInEventHandlers, node.GetLocation(), _messageArgs)); } } }