示例#1
0
        private SyntaxList <MemberDeclarationSyntax> BuildMembers(SyntaxList <MemberDeclarationSyntax> members, List <SyntaxNode> tagged)
        {
            SyntaxList <MemberDeclarationSyntax> newMembers = new SyntaxList <MemberDeclarationSyntax>();

            if (members != null && members.Count() > 0)
            {
                foreach (MemberDeclarationSyntax member in members)
                {
                    MemberDeclarationSyntax newSubMember = this.BuildNode(member, tagged);
                    if (newSubMember != null)
                    {
                        newMembers = newMembers.Add(newSubMember);
                    }
                }
            }

            return(newMembers);
        }
示例#2
0
        private SyntaxTree renameProgramClass(SyntaxTree tree)
        {
            ClassDeclarationSyntax mainClass = this.getTopLevelClasses(tree).First();
            SyntaxTree             newTree   = null;

            if (mainClass != null)
            {
                SyntaxToken newIdentifier = SyntaxFactory.Identifier(SESCriptBuilderInstance.ProgramName);
                SyntaxList <MemberDeclarationSyntax> members = mainClass.Members;
                if (members != null && members.Count() > 0)
                {
                    SyntaxList <MemberDeclarationSyntax> newMembers = new SyntaxList <MemberDeclarationSyntax>();
                    foreach (MemberDeclarationSyntax member in members)
                    {
                        MemberDeclarationSyntax newMember = member;
                        if (member.GetType() == typeof(ConstructorDeclarationSyntax))
                        {
                            ConstructorDeclarationSyntax cons = member as ConstructorDeclarationSyntax;
                            SyntaxToken identifier            = cons.Identifier;
                            if (identifier != null)
                            {
                                newMember = SyntaxFactory.ConstructorDeclaration(cons.AttributeLists, cons.Modifiers, newIdentifier, cons.ParameterList, cons.Initializer, cons.Body, cons.ExpressionBody, cons.SemicolonToken);
                            }
                        }
                        newMembers = newMembers.Add(newMember);
                    }

                    members = newMembers;
                }

                ClassDeclarationSyntax newClass = mainClass.WithIdentifier(newIdentifier).WithMembers(members);

                if (newClass != null)
                {
                    IEnumerable <NamespaceDeclarationSyntax> namespaces = tree.GetRootAsync().Result.DescendantNodes().OfType <NamespaceDeclarationSyntax>();
                    CompilationUnitSyntax newComp = null;
                    CompilationUnitSyntax oldComp = tree.GetCompilationUnitRoot();
                    SyntaxList <MemberDeclarationSyntax> newClassMembers = new SyntaxList <MemberDeclarationSyntax>();
                    newClassMembers = newClassMembers.Add(newClass);
                    if (namespaces != null && namespaces.Count() > 0)
                    {
                        NamespaceDeclarationSyntax ns = namespaces.First();
                        if (ns != null)
                        {
                            NamespaceDeclarationSyntax           newNs        = ns.WithMembers(newClassMembers);
                            SyntaxList <MemberDeclarationSyntax> newNsMembers = new SyntaxList <MemberDeclarationSyntax>();
                            newNsMembers = newNsMembers.Add(newNs);
                            newComp      = oldComp.WithMembers(newNsMembers);
                        }
                    }
                    else
                    {
                        newComp = oldComp.WithMembers(newClassMembers);
                    }

                    if (newComp != null)
                    {
                        newTree = newComp.SyntaxTree;
                    }
                }
            }
            return(newTree);
        }
示例#3
0
        Tuple <ExpressionSyntax, List <VariableDeclarationSyntax>, List <ExpressionSyntax> > CollectCondition(ExpressionSyntax expressionSyntax, SyntaxList <SwitchLabelSyntax> labels, TypeSyntax keyType)
        {
            var conditionList = new List <ExpressionSyntax>();
            var variables     = new List <VariableDeclarationSyntax>();
            var whens         = new List <ExpressionSyntax>();

            if (labels.Count == 0 || labels.OfType <DefaultSwitchLabelSyntax>().Any())
            {
                return(new Tuple <ExpressionSyntax, List <VariableDeclarationSyntax>, List <ExpressionSyntax> >(null, variables, whens));
            }

            var patternsCount = labels.Count(l => l is CasePatternSwitchLabelSyntax);

            foreach (var item in labels)
            {
                try
                {
                    if (item is CaseSwitchLabelSyntax)
                    {
                        var label = (CaseSwitchLabelSyntax)item;

                        if (label.Value is CastExpressionSyntax ce && ce.Expression.Kind() == SyntaxKind.DefaultLiteralExpression)
                        {
                            conditionList.Add(SyntaxFactory.BinaryExpression(SyntaxKind.LogicalAndExpression,
                                                                             SyntaxFactory.BinaryExpression(SyntaxKind.IsExpression, expressionSyntax, ce.Type),
                                                                             //SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, expressionSyntax, SyntaxFactory.DefaultExpression(ce.Type))
                                                                             SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(
                                                                                                                    SyntaxKind.SimpleMemberAccessExpression,
                                                                                                                    SyntaxFactory.IdentifierName("System.Object"),
                                                                                                                    SyntaxFactory.IdentifierName("Equals")), SyntaxFactory.ArgumentList(
                                                                                                                    SyntaxFactory.SeparatedList <ArgumentSyntax>(
                                                                                                                        new SyntaxNodeOrToken[] {
                                SyntaxFactory.Argument(expressionSyntax),
                                SyntaxFactory.Token(SyntaxKind.CommaToken),
                                SyntaxFactory.Argument(
                                    SyntaxFactory.DefaultExpression(ce.Type)
                                    )
                            })))
                                                                             ));
                        }
                        else
                        {
                            conditionList.Add(SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, expressionSyntax, label.Value));
                        }
                    }
                    else if (item is CasePatternSwitchLabelSyntax)
                    {
                        var    label   = (CasePatternSwitchLabelSyntax)item;
                        string varName = null;
                        if (label.Pattern is DeclarationPatternSyntax)
                        {
                            var declarationPattern = (DeclarationPatternSyntax)label.Pattern;
                            var designation        = declarationPattern.Designation as SingleVariableDesignationSyntax;

                            if (designation != null)
                            {
                                var declarationType = declarationPattern.Type;

                                if (declarationType.IsVar)
                                {
                                    declarationType = keyType;
                                }

                                var varDecl = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var")).WithVariables(SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                                                                                                                                       SyntaxFactory.VariableDeclarator(
                                                                                                                                           SyntaxFactory.Identifier(designation.Identifier.ValueText)
                                                                                                                                           ).WithInitializer(SyntaxFactory.EqualsValueClause(patternsCount > 1 ? (ExpressionSyntax)SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, expressionSyntax, declarationType) : SyntaxFactory.CastExpression(declarationType, expressionSyntax)))
                                                                                                                                       )).WithTrailingTrivia(SyntaxFactory.Whitespace("\n")).NormalizeWhitespace();
                                varName = designation.Identifier.ValueText;
                                variables.Add(varDecl);

                                conditionList.Add(SyntaxFactory.BinaryExpression(SyntaxKind.IsExpression, expressionSyntax, declarationType));
                            }
                        }
                        else if (label.Pattern is ConstantPatternSyntax)
                        {
                            var constPattern = (ConstantPatternSyntax)label.Pattern;
                            conditionList.Add(SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, expressionSyntax, constPattern.Expression));
                        }

                        if (label.WhenClause != null)
                        {
                            var c = label.WhenClause.Condition;
                            if (patternsCount > 1 && NeedsParentheses(c))
                            {
                                c = SyntaxFactory.ParenthesizedExpression(c);
                            }

                            if (varName != null && patternsCount > 1)
                            {
                                whens.Add(SyntaxFactory.BinaryExpression(SyntaxKind.LogicalAndExpression, SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, SyntaxFactory.IdentifierName(varName), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), c));
                            }
                            else
                            {
                                whens.Add(c);
                            }
                        }
                    }
                }
示例#4
0
        private void VisitMethods(SyntaxNodeAnalysisContext ctx)
        {
            ClassDeclarationSyntax node = ctx.Node as ClassDeclarationSyntax;

            if (node == null)
            {
                return;
            }

            // Ensures that the analyzed class has a dependency to Controller
            if (node.DescendantNodesAndSelf()
                .OfType <BaseListSyntax>()
                .Where(childrenNode => childrenNode.ToString().Contains("Controller"))
                .Count()
                .Equals(0))
            {
                return;
            }

            IEnumerable <MethodDeclarationSyntax> methodsWithParameters = node.DescendantNodesAndSelf()
                                                                          .OfType <MethodDeclarationSyntax>()
                                                                          .Where(method => !method.ParameterList.Parameters.Count().Equals(0))
                                                                          .Where(method => method.Modifiers.ToString().Equals("public"))
                                                                          .Where(method => method.ReturnType.ToString().Equals("string"));

            foreach (MethodDeclarationSyntax method in methodsWithParameters)
            {
                SyntaxList <StatementSyntax>             methodStatements  = method.Body.Statements;
                IEnumerable <InvocationExpressionSyntax> methodInvocations = method.DescendantNodes().OfType <InvocationExpressionSyntax>();

                if (!methodStatements.Count().Equals(0))
                {
                    DataFlowAnalysis flow = ctx.SemanticModel.AnalyzeDataFlow(methodStatements.First(), methodStatements.Last());

                    // Returns from the Data Flow Analysis of sensible data
                    // Sensible data is: Data passed as a parameter that is also returned as is by the method
                    IEnumerable <ISymbol> sensibleVariables = flow.DataFlowsIn.Union(flow.VariablesDeclared.Except(flow.AlwaysAssigned))
                                                              .Union(flow.WrittenInside)
                                                              .Intersect(flow.WrittenOutside);

                    if (!sensibleVariables.Count().Equals(0))
                    {
                        foreach (ISymbol sensibleVariable in sensibleVariables)
                        {
                            bool sensibleVariableIsEncoded = false;
                            foreach (InvocationExpressionSyntax methodInvocation in methodInvocations)
                            {
                                SeparatedSyntaxList <ArgumentSyntax> arguments = methodInvocation.ArgumentList.Arguments;
                                if (!arguments.Count().Equals(0))
                                {
                                    if (arguments.First().ToString().Contains(sensibleVariable.Name.ToString()))
                                    {
                                        sensibleVariableIsEncoded = true;
                                    }
                                }
                            }

                            if (!sensibleVariableIsEncoded)
                            {
                                ctx.ReportDiagnostic(Diagnostic.Create(Rule, method.GetLocation()));
                            }
                        }
                    }
                }
            }
        }
示例#5
0
        Tuple <ExpressionSyntax, List <VariableDeclarationSyntax>, List <ExpressionSyntax> > CollectCondition(ExpressionSyntax expressionSyntax, SyntaxList <SwitchLabelSyntax> labels, TypeSyntax keyType)
        {
            var conditionList = new List <ExpressionSyntax>();
            var variables     = new List <VariableDeclarationSyntax>();
            var whens         = new List <ExpressionSyntax>();

            if (labels.Count == 0 || labels.OfType <DefaultSwitchLabelSyntax>().Any())
            {
                return(new Tuple <ExpressionSyntax, List <VariableDeclarationSyntax>, List <ExpressionSyntax> >(null, variables, whens));
            }

            var patternsCount = labels.Count(l => l is CasePatternSwitchLabelSyntax);

            foreach (var item in labels)
            {
                if (item is CaseSwitchLabelSyntax)
                {
                    var label = (CaseSwitchLabelSyntax)item;
                    conditionList.Add(SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, expressionSyntax, label.Value));
                }
                else if (item is CasePatternSwitchLabelSyntax)
                {
                    var    label   = (CasePatternSwitchLabelSyntax)item;
                    string varName = null;
                    if (label.Pattern is DeclarationPatternSyntax)
                    {
                        var declarationPattern = (DeclarationPatternSyntax)label.Pattern;
                        var designation        = declarationPattern.Designation as SingleVariableDesignationSyntax;

                        if (designation != null)
                        {
                            var declarationType = declarationPattern.Type;

                            if (declarationType.IsVar)
                            {
                                declarationType = keyType;
                            }

                            var varDecl = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var")).WithVariables(SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                                                                                                                                   SyntaxFactory.VariableDeclarator(
                                                                                                                                       SyntaxFactory.Identifier(designation.Identifier.ValueText)
                                                                                                                                       ).WithInitializer(SyntaxFactory.EqualsValueClause(patternsCount > 1 ? (ExpressionSyntax)SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, expressionSyntax, declarationType) : SyntaxFactory.CastExpression(declarationType, expressionSyntax)))
                                                                                                                                   )).WithTrailingTrivia(SyntaxFactory.Whitespace("\n")).NormalizeWhitespace();
                            varName = designation.Identifier.ValueText;
                            variables.Add(varDecl);

                            conditionList.Add(SyntaxFactory.BinaryExpression(SyntaxKind.IsExpression, expressionSyntax, declarationType));
                        }
                    }

                    if (label.WhenClause != null)
                    {
                        var c = label.WhenClause.Condition;
                        if (patternsCount > 1 && NeedsParentheses(c))
                        {
                            c = SyntaxFactory.ParenthesizedExpression(c);
                        }

                        if (varName != null && patternsCount > 1)
                        {
                            whens.Add(SyntaxFactory.BinaryExpression(SyntaxKind.LogicalAndExpression, SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, SyntaxFactory.IdentifierName(varName), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), c));
                        }
                        else
                        {
                            whens.Add(c);
                        }
                    }
                }
            }

            for (int i = 0; i < conditionList.Count; ++i)
            {
                var cond = conditionList[i];
                var be   = cond as BinaryExpressionSyntax;

                if (be != null)
                {
                    if (NeedsParentheses(be.Right))
                    {
                        conditionList[i] = be.WithRight(SyntaxFactory.ParenthesizedExpression(be.Right));
                    }
                }
                else
                {
                    if (NeedsParentheses(cond))
                    {
                        conditionList[i] = SyntaxFactory.ParenthesizedExpression(cond);
                    }
                }
            }

            if (conditionList.Count == 1)
            {
                return(new Tuple <ExpressionSyntax, List <VariableDeclarationSyntax>, List <ExpressionSyntax> >(conditionList.First(), variables, whens));
            }

            ExpressionSyntax condition = conditionList[0];

            for (int i = 1; i < conditionList.Count; ++i)
            {
                condition = SyntaxFactory.BinaryExpression(SyntaxKind.LogicalOrExpression, condition, conditionList[i]);
            }
            return(new Tuple <ExpressionSyntax, List <VariableDeclarationSyntax>, List <ExpressionSyntax> >(condition, variables, whens));
        }