Ejemplo n.º 1
0
        public static void Rewrite(string basePath, string githubBaseLink, Solution solution,
                                   string serializedClustersFile, string targetTypeName)
        {
            Func <string, int, string> pathProcessor = (fullPath, lineNumber) =>
            {
                var relativePath = fullPath.Substring(basePath.Length);
                return(githubBaseLink + relativePath.Replace('\\', '/') + "#L" + (lineNumber + 1));
            };

            Console.WriteLine("Collecting type constraint graph...");
            var typeRelations = new TypeConstraints(pathProcessor);

            var     projectGraph = solution.GetProjectDependencyGraph();
            UDCTree UDCHierarchy = new UDCTree();
            var     compilations = new List <CSharpCompilation>();

            foreach (var projectId in projectGraph.GetTopologicallySortedProjects())
            {
                Compilation compilation;
                try
                {
                    var project = solution.GetProject(projectId);
                    if (project.FilePath.ToLower().Contains("test") ||
                        !(project.FilePath.ToLower().EndsWith(".csproj")))
                    {
                        Console.WriteLine($"Excluding {project.FilePath} since it seems to be test-related");
                        continue;
                    }
                    compilation = project.GetCompilationAsync().Result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception while compiling project {0}: {1}", projectId, ex);
                    continue;
                }
                if (compilation == null)
                {
                    continue;
                }
                foreach (var error in compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error))
                {
                    Console.WriteLine(error.GetMessage());
                }
                if (compilation is CSharpCompilation cSharpCompilation)
                {
                    UDCHierarchy.ParseTypesInCompilation(cSharpCompilation);
                    typeRelations.AddFromCompilation(cSharpCompilation);
                    compilations.Add(cSharpCompilation);
                }
            }

            Console.WriteLine("Starting rewriting of builtin type uses after reading in serialized clusterings...");
            RewriterFromJson rJson = new RewriterFromJson(serializedClustersFile, basePath, typeRelations);
            var r = new Rewriter(compilations, rJson.Clusters, rJson.AncestorMap, targetTypeName);

            r.RewriteTypes();
            string errors = JsonConvert.SerializeObject(r.ErrorHistogram, Formatting.Indented);

            Console.WriteLine(errors);
        }
Ejemplo n.º 2
0
        public void startInference(UDCTree UDCHierarchy, int depth = 0)
        {
            SubtypeMiner miner        = null;
            var          typesAtDepth = UDCHierarchy.GetAllTypesAtDepth(depth);

            foreach (var v in typesAtDepth)
            {
                UDCTree subTree = new UDCTree(v);
                Console.WriteLine("Running Inference for {0} ...", v.Value.MetadataName);
                var typeNamesOfDescendants = UDCHierarchy.GetTypeNamesOfDescendants(v);
                miner = new SubtypeMiner(new HashSet <string>(typeNamesOfDescendants), _typeRelations, 50);
                SubTreeResults res = new SubTreeResults(subTree, miner);
                MiningResults.Add(res);

                if (miner.NumRelationships == 0)
                {
                    continue;
                }
                res.computeScores();
                try
                {
                    _typeRelations.ToDot("results/" + v.Value.ToDisplayString() + ".dot", _pathProcessor, res.clusteringResult);
                    writeResultsToDisk(_pathProcessor, res, ResultsDirectory + v.Value.ToDisplayString() + "_result.txt");
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                    continue;
                }
            }
        }
Ejemplo n.º 3
0
        public void startTypeSpecificNameFlowInference(UDCTree UDCHierarchy, int depth = 0)
        {
            var          typeLocationDict = UDCHierarchy.getUDTLineSpans();
            SubtypeMiner miner            = null;
            var          typesAtDepth     = UDCHierarchy.GetAllTypesAtDepth(depth);

            foreach (var v in UDCHierarchy.getAllTypes())
            {
                if (v.Value.Locations.Length == 0 || !v.Value.Locations.All(x => x.IsInSource))
                {
                    continue;
                }
                UDCTree subTree = new UDCTree(v);
                Console.WriteLine("Running Inference for {0} ...", v.Value.MetadataName);
                miner = new SubtypeMiner(null, _typeRelations, 50, true, v.Value);
                SubTreeResults res = new SubTreeResults(subTree, miner);

                try {
                    MiningResults.Add(res);
                    if (miner.NumRelationships == 0)
                    {
                        continue;
                    }
                    res.computeScores();
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                    continue;
                }
                try
                {
                    //_typeRelations.ToDot(ResultsDirectory + v.Value.ToDisplayString() + ".dot", _pathProcessor, res.clusteringResult);
                    writeResultsToDisk(_pathProcessor, res, ResultsDirectory + v.Value.ToDisplayString() + "_result.txt", miner.GetLineSpanOfType(v.Value));
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                    continue;
                }
            }
        }
Ejemplo n.º 4
0
        public void startConsolidatedInference(UDCTree UDCHierarchy, int depth = 0)
        {
            SubtypeMiner  miner        = null;
            List <string> alltypes     = new List <string>();
            var           typesAtDepth = UDCHierarchy.GetAllTypesAtDepth(depth);

            foreach (var v in typesAtDepth)
            {
                List <string> typeNamesOfDescendants = new List <string>();
                Console.WriteLine("Running Inference for {0} ...", v.Value.MetadataName);
                try{
                    typeNamesOfDescendants = UDCHierarchy.GetTypeNamesOfDescendants(v);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                alltypes = alltypes.Concat(typeNamesOfDescendants).ToList();
            }

            alltypes = randomUniformSample(alltypes);
            UDCTree subTree = new UDCTree(typesAtDepth.First());

            miner = new SubtypeMiner(new HashSet <string>(alltypes), _typeRelations, 50);
            SubTreeResults res = new SubTreeResults(subTree, miner);

            MiningResults.Add(res);

            if (miner.NumRelationships > 0)
            {
                res.computeScores();
                try
                {
                    //_typeRelations.ToDot(ResultsDirectory + "consolidated.dot", _pathProcessor, res.clusteringResult);
                    writeResultsToDisk(_pathProcessor, res, ResultsDirectory + "consolidated" + "_result.txt");
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                }
            }
        }
Ejemplo n.º 5
0
        public static void EvaluateOnBuiltIns(
            List <CSharpCompilation> compilations, Func <string, int, string> pathProcessor,
            TypeConstraints typeRelations, string results_path, bool deserializeClusters = false,
            string slnName = null)
        {
            Dictionary <string, Tuple <string, string> > serialisedClusters = new Dictionary <string, Tuple <string, string> >();

            void bootstrapSerialisedClusters()
            {
                serialisedClusters["BEPUphysics.sln"]           = new Tuple <string, string>(@"E:\bepuphysics1", @"F:\clusterings\bepuphysics-float.json");
                serialisedClusters["Microsoft.Bot.Builder.sln"] = new Tuple <string, string>(@"E:\BotBuilder\CSharp\", @"F:\clusterings\botbuilder-str.json");
                serialisedClusters["CommandLine.sln"]           = new Tuple <string, string>(@"E:\commandline\", @"F:\clusterings\commandline-str.json");
                serialisedClusters["CommonMark.sln"]            = new Tuple <string, string>(@"E:\CommonMark.NET\", @"F:\clusterings\commonmark-str.json");
                serialisedClusters["Hangfire.sln"]              = new Tuple <string, string>(@"E:\Hangfire\", @"F:\clusterings\hangfire-str.json");
                serialisedClusters["Humanizer.sln"]             = new Tuple <string, string>(@"E:\Humanizer\", @"F:\clusterings\humanizer-str.json");
                serialisedClusters["QuantConnect.Lean.sln"]     = new Tuple <string, string>(@"E:\Lean\", @"F:\clusterings\lean-str.json");
                serialisedClusters["Nancy.sln"]                 = new Tuple <string, string>(@"E:\Nancy\", @"F:\clusterings\nancy-str.json");
                serialisedClusters["Newtonsoft.Json.Net40.sln"] = new Tuple <string, string>(@"E:\Newtonsoft.Json\", @"F:\clusterings\newtonsoft-str.json");
                serialisedClusters["Ninject.sln"]               = new Tuple <string, string>(@"E:\Ninject\", @"F:\clusterings\ninject-str.json");
                serialisedClusters["NLog.sln"]      = new Tuple <string, string>(@"E:\NLog\", @"F:\clusterings\nlog-str.json");
                serialisedClusters["Quartz.sln"]    = new Tuple <string, string>(@"E:\quartznet\", @"F:\clusterings\quartznet-str.json");
                serialisedClusters["RavenDB.sln"]   = new Tuple <string, string>(@"E:\ravendb\", @"F:\clusterings\ravendb-str.json");
                serialisedClusters["RestSharp.sln"] = new Tuple <string, string>(@"E:\RestSharp\", @"F:\clusterings\restsharp-str.json");
                serialisedClusters["Wox.sln"]       = new Tuple <string, string>(@"E:\Wox\", @"F:\clusterings\wox-str.json");
            }

            bootstrapSerialisedClusters();

            string  typename  = "string";
            UDCTree hierarchy = null;

            foreach (var compilation in compilations)
            {
                ITypeSymbol t = compilation.GetTypeByMetadataName(typename);
                if (t != null)
                {
                    hierarchy = new UDCTree(t);
                    break;
                }
            }

            Rewriter r;

            if (deserializeClusters)
            {
                var serializedClustersFile = serialisedClusters[slnName].Item2;
                var repoPath           = serialisedClusters[slnName].Item1;
                RewriterFromJson rJson = new RewriterFromJson(serializedClustersFile, repoPath, typeRelations);
                r = new Rewriter(compilations, rJson.Clusters, rJson.AncestorMap, typename);
            }
            else
            {
                GodClassResults results = new GodClassResults(pathProcessor, typeRelations, results_path);
                results.startInference(hierarchy);
                r = new Rewriter(compilations, results.MiningResults.First().clusteringResult, results.MiningResults.First().ancestorMap, typename);
            }

            r.RewriteTypes();
            string errors = JsonConvert.SerializeObject(r.ErrorHistogram, Formatting.Indented);

            try
            {
                if (!Directory.Exists(results_path))
                {
                    Directory.CreateDirectory(results_path);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                System.Environment.Exit(-1);
            }

            System.IO.DirectoryInfo di = new DirectoryInfo(results_path);

            foreach (FileInfo f in di.GetFiles())
            {
                f.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true);
            }
            System.IO.StreamWriter file = new System.IO.StreamWriter(results_path + "/" + typename + "_rewriting_errors.txt");
            file.WriteLine(errors);
            file.Close();
        }
Ejemplo n.º 6
0
        public static void ExtractFromSolution(string basePath, string githubBaseLink, Solution solution)
        {
            Func <string, int, string> pathProcessor = (fullPath, lineNumber) =>
            {
                var relativePath = fullPath.Substring(basePath.Length);
                return(githubBaseLink + relativePath.Replace('\\', '/') + "#L" + (lineNumber + 1));
            };


            Console.WriteLine("Collecting type constraint graph...");
            var typeRelations = new TypeConstraints(pathProcessor);

            var     projectGraph = solution.GetProjectDependencyGraph();
            UDCTree UDCHierarchy = new UDCTree();
            var     compilations = new List <CSharpCompilation>();

            foreach (var projectId in projectGraph.GetTopologicallySortedProjects())
            {
                Compilation compilation;
                try
                {
                    var project = solution.GetProject(projectId);
                    if (project.FilePath.ToLower().Contains("test") ||
                        !(project.FilePath.ToLower().EndsWith(".csproj")))
                    {
                        Console.WriteLine($"Excluding {project.FilePath} since it seems to be test-related");
                        continue;
                    }
                    compilation = project.GetCompilationAsync().Result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception while compiling project {0}: {1}", projectId, ex);
                    continue;
                }
                if (compilation == null)
                {
                    continue;
                }
                foreach (var error in compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error))
                {
                    Console.WriteLine(error.GetMessage());
                }
                if (compilation is CSharpCompilation cSharpCompilation)
                {
                    UDCHierarchy.ParseTypesInCompilation(cSharpCompilation);
                    typeRelations.AddFromCompilation(cSharpCompilation);
                    compilations.Add(cSharpCompilation);
                }
            }

            //AnalysisType t = AnalysisType.TypeSpecificFlowClustering;

            var analysisTriggers = new List <AnalysisType> {
                //AnalysisType.UDCSanityCheck,
                //AnalysisType.TypeSpecificFlowClustering,
                AnalysisType.RewritingFromSerialization,
            };

            foreach (var i in analysisTriggers)
            {
                switch (i)
                {
                case AnalysisType.TypeSpecificFlowClustering:
                    var TSFresults_path = "results/" + "TypeSpecificClustering/" + basePath.Substring(basePath.LastIndexOf('\\') + 1);
                    Console.WriteLine("Starting coloring for name flows in individual user defined types...");
                    GodClassResults typeSpecificClustering = new GodClassResults(pathProcessor, typeRelations, TSFresults_path);
                    typeSpecificClustering.startTypeSpecificNameFlowInference(UDCHierarchy, 0);
                    break;

                case AnalysisType.UDCSanityCheck:
                    var sanityResults_path = "results/" + "UDCSanityCheck/" + basePath.Substring(basePath.LastIndexOf('\\') + 1);
                    Console.WriteLine("Starting coloring for sanity checking on user defined types...");
                    GodClassResults sanityCheck = new GodClassResults(pathProcessor, typeRelations, sanityResults_path);
                    sanityCheck.startConsolidatedInference(UDCHierarchy, 0);
                    break;

                case AnalysisType.Rewriting:
                    var rewritingResults_path = "results/" + "rewriting/" + basePath.Substring(basePath.LastIndexOf('\\') + 1);
                    Console.WriteLine("Starting rewriting of builtin type uses...");
                    EvaluateOnBuiltIns(compilations, pathProcessor, typeRelations, rewritingResults_path, false);
                    break;

                case AnalysisType.RewritingFromSerialization:
                    var rewritingFromSerializationResults_path = "results/" + "rewritingFromSerialised/" + basePath.Substring(basePath.LastIndexOf('\\') + 1);
                    Console.WriteLine("Starting rewriting of builtin type uses after reading in serilised clusterings...");
                    EvaluateOnBuiltIns(compilations, pathProcessor, typeRelations, rewritingFromSerializationResults_path, true,
                                       basePath.Substring(basePath.LastIndexOf('\\') + 1));
                    break;

                default:
                    break;
                }
            }
            Console.WriteLine("Done!");
        }
Ejemplo n.º 7
0
 public SubTreeResults(UDCTree subTree, SubtypeMiner miner)
 {
     SubTree = subTree;
     _miner  = miner;
 }