Example #1
1
        public static CompilationResult GetAssemblyFromCompilation(
            IAssemblyLoadContext loader,
            Compilation compilation)
        {
            EmitResult result;
            using (var ms = new MemoryStream())
            {
                using (var pdb = new MemoryStream())
                {
                    if (PlatformHelper.IsMono)
                    {
                        result = compilation.Emit(ms, pdbStream: null);
                    }
                    else
                    {
                        result = compilation.Emit(ms, pdbStream: pdb);
                    }

                    if (!result.Success)
                    {
                        var formatter = new DiagnosticFormatter();
                        var errorMessages = result.Diagnostics
                                             .Where(IsError)
                                             .Select(d => formatter.Format(d));

                        return CompilationResult.FromErrorMessages(errorMessages);
                    }

                    ms.Seek(0, SeekOrigin.Begin);

                    Assembly assembly;
                    if (PlatformHelper.IsMono)
                    {
                        assembly = loader.LoadStream(ms, assemblySymbols: null);
                    }
                    else
                    {
                        pdb.Seek(0, SeekOrigin.Begin);
                        assembly = loader.LoadStream(ms, pdb);
                    }

                    return CompilationResult.FromAssembly(assembly);
                }
            }
        }
        public static CompiledAssemblyResult GetAssemblyFromCompilation(
            [NotNull]CSharpCompilation compilation)
        {
            Check.NotNull(compilation, nameof(compilation));

            EmitResult result;
            using (var ms = new MemoryStream())
            {
                using (var pdb = new MemoryStream())
                {
                    if (PlatformHelper.IsMono)
                    {
                        result = compilation.Emit(ms, pdbStream: null);
                    }
                    else
                    {
                        result = compilation.Emit(ms, pdbStream: pdb);
                    }

                    if (!result.Success)
                    {
                        var formatter = new DiagnosticFormatter();
                        var errorMessages = result.Diagnostics
                                             .Where(IsError)
                                             .Select(d => formatter.Format(d));

                        return CompiledAssemblyResult.FromErrorMessages(errorMessages);
                    }

                    Assembly assembly;
                    if (PlatformHelper.IsMono)
                    {
                        var assemblyLoadMethod = typeof(Assembly).GetTypeInfo().GetDeclaredMethods("Load")
                            .First(
                                m =>
                                {
                                    var parameters = m.GetParameters();
                                    return parameters.Length == 1 && parameters[0].ParameterType == typeof(byte[]);
                                });
                        assembly = (Assembly)assemblyLoadMethod.Invoke(null, new[] { ms.ToArray() });
                    }
                    else
                    {
                        var assemblyLoadMethod = typeof(Assembly).GetTypeInfo().GetDeclaredMethods("Load")
                            .First(
                                m =>
                                {
                                    var parameters = m.GetParameters();
                                    return parameters.Length == 2
                                        && parameters[0].ParameterType == typeof(byte[])
                                        && parameters[1].ParameterType == typeof(byte[]);
                                });
                        assembly = (Assembly)assemblyLoadMethod.Invoke(null, new[] { ms.ToArray(), pdb.ToArray() });
                    }

                    return CompiledAssemblyResult.FromAssembly(assembly);
                }
            }
        }
        internal bool PrintErrors(IEnumerable <Diagnostic> diagnostics, TextWriter consoleOutput)
        {
            bool hasErrors = false;

            foreach (var diag in diagnostics)
            {
                if (_reportedDiagnostics.Contains(diag))
                {
                    // TODO: This invariant fails (at least) in the case where we see a member declaration "x = 1;".
                    // First we attempt to parse a member declaration starting at "x".  When we see the "=", we
                    // create an IncompleteMemberSyntax with return type "x" and an error at the location of the "x".
                    // Then we parse a member declaration starting at "=".  This is an invalid member declaration start
                    // so we attach an error to the "=" and attach it (plus following tokens) to the IncompleteMemberSyntax
                    // we previously created.
                    //this assert isn't valid if we change the design to not bail out after each phase.
                    //System.Diagnostics.Debug.Assert(diag.Severity != DiagnosticSeverity.Error);
                    continue;
                }
                else if (diag.Severity == DiagnosticSeverity.Hidden)
                {
                    // Not reported from the command-line compiler.
                    continue;
                }

                // Catch exceptions from diagnostic formatter as diagnostic descriptors for analyzer diagnostics can throw an exception while formatting diagnostic message.
                try
                {
                    consoleOutput.WriteLine(DiagnosticFormatter.Format(diag, this.Culture));
                }
                catch (Exception ex)
                {
                    var exceptionDiagnostic = AnalyzerExecutor.GetDescriptorDiagnostic(diag.Id, ex);
                    consoleOutput.WriteLine(DiagnosticFormatter.Format(exceptionDiagnostic, this.Culture));
                }

                if (diag.Severity == DiagnosticSeverity.Error)
                {
                    hasErrors = true;
                }

                _reportedDiagnostics.Add(diag);
            }

            return(hasErrors);
        }
        public bool ReportErrors(IEnumerable <Diagnostic> diagnostics, TextWriter consoleOutput, ErrorLogger2 errorLoggerOpt)
        {
            bool hasErrors = false;

            foreach (var diag in diagnostics)
            {
                if (_reportedDiagnostics.Contains(diag))
                {
                    // TODO: This invariant fails (at least) in the case where we see a member declaration "x = 1;".
                    // First we attempt to parse a member declaration starting at "x".  When we see the "=", we
                    // create an IncompleteMemberSyntax with return type "x" and an error at the location of the "x".
                    // Then we parse a member declaration starting at "=".  This is an invalid member declaration start
                    // so we attach an error to the "=" and attach it (plus following tokens) to the IncompleteMemberSyntax
                    // we previously created.
                    //this assert isn't valid if we change the design to not bail out after each phase.
                    //System.Diagnostics.Debug.Assert(diag.Severity != DiagnosticSeverity.Error);
                    continue;
                }
                else if (diag.Severity == DiagnosticSeverity.Hidden)
                {
                    // Not reported from the command-line compiler.
                    continue;
                }

                // We want to report diagnostics with source suppression in the error log file.
                // However, these diagnostics should not be reported on the console output.
                errorLoggerOpt?.InvokeAction("LogDiagnostic", diag);
                if (diag.IsSuppressed)
                {
                    continue;
                }

                consoleOutput.WriteLine(DiagnosticFormatter.Format(diag));

                if (diag.Severity == DiagnosticSeverity.Error)
                {
                    hasErrors = true;
                }

                _reportedDiagnostics.Add(diag);
            }

            return(hasErrors);
        }
Example #5
0
        internal bool PrintErrors(IEnumerable <Diagnostic> diagnostics, TextWriter consoleOutput)
        {
            bool hasErrors = false;

            foreach (var diag in diagnostics)
            {
                if (reportedDiagnostics.Contains(diag))
                {
                    // TODO: This invariant fails (at least) in the case where we see a member declaration "x = 1;".
                    // First we attempt to parse a member declaration starting at "x".  When we see the "=", we
                    // create an IncompleteMemberSyntax with return type "x" and an error at the location of the "x".
                    // Then we parse a member declaration starting at "=".  This is an invalid member declaration start
                    // so we attach an error to the "=" and attach it (plus following tokens) to the IncompleteMemberSyntax
                    // we previously created.
                    //this assert isn't valid if we change the design to not bail out after each phase.
                    //System.Diagnostics.Debug.Assert(diag.Severity != DiagnosticSeverity.Error);
                    continue;
                }
                else if (diag.Severity == DiagnosticSeverity.Info)
                {
                    // Not reported from the command-line compiler.
                    continue;
                }

                consoleOutput.WriteLine(DiagnosticFormatter.Format(diag, this.Culture));

                if (diag.Severity == DiagnosticSeverity.Error || diag.IsWarningAsError)
                {
                    hasErrors = true;
                }

                reportedDiagnostics.Add(diag);
            }

            return(hasErrors);
        }
Example #6
0
 protected virtual void PrintError(Diagnostic diagnostic, TextWriter consoleOutput)
 {
     consoleOutput.WriteLine(DiagnosticFormatter.Format(diagnostic, Culture));
 }
        private static DiagnosticResult CreateDiagnosticResult(bool success, IEnumerable<Diagnostic> diagnostics)
        {
            var formatter = new DiagnosticFormatter();

            var errors = diagnostics.Where(IsError)
                                .Select(d => formatter.Format(d)).ToList();

            var warnings = diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning)
                                  .Select(d => formatter.Format(d)).ToList();

            return new DiagnosticResult(success, warnings, errors);
        }
        internal static IEnumerable<string> Convert(IEnumerable<Diagnostic> diagnostics)
        {
            var formatter = new DiagnosticFormatter();

            return diagnostics.Select(d => formatter.Format(d)).ToList();
        }
Example #9
0
        private static AssemblyLoadResult ReportCompilationError(EmitResult result)
        {
            #if DESKTOP // TODO: Temporary due to CoreCLR and Desktop Roslyn being out of sync
            var formatter = DiagnosticFormatter.Instance;
            #else
            var formatter = new DiagnosticFormatter();
            #endif
            var errors = new List<string>(result.Diagnostics.Select(d => formatter.Format(d)));

            return new AssemblyLoadResult(errors);
        }
Example #10
0
 private CompilationMessage GetCompilationMessage(DiagnosticFormatter formatter, Diagnostic diagnostic)
 {
     return new CompilationMessage(formatter.Format(diagnostic));
 }