Example #1
0
        private static CSharpCompilation CreateCompilation(string cmdLine, params CSharpSyntaxTree[] sources)
        {
            MetadataReference[] refs = new MetadataReference[]
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location)
            };

            var args       = CSharpCommandLineParser.SplitCommandLineIntoArguments(cmdLine, false);
            var cmdParser  = new CSharpCommandLineParser();
            var parsedArgs = cmdParser.Parse(args, ".", null);

            if (sources.Contains(VulcanRuntime))
            {
                sources = sources.Where(s => s != VulcanRuntime).ToArray();
                refs    = refs.Concat(new[] { LoadVulcanRuntime() }).ToArray();
            }

            return(CSharpCompilation.Create(
                       System.IO.Path.GetRandomFileName(),
                       syntaxTrees: sources,
                       references: refs,
                       options: parsedArgs.CompilationOptions.WithOutputKind(OutputKind.DynamicallyLinkedLibrary)
                       ));
        }
Example #2
0
 private static MetadataReference LoadVulcanRuntime()
 {
     if (_vrt == null)
     {
         MetadataReference[] refs = new MetadataReference[]
         {
             MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
             MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location)
         };
         var args       = CSharpCommandLineParser.SplitCommandLineIntoArguments("/dialect:vulcan /r:VulcanRTFuncs.dll /r:VulcanRT.dll /unsafe", false);
         var cmdParser  = new CSharpCommandLineParser();
         var parsedArgs = cmdParser.Parse(args, ".", null);
         var c          = CSharpCompilation.Create(
             "VulcanRTdummy.dll",
             syntaxTrees: new[] { VulcanRuntime },
             references: refs,
             options: parsedArgs.CompilationOptions.WithOutputKind(OutputKind.DynamicallyLinkedLibrary)
             );
         var        ms = new System.IO.MemoryStream();
         EmitResult r  = c.Emit(ms);
         if (!r.Success)
         {
             string err = "";
             r.Diagnostics.Where(d => d.IsWarningAsError || d.Severity == DiagnosticSeverity.Error).ToList().ForEach(d => err += d.ToString() + "\r\n");
             throw new Exception(err);
         }
         ms.Seek(0, System.IO.SeekOrigin.Begin);
         _vrt  = ms.ToArray();
         _vrta = System.Reflection.Assembly.Load(_vrt);
         System.AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve;
     }
     return(MetadataReference.CreateFromStream(new System.IO.MemoryStream(_vrt)));
 }
Example #3
0
        public static string[] SplitToArgs(this string line)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                return(new string[0]);
            }

            return(CSharpCommandLineParser.SplitCommandLineIntoArguments(line.Trim(), true).ToArray());
        }
Example #4
0
        public static Compilation CreateReproCompilation()
        {
            var projectDir    = Environment.GetEnvironmentVariable(TestProjectEnvVarName);
            var cmdLineParser = new CSharpCommandLineParser();
            var responseFile  = Path.Combine(projectDir, "repro.rsp");
            var compiler      = new MockCSharpCompiler(responseFile, projectDir, Array.Empty <string>());
            var output        = new StringWriter();

            return(compiler.CreateCompilation(output, null, null));
        }
Example #5
0
        public static CSharpSyntaxTree ParseStartFunction(string cmdLine, string body)
        {
            var args       = CSharpCommandLineParser.SplitCommandLineIntoArguments(cmdLine, false);
            var cmdParser  = new CSharpCommandLineParser();
            var parsedArgs = cmdParser.Parse(args, ".", null);

            return((CSharpSyntaxTree)CSharpSyntaxTree.ParseText(
                       "FUNCTION Start() AS VOID\r\n" + body,
                       options: parsedArgs.ParseOptions
                       ));
        }
Example #6
0
        public static CSharpSyntaxTree ParseSource(string cmdLine, string source)
        {
            var args       = CSharpCommandLineParser.SplitCommandLineIntoArguments(cmdLine, false);
            var cmdParser  = new CSharpCommandLineParser();
            var parsedArgs = cmdParser.Parse(args, ".", null);

            return((CSharpSyntaxTree)CSharpSyntaxTree.ParseText(
                       source,
                       options: parsedArgs.ParseOptions
                       ));
        }
        private static CSharpCommandLineArguments BuildCommandLineArguments(CompilerParameters parameters, TextWriter outputWriter)
        {
            IEnumerable <string> enumerable = CSharpCompilationArguments.BuildCommandLineArgArray(parameters);

            outputWriter.Write(Res.Compilation_Arguments);
            foreach (string current in enumerable)
            {
                outputWriter.Write(current + " ");
            }
            outputWriter.WriteLine();
            enumerable = enumerable.Concat(new string[]
            {
                "SuppressNoSourceFileSpecifiedWarning"
            });
            string currentDirectory          = Environment.CurrentDirectory;
            string environmentVariable       = Environment.GetEnvironmentVariable("LIB");
            CSharpCommandLineParser @default = CSharpCommandLineParser.Default;

            return(@default.Parse(enumerable, currentDirectory, environmentVariable));
        }
Example #8
0
 protected CSharpCompiler(CSharpCommandLineParser parser, string responseFile, string[] args, BuildPaths buildPaths, string additionalReferenceDirectories, IAnalyzerAssemblyLoader assemblyLoader)
     : base(parser, responseFile, args, buildPaths, additionalReferenceDirectories, assemblyLoader)
 {
     _diagnosticFormatter = new CommandLineDiagnosticFormatter(buildPaths.WorkingDirectory, Arguments.PrintFullPaths, Arguments.ShouldIncludeErrorEndLocation);
     _tempDirectory       = buildPaths.TempDirectory;
 }
 public AnalyzerLoaderMockCSharpCompiler(CSharpCommandLineParser parser, string?responseFile, string[] args, BuildPaths buildPaths, string?additionalReferenceDirectories, IAnalyzerAssemblyLoader assemblyLoader, GeneratorDriverCache?driverCache = null, ICommonCompilerFileSystem?fileSystem = null)
     : base(parser, responseFile, args, buildPaths, additionalReferenceDirectories, assemblyLoader, driverCache, fileSystem)
 {
 }