예제 #1
0
        private bool CheckBlock(IBlockOperation blockOperation, IExpressionStatementOperation expressionOperation, IInvocationOperation invocation)
        {
            if (blockOperation.Parent is IUsingOperation || blockOperation.Parent is ICatchClauseOperation)
            {
                return(false);
            }

            if (blockOperation.Operations.Length == 1)
            {
                return(true);
            }

            int index = blockOperation.Operations.IndexOf(expressionOperation);

            if (index != blockOperation.Operations.Length - 1)
            {
                return(false);
            }

            // the assert.fail is the last operation.  Check if they are ending the loop with an if(blah) continue; fail;

            IOperation previous = blockOperation.Operations[index - 1];

            if (previous is IConditionalOperation conditional && conditional.WhenFalse is null)
            {
            private bool ValidateExpression(IExpressionStatementOperation expressionStatement)
            {
                if (expressionStatement.Operation == null || expressionStatement.Operation.Kind != OperationKind.Invocation)
                {
                    return(false);
                }

                var invocationExpression = (IInvocationOperation)expressionStatement.Operation;

                if (!_callsDisposeBool)
                {
                    bool result = IsDisposeBoolCall(invocationExpression, _type, expectedValue: true);
                    if (result)
                    {
                        _callsDisposeBool = true;
                    }

                    return(result);
                }
                else if (!_callsSuppressFinalize)
                {
                    bool result = IsSuppressFinalizeCall(invocationExpression);
                    if (result)
                    {
                        _callsSuppressFinalize = true;
                    }

                    return(result);
                }

                return(false);
            }
            private bool ValidateExpression(IExpressionStatementOperation expressionStatement)
            {
                if (expressionStatement.Operation?.Kind != OperationKind.Invocation)
                {
                    return(false);
                }

                var invocation = (IInvocationOperation)expressionStatement.Operation;

                // Valid calls are either to Dispose(false), or to the Finalize method of the base type
                if (!_callDispose)
                {
                    bool result = IsDisposeBoolCall(invocation, _type, expectedValue: false);
                    if (result)
                    {
                        _callDispose = true;
                    }

                    return(result);
                }
                else if (_type.BaseType != null && invocation.Instance != null && invocation.Instance.Kind == OperationKind.InstanceReference)
                {
                    IMethodSymbol methodSymbol           = invocation.TargetMethod;
                    IInstanceReferenceOperation receiver = (IInstanceReferenceOperation)invocation.Instance;

                    return(methodSymbol.IsFinalizer() && Equals(receiver.Type.OriginalDefinition, _type.BaseType.OriginalDefinition));
                }

                return(false);
            }
        private bool TryFindAssignmentExpression(
            IBlockOperation containingBlock,
            IConditionalOperation ifOperation,
            ISymbol localOrParameter,
            out IExpressionStatementOperation expressionStatement,
            out IAssignmentOperation assignmentExpression
            )
        {
            var ifOperationIndex = containingBlock.Operations.IndexOf(ifOperation);

            // walk forward until we find an assignment of this local/parameter into
            // something else.
            for (var i = ifOperationIndex + 1; i < containingBlock.Operations.Length; i++)
            {
                expressionStatement =
                    containingBlock.Operations[i] as IExpressionStatementOperation;
                if (expressionStatement == null)
                {
                    continue;
                }

                assignmentExpression = expressionStatement.Operation as IAssignmentOperation;
                if (assignmentExpression == null)
                {
                    continue;
                }

                if (
                    !TryGetLocalOrParameterSymbol(
                        assignmentExpression.Value,
                        out var assignmentValue
                        )
                    )
                {
                    continue;
                }

                if (!Equals(localOrParameter, assignmentValue))
                {
                    continue;
                }

                return(true);
            }

            expressionStatement  = null;
            assignmentExpression = null;
            return(false);
        }
        private static bool ValueIsAccessed(
            SemanticModel semanticModel,
            IConditionalOperation ifOperation,
            IBlockOperation containingBlock,
            ISymbol localOrParameter,
            IExpressionStatementOperation expressionStatement,
            IAssignmentOperation assignmentExpression
            )
        {
            var statements               = containingBlock.Operations;
            var ifOperationIndex         = statements.IndexOf(ifOperation);
            var expressionStatementIndex = statements.IndexOf(expressionStatement);

            if (expressionStatementIndex > ifOperationIndex + 1)
            {
                // There are intermediary statements between the check and the assignment.
                // Make sure they don't try to access the local.
                var dataFlow = semanticModel.AnalyzeDataFlow(
                    statements[ifOperationIndex + 1].Syntax,
                    statements[expressionStatementIndex - 1].Syntax
                    );

                if (
                    dataFlow.ReadInside.Contains(localOrParameter) ||
                    dataFlow.WrittenInside.Contains(localOrParameter)
                    )
                {
                    return(true);
                }
            }

            // Also, have to make sure there is no read/write of the local/parameter on the left
            // of the assignment.  For example: map[val.Id] = val;
            var exprDataFlow = semanticModel.AnalyzeDataFlow(assignmentExpression.Target.Syntax);

            return(exprDataFlow.ReadInside.Contains(localOrParameter) ||
                   exprDataFlow.WrittenInside.Contains(localOrParameter));
        }
예제 #6
0
        private void MethodSummary(ImmutableArray <BasicBlock> bb, List <string> sharedFields)
        {
            foreach (BasicBlock b in bb)
            {
                var a = b.GetType();
                var c = b.BranchValue;

                if (b.Operations != null)
                {
                    foreach (IOperation inst in b.Operations)
                    {
                        //Console.WriteLine($"inst.Syntax.ToSring() '{inst.Syntax.ToString()}'");

                        if (inst.Kind.ToString() == "ExpressionStatement")
                        {
                            IExpressionStatementOperation exprInst = (IExpressionStatementOperation)inst;

                            if (exprInst.Operation != null)
                            {
                                //if (exprInst.Operation.Kind.ToString() == Kind = SimpleAssignment)
                                //if (exprInst.Operation.Kind.ToString() == Kind = SimpleAssignment)
                                if (exprInst.Operation.Kind.ToString() == "SimpleAssignment")
                                {
                                    ISimpleAssignmentOperation assignInst = (ISimpleAssignmentOperation)exprInst.Operation;
                                    ProcessSimpleAssignmentOper(assignInst, sharedFields, inst);
                                }
                                else if (exprInst.Operation.Kind.ToString() == "Await")
                                {
                                    IAwaitOperation awaitInst = (IAwaitOperation)exprInst.Operation;

                                    if (awaitInst.Operation.Kind.ToString() == "Invocation")
                                    {
                                        IInvocationOperation invocInst = (IInvocationOperation)awaitInst.Operation;
                                        string leftOperand             = "";
                                        ProcessInvocationOper(invocInst, inst, leftOperand);
                                        isAsync = true;
                                        //conflictUnit.Add(new Tuple<Accesses, string, IOperation>(Accesses.Invocation, "", inst));
                                    }
                                    else if (awaitInst.Operation.Kind.ToString() == "Invalid")
                                    {
                                        ExpressionSyntax localvar = (ExpressionSyntax)awaitInst.Operation.Syntax;
                                        //var v = localvar.Kind().ToString();
                                        if (localvar.Kind().ToString() == "InvocationExpression")
                                        {
                                            InvocationExpressionSyntax invocInst = (InvocationExpressionSyntax)localvar;
                                            string leftOperand = "";
                                            ProcessInvocationExpr(invocInst, inst, leftOperand);
                                            isAsync = true;
                                        }
                                    }
                                    else if (awaitInst.Operation.Kind.ToString() == "IdentifierName")
                                    {
                                        //IdentifierNameSyntax indentInst = (IdentifierNameSyntax)awaitInst.Operation;
                                        isAsync = true;
                                        //rightOperand = indentInst.Identifier.ValueText;
                                        //awaitInvocRel.Add(new Tuple<string, string>(rightOperand, leftOperand));
                                    }
                                    conflictUnit.Add(new Tuple <Accesses, string, IOperation>(Accesses.Await, invocSuffix, inst));
                                }
                                else if (exprInst.Operation.Kind.ToString() == "Invocation")
                                {
                                    IInvocationOperation invocInst = (IInvocationOperation)exprInst.Operation;
                                    string leftOperand             = "";
                                    ProcessInvocationOper(invocInst, inst, leftOperand);
                                    //conflictUnit.Add(new Tuple<Accesses, string, IOperation>(Accesses.Invocation, "", inst));

                                    // invocSyntax = Regex.Replace(invocSyntax, @"(\w.*)(\.Wait\(\))$", "$1");
                                    //invocSyntax = Regex.Replace(invocSyntax, @"(\w.*)(\.)(\w.*)$", "$3");
                                }
                            }
                        }
                        else if (inst.Kind.ToString() == "SimpleAssignment")
                        {
                            ISimpleAssignmentOperation exprInst = (ISimpleAssignmentOperation)inst;
                            ProcessSimpleAssignmentOper(exprInst, sharedFields, inst);
                        }
                    }
                }
            }
        }
 protected override bool IsExpressionOfExpressionBody(IExpressionStatementOperation expressionStatementOperation)
 => expressionStatementOperation.Parent is IBlockOperation blockOperation &&
 public override void VisitExpressionStatement(IExpressionStatementOperation operation)
 {
     Visit(operation.Operation);
 }
 protected abstract bool IsExpressionOfExpressionBody(IExpressionStatementOperation expressionStatement);
 // C# does not have an explicit "call" statement syntax for invocations with explicit value discard.
 protected override bool IsCallStatement(IExpressionStatementOperation expressionStatement)
 => false;
예제 #11
0
 public override IOperation VisitExpressionStatement(IExpressionStatementOperation operation, object argument)
 {
     return(new ExpressionStatement(Visit(operation.Operation), ((Operation)operation).SemanticModel, operation.Syntax, operation.Type, operation.ConstantValue, operation.IsImplicit));
 }
 protected abstract bool IsCallStatement(IExpressionStatementOperation expressionStatement);
예제 #13
0
 public override string VisitExpressionStatement(IExpressionStatementOperation operation, None _) =>
 operation.Operation.Accept(this);
예제 #14
0
 public override void VisitExpressionStatement(IExpressionStatementOperation operation)
 {
     Assert.Equal(OperationKind.ExpressionStatement, operation.Kind);
     Assert.Same(operation.Operation, operation.Children.Single());
 }
예제 #15
0
 public override bool VisitExpressionStatement([NotNull] IExpressionStatementOperation operation1,
                                               [CanBeNull] IOperation argument)
 {
     return(argument is IExpressionStatementOperation operation2 && AreBaseOperationsEqual(operation1, operation2));
 }
예제 #16
0
 public virtual void VisitExpressionStatement(IExpressionStatementOperation operation)
 {
     DefaultVisit(operation);
 }
예제 #17
0
 public override void VisitExpressionStatement([NotNull] IExpressionStatementOperation operation)
 {
     base.VisitExpressionStatement(operation);
 }
예제 #18
0
 private static AbstractStatement ReadExpressionStatement(IExpressionStatementOperation op)
 {
     return(new ExpressionStatement {
         Expression = ReadExpression(op.Operation)
     });
 }
예제 #19
0
 public override Scope VisitExpressionStatement(IExpressionStatementOperation operation, Scope currentScope) =>
 operation.Operation.Accept(this, currentScope);