protected sealed override Task<CompilationResult> InnerCompile(OutputKind outputKind, string outputName, CancellationToken cancellationToken) { var compilation = CreateCompilation( Parse(), outputKind, outputName ?? "tmp" + Guid.NewGuid().ToString("N")); var ms = new MemoryStream(); var emitResult = compilation.Emit(ms, cancellationToken: cancellationToken); var compileResult = new CompilationResult {OutputKind = outputKind}; compileResult.Messages = emitResult.Diagnostics; compileResult.Succeeded = emitResult.Success; if (emitResult.Success) { ms.Seek(0, SeekOrigin.Begin); compileResult.Output = ms; } else { compileResult.Reason = ResultReasons.Compilation.Failed; ms.Dispose(); } return Task.FromResult(compileResult); }
public PEDeltaAssemblyBuilder( SourceAssemblySymbol sourceAssembly, string outputName, OutputKind outputKind, ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources, Func<AssemblySymbol, AssemblyIdentity> assemblySymbolMapper, EmitBaseline previousGeneration, IEnumerable<SemanticEdit> edits) : base(sourceAssembly, outputName, outputKind, serializationProperties, manifestResources, assemblySymbolMapper, additionalTypes: ImmutableArray<NamedTypeSymbol>.Empty, metadataOnly:false) { var context = new EmitContext(this, null, new DiagnosticBag()); var module = previousGeneration.OriginalMetadata; var compilation = sourceAssembly.DeclaringCompilation; var metadataAssembly = compilation.GetBoundReferenceManager().CreatePEAssemblyForAssemblyMetadata(AssemblyMetadata.Create(module), MetadataImportOptions.All); var metadataDecoder = new Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE.MetadataDecoder(metadataAssembly.PrimaryModule); previousGeneration = EnsureInitialized(previousGeneration, metadataDecoder); var matchToMetadata = new SymbolMatcher(previousGeneration.AnonymousTypeMap, sourceAssembly, context, metadataAssembly); SymbolMatcher matchToPrevious = null; if (previousGeneration.Ordinal > 0) { var previousAssembly = ((CSharpCompilation)previousGeneration.Compilation).SourceAssembly; var previousContext = new EmitContext((PEModuleBuilder)previousGeneration.PEModuleBuilder, null, new DiagnosticBag()); matchToPrevious = new SymbolMatcher(previousGeneration.AnonymousTypeMap, sourceAssembly, context, previousAssembly, previousContext); } this.previousDefinitions = new CSharpDefinitionMap(previousGeneration.OriginalMetadata.Module, edits, metadataDecoder, matchToMetadata, matchToPrevious); this.previousGeneration = previousGeneration; this.changes = new SymbolChanges(this.previousDefinitions, edits); }
public ModuleData(AssemblyIdentity identity, OutputKind kind, ImmutableArray<byte> image, ImmutableArray<byte> pdb, bool inMemoryModule) { this.Id = new ModuleDataId(identity.Name, identity.GetDisplayName(), GetMvid(image)); this.Kind = kind; this.Image = image; this.Pdb = pdb; this.InMemoryModule = inMemoryModule; }
public ModuleData(string netModuleName, ImmutableArray<byte> image, ImmutableArray<byte> pdb, bool inMemoryModule) { this.Id = new ModuleDataId(netModuleName, netModuleName, GetMvid(image)); this.Kind = OutputKind.NetModule; this.Image = image; this.Pdb = pdb; this.InMemoryModule = inMemoryModule; }
public PEDeltaAssemblyBuilder( SourceAssemblySymbol sourceAssembly, EmitOptions emitOptions, OutputKind outputKind, Cci.ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources, EmitBaseline previousGeneration, IEnumerable<SemanticEdit> edits, Func<ISymbol, bool> isAddedSymbol) : base(sourceAssembly, emitOptions, outputKind, serializationProperties, manifestResources, additionalTypes: ImmutableArray<NamedTypeSymbol>.Empty) { var initialBaseline = previousGeneration.InitialBaseline; var context = new EmitContext(this, null, new DiagnosticBag()); // Hydrate symbols from initial metadata. Once we do so it is important to reuse these symbols across all generations, // in order for the symbol matcher to be able to use reference equality once it maps symbols to initial metadata. var metadataSymbols = GetOrCreateMetadataSymbols(initialBaseline, sourceAssembly.DeclaringCompilation); var metadataDecoder = (MetadataDecoder)metadataSymbols.MetadataDecoder; var metadataAssembly = (PEAssemblySymbol)metadataDecoder.ModuleSymbol.ContainingAssembly; var matchToMetadata = new CSharpSymbolMatcher(metadataSymbols.AnonymousTypes, sourceAssembly, context, metadataAssembly); CSharpSymbolMatcher matchToPrevious = null; if (previousGeneration.Ordinal > 0) { var previousAssembly = ((CSharpCompilation)previousGeneration.Compilation).SourceAssembly; var previousContext = new EmitContext((PEModuleBuilder)previousGeneration.PEModuleBuilder, null, new DiagnosticBag()); matchToPrevious = new CSharpSymbolMatcher( previousGeneration.AnonymousTypeMap, sourceAssembly: sourceAssembly, sourceContext: context, otherAssembly: previousAssembly, otherContext: previousContext, otherSynthesizedMembersOpt: previousGeneration.SynthesizedMembers); } _previousDefinitions = new CSharpDefinitionMap(previousGeneration.OriginalMetadata.Module, edits, metadataDecoder, matchToMetadata, matchToPrevious); _previousGeneration = previousGeneration; _changes = new SymbolChanges(_previousDefinitions, edits, isAddedSymbol); // Workaround for https://github.com/dotnet/roslyn/issues/3192. // When compiling state machine we stash types of awaiters and state-machine hoisted variables, // so that next generation can look variables up and reuse their slots if possible. // // When we are about to allocate a slot for a lifted variable while compiling the next generation // we map its type to the previous generation and then check the slot types that we stashed earlier. // If the variable type matches we reuse it. In order to compare the previous variable type with the current one // both need to be completely lowered (translated). Standard translation only goes one level deep. // Generic arguments are not translated until they are needed by metadata writer. // // In order to get the fully lowered form we run the type symbols of stashed variables thru a deep translator // that translates the symbol recursively. _deepTranslator = new CSharpSymbolMatcher.DeepTranslator(sourceAssembly.GetSpecialType(SpecialType.System_Object)); }
protected sealed override CodeAnalysis.Compilation CreateCompilation(IEnumerable<SyntaxTree> syntaxTrees, OutputKind outputKind, string outputName) { var compilation = CSharpCompilation.Create( outputName, syntaxTrees, References, GetCompilationOptions(outputKind)); return compilation; }
public PEAssemblyBuilder( SourceAssemblySymbol sourceAssembly, Cci.ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources, OutputKind outputKind, EmitOptions emitOptions) :base(sourceAssembly.DeclaringCompilation, (SourceModuleSymbol)sourceAssembly.Modules[0], serializationProperties, manifestResources, outputKind, emitOptions) { _sourceAssembly = sourceAssembly; _metadataName = (emitOptions.OutputNameOverride == null) ? sourceAssembly.MetadataName : FileNameUtilities.ChangeExtension(emitOptions.OutputNameOverride, extension: null); AssemblyOrModuleSymbolToModuleRefMap.Add(sourceAssembly, this); }
public async Task<CompilationResult> Compile(OutputKind outputKind, string outputName = null) { var cancellationTokenSource = new CancellationTokenSource(); try { var task = InnerCompile(outputKind, outputName, cancellationTokenSource.Token); cancellationTokenSource.CancelAfter(Timeout); return await task.ConfigureAwait(false); } catch (OperationCanceledException) { if (cancellationTokenSource.IsCancellationRequested) { // Trace.TraceInformation("{0}:InnerCompile timed-out.", GetType().Name); return new CompilationResult { Succeeded = false, Reason = ResultReasons.TimedOutError, // Messages = new[] {"Maximum compilation time reached"}, OutputKind = outputKind }; } else { // Trace.TraceWarning("{0}:InnerCompile was unexpectedly canceled. Exception: {1}", GetType().Name, ocex); return new CompilationResult { Succeeded = false, Reason = ResultReasons.CanceledError, // Messages = new[] {"Compilation was canceled"}, OutputKind = outputKind }; } } catch (Exception) { // Trace.TraceError("{0}:InnerCompile failed. Exception: {1}", GetType().Name, ex); return new CompilationResult { Succeeded = false, Reason = ResultReasons.UnknownError, // Errors = new[] {"Unknown error"}, OutputKind = outputKind }; } finally { cancellationTokenSource.Dispose(); } }
public PEAssemblyBuilderBase( SourceAssemblySymbol sourceAssembly, EmitOptions emitOptions, OutputKind outputKind, Cci.ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources, ImmutableArray<NamedTypeSymbol> additionalTypes) : base((SourceModuleSymbol)sourceAssembly.Modules[0], emitOptions, outputKind, serializationProperties, manifestResources) { Debug.Assert((object)sourceAssembly != null); _sourceAssembly = sourceAssembly; _additionalTypes = additionalTypes.NullToEmpty(); _metadataName = (emitOptions.OutputNameOverride == null) ? sourceAssembly.MetadataName : FileNameUtilities.ChangeExtension(emitOptions.OutputNameOverride, extension: null); AssemblyOrModuleSymbolToModuleRefMap.Add(sourceAssembly, this); }
public PEAssemblyBuilderBase( SourceAssemblySymbol sourceAssembly, string outputName, OutputKind outputKind, ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources, Func<AssemblySymbol, AssemblyIdentity> assemblySymbolMapper, ImmutableArray<NamedTypeSymbol> additionalTypes) : base((SourceModuleSymbol)sourceAssembly.Modules[0], outputName, outputKind, serializationProperties, manifestResources, assemblySymbolMapper) { Debug.Assert((object)sourceAssembly != null); this.sourceAssembly = sourceAssembly; this.additionalTypes = additionalTypes.NullToEmpty(); this.metadataName = outputName == null ? sourceAssembly.MetadataName : PathUtilities.RemoveExtension(outputName); AssemblyOrModuleSymbolToModuleRefMap.Add(sourceAssembly, this); }
public static CSharpCompilationOptions GetCompilationOptions(OutputKind outputKind) { switch (outputKind) { case OutputKind.ConsoleApplication: return new CSharpCompilationOptions( CodeAnalysis.OutputKind.ConsoleApplication, allowUnsafe: false, concurrentBuild: false, optimizationLevel: OptimizationLevel.Debug); case OutputKind.DynamicallyLinkedLibrary: return new CSharpCompilationOptions( CodeAnalysis.OutputKind.DynamicallyLinkedLibrary, allowUnsafe: false, concurrentBuild: false, optimizationLevel: OptimizationLevel.Debug); default: throw new NotSupportedException(); } }
internal static void VerifyParamArrayAttribute(ParameterSymbol parameter, SourceModuleSymbol module, bool expected = true, OutputKind outputKind = OutputKind.ConsoleApplication) { Assert.Equal(expected, parameter.IsParams); var emitModule = new PEAssemblyBuilder(module.ContainingSourceAssembly, null, outputKind, GetDefaultModulePropertiesForSerialization(), SpecializedCollections.EmptyEnumerable<ResourceDescription>()); var paramArrayAttributeCtor = (MethodSymbol)emitModule.Compilation.GetWellKnownTypeMember(WellKnownMember.System_ParamArrayAttribute__ctor); bool found = false; var context = new EmitContext(emitModule, null, new DiagnosticBag()); foreach (Microsoft.Cci.ICustomAttribute attr in parameter.GetSynthesizedAttributes()) { if (paramArrayAttributeCtor == (MethodSymbol)attr.Constructor(context)) { Assert.False(found, "Multiple ParamArrayAttribute"); found = true; } } Assert.Equal(expected, found); context.Diagnostics.Verify(); }
protected PEModuleBuilder( PhpCompilation compilation, SourceModuleSymbol sourceModule, Cci.ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources, OutputKind outputKind, EmitOptions emitOptions) { Debug.Assert(sourceModule != null); Debug.Assert(serializationProperties != null); _compilation = compilation; _sourceModule = sourceModule; _serializationProperties = serializationProperties; this.ManifestResources = manifestResources; _outputKind = outputKind; _emitOptions = emitOptions; this.CompilationState = new CommonModuleCompilationState(); _debugDocuments = new ConcurrentDictionary<string, Cci.DebugSourceDocument>(compilation.IsCaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase); _synthesized = new SynthesizedManager(this); AssemblyOrModuleSymbolToModuleRefMap.Add(sourceModule, this); }
public CSharpProject(string projectFile) { if (!File.Exists(projectFile)) throw new Exception(string.Format("project file not found \"{0}\"", projectFile)); WriteLine(1, "compile project \"{0}\"", projectFile); _projectFile = projectFile; _projectDirectory = Path.GetDirectoryName(projectFile); _projectDocument = XDocument.Load(projectFile); _frameworkDirectory = GetValue("FrameworkDirectory"); WriteLine(2, " framework directory : \"{0}\"", _frameworkDirectory); _assemblyName = GetValue("AssemblyName"); WriteLine(2, " assembly name : \"{0}\"", _assemblyName); string outputDirectory = PathCombine(_projectDirectory, GetValue("OutputDirectory")); WriteLine(2, " output directory : \"{0}\"", outputDirectory); _languageVersion = GetLanguageVersion(GetValue("LanguageVersion")); WriteLine(2, " language version : \"{0}\"", _languageVersion); _outputKind = GetOutputKind(GetValue("OutputKind")); WriteLine(2, " output kind : \"{0}\"", _outputKind); _optimizationLevel = GetOptimizationLevel(GetValue("OptimizationLevel")); WriteLine(2, " optimization level : \"{0}\"", _optimizationLevel); _platform = GetPlatform(GetValue("Platform")); WriteLine(2, " platform : \"{0}\"", _platform); _generalDiagnosticOption = ReportDiagnostic.Default; WriteLine(2, " general diagnostic option : \"{0}\"", _generalDiagnosticOption); _warningLevel = 4; WriteLine(2, " warning level : \"{0}\"", _warningLevel); _outputPath = PathCombine(outputDirectory, GetValue("OutputPath")); WriteLine(2, " output path : \"{0}\"", _outputPath); _pdbPath = PathCombine(outputDirectory, GetValue("PdbPath")); WriteLine(2, " pdb path : \"{0}\"", _pdbPath); _win32ResourceFile = PathCombine(_projectDirectory, GetValue("Win32ResourceFile")); WriteLine(2, " win32 resource file : \"{0}\"", _win32ResourceFile); _preprocessorSymbols = GetPreprocessorSymbols(); _sourceFiles = GetSources(); _resourceFiles = GetResourceFiles(); _assembliesFiles = GetAssembliesFiles(); }
/// <summary> /// Compiles the given compilation and returns the assembly. /// </summary> /// <param name="compilation">Compilation</param> /// <param name="outputKind">OutputKind</param> /// <returns>Assembly</returns> private static Assembly ToAssembly(CodeAnalysis.Compilation compilation, OutputKind outputKind) { string assemblyFileName = null; if (outputKind == OutputKind.ConsoleApplication) { assemblyFileName = compilation.AssemblyName + ".exe"; } else if (outputKind == OutputKind.DynamicallyLinkedLibrary) { assemblyFileName = compilation.AssemblyName + ".dll"; } EmitResult emitResult = null; using (var ms = new MemoryStream()) { emitResult = compilation.Emit(ms); if (emitResult.Success) { var assembly = Assembly.Load(ms.GetBuffer()); return assembly; } } Output.Print("---"); Output.Print("Note: the errors below correspond to the intermediate C#-IR, " + "which can be printed using /debug."); Output.Print("---"); var message = string.Join("\r\n", emitResult.Diagnostics); throw new ApplicationException(message); }
private void TestDebuggableAttributeCommon( string source, Action<CSharpCompilation> validator, bool includeMscorlibRef, bool compileAndVerify, OutputKind outputKind, OptimizationLevel optimizations) { var compilation = CSharpCompilation.Create("comp", new[] { Parse(source) }, includeMscorlibRef ? new[] { MscorlibRef } : null, new CSharpCompilationOptions(outputKind, optimizationLevel: optimizations)); validator(compilation); if (compileAndVerify) { // NYI: /addmodule support // TODO: PEVerify currently fails for netmodules with error: "The module X was expected to contain an assembly manifest". // TODO: Remove the 'verify' named argument once /addmodule support has been added. CompileAndVerify(compilation, verify: !outputKind.IsNetModule()); } }
private static void ValidateWin32Settings(string win32ResourceFile, string win32IconResourceFile, string win32ManifestFile, OutputKind outputKind, IList<Diagnostic> diagnostics) { if (win32ResourceFile != null) { if (win32IconResourceFile != null) { AddDiagnostic(diagnostics, ErrorCode.ERR_CantHaveWin32ResAndIcon); } if (win32ManifestFile != null) { AddDiagnostic(diagnostics, ErrorCode.ERR_CantHaveWin32ResAndManifest); } } if (outputKind.IsNetModule() && win32ManifestFile != null) { AddDiagnostic(diagnostics, ErrorCode.WRN_CantHaveManifestForModule); } }
protected abstract CompilationOptions CreateCompilationOptions(OutputKind outputKind);
public async Task ApplicationConfigurationGenerator_GenerateInitialize_fails_if_project_type_unsupported(OutputKind projectType) { var test = new CSharpIncrementalSourceGeneratorVerifier <ApplicationConfigurationGenerator> .Test { TestState = { OutputKind = projectType, Sources = { SourceCompilationFailed }, ExpectedDiagnostics = { DiagnosticResult.CompilerError("WFAC001").WithArguments("WindowsApplication"), } }, }; await test.RunAsync().ConfigureAwait(false); }
public ScriptRunner(string?code, ImmutableList <SyntaxTree>?syntaxTrees = null, CSharpParseOptions?parseOptions = null, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, Platform platform = Platform.AnyCpu, IEnumerable <MetadataReference>?references = null, IEnumerable <string>?usings = null, string?filePath = null, string?workingDirectory = null, MetadataReferenceResolver?metadataResolver = null, SourceReferenceResolver?sourceResolver = null, InteractiveAssemblyLoader?assemblyLoader = null, OptimizationLevel optimizationLevel = OptimizationLevel.Debug, bool checkOverflow = false, bool allowUnsafe = true, bool registerDependencies = false) { _optimizationLevel = optimizationLevel; _checkOverflow = checkOverflow; _allowUnsafe = allowUnsafe; _registerDependencies = registerDependencies; Code = code; SyntaxTrees = syntaxTrees; OutputKind = outputKind; Platform = platform; _assemblyLoader = assemblyLoader ?? new InteractiveAssemblyLoader(); ParseOptions = (parseOptions ?? new CSharpParseOptions()) .WithKind(SourceCodeKind.Script) .WithPreprocessorSymbols(RoslynHost.PreprocessorSymbols); References = references?.AsImmutable() ?? ImmutableArray <MetadataReference> .Empty; Usings = usings?.AsImmutable() ?? ImmutableArray <string> .Empty; FilePath = filePath ?? string.Empty; MetadataResolver = metadataResolver ?? ScriptMetadataResolver.Default; SourceResolver = sourceResolver ?? (workingDirectory != null ? new SourceFileResolver(ImmutableArray <string> .Empty, workingDirectory) : SourceFileResolver.Default); }
/// <summary> /// Create a compilation given sources /// </summary> /// <param name="sources">Sources to compile</param> /// <param name="targetFramework">Target framework of the compilation</param> /// <param name="outputKind">Output type</param> /// <param name="refs">Addtional metadata references</param> /// <param name="preprocessorSymbols">Prepocessor symbols</param> /// <returns>The resulting compilation</returns> public static Task <Compilation> CreateCompilation(string[] sources, TestTargetFramework targetFramework = TestTargetFramework.Net, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, IEnumerable <MetadataReference>?refs = null, IEnumerable <string>?preprocessorSymbols = null) { return(CreateCompilation( sources.Select(source => CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Preview, preprocessorSymbols: preprocessorSymbols))).ToArray(), targetFramework, outputKind, refs)); }
/// <summary> /// A helper method to create <see cref="CategoricalTransform"/>. /// </summary> /// <param name="env">Host Environment.</param> /// <param name="input">Input <see cref="IDataView"/>. This is the output from previous transform or loader.</param> /// <param name="name">Name of the output column.</param> /// <param name="source">Name of the column to be transformed. If this is null '<paramref name="name"/>' will be used.</param> /// <param name="outputKind">The type of output expected.</param> public static IDataView Create(IHostEnvironment env, IDataView input, string name, string source = null, OutputKind outputKind = CategoricalEstimator.Defaults.OutKind) { return(new CategoricalEstimator(env, name, source, outputKind).Fit(input).Transform(input) as IDataView); }
/// Initializes an instance of the <see cref="OneHotEncodingEstimator"/>. /// <param name="env">Host Environment.</param> /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param> /// <param name="inputColumnName">Name of the column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param> /// <param name="outputKind">The type of output expected.</param> internal OneHotEncodingEstimator(IHostEnvironment env, string outputColumnName, string inputColumnName = null, OutputKind outputKind = Defaults.OutKind) : this(env, new[] { new ColumnOptions(outputColumnName, inputColumnName ?? outputColumnName, outputKind) }) { }
internal static bool IsNetModule(this OutputKind kind) { return(kind == OutputKind.NetModule); }
internal static bool IsWindowsRuntime(this OutputKind kind) { return(kind == OutputKind.WindowsRuntimeMetadata); }
private static async Task VerifyRefactoringAsync(string testCode, string fixedCode, int index = 0, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary) { await new VerifyCS.Test { TestCode = testCode, FixedCode = fixedCode, LanguageVersion = LanguageVersionExtensions.CSharpNext, CodeActionIndex = index, TestState = { OutputKind = outputKind, }, }.RunAsync(); }
internal OneHotEncodingEstimator(IHostEnvironment env, ColumnOptions[] columns, IDataView keyData = null) { Contracts.CheckValue(env, nameof(env)); _host = env.Register(nameof(OneHotEncodingEstimator)); _term = new ValueToKeyMappingEstimator(_host, columns, keyData); var binaryCols = new List <(string outputColumnName, string inputColumnName)>(); var cols = new List <(string outputColumnName, string inputColumnName, bool bag)>(); for (int i = 0; i < columns.Length; i++) { var column = columns[i]; OutputKind kind = columns[i].OutputKind; switch (kind) { default: throw _host.ExceptUserArg(nameof(column.OutputKind)); case OutputKind.Key: continue; case OutputKind.Binary: binaryCols.Add((column.OutputColumnName, column.OutputColumnName)); break; case OutputKind.Indicator: cols.Add((column.OutputColumnName, column.OutputColumnName, false)); break; case OutputKind.Bag: cols.Add((column.OutputColumnName, column.OutputColumnName, true)); break; } } IEstimator <ITransformer> toBinVector = null; IEstimator <ITransformer> toVector = null; if (binaryCols.Count > 0) { toBinVector = new KeyToBinaryVectorMappingEstimator(_host, binaryCols.Select(x => (x.outputColumnName, x.inputColumnName)).ToArray()); } if (cols.Count > 0) { toVector = new KeyToVectorMappingEstimator(_host, cols.Select(x => new KeyToVectorMappingEstimator.ColumnOptions(x.outputColumnName, x.inputColumnName, x.bag)).ToArray()); } if (toBinVector != null && toVector != null) { _toSomething = toVector.Append(toBinVector); } else { if (toBinVector != null) { _toSomething = toBinVector; } else { _toSomething = toVector; } } }
public async Task ApplicationConfigurationGenerator_GenerateInitialize_pass_if_supported_project_type(OutputKind projectType) { SourceText generatedCode = LoadFileContent("GenerateInitialize_default_boilerplate"); var test = new CSharpIncrementalSourceGeneratorVerifier <ApplicationConfigurationGenerator> .Test { TestState = { OutputKind = projectType, Sources = { SourceCompilable },
private static Stream OpenManifestStream(CommonMessageProvider messageProvider, OutputKind outputKind, CommandLineArguments arguments, DiagnosticBag diagnostics) { return(outputKind.IsNetModule() ? null : OpenStream(messageProvider, arguments.Win32Manifest, arguments.BaseDirectory, messageProvider.ERR_CantOpenWin32Manifest, diagnostics)); }
internal CompilationOptions( OutputKind outputKind, bool reportSuppressedDiagnostics, string?moduleName, string?mainTypeName, string?scriptClassName, string?cryptoKeyContainer, string?cryptoKeyFile, ImmutableArray <byte> cryptoPublicKey, bool?delaySign, bool publicSign, OptimizationLevel optimizationLevel, bool checkOverflow, Platform platform, ReportDiagnostic generalDiagnosticOption, int warningLevel, ImmutableDictionary <string, ReportDiagnostic> specificDiagnosticOptions, bool concurrentBuild, bool deterministic, DateTime currentLocalTime, bool debugPlusMode, XmlReferenceResolver?xmlReferenceResolver, SourceReferenceResolver?sourceReferenceResolver, SyntaxTreeOptionsProvider?syntaxTreeOptionsProvider, MetadataReferenceResolver?metadataReferenceResolver, AssemblyIdentityComparer?assemblyIdentityComparer, StrongNameProvider?strongNameProvider, MetadataImportOptions metadataImportOptions, bool referencesSupersedeLowerVersions) { this.OutputKind = outputKind; this.ModuleName = moduleName; this.MainTypeName = mainTypeName; this.ScriptClassName = scriptClassName ?? WellKnownMemberNames.DefaultScriptClassName; this.CryptoKeyContainer = cryptoKeyContainer; this.CryptoKeyFile = string.IsNullOrEmpty(cryptoKeyFile) ? null : cryptoKeyFile; this.CryptoPublicKey = cryptoPublicKey.NullToEmpty(); this.DelaySign = delaySign; this.CheckOverflow = checkOverflow; this.Platform = platform; this.GeneralDiagnosticOption = generalDiagnosticOption; this.WarningLevel = warningLevel; this.SpecificDiagnosticOptions = specificDiagnosticOptions; this.ReportSuppressedDiagnostics = reportSuppressedDiagnostics; this.OptimizationLevel = optimizationLevel; this.ConcurrentBuild = concurrentBuild; this.Deterministic = deterministic; this.CurrentLocalTime = currentLocalTime; this.DebugPlusMode = debugPlusMode; this.XmlReferenceResolver = xmlReferenceResolver; this.SourceReferenceResolver = sourceReferenceResolver; this.SyntaxTreeOptionsProvider = syntaxTreeOptionsProvider; this.MetadataReferenceResolver = metadataReferenceResolver; this.StrongNameProvider = strongNameProvider; this.AssemblyIdentityComparer = assemblyIdentityComparer ?? AssemblyIdentityComparer.Default; this.MetadataImportOptions = metadataImportOptions; this.ReferencesSupersedeLowerVersions = referencesSupersedeLowerVersions; this.PublicSign = publicSign; _lazyErrors = new Lazy <ImmutableArray <Diagnostic> >(() => { var builder = ArrayBuilder <Diagnostic> .GetInstance(); ValidateOptions(builder); return(builder.ToImmutableAndFree()); }); }
protected abstract CompilationOptions CommonWithOutputKind(OutputKind kind);
public ICompilerResult Compile() { // CSharpParseOptions : // LanguageVersion languageVersion = LanguageVersion.CSharp6 // DocumentationMode documentationMode = DocumentationMode.Parse // SourceCodeKind kind = SourceCodeKind.Regular // Regular : No scripting. Used for .cs/.vb file parsing. // Script : Allows top-level statements, declarations, and optional trailing expression. Used for parsing .csx/.vbx and interactive submissions. // Interactive : The same as Microsoft.CodeAnalysis.SourceCodeKind.Script. // IEnumerable<string> preprocessorSymbols = null // .WithFeatures(IEnumerable<KeyValuePair<string, string>> features); // Enable some experimental language features for testing. CSharpParseOptions parseOptions = CSharpParseOptions.Default.WithLanguageVersion(GetLanguageVersion(_languageVersion)); if (_preprocessorSymbols != null) { parseOptions = parseOptions.WithPreprocessorSymbols(_preprocessorSymbols); } // CSharpCompilationOptions : // OutputKind outputKind // bool reportSuppressedDiagnostics = false // string moduleName = null // string mainTypeName = null // string scriptClassName = null // IEnumerable<string> usings = null // OptimizationLevel optimizationLevel = OptimizationLevel.Debug // bool checkOverflow = false // bool allowUnsafe = false // string cryptoKeyContainer = null // string cryptoKeyFile = null // ImmutableArray<byte> cryptoPublicKey = default(ImmutableArray<byte>) // bool? delaySign = default(bool?) // Platform platform = Platform.AnyCpu // ReportDiagnostic generalDiagnosticOption = ReportDiagnostic.Default // int warningLevel = 4 // IEnumerable<KeyValuePair<string, ReportDiagnostic>> specificDiagnosticOptions = null // bool concurrentBuild = true // bool deterministic = false // XmlReferenceResolver xmlReferenceResolver = null // SourceReferenceResolver sourceReferenceResolver = null // MetadataReferenceResolver metadataReferenceResolver = null // AssemblyIdentityComparer assemblyIdentityComparer = null // StrongNameProvider strongNameProvider = null // bool publicSign = false // [Obsolete] CommonWithFeatures(ImmutableArray<string> features) OutputKind outputKind = GetOutputKind(_target); Platform platform = GetPlatform(_platform); OptimizationLevel optimizationLevel = OptimizationLevel.Release; if (_debugInformation) { optimizationLevel = OptimizationLevel.Debug; } CSharpCompilationOptions compilationOptions = new CSharpCompilationOptions(outputKind, optimizationLevel: optimizationLevel, platform: platform, generalDiagnosticOption: _generalDiagnosticOption, warningLevel: _warningLevel); CSharpCompilation compilation = CSharpCompilation.Create(zPath.GetFileNameWithoutExtension(_outputAssembly), syntaxTrees: ParseSources2(parseOptions), references: GetAssemblyReferences(), options: compilationOptions); // EmitResult Emit(this CSharpCompilation compilation, string outputPath, string pdbPath = null, string xmlDocumentationPath = null, string win32ResourcesPath = null, IEnumerable<ResourceDescription> manifestResources = null, // CancellationToken cancellationToken = default(CancellationToken)); //_generateInMemory, _compilerOptions string pdbPath = zpath.PathSetExtension(_outputAssembly, ".pdb"); EmitResult result = compilation.Emit(_outputAssembly, pdbPath: pdbPath, win32ResourcesPath: _win32ResourceFile, manifestResources: GetResourceDescriptions()); return(new EmitCompilerResults(result, _outputAssembly, _messageFilter)); }
/// <summary> /// Create a compilation given source /// </summary> /// <param name="source">Source to compile</param> /// <param name="targetFramework">Target framework of the compilation</param> /// <param name="outputKind">Output type</param> /// <param name="refs">Addtional metadata references</param> /// <param name="preprocessorSymbols">Prepocessor symbols</param> /// <returns>The resulting compilation</returns> public static Task <Compilation> CreateCompilation(string source, TestTargetFramework targetFramework = TestTargetFramework.Net, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, IEnumerable <MetadataReference>?refs = null, IEnumerable <string>?preprocessorSymbols = null) { return(CreateCompilation(new[] { source }, targetFramework, outputKind, refs, preprocessorSymbols)); }
public abstract Compilation CreatCompilation(string compilationName, SyntaxTree syntaxTree, IEnumerable <MetadataReference> references, OutputKind outputKind);
/// <summary> /// Create a compilation given sources /// </summary> /// <param name="sources">Sources to compile</param> /// <param name="targetFramework">Target framework of the compilation</param> /// <param name="outputKind">Output type</param> /// <param name="refs">Addtional metadata references</param> /// <returns>The resulting compilation</returns> public static async Task <Compilation> CreateCompilation(SyntaxTree[] sources, TestTargetFramework targetFramework = TestTargetFramework.Net, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, IEnumerable <MetadataReference>?refs = null) { var referenceAssemblies = await GetReferenceAssemblies(targetFramework); // [TODO] Can remove once ancillary logic is removed. if (targetFramework is TestTargetFramework.Net) { referenceAssemblies = referenceAssemblies.Add(GetAncillaryReference()); } if (refs is not null) { referenceAssemblies = referenceAssemblies.AddRange(refs); } return(CSharpCompilation.Create("compilation", sources, referenceAssemblies, new CSharpCompilationOptions(outputKind, allowUnsafe: true, specificDiagnosticOptions: BindingRedirectWarnings))); }
/// <summary> /// Creates a new options instance with the specified output kind. /// </summary> public CompilationOptions WithOutputKind(OutputKind kind) { return CommonWithOutputKind(kind); }
// Expects correct arguments. internal CompilationOptions( OutputKind outputKind, string moduleName, string mainTypeName, string scriptClassName, string cryptoKeyContainer, string cryptoKeyFile, bool? delaySign, OptimizationLevel optimizationLevel, bool checkOverflow, int fileAlignment, ulong baseAddress, Platform platform, ReportDiagnostic generalDiagnosticOption, int warningLevel, ImmutableDictionary<string, ReportDiagnostic> specificDiagnosticOptions, bool highEntropyVirtualAddressSpace, SubsystemVersion subsystemVersion, bool concurrentBuild, XmlReferenceResolver xmlReferenceResolver, SourceReferenceResolver sourceReferenceResolver, MetadataReferenceResolver metadataReferenceResolver, MetadataReferenceProvider metadataReferenceProvider, AssemblyIdentityComparer assemblyIdentityComparer, StrongNameProvider strongNameProvider, MetadataImportOptions metadataImportOptions, ImmutableArray<string> features) { this.OutputKind = outputKind; this.ModuleName = moduleName; this.MainTypeName = mainTypeName; this.ScriptClassName = scriptClassName; this.CryptoKeyContainer = cryptoKeyContainer; this.CryptoKeyFile = cryptoKeyFile; this.DelaySign = delaySign; this.CheckOverflow = checkOverflow; this.FileAlignment = fileAlignment; this.BaseAddress = baseAddress; this.Platform = platform; this.GeneralDiagnosticOption = generalDiagnosticOption; this.WarningLevel = warningLevel; this.SpecificDiagnosticOptions = specificDiagnosticOptions; this.HighEntropyVirtualAddressSpace = highEntropyVirtualAddressSpace; this.OptimizationLevel = optimizationLevel; this.ConcurrentBuild = concurrentBuild; this.SubsystemVersion = subsystemVersion; this.XmlReferenceResolver = xmlReferenceResolver; this.SourceReferenceResolver = sourceReferenceResolver; this.MetadataReferenceResolver = metadataReferenceResolver; this.MetadataReferenceProvider = metadataReferenceProvider; this.StrongNameProvider = strongNameProvider; this.AssemblyIdentityComparer = assemblyIdentityComparer ?? AssemblyIdentityComparer.Default; this.MetadataImportOptions = metadataImportOptions; this.Features = features; this.lazyErrors = new Lazy<ImmutableArray<Diagnostic>>(() => { var builder = ArrayBuilder<Diagnostic>.GetInstance(); ValidateOptions(builder); return builder.ToImmutableAndFree(); }); }
private Compilation GetCompilation(string content, string assemblyName, IEnumerable <MetadataReference> references, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary) { return(CSharpCompilation.Create(assemblyName, new[] { CSharpSyntaxTree.ParseText(content) }, references, new CSharpCompilationOptions(outputKind))); }
internal CompilationTestData Evaluate( string source, OutputKind outputKind, string methodName, string expr, int atLineNumber = -1, bool includeSymbols = true) { ResultProperties resultProperties; string error; var result = Evaluate(source, outputKind, methodName, expr, out resultProperties, out error, atLineNumber, includeSymbols); Assert.Null(error); return result; }
private static Stream OpenManifestStream(CommonMessageProvider messageProvider, OutputKind outputKind, CommandLineArguments arguments, List<DiagnosticInfo> errorList) { return outputKind.IsNetModule() ? null : OpenStream(messageProvider, arguments.Win32Manifest, arguments.BaseDirectory, messageProvider.ERR_CantOpenWin32Manifest, errorList); }
internal static bool IsValid(this OutputKind value) { return(value >= OutputKind.ConsoleApplication && value <= OutputKind.WindowsRuntimeApplication); }
protected void ReadCompilationOptionsFrom( ObjectReader reader, out OutputKind outputKind, out bool reportSuppressedDiagnostics, out string moduleName, out string mainTypeName, out string scriptClassName, out OptimizationLevel optimizationLevel, out bool checkOverflow, out string cryptoKeyContainer, out string cryptoKeyFile, out ImmutableArray <byte> cryptoPublicKey, out bool?delaySign, out Platform platform, out ReportDiagnostic generalDiagnosticOption, out int warningLevel, out IEnumerable <KeyValuePair <string, ReportDiagnostic> > specificDiagnosticOptions, out bool concurrentBuild, out bool deterministic, out bool publicSign, out XmlReferenceResolver xmlReferenceResolver, out SourceReferenceResolver sourceReferenceResolver, out MetadataReferenceResolver metadataReferenceResolver, out AssemblyIdentityComparer assemblyIdentityComparer, out StrongNameProvider strongNameProvider, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); outputKind = (OutputKind)reader.ReadInt32(); reportSuppressedDiagnostics = reader.ReadBoolean(); moduleName = reader.ReadString(); mainTypeName = reader.ReadString(); scriptClassName = reader.ReadString(); optimizationLevel = (OptimizationLevel)reader.ReadInt32(); checkOverflow = reader.ReadBoolean(); // REVIEW: is it okay this being not part of snapshot? cryptoKeyContainer = reader.ReadString(); cryptoKeyFile = reader.ReadString(); cryptoPublicKey = reader.ReadArray <byte>().ToImmutableArrayOrEmpty(); delaySign = reader.ReadBoolean() ? (bool?)reader.ReadBoolean() : null; platform = (Platform)reader.ReadInt32(); generalDiagnosticOption = (ReportDiagnostic)reader.ReadInt32(); warningLevel = reader.ReadInt32(); // REVIEW: I don't think there is a guarantee on ordering of elements in the immutable dictionary. // unfortunately, we need to sort them to make it deterministic // not sure why CompilationOptions uses SequencialEqual to check options equality // when ordering can change result of it even if contents are same. var count = reader.ReadInt32(); List <KeyValuePair <string, ReportDiagnostic> > specificDiagnosticOptionsList = null; if (count > 0) { specificDiagnosticOptionsList = new List <KeyValuePair <string, ReportDiagnostic> >(count); for (var i = 0; i < count; i++) { var key = reader.ReadString(); var value = (ReportDiagnostic)reader.ReadInt32(); specificDiagnosticOptionsList.Add(KeyValuePair.Create(key, value)); } } specificDiagnosticOptions = specificDiagnosticOptionsList ?? SpecializedCollections.EmptyEnumerable <KeyValuePair <string, ReportDiagnostic> >(); concurrentBuild = reader.ReadBoolean(); deterministic = reader.ReadBoolean(); publicSign = reader.ReadBoolean(); // REVIEW: What should I do with these. are these service required when compilation is built ourselves, not through // compiler. xmlReferenceResolver = XmlFileResolver.Default; sourceReferenceResolver = SourceFileResolver.Default; metadataReferenceResolver = null; assemblyIdentityComparer = DesktopAssemblyIdentityComparer.Default; strongNameProvider = new DesktopStrongNameProvider(); }
public PEAssemblyBuilder( SourceAssemblySymbol sourceAssembly, EmitOptions emitOptions, OutputKind outputKind, Cci.ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources) : base(sourceAssembly, emitOptions, outputKind, serializationProperties, manifestResources, ImmutableArray<NamedTypeSymbol>.Empty) { }
/// <summary> /// Create a compilation given sources /// </summary> /// <param name="sources">Sources to compile</param> /// <param name="outputKind">Output type</param> /// <param name="allowUnsafe">Whether or not use of the unsafe keyword should be allowed</param> /// <returns>The resulting compilation</returns> public static async Task <Compilation> CreateCompilation(SyntaxTree[] sources, TestTargetFramework targetFramework = TestTargetFramework.Net, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, bool allowUnsafe = true, IEnumerable <string>?preprocessorSymbols = null) { var(mdRefs, ancillary) = GetReferenceAssemblies(targetFramework); var referenceAssemblies = await ResolveReferenceAssemblies(mdRefs); // [TODO] Can remove once ancillary logic is removed. if (targetFramework is TestTargetFramework.Net6 or TestTargetFramework.Net) { referenceAssemblies = referenceAssemblies.Add(ancillary); } return(CSharpCompilation.Create("compilation", sources, referenceAssemblies, new CSharpCompilationOptions(outputKind, allowUnsafe: allowUnsafe))); }
// It is identical with the internal extension method 'GetDefaultExtension' defined in OutputKind.cs. // However, we could not apply for InternalVisibleToTest due to other parts of this assembly // complaining about CS0507: "cannot change access modifiers when overriding 'access' inherited member". private static string GetDefaultExtension(OutputKind kind) { switch (kind) { case OutputKind.ConsoleApplication: case OutputKind.WindowsApplication: case OutputKind.WindowsRuntimeApplication: return ".exe"; case OutputKind.DynamicallyLinkedLibrary: return ".dll"; case OutputKind.NetModule: return ".netmodule"; case OutputKind.WindowsRuntimeMetadata: return ".winmdobj"; default: return ".dll"; } }
/// <summary> /// Create a compilation given source /// </summary> /// <param name="source">Source to compile</param> /// <param name="outputKind">Output type</param> /// <param name="allowUnsafe">Whether or not use of the unsafe keyword should be allowed</param> /// <returns>The resulting compilation</returns> public static Task <Compilation> CreateCompilation(string source, TestTargetFramework targetFramework = TestTargetFramework.Net, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, bool allowUnsafe = true, IEnumerable <string>?preprocessorSymbols = null) { return(CreateCompilation(new[] { source }, targetFramework, outputKind, allowUnsafe, preprocessorSymbols)); }
private void CheckManifestString(string source, OutputKind outputKind, string explicitManifest, string expectedManifest) { var dir = Temp.CreateDirectory(); var sourceFile = dir.CreateFile("Test.cs").WriteAllText(source); string outputFileName; string target; switch (outputKind) { case OutputKind.ConsoleApplication: outputFileName = "Test.exe"; target = "exe"; break; case OutputKind.WindowsApplication: outputFileName = "Test.exe"; target = "winexe"; break; case OutputKind.DynamicallyLinkedLibrary: outputFileName = "Test.dll"; target = "library"; break; case OutputKind.NetModule: outputFileName = "Test.netmodule"; target = "module"; break; case OutputKind.WindowsRuntimeMetadata: outputFileName = "Test.winmdobj"; target = "winmdobj"; break; case OutputKind.WindowsRuntimeApplication: outputFileName = "Test.exe"; target = "appcontainerexe"; break; default: throw TestExceptionUtilities.UnexpectedValue(outputKind); } MockCSharpCompiler csc; if (explicitManifest == null) { csc = new MockCSharpCompiler(null, dir.Path, new[] { string.Format("/target:{0}", target), string.Format("/out:{0}", outputFileName), Path.GetFileName(sourceFile.Path), }); } else { var manifestFile = dir.CreateFile("Test.config").WriteAllText(explicitManifest); csc = new MockCSharpCompiler(null, dir.Path, new[] { string.Format("/target:{0}", target), string.Format("/out:{0}", outputFileName), string.Format("/win32manifest:{0}", Path.GetFileName(manifestFile.Path)), Path.GetFileName(sourceFile.Path), }); } int actualExitCode = csc.Run(new StringWriter(CultureInfo.InvariantCulture)); Assert.Equal(0, actualExitCode); //Open as data IntPtr lib = LoadLibraryEx(Path.Combine(dir.Path, outputFileName), IntPtr.Zero, 0x00000002); if (lib == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); const string resourceType = "#24"; var resourceId = outputKind == OutputKind.DynamicallyLinkedLibrary ? "#2" : "#1"; uint manifestSize; if (expectedManifest == null) { Assert.Throws<Win32Exception>(() => Win32Res.GetResource(lib, resourceId, resourceType, out manifestSize)); } else { IntPtr manifestResourcePointer = Win32Res.GetResource(lib, resourceId, resourceType, out manifestSize); string actualManifest = Win32Res.ManifestResourceToXml(manifestResourcePointer, manifestSize); Assert.Equal(expectedManifest, actualManifest); } FreeLibrary(lib); }
public PEDeltaAssemblyBuilder( SourceAssemblySymbol sourceAssembly, EmitOptions emitOptions, OutputKind outputKind, Cci.ModulePropertiesForSerialization serializationProperties, IEnumerable <ResourceDescription> manifestResources, EmitBaseline previousGeneration, IEnumerable <SemanticEdit> edits, Func <ISymbol, bool> isAddedSymbol) : base(sourceAssembly, emitOptions, outputKind, serializationProperties, manifestResources, additionalTypes: ImmutableArray <NamedTypeSymbol> .Empty) { var initialBaseline = previousGeneration.InitialBaseline; var context = new EmitContext(this, null, new DiagnosticBag(), metadataOnly: false, includePrivateMembers: true); // Hydrate symbols from initial metadata. Once we do so it is important to reuse these symbols across all generations, // in order for the symbol matcher to be able to use reference equality once it maps symbols to initial metadata. var metadataSymbols = GetOrCreateMetadataSymbols(initialBaseline, sourceAssembly.DeclaringCompilation); var metadataDecoder = (MetadataDecoder)metadataSymbols.MetadataDecoder; var metadataAssembly = (PEAssemblySymbol)metadataDecoder.ModuleSymbol.ContainingAssembly; var matchToMetadata = new CSharpSymbolMatcher( metadataSymbols.AnonymousTypes, metadataSymbols.AnonymousDelegates, metadataSymbols.AnonymousDelegatesWithFixedTypes, sourceAssembly, context, metadataAssembly); CSharpSymbolMatcher?matchToPrevious = null; if (previousGeneration.Ordinal > 0) { RoslynDebug.AssertNotNull(previousGeneration.Compilation); RoslynDebug.AssertNotNull(previousGeneration.PEModuleBuilder); var previousAssembly = ((CSharpCompilation)previousGeneration.Compilation).SourceAssembly; var previousContext = new EmitContext((PEModuleBuilder)previousGeneration.PEModuleBuilder, null, new DiagnosticBag(), metadataOnly: false, includePrivateMembers: true); matchToPrevious = new CSharpSymbolMatcher( previousGeneration.AnonymousTypeMap, previousGeneration.AnonymousDelegates, previousGeneration.AnonymousDelegatesWithFixedTypes, sourceAssembly: sourceAssembly, sourceContext: context, otherAssembly: previousAssembly, otherContext: previousContext, otherSynthesizedMembers: previousGeneration.SynthesizedMembers, otherDeletedMembers: previousGeneration.DeletedMembers); } _previousDefinitions = new CSharpDefinitionMap(edits, metadataDecoder, matchToMetadata, matchToPrevious); _previousGeneration = previousGeneration; _changes = new CSharpSymbolChanges(_previousDefinitions, edits, isAddedSymbol); // Workaround for https://github.com/dotnet/roslyn/issues/3192. // When compiling state machine we stash types of awaiters and state-machine hoisted variables, // so that next generation can look variables up and reuse their slots if possible. // // When we are about to allocate a slot for a lifted variable while compiling the next generation // we map its type to the previous generation and then check the slot types that we stashed earlier. // If the variable type matches we reuse it. In order to compare the previous variable type with the current one // both need to be completely lowered (translated). Standard translation only goes one level deep. // Generic arguments are not translated until they are needed by metadata writer. // // In order to get the fully lowered form we run the type symbols of stashed variables through a deep translator // that translates the symbol recursively. _deepTranslator = new CSharpSymbolMatcher.DeepTranslator(sourceAssembly.GetSpecialType(SpecialType.System_Object)); }
internal CompilationTestData Evaluate( string source, OutputKind outputKind, string methodName, string expr, out ResultProperties resultProperties, out string error, int atLineNumber = -1, bool includeSymbols = true) { var compilation0 = CreateCompilationWithMscorlib( source, options: (outputKind == OutputKind.DynamicallyLinkedLibrary) ? TestOptions.DebugDll : TestOptions.DebugExe); var runtime = CreateRuntimeInstance(compilation0, includeSymbols); var context = CreateMethodContext(runtime, methodName, atLineNumber); var testData = new CompilationTestData(); ImmutableArray<AssemblyIdentity> missingAssemblyIdentities; var result = context.CompileExpression( expr, DkmEvaluationFlags.TreatAsExpression, NoAliases, DebuggerDiagnosticFormatter.Instance, out resultProperties, out error, out missingAssemblyIdentities, EnsureEnglishUICulture.PreferredOrNull, testData); Assert.Empty(missingAssemblyIdentities); return testData; }
protected override CompilationOptions CommonWithOutputKind(OutputKind kind) { return(WithOutputKind(kind)); }
// Expects correct arguments. internal CompilationOptions( OutputKind outputKind, bool reportSuppressedDiagnostics, string moduleName, string mainTypeName, string scriptClassName, string cryptoKeyContainer, string cryptoKeyFile, ImmutableArray<byte> cryptoPublicKey, bool? delaySign, OptimizationLevel optimizationLevel, bool checkOverflow, Platform platform, ReportDiagnostic generalDiagnosticOption, int warningLevel, ImmutableDictionary<string, ReportDiagnostic> specificDiagnosticOptions, bool concurrentBuild, bool extendedCustomDebugInformation, bool debugPlusMode, XmlReferenceResolver xmlReferenceResolver, SourceReferenceResolver sourceReferenceResolver, MetadataReferenceResolver metadataReferenceResolver, AssemblyIdentityComparer assemblyIdentityComparer, StrongNameProvider strongNameProvider, MetadataImportOptions metadataImportOptions) { this.OutputKind = outputKind; this.ModuleName = moduleName; this.MainTypeName = mainTypeName; this.ScriptClassName = scriptClassName ?? WellKnownMemberNames.DefaultScriptClassName; this.CryptoKeyContainer = cryptoKeyContainer; this.CryptoKeyFile = cryptoKeyFile; this.CryptoPublicKey = cryptoPublicKey.NullToEmpty(); this.DelaySign = delaySign; this.CheckOverflow = checkOverflow; this.Platform = platform; this.GeneralDiagnosticOption = generalDiagnosticOption; this.WarningLevel = warningLevel; this.SpecificDiagnosticOptions = specificDiagnosticOptions; this.ReportSuppressedDiagnostics = reportSuppressedDiagnostics; this.OptimizationLevel = optimizationLevel; this.ConcurrentBuild = concurrentBuild; this.ExtendedCustomDebugInformation = extendedCustomDebugInformation; this.DebugPlusMode = debugPlusMode; this.XmlReferenceResolver = xmlReferenceResolver; this.SourceReferenceResolver = sourceReferenceResolver; this.MetadataReferenceResolver = metadataReferenceResolver; this.StrongNameProvider = strongNameProvider; this.AssemblyIdentityComparer = assemblyIdentityComparer ?? AssemblyIdentityComparer.Default; this.MetadataImportOptions = metadataImportOptions; _lazyErrors = new Lazy<ImmutableArray<Diagnostic>>(() => { var builder = ArrayBuilder<Diagnostic>.GetInstance(); ValidateOptions(builder); return builder.ToImmutableAndFree(); }); }
protected override CompilationOptions CommonWithOutputKind(OutputKind kind) => WithOutputKind(kind);
internal override void ValidateOptions(ArrayBuilder <Diagnostic> builder) { ValidateOptions(builder, MessageProvider.Instance); // /main & /target:{library|netmodule|winmdobj} if (this.MainTypeName != null) { if (this.OutputKind.IsValid() && !this.OutputKind.IsApplication()) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_NoMainOnDLL)); } if (!MainTypeName.IsValidClrTypeName()) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadCompilationOptionValue, nameof(MainTypeName), MainTypeName)); } } if (!Platform.IsValid()) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadPlatformType, Platform.ToString())); } if (ModuleName != null) { MetadataHelpers.CheckAssemblyOrModuleName(ModuleName, MessageProvider.Instance, (int)ErrorCode.ERR_BadModuleName, builder); } if (!OutputKind.IsValid()) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadCompilationOptionValue, nameof(OutputKind), OutputKind.ToString())); } if (!OptimizationLevel.IsValid()) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadCompilationOptionValue, nameof(OptimizationLevel), OptimizationLevel.ToString())); } if (ScriptClassName == null || !ScriptClassName.IsValidClrTypeName()) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadCompilationOptionValue, nameof(ScriptClassName), ScriptClassName ?? "null")); } if (WarningLevel < 0) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadCompilationOptionValue, nameof(WarningLevel), WarningLevel)); } if (Usings != null && Usings.Any(u => !u.IsValidClrNamespaceName())) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadCompilationOptionValue, nameof(Usings), Usings.Where(u => !u.IsValidClrNamespaceName()).First() ?? "null")); } if (Platform == Platform.AnyCpu32BitPreferred && OutputKind.IsValid() && !(OutputKind == OutputKind.ConsoleApplication || OutputKind == OutputKind.WindowsApplication || OutputKind == OutputKind.WindowsRuntimeApplication)) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadPrefer32OnLib)); } if (!MetadataImportOptions.IsValid()) { builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadCompilationOptionValue, nameof(MetadataImportOptions), MetadataImportOptions.ToString())); } // TODO: add check for // (kind == 'arm' || kind == 'appcontainer' || kind == 'winmdobj') && // (version >= "6.2") }
private void GetCompilationAndModuleNames( List<Diagnostic> diagnostics, OutputKind outputKind, List<CommandLineSourceFile> sourceFiles, bool sourceFilesSpecified, string moduleAssemblyName, ref string outputFileName, ref string moduleName, out string compilationName) { string simpleName; if (outputFileName == null) { // In C#, if the output file name isn't specified explicitly, then executables take their // names from the files containing their entrypoints and libraries derive their names from // their first input files. if (!IsInteractive && !sourceFilesSpecified) { AddDiagnostic(diagnostics, ErrorCode.ERR_OutputNeedsName); simpleName = null; } else if (outputKind.IsApplication()) { simpleName = null; } else { simpleName = PathUtilities.RemoveExtension(PathUtilities.GetFileName(sourceFiles.FirstOrDefault().Path)); outputFileName = simpleName + outputKind.GetDefaultExtension(); if (simpleName.Length == 0 && !outputKind.IsNetModule()) { AddDiagnostic(diagnostics, ErrorCode.FTL_InputFileNameTooLong, outputFileName); outputFileName = simpleName = null; } } } else { simpleName = PathUtilities.RemoveExtension(outputFileName); if (simpleName.Length == 0) { AddDiagnostic(diagnostics, ErrorCode.FTL_InputFileNameTooLong, outputFileName); outputFileName = simpleName = null; } } if (outputKind.IsNetModule()) { Debug.Assert(!IsInteractive); compilationName = moduleAssemblyName; } else { if (moduleAssemblyName != null) { AddDiagnostic(diagnostics, ErrorCode.ERR_AssemblyNameOnNonModule); } compilationName = simpleName; } if (moduleName == null) { moduleName = outputFileName; } }
public Task <byte[]> CompileGruntExecutorCode(int id, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, bool Compress = false) { return(_connection.InvokeAsync <byte[]>("CompileGruntExecutorCode", id, outputKind, Compress)); }
private static MetadataReference LoadTestEmittedExecutableForSymbolValidation( ImmutableArray<byte> image, OutputKind outputKind, string display = null) { var moduleMetadata = ModuleMetadata.CreateFromImage(image); moduleMetadata.Module.PretendThereArentNoPiaLocalTypes(); if (outputKind == OutputKind.NetModule) { return moduleMetadata.GetReference(display: display); } else { return AssemblyMetadata.Create(moduleMetadata).GetReference(display: display); } }
// Expects correct arguments. internal CompilationOptions( OutputKind outputKind, string moduleName, string mainTypeName, string scriptClassName, string cryptoKeyContainer, string cryptoKeyFile, bool?delaySign, bool optimize, bool checkOverflow, int fileAlignment, ulong baseAddress, Platform platform, ReportDiagnostic generalDiagnosticOption, int warningLevel, IEnumerable <KeyValuePair <string, ReportDiagnostic> > specificDiagnosticOptions, bool highEntropyVirtualAddressSpace, DebugInformationKind debugInformationKind, SubsystemVersion subsystemVersion, bool concurrentBuild, XmlReferenceResolver xmlReferenceResolver, SourceReferenceResolver sourceReferenceResolver, MetadataReferenceResolver metadataReferenceResolver, MetadataReferenceProvider metadataReferenceProvider, AssemblyIdentityComparer assemblyIdentityComparer, StrongNameProvider strongNameProvider, MetadataImportOptions metadataImportOptions, ImmutableArray <string> features) { this.OutputKind = outputKind; this.ModuleName = moduleName; this.MainTypeName = mainTypeName; this.ScriptClassName = scriptClassName; this.CryptoKeyContainer = cryptoKeyContainer; this.CryptoKeyFile = cryptoKeyFile; this.DelaySign = delaySign; this.CheckOverflow = checkOverflow; this.FileAlignment = fileAlignment; this.BaseAddress = baseAddress; this.Platform = platform; this.GeneralDiagnosticOption = generalDiagnosticOption; this.WarningLevel = warningLevel; this.SpecificDiagnosticOptions = specificDiagnosticOptions.ToImmutableDictionaryOrEmpty(); this.HighEntropyVirtualAddressSpace = highEntropyVirtualAddressSpace; this.DebugInformationKind = debugInformationKind; this.Optimize = optimize; this.ConcurrentBuild = concurrentBuild; this.SubsystemVersion = subsystemVersion; this.XmlReferenceResolver = xmlReferenceResolver; this.SourceReferenceResolver = sourceReferenceResolver; this.MetadataReferenceResolver = metadataReferenceResolver; this.MetadataReferenceProvider = metadataReferenceProvider; this.StrongNameProvider = strongNameProvider; this.AssemblyIdentityComparer = assemblyIdentityComparer ?? AssemblyIdentityComparer.Default; this.MetadataImportOptions = metadataImportOptions; this.Features = features; this.lazyErrors = new Lazy <ImmutableArray <Diagnostic> >(() => { var builder = ArrayBuilder <Diagnostic> .GetInstance(); ValidateOptions(builder); return(builder.ToImmutableAndFree()); }); }
/// <summary> /// Compiles the given compilation to a file. /// </summary> /// <param name="compilation">Compilation</param> /// <param name="outputKind">OutputKind</param> /// <param name="outputPath">OutputPath</param> /// <returns>Output</returns> private static string ToFile(CodeAnalysis.Compilation compilation, OutputKind outputKind, string outputPath) { string assemblyFileName = null; if (outputKind == OutputKind.ConsoleApplication) { assemblyFileName = compilation.AssemblyName + ".exe"; } else if (outputKind == OutputKind.DynamicallyLinkedLibrary) { assemblyFileName = compilation.AssemblyName + ".dll"; } string fileName = null; if (!Configuration.OutputFilePath.Equals("")) { fileName = Configuration.OutputFilePath + Path.DirectorySeparatorChar + assemblyFileName; CompilationEngine.OutputDirectoryMap.Add(compilation.AssemblyName, Configuration.OutputFilePath); } else { fileName = Path.GetDirectoryName(outputPath) + Path.DirectorySeparatorChar + assemblyFileName; CompilationEngine.OutputDirectoryMap.Add(compilation.AssemblyName, Path.GetDirectoryName(outputPath)); } CompilationEngine.ProjectAssemblyPathMap.Add(compilation.AssemblyName, fileName); // Link external references. CompilationEngine.LinkExternalAssembliesToProject(compilation); EmitResult emitResult = null; using (var outputFile = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { emitResult = compilation.Emit(outputFile); if (emitResult.Success) { Output.Print("... Writing " + fileName); return fileName; } } Output.Print("---"); Output.Print("Note: the errors below correspond to the intermediate C#-IR, " + "which can be printed using /debug."); Output.Print("---"); var message = string.Join("\r\n", emitResult.Diagnostics); throw new ApplicationException(message); }
/// <summary> /// Creates a new options instance with the specified output kind. /// </summary> public CompilationOptions WithOutputKind(OutputKind kind) { return(CommonWithOutputKind(kind)); }