public CSharpProject(Solution solution, string title, string fileName) { this.Solution = solution; this.Title = title; this.FileName = fileName; var p = new Microsoft.Build.Evaluation.Project(fileName); this.AssemblyName = p.GetPropertyValue("AssemblyName"); this.CompilerSettings.AllowUnsafeBlocks = GetBoolProperty(p, "AllowUnsafeBlocks") ?? false; this.CompilerSettings.CheckForOverflow = GetBoolProperty(p, "CheckForOverflowUnderflow") ?? false; foreach (string symbol in p.GetPropertyValue("DefineConstants").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { this.CompilerSettings.ConditionalSymbols.Add(symbol.Trim()); } foreach (var item in p.GetItems("Compile")) { Files.Add(new CSharpFile(this, Path.Combine(p.DirectoryPath, item.EvaluatedInclude))); } List<IAssemblyReference> references = new List<IAssemblyReference>(); string mscorlib = FindAssembly(Program.AssemblySearchPaths, "mscorlib"); if (mscorlib != null) { references.Add(Program.LoadAssembly(mscorlib)); } else { Console.WriteLine("Could not find mscorlib"); } bool hasSystemCore = false; foreach (var item in p.GetItems("Reference")) { string assemblyFileName = null; if (item.HasMetadata("HintPath")) { assemblyFileName = Path.Combine(p.DirectoryPath, item.GetMetadataValue("HintPath")); if (!File.Exists(assemblyFileName)) assemblyFileName = null; } if (assemblyFileName == null) { assemblyFileName = FindAssembly(Program.AssemblySearchPaths, item.EvaluatedInclude); } if (assemblyFileName != null) { if (Path.GetFileName(assemblyFileName).Equals("System.Core.dll", StringComparison.OrdinalIgnoreCase)) hasSystemCore = true; references.Add(Program.LoadAssembly(assemblyFileName)); } else { Console.WriteLine("Could not find referenced assembly " + item.EvaluatedInclude); } } if (!hasSystemCore && FindAssembly(Program.AssemblySearchPaths, "System.Core") != null) references.Add(Program.LoadAssembly(FindAssembly(Program.AssemblySearchPaths, "System.Core"))); foreach (var item in p.GetItems("ProjectReference")) { references.Add(new ProjectReference(solution, item.GetMetadataValue("Name"))); } this.ProjectContent = new CSharpProjectContent() .SetAssemblyName(this.AssemblyName) .SetCompilerSettings(this.CompilerSettings) .AddAssemblyReferences(references) .UpdateProjectContent(null, Files.Select(f => f.ParsedFile)); }
public static void Main(string[] args) { using (new Timer("Loading solution... ")) { solution = new Solution(SolutionFile); } Console.WriteLine("Loaded {0} lines of code ({1:f1} MB) in {2} files in {3} projects.", solution.AllFiles.Sum(f => 1 + f.OriginalText.Count(c => c == '\n')), solution.AllFiles.Sum(f => f.OriginalText.Length) / 1024.0 / 1024.0, solution.AllFiles.Count(), solution.Projects.Count); //VisitorBenchmark.Run(solution.AllFiles.Select(f => f.SyntaxTree)); using (new Timer("ID String test... ")) TypeSystemTests.IDStringConsistencyCheck(solution); using (new Timer("Resolve unresolved members... ")) TypeSystemTests.ResolvedUnresolvedMembers(solution); //RunTestOnAllFiles("Roundtripping test", RoundtripTest.RunTest); RunTestOnAllFiles("Resolver test", ResolverTest.RunTest); RunTestOnAllFiles("Resolver test (no parsed file)", ResolverTest.RunTestWithoutUnresolvedFile); RunTestOnAllFiles("Resolver test (randomized order)", RandomizedOrderResolverTest.RunTest); new FindReferencesConsistencyCheck(solution).Run(); RunTestOnAllFiles("Pattern Matching test", PatternMatchingTest.RunTest); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
public static void ResolvedUnresolvedMembers(Solution solution) { foreach (var project in solution.Projects) { var compilation = project.Compilation; var assemblyContext = new SimpleTypeResolveContext(compilation.MainAssembly); foreach (var typeDef in compilation.MainAssembly.GetAllTypeDefinitions()) { foreach (var part in typeDef.Parts) { if (!typeDef.Equals(part.Resolve(assemblyContext))) throw new InvalidOperationException(); } foreach (var member in IncludeAccessors(typeDef.Members)) { var resolvedMember = member.UnresolvedMember.Resolve(assemblyContext); if (!member.Equals(resolvedMember)) throw new InvalidOperationException(); } // ToMemberReference() requires an appropriate generic context if the member // contains open generics; otherwise the main context of the compilation is sufficient. ITypeResolveContext context; if (typeDef.TypeParameterCount > 0) context = new SimpleTypeResolveContext(typeDef); else context = compilation.TypeResolveContext; // Include (potentially specialized) inherited members when testing ToMemberReference() foreach (var member in IncludeAccessors(typeDef.GetMembers())) { var resolvedMember = member.ToMemberReference().Resolve(context); if (!member.Equals(resolvedMember)) throw new InvalidOperationException(); } } } }
public static void IDStringConsistencyCheck(Solution solution) { foreach (var project in solution.Projects) { var compilation = project.Compilation; HashSet<string> idStrings = new HashSet<string>(); var context = compilation.TypeResolveContext; foreach (var typeDef in compilation.MainAssembly.GetAllTypeDefinitions()) { Check(typeDef, context, idStrings); foreach (var member in typeDef.Members) { Check(member, context, idStrings); } } } }
public CSharpProject(Solution solution, string title, string fileName) { // Normalize the file name fileName = Path.GetFullPath(fileName); this.Solution = solution; this.Title = title; this.FileName = fileName; // Use MSBuild to open the .csproj var msbuildProject = new Microsoft.Build.Evaluation.Project(fileName); // Figure out some compiler settings this.AssemblyName = msbuildProject.GetPropertyValue("AssemblyName"); this.CompilerSettings.AllowUnsafeBlocks = GetBoolProperty(msbuildProject, "AllowUnsafeBlocks") ?? false; this.CompilerSettings.CheckForOverflow = GetBoolProperty(msbuildProject, "CheckForOverflowUnderflow") ?? false; string defineConstants = msbuildProject.GetPropertyValue("DefineConstants"); foreach (string symbol in defineConstants.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries)) this.CompilerSettings.ConditionalSymbols.Add(symbol.Trim()); // Initialize the unresolved type system IProjectContent pc = new CSharpProjectContent(); pc = pc.SetAssemblyName(this.AssemblyName); pc = pc.SetProjectFileName(fileName); pc = pc.SetCompilerSettings(this.CompilerSettings); // Parse the C# code files foreach (var item in msbuildProject.GetItems("Compile")) { var file = new CSharpFile(this, Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude)); Files.Add(file); } // Add parsed files to the type system pc = pc.AddOrUpdateFiles(Files.Select(f => f.UnresolvedTypeSystemForFile)); // Add referenced assemblies: foreach (string assemblyFile in ResolveAssemblyReferences(msbuildProject)) { IUnresolvedAssembly assembly = solution.LoadAssembly(assemblyFile); pc = pc.AddAssemblyReferences(new [] { assembly }); } // Add project references: foreach (var item in msbuildProject.GetItems("ProjectReference")) { string referencedFileName = Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude); // Normalize the path; this is required to match the name with the referenced project's file name referencedFileName = Path.GetFullPath(referencedFileName); pc = pc.AddAssemblyReferences(new[] { new ProjectReference(referencedFileName) }); } this.ProjectContent = pc; }
public static void Main(string[] args) { using (new Timer("Loading solution... ")) { solution = new Solution(Path.GetFullPath("../../../NRefactory.sln")); } Console.WriteLine("Loaded {0} lines of code ({1:f1} MB) in {2} files in {3} projects.", solution.AllFiles.Sum(f => f.LinesOfCode), solution.AllFiles.Sum(f => f.Content.TextLength) / 1024.0 / 1024.0, solution.AllFiles.Count(), solution.Projects.Count); //RunTestOnAllFiles("Roundtripping test", RoundtripTest.RunTest); RunTestOnAllFiles("Resolver test", ResolverTest.RunTest); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
public static void Main(string[] args) { using (new Timer("Loading solution... ")) { solution = new Solution(SolutionFile); } Console.WriteLine("Loaded {0} lines of code ({1:f1} MB) in {2} files in {3} projects.", solution.AllFiles.Sum(f => f.LinesOfCode), solution.AllFiles.Sum(f => f.Content.TextLength) / 1024.0 / 1024.0, solution.AllFiles.Count(), solution.Projects.Count); //RunTestOnAllFiles("Roundtripping test", RoundtripTest.RunTest); RunTestOnAllFiles("Resolver test", ResolverTest.RunTest); RunTestOnAllFiles("Resolver test (randomized order)", RandomizedOrderResolverTest.RunTest); new FindReferencesConsistencyCheck(solution).Run(); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
public FindReferencesConsistencyCheck(Solution solution) { this.solution = solution; }
public ProjectReference(Solution solution, string projectTitle) { this.solution = solution; this.projectTitle = projectTitle; }