|
1 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
2 | 4 | using System.Linq; |
3 | 5 | using ICSharpCode.CodeConverter.CSharp; |
4 | 6 | using ICSharpCode.CodeConverter.Shared; |
5 | 7 | using ICSharpCode.CodeConverter.Util; |
6 | 8 | using Microsoft.CodeAnalysis; |
7 | 9 | using Microsoft.CodeAnalysis.CSharp; |
8 | 10 | using Microsoft.CodeAnalysis.Text; |
| 11 | +using Microsoft.CodeAnalysis.VisualBasic; |
9 | 12 | using VBSyntaxFactory = Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory; |
10 | 13 | using CSSyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
11 | 14 | using CSSyntax = Microsoft.CodeAnalysis.CSharp.Syntax; |
| 15 | +using SyntaxKind = Microsoft.CodeAnalysis.CSharp.SyntaxKind; |
12 | 16 | using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax; |
13 | 17 |
|
14 | 18 | namespace ICSharpCode.CodeConverter.VB |
15 | 19 | { |
16 | 20 | public class CSToVBConversion : ILanguageConversion |
17 | 21 | { |
| 22 | + private readonly List<SyntaxTree> _firstPassResults = new List<SyntaxTree>(); |
| 23 | + private Compilation _sourceCompilation; |
| 24 | + |
| 25 | + private VisualBasicCompilation CreateCompilation(List<SyntaxTree> vbTrees) |
| 26 | + { |
| 27 | + var references = _sourceCompilation.References.Select(ReferenceConverter.ConvertReference); |
| 28 | + return VisualBasicCompilation.Create("Conversion", vbTrees, references, |
| 29 | + new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); |
| 30 | + } |
| 31 | + |
18 | 32 | public SyntaxTree SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree) |
19 | 33 | { |
| 34 | + _sourceCompilation = sourceCompilation; |
20 | 35 | var converted = CSharpConverter.ConvertCompilationTree((CSharpCompilation)sourceCompilation, (CSharpSyntaxTree)tree); |
21 | 36 | var convertedTree = VBSyntaxFactory.SyntaxTree(converted); |
| 37 | + _firstPassResults.Add(convertedTree); |
22 | 38 | return convertedTree; |
23 | 39 | } |
24 | 40 |
|
@@ -99,7 +115,18 @@ public SyntaxNode SingleSecondPass(KeyValuePair<string, SyntaxTree> cs) |
99 | 115 |
|
100 | 116 | public string GetWarningsOrNull() |
101 | 117 | { |
102 | | - return null; |
| 118 | + var finalCompilation = CreateCompilation(_firstPassResults); |
| 119 | + var targetErrors = GetDiagnostics(finalCompilation); |
| 120 | + return targetErrors.Any() ? $"{targetErrors.Count} resulting compilation errors:{Environment.NewLine}{string.Join(Environment.NewLine, targetErrors)}" : null; |
| 121 | + } |
| 122 | + |
| 123 | + private static List<string> GetDiagnostics(Compilation compilation) |
| 124 | + { |
| 125 | + var diagnostics = compilation.GetDiagnostics() |
| 126 | + .Where(d => d.Severity == DiagnosticSeverity.Error) |
| 127 | + .Select(d => $"{d.Id}: {d.GetMessage()}") |
| 128 | + .ToList(); |
| 129 | + return diagnostics; |
103 | 130 | } |
104 | 131 |
|
105 | 132 | public SyntaxTree CreateTree(string text) |
|
0 commit comments