Beispiel #1
0
        public void GlobalSetup()
        {
            _peStream = new MemoryStream();
            _comp     = Helpers.CreateReproCompilation();

            // Call GetDiagnostics to force binding to finish and most semantic analysis to be completed
            _ = _comp.GetDiagnostics();

            if (Selection == EmitStageSelection.SerializeOnly)
            {
                _options = EmitOptions.Default.WithIncludePrivateMembers(true);

                bool embedPdb = _options.DebugInformationFormat == DebugInformationFormat.Embedded;

                var diagnostics = DiagnosticBag.GetInstance();

                _moduleBeingBuilt = _comp.CheckOptionsAndCreateModuleBuilder(
                    diagnostics,
                    manifestResources: null,
                    _options,
                    debugEntryPoint: null,
                    sourceLinkStream: null,
                    embeddedTexts: null,
                    testData: null,
                    cancellationToken: default);

                bool success = false;

                success = _comp.CompileMethods(
                    _moduleBeingBuilt,
                    emittingPdb: embedPdb,
                    emitMetadataOnly: _options.EmitMetadataOnly,
                    emitTestCoverageData: _options.EmitTestCoverageData,
                    diagnostics: diagnostics,
                    filterOpt: null,
                    cancellationToken: default);

                _comp.GenerateResourcesAndDocumentationComments(
                    _moduleBeingBuilt,
                    xmlDocumentationStream: null,
                    win32ResourcesStream: null,
                    _options.OutputNameOverride,
                    diagnostics,
                    cancellationToken: default);

                _comp.ReportUnusedImports(null, diagnostics, default);
                _moduleBeingBuilt.CompilationFinished();

                diagnostics.Free();
            }
        }
        public void CompileMethods()
        {
            LoadCompilation();

            _options = EmitOptions.Default.WithIncludePrivateMembers(true);

            bool embedPdb = _options.DebugInformationFormat == DebugInformationFormat.Embedded;

            var diagnostics = DiagnosticBag.GetInstance();

            _moduleBeingBuilt = _comp.CheckOptionsAndCreateModuleBuilder(
                diagnostics,
                manifestResources: null,
                _options,
                debugEntryPoint: null,
                sourceLinkStream: null,
                embeddedTexts: null,
                testData: null,
                cancellationToken: default);

            bool success = false;

            success = _comp.CompileMethods(
                _moduleBeingBuilt,
                emittingPdb: embedPdb,
                emitMetadataOnly: _options.EmitMetadataOnly,
                emitTestCoverageData: _options.EmitTestCoverageData,
                diagnostics: diagnostics,
                filterOpt: null,
                cancellationToken: default);

            _comp.GenerateResourcesAndDocumentationComments(
                _moduleBeingBuilt,
                xmlDocumentationStream: null,
                win32ResourcesStream: null,
                _options.OutputNameOverride,
                diagnostics,
                cancellationToken: default);

            _comp.ReportUnusedImports(null, diagnostics, default);
            _moduleBeingBuilt.CompilationFinished();

            diagnostics.Free();
        }
Beispiel #3
0
        /// <summary>
        /// Perform all the work associated with actual compilation
        /// (parsing, binding, compile, emit), resulting in diagnostics
        /// and analyzer output.
        /// </summary>
        private void CompileAndEmit(
            ref Compilation compilation,
            AdditionalTextFile[] additionalTextFiles,
            DiagnosticBag diagnostics,
            CancellationToken cancellationToken)
        {
            // Print the diagnostics produced during the parsing stage and exit if there were any errors.
            compilation.GetDiagnostics(CompilationStage.Parse, false, diagnostics, cancellationToken);
            if (diagnostics.HasAnyErrors())
            {
                return;
            }

            DiagnosticBag analyzerExceptionDiagnostics = null;

            compilation.GetDiagnostics(CompilationStage.Declare, false, diagnostics, cancellationToken);
            if (diagnostics.HasAnyErrors())
            {
                return;
            }

            cancellationToken.ThrowIfCancellationRequested();

            string outputName       = GetOutputFileName(compilation, cancellationToken);
            var    finalPeFilePath  = Path.Combine(Arguments.OutputDirectory, outputName);
            var    finalPdbFilePath = Arguments.PdbPath ?? Path.ChangeExtension(finalPeFilePath, ".pdb");

            NoThrowStreamDisposer sourceLinkStreamDisposerOpt = null;

            try
            {
                // NOTE: Unlike the PDB path, the XML doc path is not embedded in the assembly, so we don't need to pass it to emit.
                var emitOptions = Arguments.EmitOptions;
                emitOptions.OutputNameOverride = outputName;
                emitOptions.PdbFilePath        = PathUtilities.NormalizePathPrefix(finalPdbFilePath, Arguments.PathMap);

                if (!string.IsNullOrEmpty(emitOptions.PdbFilePath))
                {
                    emitOptions.PdbFilePath = Path.GetFileName(emitOptions.PdbFilePath);
                }

                if (Arguments.SourceLink != null)
                {
                    var sourceLinkStreamOpt = OpenFile(
                        Arguments.SourceLink,
                        diagnostics,
                        FileMode.Open,
                        FileAccess.Read,
                        FileShare.Read);

                    if (sourceLinkStreamOpt != null)
                    {
                        sourceLinkStreamDisposerOpt = new NoThrowStreamDisposer(
                            sourceLinkStreamOpt,
                            Arguments.SourceLink,
                            diagnostics,
                            MessageProvider);
                    }
                }

                var moduleBeingBuilt = compilation.CheckOptionsAndCreateModuleBuilder(
                    diagnostics,
                    Arguments.ManifestResources,
                    emitOptions,
                    debugEntryPoint: null,
                    sourceLinkStream: sourceLinkStreamDisposerOpt?.Stream,
                    cancellationToken: cancellationToken);

                if (moduleBeingBuilt != null)
                {
                    bool success;

                    try
                    {
                        success = compilation.CompileMethods(
                            moduleBeingBuilt,
                            Arguments.EmitPdb,
                            emitOptions.EmitMetadataOnly,
                            diagnostics,
                            filterOpt: null,
                            cancellationToken: cancellationToken);

                        if (success)
                        {
                            using (var win32ResourceStreamOpt = GetWin32Resources(MessageProvider, Arguments, compilation, diagnostics))
                            {
                                if (diagnostics.HasAnyErrors())
                                {
                                    return;
                                }

                                success = compilation.GenerateResourcesAndDocumentationComments(
                                    moduleBeingBuilt,
                                    win32ResourceStreamOpt,
                                    emitOptions.OutputNameOverride,
                                    diagnostics,
                                    cancellationToken);
                            }

                            // only report unused usings if we have success.
                            if (success)
                            {
                                compilation.ReportUnusedImports(null, diagnostics, cancellationToken);
                            }
                        }

                        compilation.CompleteTrees(null);
                    }
                    finally
                    {
                        moduleBeingBuilt.CompilationFinished();
                    }

                    if (success)
                    {
                        var peStreamProvider = new CompilerEmitStreamProvider(this, finalPeFilePath);

                        success = compilation.SerializeToPeStream(
                            moduleBeingBuilt,
                            peStreamProvider,
                            null,
                            null,
                            diagnostics: diagnostics,
                            metadataOnly: emitOptions.EmitMetadataOnly,
                            includePrivateMembers: emitOptions.IncludePrivateMembers,
                            pePdbFilePath: emitOptions.PdbFilePath,
                            cancellationToken: cancellationToken);

                        peStreamProvider.Close(diagnostics);
                    }
                }

                if (diagnostics.HasAnyErrors())
                {
                    return;
                }
            }
            finally
            {
                sourceLinkStreamDisposerOpt?.Dispose();
            }

            if (sourceLinkStreamDisposerOpt?.HasFailedToDispose == true)
            {
                return;
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (analyzerExceptionDiagnostics != null)
            {
                diagnostics.AddRange(analyzerExceptionDiagnostics);
                if (analyzerExceptionDiagnostics.HasAnyErrors())
                {
                    return;
                }
            }

            cancellationToken.ThrowIfCancellationRequested();
        }