public void Distribution()
        {
            var distribution = new Distribution(0, 10, 20);
            distribution.Ranges.Should().BeEquivalentTo(0, 10, 20);
            distribution.Values.Should().BeEquivalentTo(0, 0, 0);
            distribution.ToString().Should().BeEquivalentTo("0=0;10=0;20=0");

            distribution.Add(0);
            distribution.Values.Should().BeEquivalentTo(1, 0, 0);
            distribution.ToString().Should().BeEquivalentTo("0=1;10=0;20=0");

            distribution.Add(9);
            distribution.Values.Should().BeEquivalentTo(2, 0, 0);
            distribution.ToString().Should().BeEquivalentTo("0=2;10=0;20=0");

            distribution.Add(12);
            distribution.Values.Should().BeEquivalentTo(2, 1, 0);
            distribution.ToString().Should().BeEquivalentTo("0=2;10=1;20=0");

            distribution.Add(3);
            distribution.Values.Should().BeEquivalentTo(3, 1, 0);
            distribution.ToString().Should().BeEquivalentTo("0=3;10=1;20=0");

            distribution.Add(10);
            distribution.Values.Should().BeEquivalentTo(3, 2, 0);
            distribution.ToString().Should().BeEquivalentTo("0=3;10=2;20=0");

            distribution.Add(99);
            distribution.Values.Should().BeEquivalentTo(3, 2, 1);
            distribution.ToString().Should().BeEquivalentTo("0=3;10=2;20=1");

            distribution = new Distribution(7, 13);
            distribution.Ranges.Should().BeEquivalentTo(7, 13);
            distribution.Values.Should().BeEquivalentTo(0, 0);
            distribution.ToString().Should().BeEquivalentTo("7=0;13=0");

            distribution.Add(5);
            distribution.Values.Should().BeEquivalentTo(1, 0);
            distribution.ToString().Should().BeEquivalentTo("7=1;13=0");
        }
Example #2
0
        public static int Main(string[] args)
        {
            Write(string.Format("SonarLint for Visual Studio version {0}", typeof (Program).Assembly.GetName().Version));

            var configuration = new Configuration(XDocument.Load(args[0]));
            var diagnosticsRunner = new DiagnosticsRunner(configuration.Analyzers());

            var xmlOutSettings = new XmlWriterSettings
            {
                Encoding = Encoding.UTF8,
                Indent = true,
                IndentChars = "  "
            };

            using (var xmlOut = XmlWriter.Create(args[1], xmlOutSettings))
            {
                xmlOut.WriteComment("This XML format is not an API");
                xmlOut.WriteStartElement("AnalysisOutput");

                xmlOut.WriteStartElement("Files");
                var n = 0;
                foreach (var file in configuration.Files)
                {
                    xmlOut.Flush();
                    Write(n + "/" + configuration.Files.Count() + " files analyzed, starting to analyze: " + file);
                    n++;

                    try
                    {
                        var solution = CompilationHelper.GetSolutionFromFiles(file);

                        var compilation = solution.Projects.First().GetCompilationAsync().Result;
                        var syntaxTree = compilation.SyntaxTrees.First();

                        var metrics = new Metrics(syntaxTree);

                        xmlOut.WriteStartElement("File");
                        xmlOut.WriteElementString("Path", file);

                        xmlOut.WriteStartElement("Metrics");

                        xmlOut.WriteElementString("Lines", metrics.Lines().ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Classes", metrics.Classes().ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Accessors", metrics.Accessors().ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Statements", metrics.Statements().ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Functions", metrics.Functions().ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("PublicApi", metrics.PublicApi().ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("PublicUndocumentedApi", metrics.PublicUndocumentedApi().ToString(CultureInfo.InvariantCulture));

                        var complexity = metrics.Complexity();
                        xmlOut.WriteElementString("Complexity", complexity.ToString(CultureInfo.InvariantCulture));

                        // TODO This is a bit ridiculous, but is how SonarQube works
                        var fileComplexityDistribution = new Distribution(0, 5, 10, 20, 30, 60, 90);
                        fileComplexityDistribution.Add(complexity);
                        xmlOut.WriteElementString("FileComplexityDistribution", fileComplexityDistribution.ToString());

                        xmlOut.WriteElementString("FunctionComplexityDistribution", metrics.FunctionComplexityDistribution().ToString());

                        var comments = metrics.Comments(configuration.IgnoreHeaderComments);
                        xmlOut.WriteStartElement("Comments");
                        xmlOut.WriteStartElement("NoSonar");
                        foreach (var line in comments.NoSonar)
                        {
                            xmlOut.WriteElementString("Line", line.ToString(CultureInfo.InvariantCulture));
                        }
                        xmlOut.WriteEndElement();
                        xmlOut.WriteStartElement("NonBlank");
                        foreach (var line in comments.NonBlank)
                        {
                            xmlOut.WriteElementString("Line", line.ToString(CultureInfo.InvariantCulture));
                        }
                        xmlOut.WriteEndElement();
                        xmlOut.WriteEndElement();

                        xmlOut.WriteStartElement("LinesOfCode");
                        foreach (var line in metrics.LinesOfCode())
                        {
                            xmlOut.WriteElementString("Line", line.ToString(CultureInfo.InvariantCulture));
                        }
                        xmlOut.WriteEndElement();

                        xmlOut.WriteEndElement();

                        xmlOut.WriteStartElement("Issues");

                        foreach (var diagnostic in diagnosticsRunner.GetDiagnostics(compilation))
                        {
                            xmlOut.WriteStartElement("Issue");
                            xmlOut.WriteElementString("Id", diagnostic.Id);
                            if (diagnostic.Location != Location.None)
                            {
                                xmlOut.WriteElementString("Line", (diagnostic.GetLineNumberToReport()).ToString(CultureInfo.InvariantCulture));
                            }
                            xmlOut.WriteElementString("Message", diagnostic.GetMessage());
                            xmlOut.WriteEndElement();
                        }

                        xmlOut.WriteEndElement();

                        xmlOut.WriteEndElement();
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Failed to analyze the file: " + file);
                        Console.Error.WriteLine(e);
                        return 1;
                    }
                }

                xmlOut.WriteEndElement();

                xmlOut.WriteEndElement();
                xmlOut.WriteEndDocument();

                xmlOut.Flush();
                return 0;
            }
        }
Example #3
0
        public static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Write("Expected parameters: ");
                Write("[Input configuration path]");
                Write("[Output file path]");
                Write("[AnalyzerLanguage: 'cs' for C#, 'vbnet' for VB.Net]");

                return -1;
            }

            var language = AnalyzerLanguage.Parse(args[2]);

            Write($"SonarLint for Visual Studio version {typeof (Program).Assembly.GetName().Version}");

            var configuration = new Configuration(args[0], language);
            var diagnosticsRunner = new DiagnosticsRunner(configuration);

            var xmlOutSettings = new XmlWriterSettings
            {
                Encoding = Encoding.UTF8,
                Indent = true,
                IndentChars = "  "
            };

            using (var xmlOut = XmlWriter.Create(args[1], xmlOutSettings))
            {
                xmlOut.WriteComment("This XML format is not an API");
                xmlOut.WriteStartElement("AnalysisOutput");

                xmlOut.WriteStartElement("Files");
                var currentFileIndex = 0;
                foreach (var file in configuration.Files)
                {
                    xmlOut.Flush();
                    Write(currentFileIndex + "/" + configuration.Files.Count + " files analyzed, starting to analyze: " + file);
                    currentFileIndex++;

                    try
                    {
                        var solution = CompilationHelper.GetSolutionFromFiles(file, language);

                        var compilation = solution.Projects.First().GetCompilationAsync().Result;
                        var syntaxTree = compilation.SyntaxTrees.First();

                        var metrics = language == AnalyzerLanguage.CSharp
                            ? (MetricsBase)new Common.CSharp.Metrics(syntaxTree)
                            : new Common.VisualBasic.Metrics(syntaxTree);

                        xmlOut.WriteStartElement("File");
                        xmlOut.WriteElementString("Path", file);

                        xmlOut.WriteStartElement("Metrics");

                        xmlOut.WriteElementString("Lines", metrics.LineCount.ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Classes", metrics.ClassCount.ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Accessors", metrics.AccessorCount.ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Statements", metrics.StatementCount.ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("Functions", metrics.FunctionCount.ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("PublicApi", metrics.PublicApiCount.ToString(CultureInfo.InvariantCulture));
                        xmlOut.WriteElementString("PublicUndocumentedApi", metrics.PublicUndocumentedApiCount.ToString(CultureInfo.InvariantCulture));

                        var complexity = metrics.Complexity;
                        xmlOut.WriteElementString("Complexity", complexity.ToString(CultureInfo.InvariantCulture));

                        // TODO This is a bit ridiculous, but is how SonarQube works
                        var fileComplexityDistribution = new Distribution(0, 5, 10, 20, 30, 60, 90);
                        fileComplexityDistribution.Add(complexity);
                        xmlOut.WriteElementString("FileComplexityDistribution", fileComplexityDistribution.ToString());

                        xmlOut.WriteElementString("FunctionComplexityDistribution", metrics.FunctionComplexityDistribution.ToString());

                        var comments = metrics.GetComments(configuration.IgnoreHeaderComments);
                        xmlOut.WriteStartElement("Comments");
                        xmlOut.WriteStartElement("NoSonar");
                        foreach (var line in comments.NoSonar)
                        {
                            xmlOut.WriteElementString("Line", line.ToString(CultureInfo.InvariantCulture));
                        }
                        xmlOut.WriteEndElement();
                        xmlOut.WriteStartElement("NonBlank");
                        foreach (var line in comments.NonBlank)
                        {
                            xmlOut.WriteElementString("Line", line.ToString(CultureInfo.InvariantCulture));
                        }
                        xmlOut.WriteEndElement();
                        xmlOut.WriteEndElement();

                        xmlOut.WriteStartElement("LinesOfCode");
                        foreach (var line in metrics.LinesOfCode)
                        {
                            xmlOut.WriteElementString("Line", line.ToString(CultureInfo.InvariantCulture));
                        }
                        xmlOut.WriteEndElement();

                        xmlOut.WriteEndElement();

                        xmlOut.WriteStartElement("Issues");

                        foreach (var diagnostic in diagnosticsRunner.GetDiagnostics(compilation))
                        {
                            xmlOut.WriteStartElement("Issue");
                            xmlOut.WriteElementString("Id", diagnostic.Id);
                            if (diagnostic.Location != Location.None)
                            {
                                xmlOut.WriteElementString("Line", diagnostic.GetLineNumberToReport().ToString(CultureInfo.InvariantCulture));
                            }
                            xmlOut.WriteElementString("Message", diagnostic.GetMessage());
                            xmlOut.WriteEndElement();
                        }

                        xmlOut.WriteEndElement();

                        xmlOut.WriteEndElement();
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Failed to analyze the file: " + file);
                        Console.Error.WriteLine(e);
                        return 1;
                    }
                }

                xmlOut.WriteEndElement();

                xmlOut.WriteEndElement();
                xmlOut.WriteEndDocument();

                xmlOut.Flush();
                return 0;
            }
        }
Example #4
0
 public Distribution GetFunctionComplexityDistribution()
 {
     var distribution = new Distribution(1, 2, 4, 6, 8, 10, 12);
     foreach (var node in GetFunctionNodes())
     {
         distribution.Add(GetComplexity(node));
     }
     return distribution;
 }