private async Task <Document> MakeCatchEmptyAsync(Document document, CatchClauseSyntax catchStatement, CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            var newCatch = SyntaxFactory.CatchClause().WithDeclaration(
                SyntaxFactory.CatchDeclaration(SyntaxFactory.IdentifierName("Exception"))
                .WithIdentifier(SyntaxFactory.Identifier("ex")))
                           .WithBlock(catchStatement.Block)
                           .WithLeadingTrivia(catchStatement.GetLeadingTrivia())
                           .WithTrailingTrivia(catchStatement.GetTrailingTrivia())
                           .WithAdditionalAnnotations(Formatter.Annotation);
            var root = await document.GetSyntaxRootAsync();

            var newRoot     = root.ReplaceNode(catchStatement, newCatch);
            var newDocument = document.WithSyntaxRoot(newRoot);

            return(newDocument);
        }
        private async Task <Document> InsertExceptionClassCommentAsync(Document document, CatchClauseSyntax catchStatement, CancellationToken cancellationToken)
        {
            var block = SyntaxFactory.Block(new StatementSyntax[] { SyntaxFactory.ThrowStatement() });

            var newCatch = SyntaxFactory.CatchClause().WithDeclaration(
                SyntaxFactory.CatchDeclaration(SyntaxFactory.IdentifierName("Exception"))
                .WithIdentifier(SyntaxFactory.Identifier("ex")))
                           .WithBlock(block)
                           .WithLeadingTrivia(catchStatement.GetLeadingTrivia())
                           .WithTrailingTrivia(catchStatement.GetTrailingTrivia())
                           .WithAdditionalAnnotations(Formatter.Annotation);
            var root = await document.GetSyntaxRootAsync();

            var newRoot     = root.ReplaceNode(catchStatement, newCatch);
            var newDocument = document.WithSyntaxRoot(newRoot);

            return(newDocument);
        }
예제 #3
0
        private Decisions TraverseCatchClauses(CatchClauseSyntax ccs, ref int exitPoints, bool nested = false)
        {
            Decisions retDecision = new Decisions();
            CatchStatements retCatch = new CatchStatements();

            if (ccs.HasLeadingTrivia)
            {
                SetOuterComments(retCatch, ccs.GetLeadingTrivia().ToFullString());
            }

            if (ccs.HasTrailingTrivia)
            {
                SetInnerComments(retCatch, ccs.GetTrailingTrivia().ToFullString());
            }

            retCatch.IsNested = nested;
            var binaryExpressions = from aBinaryExpression in ccs.ChildNodes().OfType<BinaryExpressionSyntax>() select aBinaryExpression;
            foreach (BinaryExpressionSyntax bes in binaryExpressions)
            {
                Method tempMethod = TraverseBinaryExpression(bes);
                retCatch.AccessedVars.AddRange(tempMethod.AccessedVariables);
                retCatch.InvokedMethods.AddRange(tempMethod.InvokedMethods);
            }
            var catches = from aCatch in ccs.ChildNodes().OfType<CatchClauseSyntax>() select aCatch;
            foreach (CatchClauseSyntax ccs2 in catches)
            {
                Decisions tempCatch = TraverseCatchClauses(ccs2, ref exitPoints, true);
                retCatch.Nested.AddRange(tempCatch.Catches);
            }
            var elses = from aElse in ccs.ChildNodes().OfType<ElseClauseSyntax>() select aElse;
            foreach (ElseClauseSyntax ecs2 in elses)
            {
                Decisions tempElse = TraverseElseClauses(ecs2, ref exitPoints, true);
                retCatch.Nested.AddRange(tempElse.ElseStatements);
            }
            #region nested stuff
            var statements = from aStatement in ccs.ChildNodes().OfType<StatementSyntax>() select aStatement;
            foreach (StatementSyntax ss in statements)
            {
                if (ss is DoStatementSyntax)
                {
                    Decisions dwl = TraverseDoStatements(ss as DoStatementSyntax, ref exitPoints, true);
                    retCatch.Nested.AddRange(dwl.DoWhileLoops);
                }
                else if (ss is ExpressionStatementSyntax)
                {
                    Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax);
                    retCatch.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    retCatch.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                else if (ss is ForEachStatementSyntax)
                {
                    Decisions fes = TraverseForEachStatements(ss as ForEachStatementSyntax, ref exitPoints, true);
                    retCatch.Nested.AddRange(fes.ForEachStatements);
                }
                else if (ss is ForStatementSyntax)
                {
                    Decisions fs = TraverseForStatements(ss as ForStatementSyntax, ref exitPoints, true);
                    retCatch.Nested.AddRange(fs.ForStatements);
                }
                else if (ss is IfStatementSyntax)
                {
                    Decisions decision = TraverseIfStatements(ss as IfStatementSyntax, ref exitPoints, true);
                    retCatch.Nested.AddRange(decision.IfStatements);
                }
                else if (ss is LocalDeclarationStatementSyntax)
                {
                    Model.Type tempType = new Model.Type();
                    LocalDeclarationStatementSyntax ldss = ss as LocalDeclarationStatementSyntax;
                    if (ldss.Declaration != null)
                    {
                        VariableDeclarationSyntax vds = ldss.Declaration;
                        tempType.Name = vds.Type.ToString();
                        tempType.IsKnownType = true;
                        tempType.IsNotUserDefined = true;
                    }
                    Method tempMethod = TransverseAccessVars(ss as LocalDeclarationStatementSyntax);
                    //NOT SURE if this will work but here goes
                    tempMethod.AccessedVariables[0].Type = tempType;
                    retCatch.AccessedVars.AddRange(tempMethod.AccessedVariables);
                    retCatch.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                }
                else if (ss is ReturnStatementSyntax)
                {
                    exitPoints++;
                }
                else if (ss is SwitchStatementSyntax)
                {
                    Decisions switchStm = TraverseSwitchStatements(ss as SwitchStatementSyntax, ref exitPoints, true);
                    retCatch.Nested.AddRange(switchStm.SwitchStatements);
                }
                else if (ss is WhileStatementSyntax)
                {
                    Decisions wl = TraverseWhileLoops(ss as WhileStatementSyntax, ref exitPoints, true);
                    retCatch.Nested.AddRange(wl.WhileLoops);
                }
            }
            #endregion
            retDecision.Catches.Add(retCatch);
            return retDecision;
        }