Esempio n. 1
0
        public static void LoadByTxtFile(string folderPath)
        {
            string txtFilePath = IOFile.CompleteFileNameInput("AllSource.txt");

            Logger.Log("Load from txt file: " + txtFilePath);

            string content = "";

            try
            {
                using (StreamReader sr = new StreamReader(txtFilePath))
                {
                    content = sr.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Logger.Log("Txt file may not exist.");
                Logger.Log(e);
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }

            var tree            = CSharpSyntaxTree.ParseText(content);
            var model           = GetSemanticInfo(tree);
            var treeAndModelDic = new Dictionary <SyntaxTree, SemanticModel>();

            treeAndModelDic.Add(tree, model);
            var compilation = BuildCompilation(new List <SyntaxTree> {
                tree
            });

            CodeAnalyzer.AnalyzeAllTrees(treeAndModelDic, compilation);
        }
Esempio n. 2
0
        public static void LoadByFolder(string folderPath)
        {
            Logger.Log("Loading from folder: " + folderPath);
            IEnumerable <string> FileNames = Directory.EnumerateFiles(folderPath, "*.cs",
                                                                      SearchOption.AllDirectories).Where(file => !file.Contains("Test"));

            int numFiles = FileNames.Count();

            Logger.Log("Loading " + numFiles + " *.cs files.");
            // parallelization
            var treeAndModelList = FileNames.AsParallel()
                                   .Select(fileName => LoadSourceFile(fileName))
                                   .ToList();

            var treeAndModelDic = new Dictionary <SyntaxTree, SemanticModel>();

            foreach (var treeAndModel in treeAndModelList)
            {
                treeAndModelDic.Add(treeAndModel.Item1, treeAndModel.Item2);
            }
            var compilation = BuildCompilation(treeAndModelDic.Keys.ToList());

            Logger.Log("Compilation built.");
            CodeAnalyzer.AnalyzeAllTrees(treeAndModelDic, compilation);
        }
Esempio n. 3
0
        public CodeStatistics(List <Tuple <SyntaxTree, TreeStatistics> > codeStatsList)
        {
            TreeStats   = codeStatsList;
            CatchBlocks = new CatchDic();
            //APICalls = new CallDic();
            PossibleExceptionsBlocks = new PossibleExceptionsDic();
            CodeStats = new Dictionary <string, int>();
            foreach (var treetuple in codeStatsList)
            {
                if (treetuple == null)
                {
                    continue;
                }
                if (treetuple.Item2.CatchBlockList != null)
                {
                    CatchBlocks.Add(treetuple.Item2.CatchBlockList);
                }
                //if (treetuple.Item2.APICallList != null)
                //{
                //    APICalls.Add(treetuple.Item2.APICallList);
                //}
                if (treetuple.Item2.PossibleExceptionsBlockList != null)
                {
                    PossibleExceptionsBlocks.Add(treetuple.Item2.PossibleExceptionsBlockList);
                }
                if (treetuple.Item2.CodeStats != null)
                {
                    CodeAnalyzer.MergeDic <String>(ref CodeStats, treetuple.Item2.CodeStats);
                }
            }

            if (CatchBlocks.Count > 0)
            {
                CodeStats["NumBinded"]                     = CatchBlocks.NumBinded;
                CodeStats["NumMethodsNotBinded"]           = CatchBlocks.NumMethodsNotBinded;
                CodeStats["NumLoggedCatchBlock"]           = CatchBlocks.NumLogged;
                CodeStats["NumDistinctExceptionTypeCatch"] = CatchBlocks.Count;
                CodeStats["NumRecoveredBinding"]           = CatchBlocks.NumRecoveredBinding;
                CodeStats["NumNoDeclaration"]              = CatchBlocks.NumNoDeclaration;
            }

            if (PossibleExceptionsBlocks.Count > 0)
            {
                CodeStats["NumDistinctPossibleExceptions"] = PossibleExceptionsBlocks.Count;
            }

            //CodeStats["NumCallType"] = APICalls.Count;
            //CodeStats["NumAPICall"] = APICalls.NumAPICall;
            //CodeStats["NumLoggedAPICall"] = APICalls.NumLogged;
        }