Пример #1
0
        protected sealed override void Check()
        {
            var path        = (string)Arguments[0];
            var file        = File.ReadAllText(path);
            var syntaxTree  = SyntaxFactory.ParseSyntaxTree(file, path: path, encoding: Encoding.UTF8);
            var compilation = Tests.CreateCompilation(false, syntaxTree);

            compilation = Normalizer.ApplyNormalizers(compilation);
            _root       = compilation.SyntaxTrees.First().GetRoot();

            Output.Trace("{0}", _root.ToFullString());
            CheckLines();
        }
Пример #2
0
        /// <summary>
        ///   Compiles S# <paramref name="project" /> and returns the bytes of the compiled assembly.
        /// </summary>
        /// <param name="project">The project that should be compiled.</param>
        /// <param name="peBytes">Returns the compiled assembly.</param>
        /// <param name="pdbBytes">The returns the compiled program database.</param>
        public void Compile([NotNull] Project project, out byte[] peBytes, out byte[] pdbBytes)
        {
            Requires.NotNull(project, nameof(project));

            Compilation = project.GetCompilationAsync().Result;

            var diagnosticOptions = Compilation.Options.SpecificDiagnosticOptions.Add("CS0626", ReportDiagnostic.Suppress);
            var options           = (CSharpCompilationOptions)Compilation.Options;

            options = options.WithAllowUnsafe(true).WithSpecificDiagnosticOptions(diagnosticOptions);

            if (!Diagnose())
            {
                throw new CompilationException();
            }

            Compilation = Compilation.WithOptions(options);
            Compilation = Normalizer.ApplyNormalizers(Compilation);

            EmitInMemory(out peBytes, out pdbBytes);
        }
Пример #3
0
        /// <summary>
        ///   Normalizes the <paramref name="files" />.
        /// </summary>
        /// <param name="files">The files that should be normalized.</param>
        /// <param name="references">The references the files should be compiled with.</param>
        /// <param name="intermediateDirectory">The intermediate directory that files can be written to.</param>
        public string[] NormalizeProject([NotNull] string[] files, [NotNull] string[] references, [NotNull] string intermediateDirectory)
        {
            try
            {
                var diagnosticOptions = ImmutableDictionary
                                        .Create <string, ReportDiagnostic>()
                                        .Add("CS0626", ReportDiagnostic.Suppress)
                                        .Add("CS1701", ReportDiagnostic.Suppress);

                var workspace = new AdhocWorkspace();
                var project   = workspace
                                .CurrentSolution.AddProject("ssharp", "ssharp", "C#")
                                .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true,
                                                                                     specificDiagnosticOptions: diagnosticOptions, assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default))
                                .WithMetadataReferences(references.Select(p => MetadataReference.CreateFromFile(p)));

                foreach (var file in files)
                {
                    var text = SourceText.From(File.ReadAllText(file));
                    project = project.AddDocument(file, text).Project;
                }

                Compilation = project.GetCompilationAsync().Result;

                if (!Diagnose())
                {
                    return(null);
                }

                Compilation = Normalizer.ApplyNormalizers(Compilation);
                return(Compilation.SyntaxTrees.Select(tree => tree.ToString()).ToArray());
            }
            catch (Exception)
            {
                OutputCode(Path.Combine(intermediateDirectory, "failed"));
                throw;
            }
        }