/// <summary> /// Executes the preprocessor. /// </summary> /// <param name="compilation">The compilation.</param> /// <returns>The processed compilation.</returns> public CommonCompilation ProcessCompilation(CommonCompilation compilation) { var cSharpComp = compilation as Compilation; if (cSharpComp == null) return compilation; var newTrees = new List<SyntaxTree>(); foreach (var tree in cSharpComp.SyntaxTrees) { Semantics = compilation.GetSemanticModel(tree); var newRoot = (CompilationUnitSyntax)Visit(tree.GetRoot()); newTrees.Add(SyntaxTree.Create(tree.FilePath, newRoot, tree.Options)); } return compilation .RemoveAllSyntaxTrees() .AddSyntaxTrees(newTrees); }
/// <summary> /// Executes the preprocessor. /// </summary> /// <param name="compilation">The compilation.</param> /// <returns>The processed compilation.</returns> public CommonCompilation ProcessCompilation(CommonCompilation compilation) { var cSharpComp = compilation as Compilation; if (cSharpComp == null) { return(compilation); } var newTrees = new List <SyntaxTree>(); foreach (var tree in cSharpComp.SyntaxTrees) { Semantics = compilation.GetSemanticModel(tree); var newRoot = (CompilationUnitSyntax)Visit(tree.GetRoot()); newTrees.Add(SyntaxTree.Create(tree.FilePath, newRoot, tree.Options)); } return(compilation .RemoveAllSyntaxTrees() .AddSyntaxTrees(newTrees)); }
private ISemanticModel GetSemanticModelFromSyntaxTree(CommonSyntaxTree syntaxTree, string code, bool includeDocumentation = false) { _systemXmlFilesDir = GetSystemXmlFilesDir(); MetadataReference mscorlib = CreateMetadataReference("mscorlib", includeDocumentation); var metaDllReferences = new List <MetadataReference> { mscorlib }; if (this.Language == Language.VbNet) { //Need to add vb or getting error //Requested operation is not available because the runtime library function 'Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute..ctor' is not defined. MetadataReference vb = CreateMetadataReference("Microsoft.VisualBasic", includeDocumentation); metaDllReferences.Add(vb); } //Eric: this doesn't seem to work so using mscorlib only for now. List <string> gacDlls = GetGacDlls(code); foreach (string dllName in gacDlls) { string dllNameWithoutExtension = dllName.Substring(0, dllName.Length - 4); //remove .dll MetadataReference metaRef = CreateMetadataReference(dllNameWithoutExtension, includeDocumentation); metaDllReferences.Add(metaRef); } Dictionary <string, string> nugGetXmlFileNameToPath = new Dictionary <string, string>(); if (includeDocumentation) { foreach (var nuGetxmlFilePath in NuGetXmlFileReferences) { string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(nuGetxmlFilePath); nugGetXmlFileNameToPath[fileNameWithoutExtension] = nuGetxmlFilePath; } } foreach (var path in GetNonGacDlls()) { string fileName = Path.GetFileNameWithoutExtension(path); RoslynDocumentationProvider documentationProvider = null; if (includeDocumentation && nugGetXmlFileNameToPath.ContainsKey(fileName)) { documentationProvider = new RoslynDocumentationProvider(nugGetXmlFileNameToPath[fileName]); } var reference = new MetadataFileReference(path, MetadataReferenceProperties.Assembly, documentationProvider); metaDllReferences.Add(reference); } //http://msdn.microsoft.com/en-us/vstudio/hh500769.aspx#Toc306015688 CommonCompilation compilation = this.CreateCompilation("Compilation_For_AutoComplete", syntaxTrees: new[] { syntaxTree }, matadataReferences: metaDllReferences); ISemanticModel semanticModel = compilation.GetSemanticModel(syntaxTree); return(semanticModel); }