Skip to content

Commit 8243ba0

Browse files
CSToVBConversion: Allow compile errors to show up when flag toggled
1 parent 8c3352f commit 8243ba0

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

ICSharpCode.CodeConverter/CSharp/VBToCSConversion.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
54
using ICSharpCode.CodeConverter.Shared;
65
using ICSharpCode.CodeConverter.Util;
@@ -29,22 +28,11 @@ public VBToCSConversion()
2928

3029
private CSharpCompilation CreateCompilation(List<SyntaxTree> csTrees)
3130
{
32-
var references = _sourceCompilation.References.Select(ConvertReference);
31+
var references = _sourceCompilation.References.Select(ReferenceConverter.ConvertReference);
3332
return CSharpCompilation.Create("Conversion", csTrees, references,
3433
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
3534
}
3635

37-
private MetadataReference ConvertReference(MetadataReference nonLanguageSpecificRef)
38-
{
39-
if (!(nonLanguageSpecificRef is CompilationReference cr)) return nonLanguageSpecificRef;
40-
41-
using (var stream = new MemoryStream())
42-
{
43-
cr.Compilation.Emit(stream);
44-
return MetadataReference.CreateFromStream(stream);
45-
}
46-
}
47-
4836
public SyntaxTree SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree)
4937
{
5038
_sourceCompilation = sourceCompilation;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.IO;
2+
using Microsoft.CodeAnalysis;
3+
4+
namespace ICSharpCode.CodeConverter.Util
5+
{
6+
public class ReferenceConverter
7+
{
8+
public static MetadataReference ConvertReference(MetadataReference nonLanguageSpecificRef)
9+
{
10+
if (!(nonLanguageSpecificRef is CompilationReference cr)) return nonLanguageSpecificRef;
11+
12+
using (var stream = new MemoryStream())
13+
{
14+
cr.Compilation.Emit(stream);
15+
return MetadataReference.CreateFromStream(stream);
16+
}
17+
}
18+
}
19+
}

ICSharpCode.CodeConverter/VB/CSToVBConversion.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
24
using System.Linq;
35
using ICSharpCode.CodeConverter.CSharp;
46
using ICSharpCode.CodeConverter.Shared;
57
using ICSharpCode.CodeConverter.Util;
68
using Microsoft.CodeAnalysis;
79
using Microsoft.CodeAnalysis.CSharp;
810
using Microsoft.CodeAnalysis.Text;
11+
using Microsoft.CodeAnalysis.VisualBasic;
912
using VBSyntaxFactory = Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory;
1013
using CSSyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1114
using CSSyntax = Microsoft.CodeAnalysis.CSharp.Syntax;
15+
using SyntaxKind = Microsoft.CodeAnalysis.CSharp.SyntaxKind;
1216
using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
1317

1418
namespace ICSharpCode.CodeConverter.VB
1519
{
1620
public class CSToVBConversion : ILanguageConversion
1721
{
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+
1832
public SyntaxTree SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree)
1933
{
34+
_sourceCompilation = sourceCompilation;
2035
var converted = CSharpConverter.ConvertCompilationTree((CSharpCompilation)sourceCompilation, (CSharpSyntaxTree)tree);
2136
var convertedTree = VBSyntaxFactory.SyntaxTree(converted);
37+
_firstPassResults.Add(convertedTree);
2238
return convertedTree;
2339
}
2440

@@ -99,7 +115,18 @@ public SyntaxNode SingleSecondPass(KeyValuePair<string, SyntaxTree> cs)
99115

100116
public string GetWarningsOrNull()
101117
{
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;
103130
}
104131

105132
public SyntaxTree CreateTree(string text)

0 commit comments

Comments
 (0)