Beispiel #1
0
        /// <summary>
        /// Analyze Assignment Expression Nodes
        /// </summary>
        /// <param name="context"></param>
        private static void AnalyzeSimpleAssignmentExpression(SyntaxNodeAnalysisContext context)
        {
            var simpleAssExpr = context.Node;

            if (simpleAssExpr.Kind().Equals(SyntaxKind.SimpleAssignmentExpression))
            {
                var simpleAssignExpr = (AssignmentExpressionSyntax)simpleAssExpr;

                //Check Only For MemberAcessExpressionSyntax Nodes
                if (simpleAssignExpr.ChildNodes().OfType <MemberAccessExpressionSyntax>().FirstOrDefault() != null)
                {
                    //Get the invocator
                    var invocator = simpleAssignExpr.ChildNodes().OfType <MemberAccessExpressionSyntax>().FirstOrDefault().ChildNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();

                    var invocatorSymbolInfo = context.SemanticModel.GetSymbolInfo(invocator).Symbol;
                    if (invocatorSymbolInfo.Kind.Equals(SymbolKind.Local))
                    {
                        var localInvocatorSymbolInfo = (ILocalSymbol)invocatorSymbolInfo;
                        //Analze only if type is of SPEC type
                        if (localInvocatorSymbolInfo.Type.ToString().Equals(_cryslSpecificationModel.Spec_Section.Class_Name))
                        {
                            var  leftExprSymbolInfo  = context.SemanticModel.GetSymbolInfo(simpleAssignExpr.Left).Symbol;
                            var  rightExprSymbolInfo = context.SemanticModel.GetSymbolInfo(simpleAssignExpr.Right).Symbol;
                            bool isValidEvent        = false;
                            foreach (var methods in _cryslSpecificationModel.Event_Section.Methods)
                            {
                                var cryptoMethods = methods.Crypto_Signature.Select(x => x).Where(y => y.Method_Name.ToString().Equals(leftExprSymbolInfo.Name.ToString()));
                                if (cryptoMethods.Count() > 0)
                                {
                                    //Set the flag to true as a valid event
                                    IEventSectionAnalyzer analyzer = serviceProvider.GetService <IEventSectionAnalyzer>();
                                    var validEvents = analyzer.AnalyzeAssignmentExprSyntax(cryptoMethods, _cryslSpecificationModel, context, leftExprSymbolInfo);
                                    isValidEvent = validEvents.IsValidEvent;

                                    //Check for Valid Order
                                    IOrderSectionAnalyzer orderSectionAnalyzer = serviceProvider.GetService <IOrderSectionAnalyzer>();
                                    bool isOrderValid = orderSectionAnalyzer.IsValidOrder(validEvents, context.ContainingSymbol.ToString(), EventsOrderDict, EventOrderContraint);
                                    if (!isOrderValid)
                                    {
                                        //Report Diagnsotics as Violation of Order Constraint
                                    }

                                    string rightExprValue = string.Empty;
                                    if (simpleAssignExpr.Right.Kind().Equals(SyntaxKind.NumericLiteralExpression))
                                    {
                                        var literalExpressionSyntax = (LiteralExpressionSyntax)simpleAssignExpr.Right;
                                        rightExprValue = literalExpressionSyntax.Token.Value.ToString();
                                    }
                                    else if (rightExprSymbolInfo != null)
                                    {
                                        rightExprValue = rightExprSymbolInfo.Name.ToString();
                                    }
                                    //Check if primary constraints are satified if any
                                    IConstraintsSectionAnalyzer constraintsSectionAnalyzer = serviceProvider.GetService <IConstraintsSectionAnalyzer>();
                                    bool isPrimaryConstraintSatisfied = constraintsSectionAnalyzer.IsPropertyConstraintSatisfied(_cryslSpecificationModel, validEvents, rightExprValue);
                                    if (!isPrimaryConstraintSatisfied)
                                    {
                                        //Report Violation of Primary Constraints
                                    }

                                    //Check if additional constraints are satisfied if any
                                    AddConstraints additionalConstraints = new AddConstraints();
                                    _additionalConstraintsDict.TryGetValue(_cryslSpecificationModel.Spec_Section.Class_Name, out additionalConstraints);

                                    if (additionalConstraints.EventVariableDeclarator.ToString().Equals(localInvocatorSymbolInfo.Name.ToString()))
                                    {
                                        constraintsSectionAnalyzer = serviceProvider.GetService <IConstraintsSectionAnalyzer>();
                                        bool isAdditionalConstraintSatisfied = constraintsSectionAnalyzer.IsAdditionalConstraintSatisfied(additionalConstraints, leftExprSymbolInfo, rightExprValue, _cryslSpecificationModel.Object_Section.Objects_Declaration, validEvents);
                                        if (!isAdditionalConstraintSatisfied)
                                        {
                                            //Report Violation of Additional Constraints
                                        }
                                    }
                                }
                            }
                            if (!isValidEvent)
                            {
                                //Report Diagnostics as not a Valid Event
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Analyze Method Invocation Nodes
        /// </summary>
        /// <param name="context"></param>
        private static void AnalyzeMethodInvocationNode(SyntaxNodeAnalysisContext context)
        {
            var diagnostics = context.Compilation.GetDiagnostics();
            var invocationExpressionNode = context.Node;
            var memAcessExprNode         = invocationExpressionNode.ChildNodes().OfType <MemberAccessExpressionSyntax>();
            var argumentsList            = invocationExpressionNode.ChildNodes().OfType <ArgumentListSyntax>();

            foreach (var node in memAcessExprNode)
            {
                var identifierNode = node.ChildNodes().OfType <IdentifierNameSyntax>();
                foreach (var identifier in identifierNode)
                {
                    var result = _cryslSpecificationModel.Event_Section.Methods.Select(x => x.Crypto_Signature
                                                                                       .Where(y => y.Method_Name.ToString().Equals(identifier.Identifier.Value.ToString())));
                    foreach (var methods in _cryslSpecificationModel.Event_Section.Methods)
                    {
                        // Check if method signature matches with the method signature defined in events section of the Crysl.
                        var cryptoMethods = methods.Crypto_Signature.Select(y => y).Where(x => x.Method_Name.ToString().Equals(identifier.Identifier.Value.ToString()));
                        if (cryptoMethods.Count() > 0)
                        {
                            IEventSectionAnalyzer analyzer = serviceProvider.GetService <IEventSectionAnalyzer>();
                            var identifierSymbolInfo       = (IMethodSymbol)context.SemanticModel.GetSymbolInfo(identifier).Symbol;
                            //Check for Valid Events
                            ValidEvents validEvents = analyzer.AnalyzeMemAccessExprSyntax(identifier, cryptoMethods, methods, _cryslSpecificationModel, context, identifierSymbolInfo, node.Span);

                            //Check for Valid Order
                            IOrderSectionAnalyzer orderSectionAnalyzer = serviceProvider.GetService <IOrderSectionAnalyzer>();
                            bool isOrderValid = orderSectionAnalyzer.IsValidOrder(validEvents, context.ContainingSymbol.ToString(), EventsOrderDict, EventOrderContraint);

                            //Report If Order Constraint is Violated
                            if (!isOrderValid)
                            {
                                //Report Diagnostics Order Violation
                            }

                            //If Event is Valid then Check Constraints
                            if (validEvents.IsValidEvent)
                            {
                                //Check for Valid Constraints
                                foreach (var parameter in validEvents.ValidEventsDict)
                                {
                                    IConstraintsSectionAnalyzer constraintsSectionAnalyzer = serviceProvider.GetService <IConstraintsSectionAnalyzer>();
                                    List <ConstraintsModel>     satisfiedConstraintsList   = new List <ConstraintsModel>();
                                    foreach (var parameterValue in parameter.Value)
                                    {
                                        //Check for constraints only if arguments are present
                                        if (parameterValue.Parameters.Count != 0)
                                        {
                                            satisfiedConstraintsList = constraintsSectionAnalyzer.AnalyzeParameters(argumentsList, parameterValue.Parameters, _cryslSpecificationModel.Constraints_Section.Constraints);
                                            ReportConstraintsSection(context, satisfiedConstraintsList);
                                            AddConstraints additionalConstraints = new AddConstraints
                                            {
                                                EventKey = parameter.Key,
                                                EventVariableDeclarator = identifier.AncestorsAndSelf().OfType <VariableDeclaratorSyntax>().First().Identifier.Text,
                                                ConstraintsModels       = satisfiedConstraintsList
                                            };
                                            _additionalConstraintsDict.Add(identifierSymbolInfo.ReturnType.ToString(), additionalConstraints);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //Report context to diagnostics as invalid event
                            }
                        }
                    }
                }
            }
        }