Beispiel #1
0
        public static void GetAllMethodDeclarations(this SyntaxTree tree,
                                                    Dictionary <SyntaxTree, SemanticModel> treeAndModelDic, Compilation compilation)
        {
            var root             = tree.GetRoot();
            var methodDeclarList = root.DescendantNodes().OfType <BaseMethodDeclarationSyntax>();

            foreach (var method in methodDeclarList)
            {
                var methodName = ASTUtilities.GetNodeDeclaredSymbol(method, tree, treeAndModelDic, compilation).ToString();

                if (methodName != null)
                {
                    AllMyMethods.TryAdd(methodName, new MyMethod(methodName, method));
                }
            }
        }
Beispiel #2
0
        private static void getExceptionFlows(List <PossibleExceptionsBlock> possibleExceptionsList, HashSet <ClosedExceptionFlow> closedExceptionFlows, Compilation compilation)
        {
            foreach (ClosedExceptionFlow flow in closedExceptionFlows)
            {
                PossibleExceptionsBlock possibleExceptionsBlockInfo = new PossibleExceptionsBlock();

                possibleExceptionsBlockInfo.ExceptionType     = flow.getThrownTypeName();
                possibleExceptionsBlockInfo.CaughtType        = flow.getCaughtTypeName();
                possibleExceptionsBlockInfo.DeclaringMethod   = flow.getOriginalMethodBindingKey();
                possibleExceptionsBlockInfo.InvokedMethod     = flow.getInvokedMethodKey();
                possibleExceptionsBlockInfo.InvokedMethodLine = flow.getInvokedMethodLine();
                possibleExceptionsBlockInfo.FilePath          = flow.getCatchFilePath();
                possibleExceptionsBlockInfo.StartLine         = flow.getCatchStartLine();

                possibleExceptionsBlockInfo.MetaInfo["FilePath"]  = flow.getCatchFilePath();
                possibleExceptionsBlockInfo.MetaInfo["StartLine"] = flow.getCatchStartLine().ToString();

                int kind;
                if (flow.getThrownType() != null)
                {
                    kind = ASTUtilities.FindKind(flow.getThrownType(), compilation);
                }
                else
                {
                    kind = ASTUtilities.FindKind(flow.getThrownTypeName(), compilation);
                }
                possibleExceptionsBlockInfo.OperationFeatures["Kind"] = kind;

                possibleExceptionsBlockInfo.OperationFeatures["IsDocSemantic"] = flow.getIsDocSemantic() ? 1 : 0;
                possibleExceptionsBlockInfo.OperationFeatures["IsDocSyntax"]   = flow.getIsDocSyntax() ? 1 : 0;
                possibleExceptionsBlockInfo.OperationFeatures["IsThrow"]       = flow.getIsThrow() ? 1 : 0;
                possibleExceptionsBlockInfo.OperationFeatures["LevelFound"]    = (int)flow.getLevelFound();

                possibleExceptionsBlockInfo.OperationFeatures["HandlerTypeCode"] = (int)flow.getHandlerTypeCode();


                //possibleExceptionsBlockInfo.MetaInfo.put("PossibleExceptionsBlock", node.toString());

                possibleExceptionsList.Add(possibleExceptionsBlockInfo);

                //Logger.Log("Possible Exceptions block info registered.");
            }
        }
        private void FindSymbolInProject(string unitname)
        {
            //ThreadHelper.ThrowIfNotOnUIThread();
            //DTE2 dte = _serviceProvider.GetService(typeof(DTE)) as DTE2;
            //var projects = dte.ActiveSolutionProjects;
            var currentPath = _textBuffer.GetTextDocument().FilePath;

            IList <SLang.DECLARATION> decls = ASTUtilities.GetUnitsAndStandalones(_textBuffer);

            foreach (var decl in decls)
            {
                if (decl.name.identifier.Equals(unitname))
                {
                    NavigateTo(currentPath, decl.span.begin.line - 1, decl.span.begin.pos - 2);
                    return;
                }
            }
            showInfoMessage("Definition Not Found.");
        }
Beispiel #4
0
        //TODO: method that calculate handlerType even if no binding
        public static sbyte calculateHandlerTypeCode(INamedTypeSymbol caughtType, INamedTypeSymbol thrownType)
        {
            sbyte handlerTypeCode = -9;

            if (caughtType != null)
            {
                if (thrownType != null)
                {
                    //In case is the same type, it's specific handler type - code: 0
                    //In case the caught type is equal a super class of the possible thrown type, it's a subsumption - code: 1
                    //In case the possible thrown type is equal a super class of the caught type, it's a supersumption - code: 2
                    //In case it's none of the above - most likely tree of unrelated exceptions: code: 3
                    if (caughtType.Equals(thrownType) ||
                        (caughtType.BaseType != null && thrownType.BaseType != null &&
                         caughtType.BaseType.Equals(thrownType.BaseType) &&
                         caughtType.MetadataName.Equals(thrownType.MetadataName)
                        )
                        )
                    {
                        handlerTypeCode = 0;
                    }
                    else if (ASTUtilities.IsSuperType(caughtType, thrownType))
                    {
                        handlerTypeCode = 1;
                    }
                    else if (ASTUtilities.IsSuperType(thrownType, caughtType))
                    {
                        handlerTypeCode = 2;
                    }
                    else
                    {
                        //it can happen when exceptions are not related on the type tree
                        handlerTypeCode = 3;
                    }
                }
                else
                {
                    handlerTypeCode = -8;
                }
            }
            return(handlerTypeCode);
        }
Beispiel #5
0
 public static bool IsRecoverStatement(SyntaxNode statement, SemanticModel semanticModel)
 {
     if (!IsLoggingStatement(statement) && !IsSetLogicFlagStatement(statement) && !IsThrow(statement))
     {
         var recoverStatementSet = statement.DescendantNodes().OfType <IdentifierNameSyntax>();
         foreach (var recoverStatement in recoverStatementSet)
         {
             try
             {
                 var    symbol   = ASTUtilities.GetAnySymbol(semanticModel.GetSymbolInfo(recoverStatement)) as ILocalSymbol;
                 string typeName = symbol.Type.ToString();
                 if (typeName.Contains("Exception"))
                 {
                     // To check
                     return(true);
                 }
             }
             catch { }
         }
     }
     return(false);
 }
Beispiel #6
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);
        }