Exemple #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);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            string inputMode = args[0];
            string filePath  = args[1];

            if (filePath.EndsWith("\\"))
            {
                filePath = filePath.Remove(filePath.LastIndexOf('\\'));
            }
            IOFile.InputFolderPath  = Path.GetFullPath(filePath);
            IOFile.OutputFolderPath = Directory.GetCurrentDirectory();

            Logger.Initialize();
            DateTime StartTime = DateTime.Now;

            Logger.Log("Current directory is: " + IOFile.OutputFolderPath.ToString());


            Config.Load(IOFile.CompleteFileNameInput("Config.txt"));

            // traverse all the code folder for pattern check
            CodeWalker walker = new CodeWalker();

            walker.LoadByInputMode(inputMode, filePath);

            DateTime EndTime = DateTime.Now;

            Logger.Log("All done. Elapsed time: " + (EndTime - StartTime).ToString());
            Logger.Log("Press any key to terminate!");
            Logger.Close();
            //Console.ReadKey();
        }
Exemple #3
0
        public static bool IsToDo(SyntaxNode codeSnippet)
        {
            if (codeSnippet is CatchClauseSyntax)
            {
                codeSnippet = (codeSnippet as CatchClauseSyntax).Block;
            }
            bool bIsToDo = false;

            var commentList = codeSnippet.DescendantTrivia()
                              .Where(childNode => childNode.IsKind(SyntaxKind.SingleLineCommentTrivia) ||
                                     childNode.IsKind(SyntaxKind.MultiLineCommentTrivia));

            foreach (var comment in commentList)
            {
                string updatedComment = IOFile.DeleteSpace(comment.ToString());
                updatedComment = Regex.Replace(updatedComment, "<.*>", "");
                updatedComment = Regex.Replace(updatedComment, "{.*}", "");
                updatedComment = Regex.Replace(updatedComment, "\\(.*\\)", "");
                updatedComment = updatedComment.ToUpper();

                bIsToDo = updatedComment.Contains("TODO") || updatedComment.Contains("FIXME");
            }

            return(bIsToDo);
        }
Exemple #4
0
        /// <summary>
        /// To check whether an invocation is a logging statement
        /// </summary>
        static public bool IsLoggingStatement(SyntaxNode statement)
        {
            string logging = IOFile.MethodNameExtraction(statement.ToString());

            if (logging == null)
            {
                return(false);
            }

            foreach (string notlogmethod in Config.NotLogMethods)
            {
                if (notlogmethod == "")
                {
                    break;
                }
                if (logging.IndexOf(notlogmethod) > -1)
                {
                    return(false);
                }
            }
            foreach (string logmethod in Config.LogMethods)
            {
                if (logging.IndexOf(logmethod) > -1)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #5
0
 public static void Initialize()
 {
     LogFileName = IOFile.CompleteFileNameOutput(
         DateTime.Today.Date.ToShortDateString().Replace("/", "") + ".log");
     LogWriter = File.AppendText(LogFileName);
     Log("-------------------------------------------------------");
     Log("-------------------------------------------------------");
     Log("New Task.");
 }
Exemple #6
0
        public string PrintMetaInfo()
        {
            string metaInfo = null;

            foreach (var key in MetaInfo.Keys)
            {
                metaInfo += (IOFile.DeleteSpace(MetaInfo[key]) + Splitter);
            }
            return(metaInfo);
        }
Exemple #7
0
        /// <summary>
        /// Analyze the code by all trees and semantic models
        /// </summary>
        /// <param name="treeList"></param>
        /// <param name="compilation"></param>
        public static void AnalyzeAllTrees(Dictionary <SyntaxTree, SemanticModel> treeAndModelDic,
                                           Compilation compilation)
        {
            // statistics
            int numFiles = treeAndModelDic.Count;
            var treeNode = treeAndModelDic.Keys
                           .Select(tree => tree.GetRoot().DescendantNodes().Count());

            // analyze every tree simultaneously
            treeAndModelDic.Keys.AsParallel()
            .ForAll(tree => GetAllMethodDeclarations(tree, treeAndModelDic, compilation));

            //foreach (var methoddeclar in allMethodDeclarations)
            //{
            //	MergeDic(ref AllMyMethods, methoddeclar);
            //}
            Logger.Log("Cached all method declarations.");

            var codeStatsList = treeAndModelDic.Keys.AsParallel()
                                .Select(tree => AnalyzeATree(tree, treeAndModelDic, compilation)).ToList();
            CodeStatistics allStats = new CodeStatistics(codeStatsList);

            // Log statistics
            Logger.Log("Num of syntax nodes: " + treeNode.Sum());
            Logger.Log("Num of source files: " + numFiles);

            allStats.CodeStats["NumFiles"]                       = numFiles;
            allStats.CodeStats["NumDeclaredMethods"]             = AllMyMethods.Count;
            allStats.CodeStats["NumInvokedMethods"]              = InvokedMethods.Count;
            allStats.CodeStats["NumInvokedMethodsBinded"]        = InvokedMethods.Values.Count(method => method.isBinded());
            allStats.CodeStats["NumInvokedMethodsDeclared"]      = InvokedMethods.Values.Count(method => method.isDeclared());
            allStats.CodeStats["NumInvokedMethodsExtDocPresent"] = InvokedMethods.Values.Count(method => method.isExternalDocPresent());

            allStats.PrintSatistics();

            // Save all the source code into a txt file
            bool saveAllSource = false;

            if (saveAllSource == true)
            {
                var sb = new StringBuilder(treeAndModelDic.Keys.First().Length *numFiles);                  //initial length
                foreach (var stat in codeStatsList)
                {
                    sb.Append(stat.Item1.GetText());
                }
                string txtFilePath = IOFile.CompleteFileNameOutput("AllSource.txt");
                using (StreamWriter sw = new StreamWriter(txtFilePath))
                {
                    sw.Write(sb.ToString());
                }
            }
        }
Exemple #8
0
        static public bool IsGetCauseStatement(SyntaxNode statement)
        {
            string getCause = IOFile.MethodNameExtraction(statement.ToString());

            if (getCause == null)
            {
                return(false);
            }

            string[] getCauseMethods = { "InnerException" };

            foreach (string getCauseMethod in getCauseMethods)
            {
                if (getCause.IndexOf(getCauseMethod) > -1)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #9
0
        /// <summary>
        /// To check whether an invocation is a logging statement
        /// </summary>
        static public bool IsAbortStatement(SyntaxNode statement)
        {
            string aborting = IOFile.MethodNameExtraction(statement.ToString());

            if (aborting == null)
            {
                return(false);
            }

            string[] abortMethods = { "Exit", "Abort" };

            foreach (string abortMethod in abortMethods)
            {
                if (aborting.IndexOf(abortMethod) > -1)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #10
0
        public void PrintToFileCSV()
        {
            Logger.Log("Writing CatchBlock features into file...");
            StreamWriter csvSW     = new StreamWriter(IOFile.CompleteFileNameOutput("PossibleExceptionsBlock.csv"));
            StreamWriter metaCSVSW = new StreamWriter(IOFile.CompleteFileNameOutput("PossibleExceptionsBlock_Meta.csv"));

            int    catchId = 0;
            string metaKey = "";

            foreach (var meta in PossibleExceptionsBlock.MetaKeys)
            {
                metaKey += (meta + ",");
            }

            string OpFeaturesKey = "";

            foreach (var OpFeature in PossibleExceptionsBlock.OpFeaturesKeys)
            {
                OpFeaturesKey += (OpFeature + ",");
            }

            csvSW.WriteLine("id," + OpFeaturesKey + "ThrownType,CaughtType,DeclaringMethod,InvokedMethod,InvokedMethodLine,CatchFilePath,CatchStartLine");
            metaCSVSW.WriteLine("id," + metaKey);

            foreach (string exception in this.Keys)
            {
                PossibleExceptionsList possibleExceptionsList = this[exception];
                foreach (var possibleExceptionsBlock in possibleExceptionsList)
                {
                    catchId++;
                    csvSW.WriteLine(catchId + "," + possibleExceptionsBlock.PrintFeaturesCSV());
                    metaCSVSW.WriteLine(catchId + "," + possibleExceptionsBlock.PrintMetaInfoCSV());
                }
                csvSW.Flush();
                metaCSVSW.Flush();
            }

            csvSW.Close();
            metaCSVSW.Close();
            Logger.Log("Writing done.");
        }
Exemple #11
0
        public void PrintToFileCSV()
        {
            Logger.Log("Writing CatchBlock features into file...");
            StreamWriter csvSW     = new StreamWriter(IOFile.CompleteFileNameOutput("CatchBlock.csv"));
            StreamWriter metaCSVSW = new StreamWriter(IOFile.CompleteFileNameOutput("CatchBlock_Meta.csv"));

            int    catchId = 0;
            string metaKey = "";

            foreach (var meta in CatchBlock.MetaKeys)
            {
                metaKey += (meta + ",");
            }

            string OpFeaturesKey = "";

            foreach (var OpFeature in CatchBlock.OpFeaturesKeys)
            {
                OpFeaturesKey += (OpFeature + ",");
            }

            csvSW.WriteLine("id," + OpFeaturesKey + "ExceptionType,ParentMethod,ParentType,FilePath,StartLine");
            metaCSVSW.WriteLine("id," + metaKey);

            foreach (string exception in this.Keys)
            {
                CatchList catchList = this[exception];
                foreach (var catchblock in catchList)
                {
                    catchId++;
                    csvSW.WriteLine(catchId + "," + catchblock.PrintFeaturesCSV());
                    metaCSVSW.WriteLine(catchId + "," + catchblock.PrintMetaInfoCSV());
                }
                csvSW.Flush();
                metaCSVSW.Flush();
            }

            csvSW.Close();
            metaCSVSW.Close();
            Logger.Log("Writing done.");
        }