Exemplo n.º 1
0
        protected override void Run(
            IInvocationExpression invocationExpression,
            ElementProblemAnalyzerData data,
            IHighlightingConsumer consumer)
        {
            var arguments = invocationExpression.Arguments;

            // Short circuit exit:
            if (arguments.Count < 1)
            {
                return;
            }

            var resolveResult = invocationExpression.InvocationExpressionReference.Resolve();

            if (resolveResult.DeclaredElement is IMethod method)
            {
                // Following 'FDTApplicator..ctor' and 'CSharpControlFlowGraphInspector.PatchContextByObsoleteAnnotatedMethodCall'

                var contractAnnotationFdt = _contractAnnotationProvider.GetInfo(method);

                if (contractAnnotationFdt != null || _assertionMethodAnnotationProvider.GetInfo(method))
                {
                    if (resolveResult.Result.IsExtensionMethodInvocation())
                    {
                        var thisArgumentInfo = (ExtensionArgumentInfo)invocationExpression
                                               .ExtensionQualifier.NotNull("seems to be escaped by the positive resolution");

                        var conditionType = GetConditionTypeForParameter(thisArgumentInfo, contractAnnotationFdt);

                        if (conditionType != null)
                        {
                            var conditionExpression = thisArgumentInfo.Expression;
                            Assertion.Assert(arguments.Count > 0, "arguments.Count > 0");
                            var nextToConditionArgument = arguments[0];

                            CheckMessageArgument(conditionExpression, conditionType.Value, nextToConditionArgument, consumer);
                        }
                    }
                    else
                    {
                        for (var i = 0; i < arguments.Count - 1; i++)
                        {
                            var argument     = arguments[i];
                            var nextArgument = arguments[i + 1];

                            var conditionType = GetConditionTypeForParameter(argument, contractAnnotationFdt);

                            if (conditionType != null)
                            {
                                var conditionExpression = argument.Value;
                                CheckMessageArgument(conditionExpression, conditionType.Value, nextArgument, consumer);
                            }
                        }
                    }
                }
            }
        }
        public static AssertionStatement TryFromInvocationExpression(
            [NotNull] IInvocationExpression invocationExpression,
            [NotNull] AssertionMethodAnnotationProvider assertionMethodAnnotationProvider,
            [NotNull] AssertionConditionAnnotationProvider assertionConditionAnnotationProvider)
        {
            if (invocationExpression.Reference.Resolve().DeclaredElement is IMethod method && assertionMethodAnnotationProvider.GetInfo(method))
            {
                foreach (var parameter in method.Parameters)
                {
                    Debug.Assert(parameter != null);

                    var parameterAssertionCondition = assertionConditionAnnotationProvider.GetInfo(parameter);

                    if (parameterAssertionCondition == null && parameter.Type.IsBool())
                    {
                        parameterAssertionCondition = AssertionConditionType.IS_TRUE;
                    }

                    if (parameterAssertionCondition != null)
                    {
                        var argument = invocationExpression.ArgumentList?.Arguments.FirstOrDefault(
                            a =>
                        {
                            Debug.Assert(a != null);
                            return(a.MatchingParameter != null && a.MatchingParameter.Element.ShortName == parameter.ShortName);
                        });
                        if (argument?.Value != null)
                        {
                            return(new AssertionStatement(invocationExpression, (AssertionConditionType)parameterAssertionCondition, argument.Value));
                        }
                    }
                }
            }

            return(null);
        }