Beispiel #1
0
        private static void TranslateToExe(SpecSharpOptions commandLineOptions)
        //^ requires commandLineOptions.FileNames.Count > 0;
        {
            HostEnvironment hostEnvironment = new HostEnvironment();

            hostEnvironment.Errors         += hostEnvironment.HandleErrors;
            hostEnvironment.displayFileName = true;
            List <IAssemblyReference>      assemblyReferences = GetAssemblyReferences(commandLineOptions, hostEnvironment);
            List <IModuleReference>        moduleReferences   = new List <IModuleReference>();
            List <SpecSharpSourceDocument> programSources     = new List <SpecSharpSourceDocument>(1);
            IName                      name   = hostEnvironment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(commandLineOptions.FileNames[0]));
            SpecSharpAssembly          assem  = new SpecSharpAssembly(name, Path.GetFullPath(name.Value), hostEnvironment, commandLineOptions, assemblyReferences, moduleReferences, programSources);
            SpecSharpCompilationHelper helper = new SpecSharpCompilationHelper(assem.Compilation);

            foreach (string fileName in commandLineOptions.FileNames)
            {
                name = hostEnvironment.NameTable.GetNameFor(fileName);
                StreamReader instream = File.OpenText(fileName);
                programSources.Add(new SpecSharpSourceDocument(helper, name, Path.GetFullPath(fileName), instream));
            }

            if (assem.Compilation.HasErrors)
            {
                return;
            }
            var sourceLocationProvider = assem.Compilation.SourceLocationProvider;
            var localScopeProvider     = assem.Compilation.LocalScopeProvider;

            using (var pdbWriter = new PdbWriter(Path.ChangeExtension(assem.Location, "pdb"), sourceLocationProvider)) {
                PeWriter.WritePeToStream(assem, hostEnvironment, File.Create(Path.ChangeExtension(assem.Location, "exe")), sourceLocationProvider, localScopeProvider, pdbWriter);
            }
        }
Beispiel #2
0
 static void RunTestSuite(SpecSharpOptions commandLineOptions)
 {
     foreach (string fileName in commandLineOptions.FileNames)
     {
         RunTestSuite(fileName, commandLineOptions);
     }
 }
Beispiel #3
0
    private static void TranslateToExe(SpecSharpOptions commandLineOptions)
      //^ requires commandLineOptions.FileNames.Count > 0;
    {
      HostEnvironment hostEnvironment = new HostEnvironment();
      hostEnvironment.Errors += hostEnvironment.HandleErrors;
      hostEnvironment.displayFileName = true;
      List<IAssemblyReference> assemblyReferences = GetAssemblyReferences(commandLineOptions, hostEnvironment);
      List<IModuleReference> moduleReferences = new List<IModuleReference>();
      List<SpecSharpSourceDocument> programSources = new List<SpecSharpSourceDocument>(1);
      IName name = hostEnvironment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(commandLineOptions.FileNames[0]));
      SpecSharpAssembly assem = new SpecSharpAssembly(name, Path.GetFullPath(name.Value), hostEnvironment, commandLineOptions, assemblyReferences, moduleReferences, programSources);
      SpecSharpCompilationHelper helper = new SpecSharpCompilationHelper(assem.Compilation);
      foreach (string fileName in commandLineOptions.FileNames) {
        name = hostEnvironment.NameTable.GetNameFor(fileName);
        StreamReader instream = File.OpenText(fileName);
        programSources.Add(new SpecSharpSourceDocument(helper, name, Path.GetFullPath(fileName), instream));
      }

      if (assem.Compilation.HasErrors) return;
      var sourceLocationProvider = assem.Compilation.SourceLocationProvider;
      var localScopeProvider = assem.Compilation.LocalScopeProvider;
      using (var pdbWriter = new PdbWriter(Path.ChangeExtension(assem.Location, "pdb"), sourceLocationProvider)) {
        PeWriter.WritePeToStream(assem, hostEnvironment, File.Create(Path.ChangeExtension(assem.Location, "exe")), sourceLocationProvider, localScopeProvider, pdbWriter);
      }
    }
Beispiel #4
0
        public Assembly CompileProject(string projectFileName)
        {
            Assembly existing;
            if (Compilations.TryGetValue(Path.GetFullPath(projectFileName), out existing))
            {
                return existing;
            }

            var project = new Microsoft.Build.BuildEngine.Project();
            project.Load(projectFileName);

            var projectName = Environment.NameTable.GetNameFor(project.EvaluatedProperties["AssemblyName"].Value);
            var projectPath = project.FullFileName;
            var compilerOptions = new SpecSharpOptions();
            var assemblyReferences = new List<IAssemblyReference>();
            var moduleReferences = new List<IModuleReference>();
            var programSources = new List<SpecSharpSourceDocument>();
            var assembly = new SpecSharpAssembly(projectName, projectPath, Environment, compilerOptions, assemblyReferences, moduleReferences, programSources);
            var helper = new SpecSharpCompilationHelper(assembly.Compilation);

            Compilations[Path.GetFullPath(projectFileName)] = assembly;

            assemblyReferences.Add(Environment.LoadAssembly(Environment.CoreAssemblySymbolicIdentity));
            project.Build("ResolveAssemblyReferences");
            foreach (BuildItem item in project.GetEvaluatedItemsByName("ReferencePath"))
            {
                var assemblyName = new System.Reflection.AssemblyName(item.GetEvaluatedMetadata("FusionName"));
                var name = Environment.NameTable.GetNameFor(assemblyName.Name);
                var culture = assemblyName.CultureInfo != null ? assemblyName.CultureInfo.Name : "";
                var version = assemblyName.Version == null ? new Version(0, 0) : assemblyName.Version;
                var token = assemblyName.GetPublicKeyToken();
                if (token == null) token = new byte[0];
                var location = item.FinalItemSpec;
                var identity = new AssemblyIdentity(name, culture, version, token, location);
                var reference = Environment.LoadAssembly(identity);
                assemblyReferences.Add(reference);
            }

            foreach (BuildItem item in project.GetEvaluatedItemsByName("ProjectReference"))
            {
                var name = Environment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(item.FinalItemSpec));
                var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec));
                var reference = CompileProject(location);
                assemblyReferences.Add(reference);
            }

            foreach (BuildItem item in project.GetEvaluatedItemsByName("Compile"))
            {
                var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec));
                var name = Environment.NameTable.GetNameFor(location);
                var programSource = new SpecSharpSourceDocument(helper, name, location, File.ReadAllText(location));
                programSources.Add(programSource);
            }

            return assembly;
        }
Beispiel #5
0
        private static int RunTest(HostEnvironment hostEnvironment, string suiteName, string test, StringBuilder actualOutput, List <string> compilerParameters, List <string> testCaseParameters)
        {
            hostEnvironment.hasError = false;
            IName                     name               = hostEnvironment.NameTable.GetNameFor(suiteName);
            SpecSharpOptions          options            = new SpecSharpOptions(); //TODO: extract from params
            List <IAssemblyReference> assemblyReferences = new List <IAssemblyReference>();
            List <IModuleReference>   moduleReferences   = new List <IModuleReference>();

            assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity));
            IUnit unit;
            SpecSharpAssembly /*?*/    assem = null;
            SpecSharpCompilationHelper helper;

            if (hostEnvironment.previousDocument != null && compilerParameters.Contains("/incremental"))
            {
                unit   = hostEnvironment.GetIncrementalUnit(test);
                helper = (SpecSharpCompilationHelper)hostEnvironment.previousDocument.SpecSharpCompilationPart.Helper;
            }
            else
            {
                List <SpecSharpSourceDocument> programSources = new List <SpecSharpSourceDocument>(1);
                assem  = new SpecSharpAssembly(name, "", hostEnvironment, options, assemblyReferences, moduleReferences, programSources);
                helper = new SpecSharpCompilationHelper(assem.Compilation);
                programSources.Add(hostEnvironment.previousDocument = new SpecSharpSourceDocument(helper, name, "", test));
                unit = assem;
            }
            if (assem != null && assem.Compilation.HasErrors)
            {
                return(0);
            }
            if (assem != null && assem.EntryPoint.ResolvedMethod != Dummy.Method)
            {
                var memStream = new MemoryStream();
                PeWriter.WritePeToStream(assem, hostEnvironment, memStream);
                if (hostEnvironment.hasError)
                {
                    return(0);
                }
                var runtimeAssembly = System.Reflection.Assembly.Load(memStream.ToArray());
                var result          = runtimeAssembly.EntryPoint.Invoke(null, null);
                if (result is int)
                {
                    return((int)result);
                }
                return(0);
            }

            BaseCodeTraverser traverser = new BaseCodeTraverser();

            unit.Dispatch(traverser);
            return(0);
        }
Beispiel #6
0
 static void RunTestSuite(string fileName, SpecSharpOptions commandLineOptions) {
   if (Directory.Exists(fileName)) {
     int errorCount = 0;
     foreach (FileInfo fi in new DirectoryInfo(fileName).GetFiles("*", SearchOption.AllDirectories)) {
       if (!RunTestSuite(fi.Name, new StreamReader(fi.Open(FileMode.Open, FileAccess.Read))))
         errorCount++;
     }
     if (errorCount != 0)
       Console.WriteLine("\n\n*** {0} error(s) ***\n", errorCount);
   } else {
     RunTestSuite(fileName, File.OpenText(fileName));
   }
 }
Beispiel #7
0
    private int startOfCurrentLine; //^ invariant 0 <= startOfCurrentLine && startOfCurrentLine <= fragmentIndex;

    /// <summary>
    /// Allocates a preprocessor instance that can be queried for a list of source locations that represent the output from the preprocessor. This output
    /// omits preprocessor directives as well as any excluded sections. It also applies line directives by mapping some of the produced source locations
    /// onto the documents mentioned in the line directives. In other words, the produced source locations will not necessarily come from the document
    /// being preprocessed. Preprocessing happens lazily as the result of calling GetIncludedSections is enumerated.
    /// </summary>
    /// <param name="documentToProcess">The source document to preprocess.</param>
    /// <param name="options">An object that specifies any preprocessor symbols that are defined as compiler options by the environment.</param>
    internal Preprocessor(IPrimarySourceDocument documentToProcess, SpecSharpOptions options) {
      //TODO: use Dictionary<string,string> instead of StringDictionary.
      this.documentToProcess = documentToProcess;
      List<IErrorMessage> errors = this.errors = new List<IErrorMessage>();
      this.preprocessorInformation = new PreprocessorInformation(documentToProcess, errors);
      Dictionary<string, string> preprocessorDefinedSymbols = new Dictionary<string, string>();
      foreach (string definedSymbol in options.DefinedSymbols)
        preprocessorDefinedSymbols[definedSymbol] = definedSymbol;
      preprocessorDefinedSymbols["true"] = "true";
      preprocessorDefinedSymbols.Remove("false");
      this.preprocessorDefinedSymbols = preprocessorDefinedSymbols;
      this.buffer = new char[16];
      //TODO: make this a more realistic size once simple testing is done.
    }
Beispiel #8
0
 private static List<IAssemblyReference> GetAssemblyReferences(SpecSharpOptions commandLineOptions, HostEnvironment hostEnvironment) {
   List<IAssemblyReference> assemblyReferences = new List<IAssemblyReference>();
   assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity));
   foreach (string assemblyReference in commandLineOptions.ReferencedAssemblies) {
     IUnit unit = hostEnvironment.LoadUnitFrom(assemblyReference);
     if (unit == Dummy.Unit) {
       unit = hostEnvironment.LoadUnitFrom(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), assemblyReference));
     }
     IAssembly/*?*/ aref = unit as IAssembly;
     if (aref != null)
       assemblyReferences.Add(aref);
     else {
       //TODO: error message
     }
   }
   return assemblyReferences;
 }
Beispiel #9
0
 static void RunTestSuite(string fileName, SpecSharpOptions commandLineOptions)
 {
     if (Directory.Exists(fileName))
     {
         int errorCount = 0;
         foreach (FileInfo fi in new DirectoryInfo(fileName).GetFiles("*", SearchOption.AllDirectories))
         {
             if (!RunTestSuite(fi.Name, new StreamReader(fi.Open(FileMode.Open, FileAccess.Read))))
             {
                 errorCount++;
             }
         }
         if (errorCount != 0)
         {
             Console.WriteLine("\n\n*** {0} error(s) ***\n", errorCount);
         }
     }
     else
     {
         RunTestSuite(fileName, File.OpenText(fileName));
     }
 }
Beispiel #10
0
        static void Main(string[] args)
        {
            HostEnvironment hostEnvironment = new HostEnvironment();

            hostEnvironment.Errors += hostEnvironment.HandleErrors;
            SpecSharpOptions commandLineOptions = OptionParser.ParseCommandLineArguments(hostEnvironment, args);

            if (hostEnvironment.hasError)
            {
                return;
            }
            if (commandLineOptions.DisplayCommandLineHelp)
            {
                DisplayCommandLineHelp(OptionParser.GetOptionUsage());
            }
            else if (commandLineOptions.RunTestSuite)
            {
                RunTestSuite(commandLineOptions);
            }
            else
            {
                TranslateToExe(commandLineOptions);
            }
        }
Beispiel #11
0
        private static List <IAssemblyReference> GetAssemblyReferences(SpecSharpOptions commandLineOptions, HostEnvironment hostEnvironment)
        {
            List <IAssemblyReference> assemblyReferences = new List <IAssemblyReference>();

            assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity));
            foreach (string assemblyReference in commandLineOptions.ReferencedAssemblies)
            {
                IUnit unit = hostEnvironment.LoadUnitFrom(assemblyReference);
                if (unit == Dummy.Unit)
                {
                    unit = hostEnvironment.LoadUnitFrom(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), assemblyReference));
                }
                IAssembly /*?*/ aref = unit as IAssembly;
                if (aref != null)
                {
                    assemblyReferences.Add(aref);
                }
                else
                {
                    //TODO: error message
                }
            }
            return(assemblyReferences);
        }
Beispiel #12
0
 internal SpecSharpModule(IName name, string location, ISourceEditHost hostEnvironment, SpecSharpOptions options, IAssembly containingAssembly,
                          IEnumerable <IAssemblyReference> assemblyReferences, IEnumerable <IModuleReference> moduleReferences, IEnumerable <CompilationPart> compilationParts)
 //TODO: pass in information about which assemblies belong to which named unit sets
     : base(name, location, containingAssembly, assemblyReferences, moduleReferences)
 {
     this.options          = options;
     this.hostEnvironment  = hostEnvironment;
     this.compilationParts = compilationParts;
 }
Beispiel #13
0
    private static int RunTest(HostEnvironment hostEnvironment, string suiteName, string test, StringBuilder actualOutput, List<string> compilerParameters, List<string> testCaseParameters) {
      hostEnvironment.hasError = false;
      IName name = hostEnvironment.NameTable.GetNameFor(suiteName);
      SpecSharpOptions options = new SpecSharpOptions(); //TODO: extract from params
      List<IAssemblyReference> assemblyReferences = new List<IAssemblyReference>();
      List<IModuleReference> moduleReferences = new List<IModuleReference>();
      assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity));
      IUnit unit;
      SpecSharpAssembly/*?*/ assem = null;
      SpecSharpCompilationHelper helper;
      if (hostEnvironment.previousDocument != null && compilerParameters.Contains("/incremental")) {
        unit = hostEnvironment.GetIncrementalUnit(test);
        helper = (SpecSharpCompilationHelper)hostEnvironment.previousDocument.SpecSharpCompilationPart.Helper;
      } else {
        List<SpecSharpSourceDocument> programSources = new List<SpecSharpSourceDocument>(1);
        assem = new SpecSharpAssembly(name, "", hostEnvironment, options, assemblyReferences, moduleReferences, programSources);
        helper = new SpecSharpCompilationHelper(assem.Compilation);
        programSources.Add(hostEnvironment.previousDocument = new SpecSharpSourceDocument(helper, name, "", test));
        unit = assem;
      }
      if (assem != null && assem.Compilation.HasErrors) return 0;
      if (assem != null && assem.EntryPoint.ResolvedMethod != Dummy.Method) {
        var memStream = new MemoryStream();
        PeWriter.WritePeToStream(assem, hostEnvironment, memStream);
        if (hostEnvironment.hasError) return 0;
        var runtimeAssembly = System.Reflection.Assembly.Load(memStream.ToArray());
        var result = runtimeAssembly.EntryPoint.Invoke(null, null);
        if (result is int) return (int)result;
        return 0;
      }

      BaseCodeTraverser traverser = new BaseCodeTraverser();
      unit.Dispatch(traverser);
      return 0;
    }
Beispiel #14
0
        public Assembly CompileProject(string projectFileName)
        {
            Assembly existing;

            if (Compilations.TryGetValue(Path.GetFullPath(projectFileName), out existing))
            {
                return(existing);
            }

            var project = new Microsoft.Build.BuildEngine.Project();

            project.Load(projectFileName);

            var projectName        = Environment.NameTable.GetNameFor(project.EvaluatedProperties["AssemblyName"].Value);
            var projectPath        = project.FullFileName;
            var compilerOptions    = new SpecSharpOptions();
            var assemblyReferences = new List <IAssemblyReference>();
            var moduleReferences   = new List <IModuleReference>();
            var programSources     = new List <SpecSharpSourceDocument>();
            var assembly           = new SpecSharpAssembly(projectName, projectPath, Environment, compilerOptions, assemblyReferences, moduleReferences, programSources);
            var helper             = new SpecSharpCompilationHelper(assembly.Compilation);

            Compilations[Path.GetFullPath(projectFileName)] = assembly;

            assemblyReferences.Add(Environment.LoadAssembly(Environment.CoreAssemblySymbolicIdentity));
            project.Build("ResolveAssemblyReferences");
            foreach (BuildItem item in project.GetEvaluatedItemsByName("ReferencePath"))
            {
                var assemblyName = new System.Reflection.AssemblyName(item.GetEvaluatedMetadata("FusionName"));
                var name         = Environment.NameTable.GetNameFor(assemblyName.Name);
                var culture      = assemblyName.CultureInfo != null ? assemblyName.CultureInfo.Name : "";
                var version      = assemblyName.Version == null ? new Version(0, 0) : assemblyName.Version;
                var token        = assemblyName.GetPublicKeyToken();
                if (token == null)
                {
                    token = new byte[0];
                }
                var location  = item.FinalItemSpec;
                var identity  = new AssemblyIdentity(name, culture, version, token, location);
                var reference = Environment.LoadAssembly(identity);
                assemblyReferences.Add(reference);
            }

            foreach (BuildItem item in project.GetEvaluatedItemsByName("ProjectReference"))
            {
                var name      = Environment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(item.FinalItemSpec));
                var location  = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec));
                var reference = CompileProject(location);
                assemblyReferences.Add(reference);
            }

            foreach (BuildItem item in project.GetEvaluatedItemsByName("Compile"))
            {
                var location      = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec));
                var name          = Environment.NameTable.GetNameFor(location);
                var programSource = new SpecSharpSourceDocument(helper, name, location, File.ReadAllText(location));
                programSources.Add(programSource);
            }

            return(assembly);
        }
Beispiel #15
0
 internal SpecSharpAssembly(IName name, string location, ISourceEditHost hostEnvironment, SpecSharpOptions options,
                            IEnumerable <IAssemblyReference> assemblyReferences, IEnumerable <IModuleReference> moduleReferences, IEnumerable <CompilationPart> compilationParts)
     : base(name, location, name, assemblyReferences, moduleReferences, new List <IResourceReference>(0).AsReadOnly(), new List <IFileReference>(0).AsReadOnly())
 {
     this.options          = options;
     this.hostEnvironment  = hostEnvironment;
     this.compilationParts = compilationParts;
 }
Beispiel #16
0
 public SpecSharpAssembly(IName name, string location, ISourceEditHost hostEnvironment, SpecSharpOptions options,
                          IEnumerable <IAssemblyReference> assemblyReferences, IEnumerable <IModuleReference> moduleReferences, IEnumerable <SpecSharpSourceDocument> programSources)
     : base(name, location, name, assemblyReferences, moduleReferences, new List <IResourceReference>(0).AsReadOnly(), new List <IFileReference>(0).AsReadOnly())
 {
     this.options         = options;
     this.hostEnvironment = hostEnvironment;
     this.programSources  = programSources;
 }
Beispiel #17
0
 static void RunTestSuite(SpecSharpOptions commandLineOptions) {
   foreach (string fileName in commandLineOptions.FileNames)
     RunTestSuite(fileName, commandLineOptions);
 }
Beispiel #18
0
 public SpecSharpModule(IName name, string location, ISourceEditHost hostEnvironment, SpecSharpOptions options, IAssembly containingAssembly,
                        IEnumerable <IAssemblyReference> assemblyReferences, IEnumerable <IModuleReference> moduleReferences, IEnumerable <SpecSharpSourceDocument> programSources)
 //TODO: pass in information about which assemblies belong to which named unit sets
     : base(name, location, containingAssembly, assemblyReferences, moduleReferences)
 {
     this.options         = options;
     this.hostEnvironment = hostEnvironment;
     this.programSources  = programSources;
 }