// internal for testing
        internal static Stream GetWin32ResourcesInternal(CommonMessageProvider messageProvider, CommandLineArguments arguments, Compilation compilation, out IEnumerable <DiagnosticInfo> errors)
        {
            List <DiagnosticInfo> errorList = new List <DiagnosticInfo>();

            errors = errorList;

            if (arguments.Win32ResourceFile != null)
            {
                return(OpenStream(messageProvider, arguments.Win32ResourceFile, arguments.BaseDirectory, messageProvider.GetFieldOrProperty <int>("ERR_CantOpenWin32Resource"), errorList));
            }

            using (Stream manifestStream = OpenManifestStream(messageProvider, compilation.Options.OutputKind, arguments, errorList))
            {
                using (Stream iconStream = OpenStream(messageProvider, arguments.Win32Icon, arguments.BaseDirectory, messageProvider.GetFieldOrProperty <int>("ERR_CantOpenWin32Icon"), errorList))
                {
                    try
                    {
                        return(compilation.CreateDefaultWin32Resources(true, arguments.NoWin32Manifest, manifestStream, iconStream));
                    }
                    catch (Exception ex) when(ex.GetType().FullName == "Microsoft.CodeAnalysis.ResourceException")
                    {
                        errorList.Add(ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("ERR_ErrorBuildingWin32Resource"), new[] { ex.Message }));
                    }
                    catch (OverflowException ex)
                    {
                        errorList.Add(ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("ERR_ErrorBuildingWin32Resource"), new[] { ex.Message }));
                    }
                }
            }

            return(null);
        }
        internal static DiagnosticInfo ToFileReadDiagnostics(CommonMessageProvider messageProvider, Exception e, string filePath)
        {
            DiagnosticInfo diagnosticInfo;

            if (e is FileNotFoundException || e.GetType().Name == "DirectoryNotFoundException")
            {
                diagnosticInfo = ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("ERR_FileNotFound"), new[] { filePath });
            }
            else if (e is InvalidDataException)
            {
                diagnosticInfo = ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("ERR_BinaryFile"), new[] { filePath });
            }
            else
            {
                diagnosticInfo = ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("ERR_NoSourceFile"), new[] { filePath, e.Message });
            }

            return(diagnosticInfo);
        }
        private static string ResolveRelativePath(CommonMessageProvider messageProvider, string path, string baseDirectory, IList <DiagnosticInfo> errors)
        {
            string fullPath = (string)Refl.Type_FileUtilities.InvokeFunction("ResolveRelativePath", path, baseDirectory);

            if (fullPath == null)
            {
                errors.Add(ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("FTL_InputFileNameTooLong"), new[] { path }));
            }

            return(fullPath);
        }
 private static Stream OpenManifestStream(CommonMessageProvider messageProvider, OutputKind outputKind, CommandLineArguments arguments, List <DiagnosticInfo> errorList)
 {
     return(outputKind == OutputKind.NetModule
         ? null
         : OpenStream(messageProvider, arguments.Win32Manifest, arguments.BaseDirectory, messageProvider.GetFieldOrProperty <int>("ERR_CantOpenWin32Manifest"), errorList));
 }
        private static void ReportAnalyzerExecutionTime(TextWriter consoleOutput, AnalyzerDriver analyzerDriver, CultureInfo culture, bool isConcurrentBuild)
        {
            var exectimes = analyzerDriver.GetFieldOrProperty <ImmutableDictionary <DiagnosticAnalyzer, TimeSpan> >("AnalyzerExecutionTimes");

            Debug.Assert(exectimes != null);
            if (exectimes.IsEmpty)
            {
                return;
            }

            var totalAnalyzerExecutionTime         = analyzerDriver.GetFieldOrProperty <ImmutableDictionary <DiagnosticAnalyzer, TimeSpan> >("AnalyzerExecutionTimes").Sum(kvp => kvp.Value.TotalSeconds);
            Func <double, string> getFormattedTime = d => d.ToString("##0.000", culture);

            consoleOutput.WriteLine();
            consoleOutput.WriteLine(string.Format((string)Refl.Type_CodeAnalysisResources.InvokeFunction("get_AnalyzerTotalExecutionTime"), getFormattedTime(totalAnalyzerExecutionTime)));

            if (isConcurrentBuild)
            {
                consoleOutput.WriteLine((string)Refl.Type_CodeAnalysisResources.InvokeFunction("get_MultithreadedAnalyzerExecutionNote"));
            }

            var analyzersByAssembly = exectimes
                                      .GroupBy(kvp => kvp.Key.GetType().GetTypeInfo().Assembly)
                                      .OrderByDescending(kvp => kvp.Sum(entry => entry.Value.Ticks));

            consoleOutput.WriteLine();

            getFormattedTime = d => d < 0.001 ?
                               string.Format(culture, "{0,8:<0.000}", 0.001) :
                               string.Format(culture, "{0,8:##0.000}", d);
            Func <int, string>    getFormattedPercentage   = i => string.Format("{0,5}", i < 1 ? "<1" : i.ToString());
            Func <string, string> getFormattedAnalyzerName = s => "   " + s;

            // Table header
            var analyzerTimeColumn       = string.Format("{0,8}", (string)Refl.Type_CodeAnalysisResources.InvokeFunction("get_AnalyzerExecutionTimeColumnHeader"));
            var analyzerPercentageColumn = string.Format("{0,5}", "%");
            var analyzerNameColumn       = getFormattedAnalyzerName((string)Refl.Type_CodeAnalysisResources.InvokeFunction("get_AnalyzerNameColumnHeader"));

            consoleOutput.WriteLine(analyzerTimeColumn + analyzerPercentageColumn + analyzerNameColumn);

            // Table rows grouped by assembly.
            foreach (var analyzerGroup in analyzersByAssembly)
            {
                var executionTime = analyzerGroup.Sum(kvp => kvp.Value.TotalSeconds);
                var percentage    = (int)(executionTime * 100 / totalAnalyzerExecutionTime);

                analyzerTimeColumn       = getFormattedTime(executionTime);
                analyzerPercentageColumn = getFormattedPercentage(percentage);
                analyzerNameColumn       = getFormattedAnalyzerName(analyzerGroup.Key.FullName);

                consoleOutput.WriteLine(analyzerTimeColumn + analyzerPercentageColumn + analyzerNameColumn);

                // Rows for each diagnostic analyzer in the assembly.
                foreach (var kvp in analyzerGroup.OrderByDescending(kvp => kvp.Value))
                {
                    executionTime = kvp.Value.TotalSeconds;
                    percentage    = (int)(executionTime * 100 / totalAnalyzerExecutionTime);

                    analyzerTimeColumn       = getFormattedTime(executionTime);
                    analyzerPercentageColumn = getFormattedPercentage(percentage);
                    analyzerNameColumn       = getFormattedAnalyzerName("   " + kvp.Key.ToString());

                    consoleOutput.WriteLine(analyzerTimeColumn + analyzerPercentageColumn + analyzerNameColumn);
                }

                consoleOutput.WriteLine();
            }
        }