Beispiel #1
0
        public void ProjectConfiguration_WhenFileChanges_RebuildsCache()
        {
            var firstOptions  = TestHelper.CreateOptions($@"ResourceTests\SonarProjectConfig\Path_Windows\SonarProjectConfig.xml");
            var secondOptions = TestHelper.CreateOptions($@"ResourceTests\SonarProjectConfig\Path_Unix\SonarProjectConfig.xml");
            var sut           = new SonarAnalysisContext(new DummyContext(), Enumerable.Empty <DiagnosticDescriptor>());
            var firstConfig   = sut.ProjectConfiguration(firstOptions);

            sut.ProjectConfiguration(secondOptions).Should().NotBeSameAs(firstConfig);
        }
Beispiel #2
0
        public void RegisterMlirAndCbdeInOneStep(SonarAnalysisContext context)
        {
            if (cbdeExecutablePath == null)
            {
                return;
            }

            context.RegisterCompilationAction(
                c =>
            {
                // Do not run CBDE in SonarLint. OutPath is present only when run from S4MSB.
                var outPath = unitTest ? Path.GetTempPath() : context.ProjectConfiguration(c.Options).OutPath;
                if (string.IsNullOrEmpty(outPath))
                {
                    return;
                }
                var compilationHash = c.Compilation.GetHashCode();
                InitializePathsAndLog(outPath, c.Compilation.Assembly.Name, compilationHash);
                Log("CBDE: Compilation phase");
                var exporterMetrics = new MlirExporterMetrics();
                try
                {
                    var watch    = Stopwatch.StartNew();
                    var cpuWatch = ThreadCpuStopWatch.StartNew();
                    foreach (var tree in c.Compilation.SyntaxTrees)
                    {
                        csSourceFileNames.Add(tree.FilePath);
                        Log($"CBDE: Generating MLIR for source file {tree.FilePath} in context {compilationHash}");
                        var mlirFileName = ManglePath(tree.FilePath) + ".mlir";
                        ExportFunctionMlir(tree, c.Compilation.GetSemanticModel(tree), exporterMetrics, mlirFileName);
                        LogIfFailure($"- generated mlir file {mlirFileName}");
                        Log($"CBDE: Done with file {tree.FilePath} in context {compilationHash}");
                    }
                    Log($"CBDE: MLIR generation time: {watch.ElapsedMilliseconds} ms");
                    Log($"CBDE: MLIR generation cpu time: {cpuWatch.ElapsedMilliseconds} ms");
                    watch.Restart();
                    RunCbdeAndRaiseIssues(c);
                    Log($"CBDE: CBDE execution and reporting time: {watch.ElapsedMilliseconds} ms");
                    Log($"CBDE: CBDE execution and reporting cpu time: {cpuWatch.ElapsedMilliseconds} ms");
                    Log("CBDE: End of compilation");
                    lock (MetricsFileLock)
                    {
                        File.AppendAllText(cbdeMetricsLogFile, exporterMetrics.Dump());
                    }
                }
                catch (Exception e)
                {
                    Log("An exception has occured: " + e.Message + "\n" + e.StackTrace);
                    var message = $@"Top level error in CBDE handling: {e.Message}
Details: {moreDetailsMessage}
Inner exception: {e.InnerException}
Stack trace: {e.StackTrace}";
                    // Roslyn/MSBuild is currently cutting exception message at the end of the line instead
                    // of displaying the full message. As a workaround, we replace the line ending with ' ## '.
                    // See https://github.com/dotnet/roslyn/issues/1455 and https://github.com/dotnet/roslyn/issues/24346
                    throw new CbdeException(message.Replace("\n", " ## ").Replace("\r", ""));
                }
            });
        }
Beispiel #3
0
        public void ProjectConfiguration_LoadsExpectedValues()
        {
            var options = TestHelper.CreateOptions($@"ResourceTests\SonarProjectConfig\Path_Windows\SonarProjectConfig.xml");

            var sut    = new SonarAnalysisContext(new DummyContext(), Enumerable.Empty <DiagnosticDescriptor>());
            var config = sut.ProjectConfiguration(options);

            config.AnalysisConfigPath.Should().Be(@"c:\foo\bar\.sonarqube\conf\SonarQubeAnalysisConfig.xml");
        }
Beispiel #4
0
        public void ProjectConfiguration_UsesCachedValue()
        {
            var options     = TestHelper.CreateOptions($@"ResourceTests\SonarProjectConfig\Path_Windows\SonarProjectConfig.xml");
            var context     = new DummyContext();
            var firstSut    = new SonarAnalysisContext(context, Enumerable.Empty <DiagnosticDescriptor>());
            var secondSut   = new SonarAnalysisContext(context, Enumerable.Empty <DiagnosticDescriptor>());
            var firstConfig = firstSut.ProjectConfiguration(options);

            secondSut.ProjectConfiguration(options).Should().BeSameAs(firstConfig);
        }
        protected static bool IsWebConfigCookieSet(SonarAnalysisContext context, AnalyzerOptions options, string attribute)
        {
            foreach (var fullPath in context.ProjectConfiguration(options).FilesToAnalyze.FindFiles("web.config"))
            {
                var webConfig = File.ReadAllText(fullPath);
                if (webConfig.Contains("<system.web>") && XmlHelper.ParseXDocument(webConfig) is { } doc &&
                    doc.XPathSelectElements("configuration/system.web/httpCookies").Any(x => x.GetAttributeIfBoolValueIs(attribute, true) != null))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #6
0
        public void ProjectConfiguration_WhenAdditionalFileNotPresent_ReturnsEmptyConfig(string folder)
        {
            var options = TestHelper.CreateOptions(folder);

            var sut    = new SonarAnalysisContext(new DummyContext(), Enumerable.Empty <DiagnosticDescriptor>());
            var config = sut.ProjectConfiguration(options);

            config.AnalysisConfigPath.Should().BeNull();
            config.ProjectPath.Should().BeNull();
            config.FilesToAnalyzePath.Should().BeNull();
            config.OutPath.Should().BeNull();
            config.ProjectType.Should().Be(ProjectType.Unknown);
            config.TargetFramework.Should().BeNull();
        }
        private void CheckWebConfig(SonarAnalysisContext context, CompilationAnalysisContext c)
        {
            if (!IsEnabled(c.Options))
            {
                return;
            }

            foreach (var fullPath in context.ProjectConfiguration(c.Options).FilesToAnalyze.FindFiles(WebConfigRegex).Where(ShouldProcess))
            {
                var webConfig = File.ReadAllText(fullPath);
                if (webConfig.Contains("<system.web>") && XmlHelper.ParseXDocument(webConfig) is { } doc)
                {
                    ReportValidateRequest(doc, fullPath, c);
                    ReportRequestValidationMode(doc, fullPath, c);
                }
            }
Beispiel #8
0
        protected void ReadParameters(SonarAnalysisContext context, CompilationAnalysisContext c)
        {
            var settings = PropertiesHelper.GetSettings(c.Options);
            var outPath  = context.ProjectConfiguration(c.Options).OutPath;

            // For backward compatibility with S4MSB <= 5.0
            if (outPath == null && c.Options.AdditionalFiles.FirstOrDefault(IsProjectOutFolderPath) is { } projectOutFolderAdditionalFile)
            {
                outPath = File.ReadAllLines(projectOutFolderAdditionalFile.Path).First();
            }
            if (settings.Any() && !string.IsNullOrEmpty(outPath))
            {
                IgnoreHeaderComments = PropertiesHelper.ReadIgnoreHeaderCommentsProperty(settings, c.Compilation.Language);
                AnalyzeGeneratedCode = PropertiesHelper.ReadAnalyzeGeneratedCodeProperty(settings, c.Compilation.Language);
                OutPath           = Path.Combine(outPath, c.Compilation.Language == LanguageNames.CSharp ? "output-cs" : "output-vbnet");
                IsAnalyzerEnabled = true;
            }
        }