private void WholeTreeVerificationAnalysis(SyntaxTreeAnalysisContext treeContext) { lock (_mutex) { if (treeContext.Tree.GetText().ToString().Length < 10) { return; } var translationProcess = TranslationProcess.CreateFromSyntaxTree(treeContext.Tree); var result = translationProcess.Execute(); /* * Do not double-report errors. These are already reported by WholeTreeTranslationAnalysis. * * foreach (var diagnostic in result.Errors) * { * * if (diagnostic.Node != null) * { * treeContext.ReportDiagnostic(Diagnostic.Create( * rules[diagnostic.Diagnostic.ErrorCode], diagnostic.Node.GetLocation(), * diagnostic.DiagnosticArguments) * ); * } * else * { * treeContext.ReportDiagnostic(Diagnostic.Create( * rules[diagnostic.Diagnostic.ErrorCode], null, diagnostic.DiagnosticArguments) * ); * } * } */ if (!result.WasTranslationSuccessful) { return; // translation errors are handled by the other method } if (result.Silvernode.ToString().Trim() == "") { return; } var verifier = new CarbonNailgunBackend(); var verificationResult = verifier.Verify(result.Silvernode); foreach (var diagnostic in verificationResult.Errors) { if (diagnostic.Node != null) { treeContext.ReportDiagnostic(Diagnostic.Create( rules[diagnostic.Diagnostic.ErrorCode], diagnostic.Node.GetLocation(), diagnostic.DiagnosticArguments) ); } else { treeContext.ReportDiagnostic(Diagnostic.Create( rules[diagnostic.Diagnostic.ErrorCode], null, diagnostic.DiagnosticArguments) ); } } } }
public override void CollectTypesInto(TranslationProcess translationProcess, SemanticModel semantics) { foreach (Sharpnode sharpnode in this.children) { sharpnode.CollectTypesInto(translationProcess, semantics); } }
public static TranslationProcessResult Translate(string filename) { TranslationProcess process = TranslationProcess.Create(new List <string> { filename }, new List <string>(), new List <string>(), new TranslationConfiguration()); var result = process.Execute(); return(result); }
public void TransOnly(string test) { string dir = AppDomain.CurrentDomain.BaseDirectory; string fullFilename = System.IO.Path.Combine(dir, test); var translation = TranslationProcess.Create(new List <string>() { fullFilename }, new List <string>(), new List <string>(), new TranslationConfiguration()); var result = translation.Execute(); Assert.True(result.WasTranslationSuccessful, string.Join("\n", result.Errors)); }
public override void CollectTypesInto(TranslationProcess translationProcess, SemanticModel semantics) { var classSymbol = semantics.GetDeclaredSymbol(this.OriginalNode as ClassDeclarationSyntax); // Should translate at all? var attributes = classSymbol.GetAttributes(); switch (VerificationSettings.ShouldVerify(attributes, translationProcess.Configuration.VerifyUnmarkedItems)) { case VerificationSetting.DoNotVerify: return; case VerificationSetting.Contradiction: return; } this.TypeIfCollected = translationProcess.AddToCollectedTypes(this, semantics); foreach (Sharpnode node in this.children) { // Collect fields if (node is FieldDeclarationSharpnode) { FieldDeclarationSharpnode fieldDeclaration = (FieldDeclarationSharpnode)node; var fieldSymbol = fieldDeclaration.GetSymbol(semantics); if (fieldSymbol.IsConst) { continue; // Constants are inlined. } if (fieldSymbol.IsStatic) { translationProcess.AddError(new Error(Diagnostics.SSIL108_FeatureNotSupported, node.OriginalNode, "static fields")); continue; } var typeSymbol = fieldSymbol.Type; Error error; this.TypeIfCollected.InstanceFields.Add( new CollectedField( translationProcess.IdentifierTranslator.RegisterAndGetIdentifier(((FieldDeclarationSharpnode)node).GetSymbol(semantics)), TypeTranslator.TranslateType(typeSymbol, node.OriginalNode, out error) )); if (error != null) { translationProcess.AddError(error); } } } }
public void Compare(string test) { string dir = AppDomain.CurrentDomain.BaseDirectory; string fullFilename = System.IO.Path.Combine(dir, test); string fullSilvername = System.IO.Path.Combine(dir, System.IO.Path.GetDirectoryName(test), System.IO.Path.GetFileNameWithoutExtension(test) + ".vpr"); var translation = TranslationProcess.Create(new List <string>() { fullFilename }, new List <string>(), new List <string>(), new TranslationConfiguration()); var result = translation.Execute(); Assert.True(result.WasTranslationSuccessful, string.Join("\n", result.Errors)); string silverResult = Reduce(result.Silvernode.ToString()); string silverExpected = Reduce(System.IO.File.ReadAllText(fullSilvername)); Assert.Equal(silverExpected, silverResult); }
private void WholeTreeTranslationAnalysis(SyntaxTreeAnalysisContext treeContext) { lock (_mutex) { var translationProcess = TranslationProcess.CreateFromSyntaxTree(treeContext.Tree); var result = translationProcess.Execute(); foreach (var diagnostic in result.Errors) { if (diagnostic.Node != null) { treeContext.ReportDiagnostic(Diagnostic.Create( rules[diagnostic.Diagnostic.ErrorCode], diagnostic.Node.GetLocation(), diagnostic.DiagnosticArguments) ); } else { treeContext.ReportDiagnostic(Diagnostic.Create( rules[diagnostic.Diagnostic.ErrorCode], null, diagnostic.DiagnosticArguments) ); } } } }
public void Sys(string test) { if (test.Contains("Simple")) { } string dir = AppDomain.CurrentDomain.BaseDirectory; string fullFilename = System.IO.Path.Combine(dir, test); string csharpCode = System.IO.File.ReadAllText(fullFilename); var translation = TranslationProcess.Create( new List <string> { fullFilename }, new List <string>(), new List <string>(), new TranslationConfiguration() ); var result = translation.Execute(); var lines = csharpCode.Split('\n'); var expectedErrorcodes = new List <Tuple <string, int> >(); bool syntaxOnly = false; int lineNumber = 0; IBackend backend = new CarbonNailgunBackend(); foreach (string line in lines) { lineNumber++; string trimmed = line.Trim().ToLower(); if (trimmed.StartsWith("//")) { string[] words = trimmed.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (words.Length < 2) { continue; } if (words[1] == "use") { if (words.Length >= 3) { if (words[2] == "silicon") { backend = new SiliconNailgunBackend(); } else if (words[2] == "carbon") { backend = new CarbonNailgunBackend(); } } } if (words[1] == "syntax") { syntaxOnly = true; } if (words[1] == "expect" && words.Length >= 3) { string code = words[2]; int errorNumber = -1; if (words.Length == 5) { if (words[3] == "at") { if (words[4] == "next") { errorNumber = lineNumber + 1; } } } expectedErrorcodes.Add(new Tuple <string, int>(code, errorNumber)); } } } var errors = result.Errors; if (!syntaxOnly && result.Errors.Count == 0) { var verificationResult = backend.Verify(result.Silvernode); errors = verificationResult.Errors; } bool someKindOfFailureIsExpected = expectedErrorcodes.Any(); Assert.True(expectedErrorcodes.Count == errors.Count, "Expected error count: " + expectedErrorcodes.Count + "\nActual errors: " + errors.Count + "\n" + string.Join("\n", errors)); foreach (var error in errors) { var appropriateTuple = expectedErrorcodes.Find(tuple => tuple.Item1 == error.Diagnostic.ErrorCode.ToLower() && (tuple.Item2 == -1 || tuple.Item2 == error.CsharpLine)); if (appropriateTuple != null) { expectedErrorcodes.Remove(appropriateTuple); } else { Assert.True(false, "Actual:\n" + string.Join("\n", errors) + "Expected:\n" + string.Join("\n", expectedErrorcodes)); } } Assert.True(result.WasTranslationSuccessful || someKindOfFailureIsExpected, string.Join("\n", result.Errors) + "\nExpected:\n" + string.Join("\n", expectedErrorcodes)); }
private static ExitCode RunVerification( List <string> verifiedFiles, List <string> assumedFiles, List <string> references) { TranslationProcess process; try { process = TranslationProcess.Create( verifiedFiles, assumedFiles, references, new TranslationConfiguration() { Verbose = verbose, VerifyUnmarkedItems = !onlyAnnotated }); } catch (Exception ex) { Console.WriteLine("The translation process could not be created."); Console.WriteLine("Error: " + ex.Message); return(ExitCode.InputError); } TranslationProcessResult result = process.Execute(); if (!quiet) { Console.WriteLine( result.WasTranslationSuccessful ? "Successfully translated." : $"Translation failed with {result.Errors.Count} errors." ); Console.WriteLine(); Console.WriteLine("Resultant Silver code: "); Console.WriteLine("======================="); } string silvercode = result.Silvernode.ToString(); if (lineNumbers) { int lineId = 1; foreach (var line in silvercode.Split('\n')) { Console.WriteLine(lineId + ": " + line); lineId++; } } else { Console.WriteLine(silvercode); } if (result.Errors.Count > 0) { if (!quiet) { Console.WriteLine("======================="); Console.WriteLine($"Errors: {result.Errors.Count}."); } foreach (Error error in result.Errors) { Console.WriteLine(error.ToString()); if (verbose) { if (!String.IsNullOrEmpty(error.Diagnostic.Details)) { Console.WriteLine("Details: " + error.Diagnostic.Details); } if (error.Node != null) { Console.WriteLine("Node source: " + error.Node.GetText().ToString().Trim()); } Console.WriteLine(); } } if (!quiet) { Console.WriteLine("======================="); } } // Write output to file if (outputSilverFile != null) { try { System.IO.File.WriteAllText(outputSilverFile, silvercode); if (!quiet) { Console.WriteLine($"Silver code written to {outputSilverFile}."); } } catch (Exception) { Console.WriteLine( $"Error - Silver code could not be written to {outputSilverFile}. Check that you have write permissions to that location and that the directory leading up to it exists."); return(ExitCode.VerificationError); } } // Run verifier if (useSilicon || useCarbon) { if (!quiet) { Console.WriteLine("======================="); } if (result.WasTranslationSuccessful) { IBackend backend; if (useSilicon) { backend = new SiliconNailgunBackend(); // new SiliconBackend(); } else { backend = new CarbonNailgunBackend(); // new CarbonBackend(); } var verificationResult = backend.Verify(result.Silvernode); if (verbose) { Console.WriteLine(verificationResult.OriginalOutput); Console.WriteLine("======================="); } if (verificationResult.VerificationSuccessful) { Console.WriteLine("Verification successful."); } if (verificationResult.Errors.Count > 0) { if (!quiet) { Console.WriteLine($"Errors: {verificationResult.Errors.Count}."); } foreach (Error error in verificationResult.Errors) { Console.WriteLine(error.ToString()); if (verbose) { if (!String.IsNullOrEmpty(error.Diagnostic.Details)) { Console.WriteLine("Details: " + error.Diagnostic.Details); } Console.WriteLine("Node source: " + error.Node.GetText().ToString().Trim()); Console.WriteLine(); } } } if (verificationResult.VerificationSuccessful) { return(ExitCode.Success); } else { return(ExitCode.VerificationError); } } else { Console.WriteLine("The translation was not successful so a backend verifier will not be run."); return(ExitCode.VerificationError); } } else if (result.WasTranslationSuccessful) { return(ExitCode.Success); } else { return(ExitCode.VerificationError); } }
/// <summary> /// Collects all C# types from all children and adds them to the collection in the <see cref="TranslationProcess"/>. If this node /// is a type declaration, this node is collected as well. If this node is a class declaration, its instance fields are also processed. /// </summary> /// <param name="translationProcess">The process to add the types into.</param> /// <param name="semantics">Semantic model of the current tree that is being translated.</param> public virtual void CollectTypesInto(TranslationProcess translationProcess, SemanticModel semantics) { // For most sharpnodes, do nothing. }