Esempio n. 1
0
 public override void VisitFinallyClause(FinallyClauseSyntax node)
 {
     throw new NotSupportedException();
 }
Esempio n. 2
0
 public TryStatementSyntax Update(SyntaxToken tryKeyword, BlockSyntax block, SyntaxList <CatchClauseSyntax> catches, FinallyClauseSyntax @finally)
 => Update(AttributeLists, tryKeyword, block, catches, @finally);
Esempio n. 3
0
 public override void VisitFinallyClause(FinallyClauseSyntax node)
 {
     // Do not call base to force the walker to stop.
     // Another walker will take care of this finally clause.
 }
Esempio n. 4
0
        public override void VisitFinallyClause(FinallyClauseSyntax node)
        {
            // NOTE: We're going to cheat a bit - we know that the block is definitely going 
            // to get a map entry, so we don't need to worry about the WithAdditionalFlags
            // binder being dropped.  That is, there's no point in adding the WithAdditionalFlags
            // binder to the map ourselves and having VisitBlock unconditionally overwrite it.
            Visit(node.Block, enclosing.WithAdditionalFlags(BinderFlags.InFinallyBlock));

            Binder finallyBinder;
            Debug.Assert(this.map.TryGetValue(node.Block, out finallyBinder) && finallyBinder.Flags.Includes(BinderFlags.InFinallyBlock));
        }
Esempio n. 5
0
        private static SyntaxToken GetFirstExcludedToken(StatementSyntax statement)
        {
            Debug.Assert(statement != null);
            switch (statement.Kind)
            {
            case SyntaxKind.Block:
                return(((BlockSyntax)statement).CloseBraceToken);

            case SyntaxKind.BreakStatement:
                return(((BreakStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.ContinueStatement:
                return(((ContinueStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.LocalDeclarationStatement:
                return(((LocalDeclarationStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.DoStatement:
                return(((DoStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.EmptyStatement:
                return(((EmptyStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.ExpressionStatement:
                return(((ExpressionStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.ForStatement:
                return(GetFirstExcludedToken(((ForStatementSyntax)statement).Statement));

            //case SyntaxKind.GotoDefaultStatement:
            //case SyntaxKind.GotoCaseStatement:
            //case SyntaxKind.GotoStatement:
            //	return ((GotoStatementSyntax)statement).SemicolonToken;
            case SyntaxKind.IfStatement:
                IfStatementSyntax ifStmt  = (IfStatementSyntax)statement;
                ElseClauseSyntax  elseOpt = ifStmt.Else;
                return(GetFirstExcludedToken(elseOpt == null ? ifStmt.Statement : elseOpt.Statement));

            case SyntaxKind.LabeledStatement:
                return(GetFirstExcludedToken(((LabeledStatementSyntax)statement).Statement));

            case SyntaxKind.ReturnStatement:
                return(((ReturnStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.SwitchStatement:
                return(((SwitchStatementSyntax)statement).CloseBraceToken);

            case SyntaxKind.ThrowStatement:
                return(((ThrowStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.TryStatement:
                TryStatementSyntax tryStmt = (TryStatementSyntax)statement;

                FinallyClauseSyntax finallyClause = tryStmt.Finally;
                if (finallyClause != null)
                {
                    return(finallyClause.Block.CloseBraceToken);
                }

                CatchClauseSyntax lastCatch = tryStmt.Catches.LastOrDefault();
                if (lastCatch != null)
                {
                    return(lastCatch.Block.CloseBraceToken);
                }
                return(tryStmt.Block.CloseBraceToken);

            case SyntaxKind.UsingStatement:
                return(GetFirstExcludedToken(((UsingStatementSyntax)statement).Statement));

            case SyntaxKind.WhileStatement:
                return(GetFirstExcludedToken(((WhileStatementSyntax)statement).Statement));

            default:
                throw ExceptionUtilities.UnexpectedValue(statement.Kind);
            }
        }
Esempio n. 6
0
 public override void VisitFinallyClause(FinallyClauseSyntax node)
 {
     base.VisitFinallyClause(node);
 }
 public FinallyStatementInterpreter(StatementInterpreterHandler statementInterpreterHandler, FinallyClauseSyntax finallyClauseSyntax)
 {
     this.statementInterpreterHandler = statementInterpreterHandler;
     this.finallyClauseSyntax         = finallyClauseSyntax;
 }
Esempio n. 8
0
 public override void VisitFinallyClause(FinallyClauseSyntax node)
 {
     VisitBlock(node.Block);
 }
Esempio n. 9
0
 // Finally体
 public virtual void VisitFinallyClauseSyntax(FinallyClauseSyntax value)
 {
     DefaultVisit(value);
 }
Esempio n. 10
0
        public override void VisitFinallyClause(FinallyClauseSyntax node)
        {
            var body = VisitSyntaxNode(node.Block);

            _currentNode = new Finally(body);
        }
 //
 // Summary:
 //     Called when the visitor visits a FinallyClauseSyntax node.
 public virtual void VisitFinallyClause(FinallyClauseSyntax node);
Esempio n. 12
0
        public void AnalyzeACatchBlock(CatchClauseSyntax catchblock)
        {
            CatchBlock catchBlockInfo = new CatchBlock();

            var tree  = catchblock.SyntaxTree;
            var model = TreeAndModelDic[tree];

            TypeSyntax       exceptionTypeSyntax      = null;
            INamedTypeSymbol exceptionNamedTypeSymbol = null;

            if (catchblock.Declaration != null)
            {
                exceptionTypeSyntax      = catchblock.Declaration.Type;
                exceptionNamedTypeSymbol = model.GetTypeInfo(exceptionTypeSyntax).ConvertedType as INamedTypeSymbol;

                if (exceptionNamedTypeSymbol != null)
                {
                    catchBlockInfo.ExceptionType = exceptionNamedTypeSymbol.ToString();

                    //Binding info:
                    if (exceptionNamedTypeSymbol.BaseType != null)
                    {
                        catchBlockInfo.OperationFeatures["Binded"]           = 1;
                        catchBlockInfo.OperationFeatures["RecoveredBinding"] = model.IsSpeculativeSemanticModel ? 1 : 0;
                        catchBlockInfo.OperationFeatures["Kind"]             = ASTUtilities.FindKind(exceptionNamedTypeSymbol, Compilation);
                    }
                    else
                    {
                        catchBlockInfo.OperationFeatures["Binded"] = 0;
                    }
                }
                else
                {
                    catchBlockInfo.ExceptionType = "!NO_NAMED_TYPE!";
                }
            }
            else
            {
                catchBlockInfo.ExceptionType = "!NO_EXCEPTION_DECLARED!";
            }

            //Basic info:
            catchBlockInfo.MetaInfo["ExceptionType"] = catchBlockInfo.ExceptionType;

            //Try info:
            var tryBlock = catchblock.Parent as TryStatementSyntax;

            catchBlockInfo.MetaInfo["TryBlock"] = tryBlock.ToString();
            catchBlockInfo.OperationFeatures["ParentNodeType"] = ASTUtilities.FindParent(tryBlock).RawKind;
            catchBlockInfo.MetaInfo["ParentNodeType"]          = ASTUtilities.FindParent(tryBlock).Kind().ToString();

            //Common Features - try/catch block
            var tryNoTriviaCount        = ASTUtilities.countLines(tryBlock.Block) - 2;
            var tryFileLinePositionSpan = tree.GetLineSpan(tryBlock.Block.Span);
            var tryStartLine            = tryFileLinePositionSpan.StartLinePosition.Line + 1;
            var tryEndLine = tryFileLinePositionSpan.EndLinePosition.Line + 1;

            catchBlockInfo.OperationFeatures["TryStartLine"] = tryStartLine;
            catchBlockInfo.OperationFeatures["TryEndLine"]   = tryEndLine;
            catchBlockInfo.MetaInfo["TryLine"]         = tryStartLine.ToString();
            catchBlockInfo.OperationFeatures["TryLOC"] = tryNoTriviaCount;

            var catchNoTriviaCount        = ASTUtilities.countLines(catchblock.Block) - 2;
            var catchFileLinePositionSpan = tree.GetLineSpan(catchblock.Block.Span);
            var catchStartLine            = catchFileLinePositionSpan.StartLinePosition.Line + 1;
            var catchEndLine = catchFileLinePositionSpan.EndLinePosition.Line + 1;

            catchBlockInfo.OperationFeatures["CatchStartLine"] = catchStartLine;
            catchBlockInfo.OperationFeatures["CatchEndLine"]   = catchEndLine;
            catchBlockInfo.OperationFeatures["CatchLOC"]       = catchNoTriviaCount;

            catchBlockInfo.OperationFeatures["CatchStart"]  = catchFileLinePositionSpan.StartLinePosition.Line;
            catchBlockInfo.OperationFeatures["CatchLength"] = catchFileLinePositionSpan.EndLinePosition.Line - catchFileLinePositionSpan.StartLinePosition.Line;

            catchBlockInfo.MetaInfo["CatchBlock"] = catchblock.ToString();

            catchBlockInfo.FilePath  = tree.FilePath;
            catchBlockInfo.StartLine = catchStartLine;

            catchBlockInfo.MetaInfo["FilePath"]  = tree.FilePath;
            catchBlockInfo.MetaInfo["StartLine"] = catchStartLine.ToString();

            //Common Features - parent type
            catchBlockInfo.ParentType             = ASTUtilities.FindParentType(tryBlock, model);
            catchBlockInfo.MetaInfo["ParentType"] = catchBlockInfo.ParentType;

            //Common Features - parent method name
            SyntaxNode parentNode = ASTUtilities.FindParentMethod(tryBlock);

            catchBlockInfo.ParentMethod             = ASTUtilities.GetMethodName(parentNode, TreeAndModelDic, Compilation);;
            catchBlockInfo.MetaInfo["ParentMethod"] = catchBlockInfo.ParentMethod;

            //Common Features
            if (parentNode.IsKind(SyntaxKind.MethodDeclaration))
            {
                parentNode = (parentNode as MethodDeclarationSyntax).Body;
            }
            if (parentNode.IsKind(SyntaxKind.ConstructorDeclaration))
            {
                parentNode = (parentNode as ConstructorDeclarationSyntax).Body;
            }

            var parentMethodNoTriviaCount        = ASTUtilities.countLines(parentNode) - 2;
            var parentMethodFileLinePositionSpan = tree.GetLineSpan(parentNode.Span);
            var parentMethodStartLine            = parentMethodFileLinePositionSpan.StartLinePosition.Line + 1;
            var parentMethodEndLine = parentMethodFileLinePositionSpan.EndLinePosition.Line + 1;

            catchBlockInfo.OperationFeatures["MethodStartLine"] = parentMethodStartLine;
            catchBlockInfo.OperationFeatures["MethodEndLine"]   = parentMethodStartLine;
            catchBlockInfo.OperationFeatures["MethodLOC"]       = parentMethodNoTriviaCount;

            //Treatment for TryStatement
            bool hasTryStatement = catchblock.DescendantNodesAndSelf()
                                   .OfType <TryStatementSyntax>().Any();
            SyntaxNode updatedCatchBlock = catchblock;

            if (hasTryStatement == true)
            {
                try
                {
                    // remove try-catch-finally block inside
                    updatedCatchBlock = tryblockremover.Visit(catchblock);
                }
                catch (System.ArgumentNullException e)
                {
                    // ignore the ArgumentNullException
                }
            }

            //Treatment for TryStatement
            //RecoverFlag - (based on inner try blocks)
            var recoverStatement = FindRecoverStatement(catchblock, model);

            if (recoverStatement != null)
            {
                catchBlockInfo.MetaInfo["RecoverFlag"]          = recoverStatement.ToString();
                catchBlockInfo.OperationFeatures["RecoverFlag"] = 1;
            }

            /*
             * Flagging inner catch
             * CatchClause is a child of a TryStatement, which is a child of a Block, which we wanna know the parent.
             * If CatchClause it's the parent, then it's an inner catch. Get the line of the parent try of that.
             */
            if (IsInnerCatch(catchblock.Parent))
            {
                catchBlockInfo.OperationFeatures["InnerCatch"]         = 1;
                catchBlockInfo.OperationFeatures["ParentTryStartLine"] = tree.GetLineSpan((FindParentCatch(catchblock.Parent).Parent as TryStatementSyntax).Block.Span).StartLinePosition.Line + 1;// tree.getLineNumber(node.getParent().getParent().getParent().getParent().getStartPosition() + 1));
            }

            //Treatment for MethodInvocation
            //Collection of data for statements of: logging, abort,

            //Logging
            var loggingStatement = FindLoggingIn(updatedCatchBlock);

            if (loggingStatement != null)
            {
                catchBlockInfo.MetaInfo["Logged"]          = loggingStatement.ToString();
                catchBlockInfo.OperationFeatures["Logged"] = 1;

                if (CountLoggingIn(updatedCatchBlock) > 1)
                {
                    catchBlockInfo.OperationFeatures["MultiLog"] = 1;
                }
            }

            //Abort
            var abortStatement = FindAbortIn(updatedCatchBlock);

            if (abortStatement != null)
            {
                catchBlockInfo.MetaInfo["Abort"]          = abortStatement.ToString();
                catchBlockInfo.OperationFeatures["Abort"] = 1;
            }

            //GetCause - C# is inner exception
            var getCauseStatement = FindGetCauseIn(updatedCatchBlock);

            if (getCauseStatement != null)
            {
                catchBlockInfo.MetaInfo["GetCause"]          = getCauseStatement.ToString();
                catchBlockInfo.OperationFeatures["GetCause"] = 1;
            }

            //Other - Other INVOCATION
            var otherStatement = FindOtherIn(updatedCatchBlock);

            if (otherStatement != null)
            {
                catchBlockInfo.MetaInfo["OtherInvocation"]          = otherStatement.ToString();
                catchBlockInfo.OperationFeatures["OtherInvocation"] = 1;
            }


            //Treatment for ThrowStatement
            //Collection of data for statements of: throw
            var throwStatement = FindThrowIn(updatedCatchBlock);

            if (throwStatement != null)
            {
                catchBlockInfo.MetaInfo["Thrown"]               = throwStatement.ToString();
                catchBlockInfo.OperationFeatures["NumThrown"]   = CountThrowIn(updatedCatchBlock);
                catchBlockInfo.OperationFeatures["NumThrowNew"] = CountThrowNewIn(updatedCatchBlock);
                catchBlockInfo.OperationFeatures["NumThrowWrapCurrentException"] = CountThrowWrapIn(updatedCatchBlock, catchblock.Declaration?.Identifier.ToString());
            }

            //Treatment for ReturnStatement
            var returnStatement = FindReturnIn(updatedCatchBlock);

            if (returnStatement != null)
            {
                catchBlockInfo.MetaInfo["Return"]          = returnStatement.ToString();
                catchBlockInfo.OperationFeatures["Return"] = 1;
            }

            //Treatment for ContinueStatement
            var continueStatement = FindContinueIn(updatedCatchBlock);

            if (continueStatement != null)
            {
                catchBlockInfo.MetaInfo["Continue"]          = continueStatement.ToString();
                catchBlockInfo.OperationFeatures["Continue"] = 1;
            }

            //var setLogicFlag = FindSetLogicFlagIn(updatedCatchBlock);
            //if (setLogicFlag != null)
            //{
            //    catchBlockInfo.MetaInfo["SetLogicFlag"] = setLogicFlag.ToString();
            //    catchBlockInfo.OperationFeatures["SetLogicFlag"] = 1;
            //}

            //var otherOperation = HasOtherOperation(updatedCatchBlock, model);
            //if (otherOperation != null)
            //{
            //    catchBlockInfo.MetaInfo["OtherOperation"] = otherOperation.ToString();
            //    catchBlockInfo.OperationFeatures["OtherOperation"] = 1;
            //}

            //EmptyBlock
            if (IsEmptyBlock(updatedCatchBlock))
            {
                catchBlockInfo.OperationFeatures["EmptyBlock"] = 1;
            }

            //CatchException
            if (exceptionNamedTypeSymbol != null)
            {
                if (exceptionNamedTypeSymbol.Equals(Compilation.GetTypeByMetadataName("System.Exception")))
                {
                    catchBlockInfo.OperationFeatures["CatchException"] = 1;
                }
                else
                {
                    catchBlockInfo.OperationFeatures["CatchException"] = 0;
                }
            }

            //ToDo
            if (IsToDo(updatedCatchBlock))
            {
                catchBlockInfo.OperationFeatures["ToDo"] = 1;
            }

            //var variableAndComments = GetVariablesAndComments(tryBlock.Block);
            //var containingMethod = GetContainingMethodName(tryBlock, model);
            //var methodNameList = GetAllInvokedMethodNamesByBFS(tryBlock.Block, treeAndModelDic, compilation);

            var tryPossibleExceptionsCustomVisitor = new PossibleExceptionsCustomVisitor(Compilation, TreeAndModelDic, 0, true, tree.FilePath, catchStartLine, exceptionNamedTypeSymbol);

            tryPossibleExceptionsCustomVisitor.Visit(tryBlock.Block);

            /*
             * Process for possible exceptions
             */
            getExceptionFlows(this.PossibleExceptionsList, tryPossibleExceptionsCustomVisitor.getClosedExceptionFlows(), Compilation);


            //catchBlockInfo.MetaInfo["TryMethods"] = possibleExceptionsCustomVisitor.PrintInvokedMethodsHandlerType();
            catchBlockInfo.MetaInfo["TryMethodsAndExceptions"] = tryPossibleExceptionsCustomVisitor.PrintInvokedMethodsPossibleExceptions();

            catchBlockInfo.OperationFeatures["NumDistinctMethods"] = tryPossibleExceptionsCustomVisitor.countInvokedMethodsHandlerType();

            catchBlockInfo.MetaInfo["TryMethodsBinded"] = tryPossibleExceptionsCustomVisitor.PrintInvokedMethodsBinded();

            catchBlockInfo.OperationFeatures["NumDistinctMethodsNotBinded"] = tryPossibleExceptionsCustomVisitor.getNumMethodsNotBinded();

            catchBlockInfo.MetaInfo["DistinctExceptions"]             = tryPossibleExceptionsCustomVisitor.PrintDistinctPossibleExceptions();
            catchBlockInfo.OperationFeatures["NumDistinctExceptions"] = tryPossibleExceptionsCustomVisitor.getDistinctPossibleExceptions().Count;

            catchBlockInfo.OperationFeatures["NumSpecificHandler"]      = tryPossibleExceptionsCustomVisitor.getNumSpecificHandler();
            catchBlockInfo.OperationFeatures["NumSubsumptionHandler"]   = tryPossibleExceptionsCustomVisitor.getNumSubsumptionHandler();
            catchBlockInfo.OperationFeatures["NumSupersumptionHandler"] = tryPossibleExceptionsCustomVisitor.getNumSupersumptionHandler();
            catchBlockInfo.OperationFeatures["NumOtherHandler"]         = tryPossibleExceptionsCustomVisitor.getNumOtherHandler();

            catchBlockInfo.OperationFeatures["MaxLevel"]         = tryPossibleExceptionsCustomVisitor.getChildrenMaxLevel();
            catchBlockInfo.OperationFeatures["NumIsDocSemantic"] = tryPossibleExceptionsCustomVisitor.getNumIsDocSemantic();
            catchBlockInfo.OperationFeatures["NumIsDocSyntax"]   = tryPossibleExceptionsCustomVisitor.getNumIsDocSyntax();
            catchBlockInfo.OperationFeatures["NumIsThrow"]       = tryPossibleExceptionsCustomVisitor.getNumIsThrow();

            //FinallyThrowing
            FinallyClauseSyntax finallyBlock = tryBlock.Finally;

            if (finallyBlock != null)
            {
                catchBlockInfo.MetaInfo["FinallyBlock"] = finallyBlock.ToString();

                var finallyPossibleExceptionsCustomVisitor = new PossibleExceptionsCustomVisitor(Compilation, TreeAndModelDic, 0, true, tree.FilePath, catchStartLine, exceptionNamedTypeSymbol);
                finallyPossibleExceptionsCustomVisitor.Visit(finallyBlock.Block);

                if (finallyBlock.DescendantNodes().OfType <ThrowStatementSyntax>().Any() ||
                    finallyPossibleExceptionsCustomVisitor.getDistinctPossibleExceptions().Count > 0)
                {
                    catchBlockInfo.OperationFeatures["FinallyThrowing"] = 1;
                }
            }

            //var methodAndExceptionList = GetAllInvokedMethodNamesAndExceptionsByBFS(tryBlock.Block, treeAndModelDic, compilation);

            //catchBlockInfo.OperationFeatures["NumMethod"] = methodAndExceptionList[0].Count;
            //catchBlockInfo.OperationFeatures["NumExceptions"] = methodAndExceptionList[1].Count;
            //catchBlockInfo.TextFeatures = methodAndExceptionList[0];
            //if (containingMethod != null)
            //{
            //    MergeDic<string>(ref catchBlockInfo.TextFeatures,
            //        new Dictionary<string, int>() { { containingMethod, 1 } });
            //}
            //MergeDic<string>(ref catchBlockInfo.TextFeatures,
            //        new Dictionary<string, int>() { { "##spliter##", 0 } }); // to seperate methods and variables
            //MergeDic<string>(ref catchBlockInfo.TextFeatures, variableAndComments);

            Catches.Add(catchBlockInfo);
        }
 public override void VisitFinallyClause(FinallyClauseSyntax node)
 {
     LogicalLineCount++;
     base.VisitFinallyClause(node);
 }
Esempio n. 14
0
 public static TryStatementSyntax TryStatement(BlockSyntax block, CatchClauseSyntax @catch, FinallyClauseSyntax @finally = null)
 {
     return(SyntaxFactory.TryStatement(block, SingletonList(@catch), @finally));
 }
Esempio n. 15
0
 public static TryStatementSyntax TryStatement(BlockSyntax block, SyntaxList <CatchClauseSyntax> catches, FinallyClauseSyntax @finally)
 => TryStatement(attributeLists: default, block, catches, @finally);
Esempio n. 16
0
        public void VisitFinallyClause(FinallyClauseSyntax node)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            node.Validate();

            if (_writer.Configuration.LineBreaksAndWrapping.PlaceOnNewLine.PlaceFinallyOnNewLine)
                _writer.WriteIndent();
            else
                _writer.WriteSpace();

            _writer.WriteKeyword(PrinterKeyword.Finally);

            node.Block.Accept(this);
        }
Esempio n. 17
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitFinallyClause(FinallyClauseSyntax node)
 {
     this.OnNodeVisited(node);
     if (!this.traverseRootOnly) base.VisitFinallyClause(node);
 }
Esempio n. 18
0
        public override Evaluation VisitFinallyClause(FinallyClauseSyntax node)
        {
            node.Block?.Accept <Evaluation>(this);

            return(base.VisitFinallyClause(node));
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitFinallyClause(FinallyClauseSyntax node)
 {
     this.OnNodeVisited(node, this.type.IsInstanceOfType(node));
     base.VisitFinallyClause(node);
 }
Esempio n. 20
0
        public override Ust VisitFinallyClause(FinallyClauseSyntax node)
        {
            var result = (BlockStatement)VisitBlock(node.Block);

            return(result);
        }
Esempio n. 21
0
 public virtual void VisitFinally(FinallyClauseSyntax node) => DefaultVisit(node);
Esempio n. 22
0
        internal static SyntaxToken GetFirstExcludedToken(StatementSyntax statement)
        {
            Debug.Assert(statement != null);
            switch (statement.Kind())
            {
            case SyntaxKind.Block:
                return(((BlockSyntax)statement).CloseBraceToken);

            case SyntaxKind.BreakStatement:
                return(((BreakStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
                return(((CheckedStatementSyntax)statement).Block.CloseBraceToken);

            case SyntaxKind.ContinueStatement:
                return(((ContinueStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.LocalDeclarationStatement:
                return(((LocalDeclarationStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.DoStatement:
                return(((DoStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.EmptyStatement:
                return(((EmptyStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.ExpressionStatement:
                return(((ExpressionStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.FixedStatement:
                return(GetFirstExcludedToken(((FixedStatementSyntax)statement).Statement));

            case SyntaxKind.ForEachStatement:
            case SyntaxKind.ForEachVariableStatement:
                return(GetFirstExcludedToken(((CommonForEachStatementSyntax)statement).Statement));

            case SyntaxKind.ForStatement:
                return(GetFirstExcludedToken(((ForStatementSyntax)statement).Statement));

            case SyntaxKind.GotoDefaultStatement:
            case SyntaxKind.GotoCaseStatement:
            case SyntaxKind.GotoStatement:
                return(((GotoStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.IfStatement:
                IfStatementSyntax ifStmt  = (IfStatementSyntax)statement;
                ElseClauseSyntax  elseOpt = ifStmt.Else;
                return(GetFirstExcludedToken(elseOpt == null ? ifStmt.Statement : elseOpt.Statement));

            case SyntaxKind.LabeledStatement:
                return(GetFirstExcludedToken(((LabeledStatementSyntax)statement).Statement));

            case SyntaxKind.LockStatement:
                return(GetFirstExcludedToken(((LockStatementSyntax)statement).Statement));

            case SyntaxKind.ReturnStatement:
                return(((ReturnStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.SwitchStatement:
                return(((SwitchStatementSyntax)statement).CloseBraceToken);

            case SyntaxKind.ThrowStatement:
                return(((ThrowStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.TryStatement:
                TryStatementSyntax tryStmt = (TryStatementSyntax)statement;

                FinallyClauseSyntax finallyClause = tryStmt.Finally;
                if (finallyClause != null)
                {
                    return(finallyClause.Block.CloseBraceToken);
                }

                CatchClauseSyntax lastCatch = tryStmt.Catches.LastOrDefault();
                if (lastCatch != null)
                {
                    return(lastCatch.Block.CloseBraceToken);
                }
                return(tryStmt.Block.CloseBraceToken);

            case SyntaxKind.UnsafeStatement:
                return(((UnsafeStatementSyntax)statement).Block.CloseBraceToken);

            case SyntaxKind.UsingStatement:
                return(GetFirstExcludedToken(((UsingStatementSyntax)statement).Statement));

            case SyntaxKind.WhileStatement:
                return(GetFirstExcludedToken(((WhileStatementSyntax)statement).Statement));

            case SyntaxKind.YieldReturnStatement:
            case SyntaxKind.YieldBreakStatement:
                return(((YieldStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.LocalFunctionStatement:
                LocalFunctionStatementSyntax localFunctionStmt = (LocalFunctionStatementSyntax)statement;
                if (localFunctionStmt.Body != null)
                {
                    return(GetFirstExcludedToken(localFunctionStmt.Body));
                }
                if (localFunctionStmt.SemicolonToken != default(SyntaxToken))
                {
                    return(localFunctionStmt.SemicolonToken);
                }
                return(localFunctionStmt.ParameterList.GetLastToken());

            default:
                throw ExceptionUtilities.UnexpectedValue(statement.Kind());
            }
        }
 public TameFinallyClauseSyntax(FinallyClauseSyntax node)
 {
     Node = node;
     AddChildren();
 }
 public FinallyClauseTranslation(FinallyClauseSyntax syntax, SyntaxTranslation parent) : base(syntax, parent)
 {
     Block = syntax.Block.Get<BlockTranslation>(this);
 }
Esempio n. 25
0
 public FinallyClauseTranslation(FinallyClauseSyntax syntax, SyntaxTranslation parent) : base(syntax, parent)
 {
     Block = syntax.Block.Get <BlockTranslation>(this);
 }
Esempio n. 26
0
        public override void VisitFinallyClause(FinallyClauseSyntax node)
        {
            // NOTE: We're going to cheat a bit - we know that the block is definitely going 
            // to get a map entry, so we don't need to worry about the WithAdditionalFlags
            // binder being dropped.  That is, there's no point in adding the WithAdditionalFlags
            // binder to the map ourselves and having VisitBlock unconditionally overwrite it.

            // If this finally block is nested inside a catch block, we need to use a distinct
            // binder flag so that we can detect the nesting order for error CS074: A throw
            // statement with no arguments is not allowed in a finally clause that is nested inside
            // the nearest enclosing catch clause.

            var additionalFlags = BinderFlags.InFinallyBlock;
            if (_enclosing.Flags.Includes(BinderFlags.InCatchBlock))
            {
                additionalFlags |= BinderFlags.InNestedFinallyBlock;
            }

            Visit(node.Block, _enclosing.WithAdditionalFlags(additionalFlags));

            Binder finallyBinder;
            Debug.Assert(_map.TryGetValue(node.Block, out finallyBinder) && finallyBinder.Flags.Includes(BinderFlags.InFinallyBlock));
        }
Esempio n. 27
0
        public static SyntaxNode TryCatchFinallySafe(this SyntaxNode subjectStatement, Dictionary <string, TypeSyntax> exceptionArgumentsInfo,
                                                     Func <IdentifierNameSyntax, SyntaxList <StatementSyntax> > generateCatchStatement, SyntaxNode root, SemanticModel model, FinallyClauseSyntax finallyClauseSyntax,
                                                     CancellationToken cancellationToken = default(CancellationToken))
        {
            var containingMethod = subjectStatement.GetContainingMemberDeclaration();

            var tryTextSpan = containingMethod.GetSpanOfAssignmentDependenciesInSpan(
                subjectStatement.FullSpan, model, cancellationToken);

            var tryBlockStatements = containingMethod.SyntaxTree
                                     .GetRoot(cancellationToken).DescendantNodes()
                                     .Where(x => tryTextSpan.Contains(x.Span))
                                     .OfType <StatementSyntax>().ToArray();

            var catchClauses = exceptionArgumentsInfo.Select(i =>
            {
                var exceptionArgumentIdentifierName = SyntaxFactory.IdentifierName(i.Key);
                return(SyntaxFactory.CatchClause()
                       .WithDeclaration(
                           SyntaxFactory.CatchDeclaration(i.Value)
                           .WithIdentifier(exceptionArgumentIdentifierName.Identifier))
                       .WithBlock(SyntaxFactory.Block(generateCatchStatement(exceptionArgumentIdentifierName))
                                  ));
            });

            var tryStatement = SyntaxFactory.TryStatement(
                SyntaxFactory.Block(SyntaxFactory.List(tryBlockStatements)),
                SyntaxFactory.List(catchClauses),
                finallyClauseSyntax)
                               .WithAdditionalAnnotations(Formatter.Annotation);

            var newMethod = containingMethod.ReplaceNodes(tryBlockStatements.ToImmutableList(), tryStatement);

            var compilationUnitSyntax = (CompilationUnitSyntax)root.ReplaceNode(containingMethod, newMethod);

            var usings = compilationUnitSyntax.Usings;

            if (!usings.Any(e => e.Name.ToString().Equals(typeof(Task).Namespace)))
            {
                var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(typeof(Task).Namespace));
                compilationUnitSyntax = compilationUnitSyntax.AddUsings(usingDirective);
            }
            if (!usings.Any(e => e.Name.ToString().Equals(typeof(Debug).Namespace)))
            {
                var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(typeof(Debug).Namespace));
                compilationUnitSyntax = compilationUnitSyntax.AddUsings(usingDirective);
            }
            return(compilationUnitSyntax);
        }