示例#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
 public string GetMetricsData()
 {
     return(exporterMetrics.Dump());
 }