public static void RunTest(CSharpFile file) { string code = file.Content.Text.Replace("\r\n", "\n"); Debug.Assert(code.IndexOf('\r') < 0); if (code.Contains("#pragma")) return; // skip code with preprocessor directives if (code.Contains("enum VarianceModifier") || file.FileName.EndsWith("ecore.cs") || file.FileName.EndsWith("method.cs")) return; // skip enum with ; at end (see TypeDeclarationTests.EnumWithSemicolonAtEnd) if (file.FileName.EndsWith("KnownTypeReference.cs") || file.FileName.EndsWith("typemanager.cs") || file.FileName.EndsWith("GetAllBaseTypesTest.cs") || file.FileName.EndsWith("Tokens.cs") || file.FileName.EndsWith("OpCode.cs") || file.FileName.EndsWith("MainWindow.cs")) return; // skip due to optional , at end of array initializer (see ArrayCreateExpressionTests.ArrayInitializerWithCommaAtEnd) if (file.FileName.EndsWith("cs-parser.cs")) return; // skip due to completely messed up comment locations if (file.FileName.Contains("FormattingTests") || file.FileName.Contains("ContextAction") || file.FileName.Contains("CodeCompletion")) return; // skip due to AttributeSectionTests.AttributeWithEmptyParenthesis if (file.FileName.EndsWith("TypeSystemTests.TestCase.cs") || file.FileName.EndsWith("AssemblyInfo.cs")) return; // skip due to AttributeSectionTests.AssemblyAttributeBeforeNamespace if (file.FileName.EndsWith("dynamic.cs") || file.FileName.EndsWith("expression.cs")) return; // skip due to PreprocessorDirectiveTests.NestedInactiveIf if (file.FileName.EndsWith("property.cs")) return; // skip due to PreprocessorDirectiveTests.CommentOnEndOfIfDirective if (file.FileName.EndsWith("DefaultResolvedTypeDefinition.cs")) return; // skip due to MethodDeclarationTests.GenericMethodWithMultipleConstraints Roundtrip(file.Project.CreateParser(), file.FileName, code); // After trying unix-style newlines, also try windows-style newlines: Roundtrip(file.Project.CreateParser(), file.FileName, code.Replace("\n", "\r\n")); }
public static void RunTest(CSharpFile file) { // TODO: also try Windows-style newlines once the parser bug with integer literals followed by \r is fixed string code = file.Content.Text.Replace("\r\n", "\n"); if (code.Contains("#pragma")) return; // skip code with preprocessor directives if (code.Contains("enum VarianceModifier") || file.FileName.EndsWith("ecore.cs") || file.FileName.EndsWith("method.cs")) return; // skip enum with ; at end (see TypeDeclarationTests.EnumWithSemicolonAtEnd) if (file.FileName.EndsWith("KnownTypeReference.cs") || file.FileName.EndsWith("typemanager.cs") || file.FileName.EndsWith("GetAllBaseTypesTest.cs") || file.FileName.EndsWith("Tokens.cs") || file.FileName.EndsWith("OpCode.cs") || file.FileName.EndsWith("MainWindow.cs")) return; // skip due to optional , at end of array initializer (see ArrayCreateExpressionTests.ArrayInitializerWithCommaAtEnd) if (file.FileName.EndsWith("cs-parser.cs")) return; // skip due to completely messed up comment locations if (file.FileName.EndsWith("PrimitiveExpressionTests.cs")) return; // skip due to PrimitiveExpressionTests.*WithLeadingDot if (file.FileName.Contains("FormattingTests") || file.FileName.Contains("ContextAction") || file.FileName.Contains("CodeCompletion")) return; // skip due to AttributeSectionTests.AttributeWithEmptyParenthesis if (file.FileName.EndsWith("TypeSystemTests.TestCase.cs")) return; // skip due to AttributeSectionTests.AssemblyAttributeBeforeNamespace if (file.FileName.EndsWith("dynamic.cs") || file.FileName.EndsWith("expression.cs")) return; // skip due to PreprocessorDirectiveTests.NestedInactiveIf if (file.FileName.EndsWith("property.cs")) return; // skip due to PreprocessorDirectiveTests.CommentOnEndOfIfDirective Roundtrip(file.Project.CreateParser(), file.FileName, code); }
public static void RunTest(CSharpFile file) { CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile); var navigator = new ValidatingResolveAllNavigator(file.FileName); resolver.ApplyNavigator(navigator, CancellationToken.None); navigator.Validate(resolver, file.SyntaxTree); }
public static void RunTest(CSharpFile file) { CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.CompilationUnit, file.ParsedFile); var navigator = new ValidatingResolveAllNavigator(file.FileName); resolver.ApplyNavigator(navigator, CancellationToken.None); navigator.Validate(file.CompilationUnit); }
public static void RunTest(CSharpFile file) { AstNode copy = file.SyntaxTree.Clone(); if (!copy.IsMatch(file.SyntaxTree)) throw new InvalidOperationException("Clone must match the compilation itself; in " + file.FileName); // Mutate identifiers: foreach (var id in copy.Descendants.OfType<Identifier>()) { if (id.Parent is ConstructorDeclaration || id.Parent is DestructorDeclaration) continue; // identifier in ctor/dtor isn't relevant for matches string oldName = id.Name; id.Name = "mutatedName"; if (copy.IsMatch(file.SyntaxTree)) throw new InvalidOperationException("Mutation in " + id.StartLocation + " did not prevent the match; in " + file.FileName); id.Name = oldName; //if (!copy.IsMatch(file.SyntaxTree)) // throw new InvalidOperationException("Clone must match the compilation itself after resetting the mutation"); } // Mutate primitive values: foreach (var pe in copy.Descendants.OfType<PrimitiveExpression>()) { if (pe.Ancestors.Any(a => a is PreProcessorDirective)) continue; object oldVal = pe.Value; pe.Value = "Mutated " + "Value"; if (copy.IsMatch(file.SyntaxTree)) throw new InvalidOperationException("Mutation in " + pe.StartLocation + " did not prevent the match; in " + file.FileName); pe.Value = oldVal; } Console.Write('.'); }
public static void RunTest(CSharpFile file) { CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile); var navigator = new ValidatingResolveAllNavigator(file.FileName); resolver.ApplyNavigator(navigator, CancellationToken.None); navigator.Validate(resolver, file.SyntaxTree); }
public static void RunTest(CSharpFile file) { CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.CompilationUnit, file.ParsedFile); var navigator = new ValidatingResolveAllNavigator(file.FileName); resolver.ApplyNavigator(navigator, CancellationToken.None); navigator.Validate(file.CompilationUnit); }
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 RunTest(CSharpFile file) { int seed; lock (sharedRnd) { seed = sharedRnd.Next(); } Random rnd = new Random(seed); var test = new RandomizedOrderResolverTest(); // Resolve all nodes, but in a random order without using a navigator. test.resolver = new CSharpAstResolver(file.Project.Compilation, file.CompilationUnit, file.ParsedFile); // For comparing whether the results are equivalent, we also use a normal 'resolve all' resolver: test.resolveAllResolver = new CSharpAstResolver(file.Project.Compilation, file.CompilationUnit, file.ParsedFile); test.resolveAllResolver.ApplyNavigator(new ResolveAllNavigator(), CancellationToken.None); // Prepare list of actions that we need to verify: var actions = new List<Func<bool>>(); bool checkResults = rnd.Next(0, 2) == 0; bool checkStateBefore = rnd.Next(0, 2) == 0; bool checkStateAfter = rnd.Next(0, 2) == 0; bool checkConversion = rnd.Next(0, 2) == 0; bool checkExpectedType = rnd.Next(0, 2) == 0; foreach (var _node in file.CompilationUnit.DescendantsAndSelf) { var node = _node; if (CSharpAstResolver.IsUnresolvableNode(node)) continue; if (checkResults) actions.Add(() => test.CheckResult(node)); if (checkStateBefore) actions.Add(() => test.CheckStateBefore(node)); if (checkStateAfter) actions.Add(() => test.CheckStateAfter(node)); var expr = node as Expression; if (expr != null) { if (checkConversion) actions.Add(() => test.CheckConversion(expr)); if (checkExpectedType) actions.Add(() => test.CheckExpectedType(expr)); } } // Fisher-Yates shuffle for (int i = actions.Count - 1; i > 0; i--) { int j = rnd.Next(0, i); var tmp = actions[i]; actions[i] = actions[j]; actions[j] = tmp; } foreach (var action in actions) { if (!action()) { Console.WriteLine("Seed for this file was: " + seed); break; } } }
public CSharpProject(MSBuildBasedProject project) { this.IProject = project; CompilerSettings.AllowUnsafeBlocks = GetBoolProperty("AllowUnsafeBlocks") ?? false; CompilerSettings.CheckForOverflow = GetBoolProperty("CheckForOverflowUnderflow") ?? false; string defineConstants = project.GetEvaluatedProperty("DefineConstants"); foreach (string symbol in defineConstants.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries)) this.CompilerSettings.ConditionalSymbols.Add(symbol.Trim()); // Parse the C# code files foreach (var item in project.GetItemsOfType(ItemType.Compile)) { var file = new CSharpFile(this, FileName.Create(item.FileName)); Files.Add(file); } }
public static void RunTestWithoutUnresolvedFile(CSharpFile file) { CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree); var navigator = new ValidatingResolveAllNavigator(file.FileName); resolver.ApplyNavigator(navigator, CancellationToken.None); navigator.Validate(resolver, file.SyntaxTree); CSharpAstResolver originalResolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile); foreach (var node in file.SyntaxTree.DescendantsAndSelf) { var originalResult = originalResolver.Resolve(node); var result = resolver.Resolve(node); if (!RandomizedOrderResolverTest.IsEqualResolveResult(result, originalResult)) { Console.WriteLine("Got different without IUnresolvedFile at " + file.FileName + ":" + node.StartLocation); } } }
public static void RunTest(CSharpFile file) { string code = file.OriginalText.Replace("\r\n", "\n"); Debug.Assert(code.IndexOf('\r') < 0); if (code.Contains("#pragma")) { return; // skip code with preprocessor directives } if (code.Contains("enum VarianceModifier") || file.FileName.EndsWith("ecore.cs") || file.FileName.EndsWith("method.cs")) { return; // skip enum with ; at end (see TypeDeclarationTests.EnumWithSemicolonAtEnd) } if (file.FileName.EndsWith("KnownTypeReference.cs") || file.FileName.EndsWith("typemanager.cs") || file.FileName.EndsWith("GetAllBaseTypesTest.cs") || file.FileName.EndsWith("Tokens.cs") || file.FileName.EndsWith("OpCode.cs") || file.FileName.EndsWith("MainWindow.cs")) { return; // skip due to optional , at end of array initializer (see ArrayCreateExpressionTests.ArrayInitializerWithCommaAtEnd) } if (file.FileName.EndsWith("cs-parser.cs")) { return; // skip due to completely messed up comment locations } if (file.FileName.Contains("FormattingTests") || file.FileName.Contains("ContextAction") || file.FileName.Contains("CodeCompletion")) { return; // skip due to AttributeSectionTests.AttributeWithEmptyParenthesis } if (file.FileName.EndsWith("TypeSystemTests.TestCase.cs") || file.FileName.EndsWith("AssemblyInfo.cs")) { return; // skip due to AttributeSectionTests.AssemblyAttributeBeforeNamespace } if (file.FileName.EndsWith("dynamic.cs") || file.FileName.EndsWith("expression.cs")) { return; // skip due to PreprocessorDirectiveTests.NestedInactiveIf } if (file.FileName.EndsWith("property.cs")) { return; // skip due to PreprocessorDirectiveTests.CommentOnEndOfIfDirective } if (file.FileName.EndsWith("DefaultResolvedTypeDefinition.cs")) { return; // skip due to MethodDeclarationTests.GenericMethodWithMultipleConstraints } Roundtrip(new CSharpParser(file.Project.CompilerSettings), file.FileName, code); // After trying unix-style newlines, also try windows-style newlines: Roundtrip(new CSharpParser(file.Project.CompilerSettings), file.FileName, code.Replace("\n", "\r\n")); }
public static void RunTest(CSharpFile file) { AstNode copy = file.SyntaxTree.Clone(); if (!copy.IsMatch(file.SyntaxTree)) { throw new InvalidOperationException("Clone must match the compilation itself; in " + file.FileName); } // Mutate identifiers: foreach (var id in copy.Descendants.OfType <Identifier>()) { if (id.Parent is ConstructorDeclaration || id.Parent is DestructorDeclaration) { continue; // identifier in ctor/dtor isn't relevant for matches } string oldName = id.Name; id.Name = "mutatedName"; if (copy.IsMatch(file.SyntaxTree)) { throw new InvalidOperationException("Mutation in " + id.StartLocation + " did not prevent the match; in " + file.FileName); } id.Name = oldName; //if (!copy.IsMatch(file.SyntaxTree)) // throw new InvalidOperationException("Clone must match the compilation itself after resetting the mutation"); } // Mutate primitive values: foreach (var pe in copy.Descendants.OfType <PrimitiveExpression>()) { if (pe.Ancestors.Any(a => a is PreProcessorDirective)) { continue; } object oldVal = pe.Value; pe.Value = "Mutated " + "Value"; if (copy.IsMatch(file.SyntaxTree)) { throw new InvalidOperationException("Mutation in " + pe.StartLocation + " did not prevent the match; in " + file.FileName); } pe.Value = oldVal; } Console.Write('.'); }
public static void RunTest(CSharpFile file) { // TODO: also try Windows-style newlines once the parser bug with integer literals followed by \r is fixed string code = file.Content.Text.Replace("\r\n", "\n"); if (code.Contains("#pragma")) { return; // skip code with preprocessor directives } if (code.Contains("enum VarianceModifier") || file.FileName.EndsWith("ecore.cs") || file.FileName.EndsWith("method.cs")) { return; // skip enum with ; at end (see TypeDeclarationTests.EnumWithSemicolonAtEnd) } if (file.FileName.EndsWith("KnownTypeReference.cs") || file.FileName.EndsWith("typemanager.cs") || file.FileName.EndsWith("GetAllBaseTypesTest.cs") || file.FileName.EndsWith("Tokens.cs") || file.FileName.EndsWith("OpCode.cs") || file.FileName.EndsWith("MainWindow.cs")) { return; // skip due to optional , at end of array initializer (see ArrayCreateExpressionTests.ArrayInitializerWithCommaAtEnd) } if (file.FileName.EndsWith("cs-parser.cs")) { return; // skip due to completely messed up comment locations } if (file.FileName.EndsWith("PrimitiveExpressionTests.cs")) { return; // skip due to PrimitiveExpressionTests.*WithLeadingDot } if (file.FileName.Contains("FormattingTests") || file.FileName.Contains("ContextAction") || file.FileName.Contains("CodeCompletion")) { return; // skip due to AttributeSectionTests.AttributeWithEmptyParenthesis } if (file.FileName.EndsWith("TypeSystemTests.TestCase.cs")) { return; // skip due to AttributeSectionTests.AssemblyAttributeBeforeNamespace } if (file.FileName.EndsWith("dynamic.cs") || file.FileName.EndsWith("expression.cs")) { return; // skip due to PreprocessorDirectiveTests.NestedInactiveIf } if (file.FileName.EndsWith("property.cs")) { return; // skip due to PreprocessorDirectiveTests.CommentOnEndOfIfDirective } Roundtrip(file.Project.CreateParser(), file.FileName, code); }
public static void RunTestWithoutUnresolvedFile(CSharpFile file) { CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree); var navigator = new ValidatingResolveAllNavigator(file.FileName); resolver.ApplyNavigator(navigator, CancellationToken.None); navigator.Validate(resolver, file.SyntaxTree); CSharpAstResolver originalResolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile); foreach (var node in file.SyntaxTree.DescendantsAndSelf) { var originalResult = originalResolver.Resolve(node); var result = resolver.Resolve(node); if (!RandomizedOrderResolverTest.IsEqualResolveResult(result, originalResult)) { Console.WriteLine("Got different without IUnresolvedFile at " + file.FileName + ":" + node.StartLocation); } } }
public CSharpProject(MSBuildBasedProject project) { this.IProject = project; CompilerSettings.AllowUnsafeBlocks = GetBoolProperty("AllowUnsafeBlocks") ?? false; CompilerSettings.CheckForOverflow = GetBoolProperty("CheckForOverflowUnderflow") ?? false; string defineConstants = project.GetEvaluatedProperty("DefineConstants"); foreach (string symbol in defineConstants.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries)) { this.CompilerSettings.ConditionalSymbols.Add(symbol.Trim()); } // Parse the C# code files foreach (var item in project.GetItemsOfType(ItemType.Compile)) { var file = new CSharpFile(this, FileName.Create(item.FileName)); Files.Add(file); } }
public static void RunTest(CSharpFile file) { int seed; lock (sharedRnd) { seed = sharedRnd.Next(); } Random rnd = new Random(seed); var test = new RandomizedOrderResolverTest(); // Resolve all nodes, but in a random order without using a navigator. test.resolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile); // For comparing whether the results are equivalent, we also use a normal 'resolve all' resolver: test.resolveAllResolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile); test.resolveAllResolver.ApplyNavigator(new ResolveAllNavigator(), CancellationToken.None); // Prepare list of actions that we need to verify: var actions = new List <Func <bool> >(); bool checkResults = rnd.Next(0, 2) == 0; bool checkStateBefore = rnd.Next(0, 2) == 0; bool checkStateAfter = rnd.Next(0, 2) == 0; bool checkConversion = rnd.Next(0, 2) == 0; bool checkExpectedType = rnd.Next(0, 2) == 0; foreach (var _node in file.SyntaxTree.DescendantsAndSelf) { var node = _node; if (CSharpAstResolver.IsUnresolvableNode(node)) { continue; } if (checkResults) { actions.Add(() => test.CheckResult(node)); } if (checkStateBefore) { actions.Add(() => test.CheckStateBefore(node)); } if (checkStateAfter) { actions.Add(() => test.CheckStateAfter(node)); } var expr = node as Expression; if (expr != null) { if (checkConversion) { actions.Add(() => test.CheckConversion(expr)); } if (checkExpectedType) { actions.Add(() => test.CheckExpectedType(expr)); } } } // Fisher-Yates shuffle for (int i = actions.Count - 1; i > 0; i--) { int j = rnd.Next(0, i); var tmp = actions[i]; actions[i] = actions[j]; actions[j] = tmp; } foreach (var action in actions) { if (!action()) { Console.WriteLine("Seed for this file was: " + seed); break; } } }
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; }