Пример #1
0
 public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
 {
     CheckThrowKeyword(node);
     foreach (var n in node.ChildNodes())
     {
         Visit(n);
     }
 }
Пример #2
0
        private Task <Document> UseNewGuid(Document document, SyntaxNode root, ObjectCreationExpressionSyntax statement)
        {
            // We're not adding the simplifier in this route because it doesn't seem to work
            // It works when putting it on the root node but that has too many consequences for other code
            // We do some simple introspection to see if it is fully qualified already or not
            var newExpression = "Guid.NewGuid()";

            if (statement.ChildNodes().First().IsKind(SyntaxKind.QualifiedName))
            {
                newExpression = "System.Guid.NewGuid()";
            }

            var newRoot = root.ReplaceNode(statement, SyntaxFactory.ParseExpression(newExpression));

            return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
        }
Пример #3
0
        /* Method that checks whether or not Singleton class is thread safe
         * by way of double check locking
         */
        public bool IsDoubleCheckLocked(ClassDeclarationSyntax classDec)
        {
            /* List of methods that might be used to
             * return instances of singleton class
             */
            List <MethodDeclarationSyntax> possibleInstanceMethods =
                new List <MethodDeclarationSyntax>();

            // Get List of all methodDeclarations in class
            List <MethodDeclarationSyntax> methodDeclarations =
                classDec.DescendantNodes().
                OfType <MethodDeclarationSyntax>().ToList();

            bool classContainsValidMethod = false;

            foreach (MethodDeclarationSyntax methodDec in methodDeclarations)
            {
                bool isPublic = false;
                bool isStatic = false;

                /* Check if method returns an instance of class first,
                 * otherwise don't do more taxing operations
                 */
                if (methodDec.ReturnType.ToString().
                    Equals(classDec.Identifier.Text))
                {
                    /* Check whether or not method is public and static,
                     * both of which instance method should be
                     */

                    SyntaxTokenList modifiers = methodDec.Modifiers;
                    foreach (SyntaxToken modifier in modifiers)
                    {
                        if (modifier.IsKind(SyntaxKind.PublicKeyword))
                        {
                            isPublic = true;
                        }
                        else if (modifier.IsKind(SyntaxKind.StaticKeyword))
                        {
                            isStatic = true;
                        }
                    }
                }

                /* isPublic and isStatic can only have been changed to true
                 * if returnType is correct. If conditions are fulfilled,
                 * add checked method to list of possible instance methods
                 */
                if (isPublic && isStatic)
                {
                    possibleInstanceMethods.Add(methodDec);
                }
            }

            foreach (
                MethodDeclarationSyntax methodDec in
                possibleInstanceMethods)
            {
                /* Can choose .First() because method can
                 * syntactically only have one Block
                 */
                BlockSyntax block = methodDec.ChildNodes().
                                    OfType <BlockSyntax>().First();

                string            identifier         = "";
                bool              containsFirstCheck = false;
                List <SyntaxNode> nodes =
                    block.ChildNodes().OfType <SyntaxNode>().ToList();

                BlockSyntax secondBlock = null;
                foreach (SyntaxNode node in nodes)
                {
                    /* Check whether or not the block contains an if-stmt
                     * that checks the instance variable for null
                     */
                    if (node.IsKind(SyntaxKind.IfStatement))
                    {
                        List <SyntaxNodeOrToken> subNodes =
                            node.ChildNodesAndTokens().
                            OfType <SyntaxNodeOrToken>().ToList();

                        bool isEqualsEquals = false;
                        bool checksForNull  = false;

                        foreach (SyntaxNodeOrToken subNode in subNodes)
                        {
                            if (subNode.IsKind(SyntaxKind.EqualsExpression))
                            {
                                List <SyntaxNodeOrToken> subSubNodes =
                                    subNode.ChildNodesAndTokens().
                                    OfType <SyntaxNodeOrToken>().ToList();

                                foreach (
                                    SyntaxNodeOrToken subSubNode in
                                    subSubNodes)
                                {
                                    if (subSubNode.IsKind(
                                            SyntaxKind.IdentifierName))
                                    {
                                        identifier = subSubNode.ToString();
                                    }
                                    else if (subSubNode.IsKind(
                                                 SyntaxKind.EqualsEqualsToken))
                                    {
                                        isEqualsEquals = true;
                                    }
                                    else if (subSubNode.IsKind(
                                                 SyntaxKind.NullLiteralExpression))
                                    {
                                        checksForNull = true;
                                    }
                                }
                            }
                        }

                        if (isEqualsEquals && checksForNull)
                        {
                            containsFirstCheck = true;
                            secondBlock        = node.ChildNodes().
                                                 OfType <BlockSyntax>().First();
                        }
                    }
                }

                bool        containsLock = false;
                BlockSyntax thirdBlock   = null;

                /* If instance variable is checked for null first,
                 * check if after that check, a lock occurs
                 */
                if (containsFirstCheck)
                {
                    List <SyntaxNode> subNodes =
                        secondBlock.ChildNodes().OfType <SyntaxNode>().ToList();

                    foreach (SyntaxNode subNode in subNodes)
                    {
                        if (subNode.IsKind(SyntaxKind.LockStatement))
                        {
                            containsLock = true;
                            thirdBlock   = subNode.ChildNodes().
                                           OfType <BlockSyntax>().First();
                        }
                    }
                }

                bool        containsSecondCheck = false;
                BlockSyntax fourthBlock         = null;

                /* If the lock occurs, check if another
                 * null-check on instance variable happens
                 */
                if (containsLock)
                {
                    List <SyntaxNode> subNodes =
                        thirdBlock.ChildNodes().
                        OfType <SyntaxNode>().ToList();

                    foreach (SyntaxNode subNode in subNodes)
                    {
                        if (subNode.IsKind(
                                SyntaxKind.IfStatement))
                        {
                            List <SyntaxNode> subSubNodes =
                                subNode.ChildNodes().
                                OfType <SyntaxNode>().ToList();

                            foreach (SyntaxNode subSubNode in subSubNodes)
                            {
                                if (subSubNode.IsKind(
                                        SyntaxKind.EqualsExpression))
                                {
                                    List <SyntaxNodeOrToken> subSubSubNodes =
                                        subSubNode.ChildNodesAndTokens().
                                        OfType <SyntaxNodeOrToken>().ToList();


                                    bool correctIdentifier = false;
                                    bool isEqualsEquals    = false;
                                    bool checksForNull     = false;

                                    foreach (
                                        SyntaxNodeOrToken subSubSubNode in
                                        subSubSubNodes)
                                    {
                                        if (subSubSubNode.IsKind(
                                                SyntaxKind.IdentifierName) &&
                                            subSubSubNode.ToString() ==
                                            identifier)
                                        {
                                            correctIdentifier = true;
                                        }
                                        else if (subSubSubNode.IsKind(
                                                     SyntaxKind.EqualsEqualsToken))
                                        {
                                            isEqualsEquals = true;
                                        }
                                        else if (subSubSubNode.IsKind(
                                                     SyntaxKind.NullLiteralExpression))
                                        {
                                            checksForNull = true;
                                        }
                                    }

                                    if (correctIdentifier &&
                                        isEqualsEquals &&
                                        checksForNull)
                                    {
                                        containsSecondCheck = true;
                                        fourthBlock         = subNode.ChildNodes().
                                                              OfType <BlockSyntax>().First();
                                    }
                                }
                            }
                        }
                    }
                }

                bool isValidDoubleCheck = false;

                /* If second null-check happens, finally check whether or not
                 * after that second null-check, a value is assigned to
                 * the instance variable of type {ClassName}
                 * in which case it can be determined that a valid
                 * double check lock has occured
                 */
                if (containsSecondCheck)
                {
                    List <SyntaxNode> subNodes =
                        fourthBlock.ChildNodes().
                        OfType <SyntaxNode>().ToList();

                    foreach (SyntaxNode subNode in subNodes)
                    {
                        if (subNode.IsKind(SyntaxKind.ExpressionStatement))
                        {
                            List <SyntaxNode> subSubNodes =
                                subNode.ChildNodes().
                                OfType <SyntaxNode>().ToList();

                            foreach (SyntaxNode subSubNode in subSubNodes)
                            {
                                if (subSubNode.IsKind
                                        (SyntaxKind.SimpleAssignmentExpression))
                                {
                                    /* SimpleAssignmentExpression can
                                     * only have an identifier name
                                     */
                                    if (subSubNode.ChildNodes().
                                        OfType <IdentifierNameSyntax>().
                                        First().Identifier.Text == identifier)
                                    {
                                        /* Only one ObjectCreationExpression
                                         * can be attached to a
                                         * SimpleAssignmentExpression
                                         */
                                        ObjectCreationExpressionSyntax
                                                objectCreationExp =
                                            subSubNode.ChildNodes().
                                            OfType <ObjectCreationExpressionSyntax>().
                                            First();

                                        /* ObjectCreationExpression can only
                                         * have one IdentifierName
                                         * attached to it
                                         */
                                        if (objectCreationExp.ChildNodes().
                                            OfType <IdentifierNameSyntax>().
                                            First().Identifier.Text ==
                                            classDec.Identifier.Text)
                                        {
                                            isValidDoubleCheck = true;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (isValidDoubleCheck)
                    {
                        classContainsValidMethod = true;
                    }
                }
            }

            return(classContainsValidMethod);
        }