Exemplo 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);
        }
Exemplo n.º 2
0
        public static void ExtractFromSolution(string repositoryPath, string githubPath, Solution solution,
                                               string saveDir, string typeToCluster = "string")
        {
            Func <string, int, string> pathProcessor = (fullPath, lineNumber) =>
            {
                var basePath     = repositoryPath;
                var relativePath = fullPath.Substring(basePath.Length);
                var githubLink   = githubPath + relativePath.Replace('\\', '/') + "#L" + (lineNumber + 1);
                return(githubLink);
            };

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

            var projectGraph = solution.GetProjectDependencyGraph();
            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"))
                    {
                        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;
                }
                foreach (var error in compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error))
                {
                    Console.WriteLine(error.GetMessage());
                }
                if (compilation is CSharpCompilation cSharpCompilation)
                {
                    typeRelations.AddFromCompilation(cSharpCompilation);
                    compilations.Add(cSharpCompilation);
                }
            }

            var extractor = new ClusteringExtractor(new HashSet <string> {
                typeToCluster
            }, typeRelations);

            var(clusters, clusterParents) = extractor.InferColors();
            ClusteringSerializerUtil.SerializeClustering(saveDir, repositoryPath, clusters, clusterParents);
        }
Exemplo n.º 3
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!");
        }