From ed67b741ae3e821b8670b3eae9231fe7e6106486 Mon Sep 17 00:00:00 2001 From: triton Date: Mon, 18 Nov 2013 02:40:42 +0000 Subject: [PATCH] Improved error handling of the code compiler code (and minor reformatting). --- src/Generator/Driver.cs | 73 ++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index f8f9df74..a8ec9821 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -1,4 +1,5 @@ -using System.CodeDom.Compiler; +using System; +using System.CodeDom.Compiler; using System.Linq; using System.Reflection; using System.Text; @@ -263,35 +264,47 @@ namespace CppSharp public void CompileCode() { - string assemblyFile; - if (string.IsNullOrEmpty(Options.LibraryName)) - assemblyFile = "out.dll"; - else - assemblyFile = Options.LibraryName + ".dll"; - - var compilerOptions = new StringBuilder(); - compilerOptions.Append(" /doc:" + Path.ChangeExtension(Path.GetFileName(assemblyFile), ".xml")); - compilerOptions.Append(" /debug:pdbonly"); - compilerOptions.Append(" /unsafe"); - - var compilerParameters = new CompilerParameters(); - compilerParameters.GenerateExecutable = false; - compilerParameters.TreatWarningsAsErrors = false; - compilerParameters.OutputAssembly = assemblyFile; - compilerParameters.GenerateInMemory = false; - compilerParameters.CompilerOptions = compilerOptions.ToString(); - compilerParameters.ReferencedAssemblies.Add(typeof(object).Assembly.Location); - var location = Assembly.GetExecutingAssembly().Location; - var locationRuntime = Path.Combine(Path.GetDirectoryName(location), "CppSharp.Runtime.dll"); - compilerParameters.ReferencedAssemblies.Add(locationRuntime); - - var providerOptions = new Dictionary(); - providerOptions.Add("CompilerVersion", "v4.0"); - var csharp = new CSharpCodeProvider(providerOptions); - var cr = csharp.CompileAssemblyFromFile(compilerParameters, Options.CodeFiles.ToArray()); - - foreach (var error in cr.Errors.Cast().Where(error => !error.IsWarning)) - Diagnostics.EmitError(error.ToString()); + try + { + var assemblyFile = string.IsNullOrEmpty(Options.LibraryName) ? + "out.dll" : Options.LibraryName + ".dll"; + + var docFile = Path.ChangeExtension(Path.GetFileName(assemblyFile), ".xml"); + + var compilerOptions = new StringBuilder(); + compilerOptions.Append(" /doc:" + docFile); + compilerOptions.Append(" /debug:pdbonly"); + compilerOptions.Append(" /unsafe"); + + var compilerParameters = new CompilerParameters + { + GenerateExecutable = false, + TreatWarningsAsErrors = false, + OutputAssembly = assemblyFile, + GenerateInMemory = false, + CompilerOptions = compilerOptions.ToString() + }; + + compilerParameters.ReferencedAssemblies.Add(typeof (object).Assembly.Location); + var location = Assembly.GetExecutingAssembly().Location; + var locationRuntime = Path.Combine(Path.GetDirectoryName(location), + "CppSharp.Runtime.dll"); + compilerParameters.ReferencedAssemblies.Add(locationRuntime); + + var codeProvider = new CSharpCodeProvider( + new Dictionary {{"CompilerVersion", "v4.0"}}); + var compilerResults = codeProvider.CompileAssemblyFromFile( + compilerParameters, Options.CodeFiles.ToArray()); + + var errors = compilerResults.Errors.Cast(); + foreach (var error in errors.Where(error => !error.IsWarning)) + Diagnostics.EmitError(error.ToString()); + } + catch (Exception exception) + { + Diagnostics.EmitError("Could not compile the generated source code"); + Diagnostics.EmitMessage(exception.ToString()); + } } public void AddTranslationUnitPass(TranslationUnitPass pass)