예제 #1
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", ""));
                }
            });
        }
예제 #2
0
 private void RegisterMlirAndCbdeInOneStep(SonarAnalysisContext context)
 {
     context.RegisterCompilationAction(
         c =>
     {
         emitLog = Environment.GetEnvironmentVariables().Contains("SONAR_DOTNET_INTERNAL_LOG_CBDE");
         if (!shouldRunInContext(c))
         {
             return;
         }
         var compilationHash = c.Compilation.GetHashCode();
         InitializePathsAndLog(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);
             throw new CbdeException($"Top level error in CBDE handling: {e.Message}{this.moreDetailsMessage}");
         }
     });
 }
예제 #3
0
 private void ExportFunctionMlir(SyntaxTree tree, SemanticModel model, MlirExporterMetrics exporterMetrics, string mlirFileName)
 {
     using (var mlirStreamWriter = new StreamWriter(Path.Combine(cbdeDirectoryAssembly, mlirFileName)))
     {
         StringBuilder perfLog = new StringBuilder();
         perfLog.AppendLine(tree.GetRoot().GetLocation().GetLineSpan().Path);
         var mlirExporter = new MlirExporter(mlirStreamWriter, model, exporterMetrics, true);
         foreach (var method in tree.GetRoot().DescendantNodes().OfType <MethodDeclarationSyntax>())
         {
             var watch    = System.Diagnostics.Stopwatch.StartNew();
             var cpuWatch = ThreadCpuStopWatch.StartNew();
             mlirExporter.ExportFunction(method);
             perfLog.AppendLine(method.Identifier + " " + watch.ElapsedMilliseconds);
             perfLog.AppendLine(method.Identifier + " " + cpuWatch.ElapsedMilliseconds);
         }
         PerformanceLog(perfLog.ToString() + "\n");
     }
 }