Skip to content

Commit 21f9fc2

Browse files
authored
Merge pull request #25130 from jaredpar/fix-basic3
VB: Unify CreateCompilation helpers
2 parents 3a6f582 + 9301c3c commit 21f9fc2

File tree

64 files changed

+396
-476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+396
-476
lines changed

build/Targets/Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>1.0.1</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
3434
<MicrosoftCodeAnalysisEditorFeaturesTextVersion>1.0.1</MicrosoftCodeAnalysisEditorFeaturesTextVersion>
3535
<MicrosoftCodeAnalysisElfieVersion>0.10.6</MicrosoftCodeAnalysisElfieVersion>
36-
<MicrosoftCodeAnalysisTestResourcesProprietaryVersion>2.0.7</MicrosoftCodeAnalysisTestResourcesProprietaryVersion>
36+
<MicrosoftCodeAnalysisTestResourcesProprietaryVersion>2.0.10</MicrosoftCodeAnalysisTestResourcesProprietaryVersion>
3737
<MicrosoftCodeAnalysisVisualBasicVersion>1.0.1</MicrosoftCodeAnalysisVisualBasicVersion>
3838
<MicrosoftCodeAnalysisVisualBasicWorkspacesVersion>1.0.1</MicrosoftCodeAnalysisVisualBasicWorkspacesVersion>
3939
<MicrosoftCodeAnalysisWorkspacesCommonVersion>1.0.1</MicrosoftCodeAnalysisWorkspacesCommonVersion>

src/Compilers/CSharp/Test/Symbol/Symbols/AnonymousTypesSemanticsTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,15 +1815,11 @@ private TestData Compile(string source, int expectedIntervals, params Diagnostic
18151815
};
18161816
}
18171817

1818-
private CSharpCompilation Compile(string source)
1819-
{
1820-
return (CSharpCompilation)GetCompilationForEmit(
1821-
new[] { source },
1822-
new MetadataReference[] { },
1823-
TestOptions.ReleaseDll,
1824-
TestOptions.Regular
1825-
);
1826-
}
1818+
private CSharpCompilation Compile(string source) =>
1819+
CreateCompilationWithMscorlib40(
1820+
source,
1821+
options: TestOptions.ReleaseDll,
1822+
parseOptions: TestOptions.Regular);
18271823

18281824
private static List<TextSpan> ExtractTextIntervals(ref string source)
18291825
{

src/Compilers/CSharp/Test/WinRT/AnonymousTypesSymbolTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,10 +1942,9 @@ .maxstack 2
19421942
IL_0008: ret
19431943
}";
19441944

1945-
19461945
CompileAndVerify(source).VerifyIL("C.Main", expectedIL);
19471946

1948-
var compilation = GetCompilationForEmit(new[] { source }, references: null, options: null, parseOptions: null);
1947+
var compilation = CreateCompilationWithMscorlib40(source);
19491948
compilation.CreateAnonymousTypeSymbol(
19501949
ImmutableArray.Create<ITypeSymbol>(compilation.GetSpecialType(SpecialType.System_Int32), compilation.GetSpecialType(SpecialType.System_Boolean)),
19511950
ImmutableArray.Create("m1", "m2"));

src/Compilers/Test/Utilities/CSharp/BasicCompilationUtils.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ private static VisualBasicCompilation CreateCompilationWithMscorlib(string sourc
4343

4444
private sealed class BasicTestBase : CommonTestBase
4545
{
46-
protected override CompilationOptions CompilationOptionsReleaseDll
47-
{
48-
get { throw new NotImplementedException(); }
49-
}
50-
51-
protected override Compilation GetCompilationForEmit(IEnumerable<string> source, IEnumerable<MetadataReference> additionalRefs, CompilationOptions options, ParseOptions parseOptions)
52-
{
53-
throw new NotImplementedException();
54-
}
55-
5646
internal override string VisualizeRealIL(IModuleSymbol peModule, CodeAnalysis.CodeGen.CompilationTestData.MethodData methodData, IReadOnlyDictionary<int, string> markers)
5747
{
5848
throw new NotImplementedException();

src/Compilers/Test/Utilities/CSharp/CSharpTestBase.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,21 @@ Action<IModuleSymbol> translate(Action<ModuleSymbol> action)
239239
verify);
240240
}
241241

242-
protected override CompilationOptions CompilationOptionsReleaseDll
243-
{
244-
get { return TestOptions.ReleaseDll; }
245-
}
242+
internal CompilationVerifier CompileAndVerifyFieldMarshal(CSharpTestSource source, Dictionary<string, byte[]> expectedBlobs, bool isField = true) =>
243+
CompileAndVerifyFieldMarshal(
244+
source,
245+
(s, _) =>
246+
{
247+
Assert.True(expectedBlobs.ContainsKey(s), "Expecting marshalling blob for " + (isField ? "field " : "parameter ") + s);
248+
return expectedBlobs[s];
249+
},
250+
isField);
251+
252+
internal CompilationVerifier CompileAndVerifyFieldMarshal(CSharpTestSource source, Func<string, PEAssembly, byte[]> getExpectedBlob, bool isField = true) =>
253+
CompileAndVerifyFieldMarshalCommon(
254+
CreateCompilationWithMscorlib40(source),
255+
getExpectedBlob,
256+
isField);
246257

247258
#region SyntaxTree Factories
248259

@@ -519,22 +530,6 @@ public static MetadataReference CreateMetadataReferenceFromIlSource(string ilSou
519530
}
520531
}
521532

522-
protected override Compilation GetCompilationForEmit(
523-
IEnumerable<string> source,
524-
IEnumerable<MetadataReference> references,
525-
CompilationOptions options,
526-
ParseOptions parseOptions)
527-
{
528-
var single = new[] { MscorlibRef };
529-
references = references != null ? single.Concat(references) : single;
530-
return CreateEmptyCompilation(
531-
source.ToArray(),
532-
references: (IEnumerable<MetadataReference>)references,
533-
options: (CSharpCompilationOptions)options,
534-
parseOptions: (CSharpParseOptions)parseOptions,
535-
assemblyName: GetUniqueName());
536-
}
537-
538533
/// <summary>
539534
/// Like CompileAndVerify, but confirms that execution raises an exception.
540535
/// </summary>

src/Compilers/Test/Utilities/VisualBasic/BasicTestBase.vb

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Public MustInherit Class BasicTestBase
186186

187187
Dim assemblyName As String = Nothing
188188
Dim sourceTrees = ParseSourceXml(source, parseOptions, assemblyName)
189-
Dim compilation = CreateEmptyCompilation(sourceTrees, allReferences, options, assemblyName)
189+
Dim compilation = CreateEmptyCompilation(sourceTrees.ToArray(), allReferences, options, assemblyName:=assemblyName)
190190

191191
Return MyBase.CompileAndVerifyCommon(
192192
compilation,
@@ -225,7 +225,7 @@ Public MustInherit Class BasicTestBase
225225
options = If(expectedOutput Is Nothing, TestOptions.ReleaseDll, TestOptions.ReleaseExe)
226226
End If
227227

228-
Dim compilation = CreateEmptyCompilation(source, allReferences, options, assemblyName, parseOptions)
228+
Dim compilation = CreateEmptyCompilation(source, allReferences, options, parseOptions, assemblyName)
229229

230230
Return MyBase.CompileAndVerifyCommon(
231231
compilation,
@@ -357,8 +357,7 @@ Public MustInherit Class BasicTestBase
357357
''' &lt;/file&gt;
358358
''' &lt;/compilation&gt;
359359
''' </param>
360-
Friend Function CompileWithCustomILSource(source As XElement,
361-
ilSource As String,
360+
Friend Function CompileWithCustomILSource(source As XElement, ilSource As String,
362361
Optional options As VisualBasicCompilationOptions = Nothing,
363362
Optional compilationVerifier As Action(Of VisualBasicCompilation) = Nothing,
364363
Optional expectedOutput As String = Nothing) As CompilationVerifier
@@ -385,6 +384,15 @@ Public MustInherit Class BasicTestBase
385384
Return CompileAndVerify(compilation, expectedOutput:=expectedOutput)
386385
End Function
387386

387+
Friend Overloads Function CompileAndVerifyFieldMarshal(source As String,
388+
expectedBlobs As Dictionary(Of String, Byte()),
389+
Optional getExpectedBlob As Func(Of String, PEAssembly, Byte()) = Nothing,
390+
Optional expectedSignatures As SignatureDescription() = Nothing,
391+
Optional isField As Boolean = True) As CompilationVerifier
392+
Dim xmlSource = <compilation><field><%= source %></field></compilation>
393+
Return CompileAndVerifyFieldMarshal(xmlSource, expectedBlobs, getExpectedBlob, expectedSignatures, isField)
394+
End Function
395+
388396
Friend Overloads Function CompileAndVerifyFieldMarshal(source As XElement,
389397
expectedBlobs As Dictionary(Of String, Byte()),
390398
Optional getExpectedBlob As Func(Of String, PEAssembly, Byte()) = Nothing,
@@ -409,25 +417,6 @@ Public MustInherit Class BasicTestBase
409417
expectedSignatures:=expectedSignatures)
410418
End Function
411419

412-
Protected Overrides ReadOnly Property CompilationOptionsReleaseDll As CompilationOptions
413-
Get
414-
Return TestOptions.ReleaseDll
415-
End Get
416-
End Property
417-
418-
Protected Overrides Function GetCompilationForEmit(
419-
source As IEnumerable(Of String),
420-
references As IEnumerable(Of MetadataReference),
421-
options As CompilationOptions,
422-
parseOptions As ParseOptions
423-
) As Compilation
424-
Return VisualBasicCompilation.Create(
425-
GetUniqueName(),
426-
syntaxTrees:=source.Select(Function(t) VisualBasicSyntaxTree.ParseText(t, options:=DirectCast(parseOptions, VisualBasicParseOptions))),
427-
references:=If(references IsNot Nothing, DefaultVbReferences.Concat(references), DefaultVbReferences),
428-
options:=DirectCast(options, VisualBasicCompilationOptions))
429-
End Function
430-
431420
Public Shared Function CreateSubmission(code As String,
432421
Optional references As IEnumerable(Of MetadataReference) = Nothing,
433422
Optional options As VisualBasicCompilationOptions = Nothing,
@@ -466,7 +455,7 @@ Public MustInherit Class BasicTestBase
466455
End Function
467456

468457
Public Shared Shadows Function GetPdbXml(source As XElement, Optional options As VisualBasicCompilationOptions = Nothing, Optional methodName As String = "") As XElement
469-
Dim compilation = CreateCompilationWithMscorlib40(source, options)
458+
Dim compilation = CreateCompilationWithMscorlib40(source, options:=options)
470459
compilation.VerifyDiagnostics()
471460
Return GetPdbXml(compilation, methodName)
472461
End Function
@@ -810,8 +799,13 @@ Public MustInherit Class BasicTestBase
810799

811800
Dim fileName = "a.vb"
812801
Dim syntaxTree = Parse(source, fileName, parseOptions)
813-
Dim defaultRefs = If(useLatestFrameworkReferences, LatestVbReferences, DefaultVbReferences)
814-
Dim compilation = CreateCompilationWithMscorlib45AndVBRuntime({syntaxTree}, references:=defaultRefs.Append({ValueTupleRef, SystemRuntimeFacadeRef}), options:=If(compilationOptions, TestOptions.ReleaseDll))
802+
Dim allReferences As IEnumerable(Of MetadataReference)
803+
If useLatestFrameworkReferences Then
804+
allReferences = TargetFrameworkUtil.Mscorlib45ExtendedReferences.Add(TestBase.MsvbRef_v4_0_30319_17929)
805+
Else
806+
allReferences = TargetFrameworkUtil.Mscorlib45ExtendedReferences.Add(TestBase.MsvbRef)
807+
End If
808+
Dim compilation = CreateEmptyCompilation({syntaxTree}, references:=allReferences, options:=If(compilationOptions, TestOptions.ReleaseDll))
815809
Dim operationTree = GetOperationTreeForTest(Of TSyntaxNode)(compilation, fileName, which)
816810
Return (operationTree.tree, operationTree.syntax, operationTree.operation, compilation)
817811
End Function
@@ -869,10 +863,15 @@ Public MustInherit Class BasicTestBase
869863

870864
Dim fileName = "a.vb"
871865
Dim syntaxTree = Parse(source, fileName, parseOptions)
872-
Dim defaultRefs = If(useLatestFramework, LatestVbReferences, DefaultVbReferences)
873-
Dim allReferences = defaultRefs.Concat({ValueTupleRef, SystemRuntimeFacadeRef})
866+
Dim allReferences As IEnumerable(Of MetadataReference) = Nothing
867+
If useLatestFramework Then
868+
allReferences = TargetFrameworkUtil.Mscorlib45ExtendedReferences.Add(TestBase.MsvbRef_v4_0_30319_17929)
869+
Else
870+
allReferences = TargetFrameworkUtil.Mscorlib45ExtendedReferences.Add(TestBase.MsvbRef)
871+
End If
872+
874873
allReferences = If(references IsNot Nothing, allReferences.Concat(references), allReferences)
875-
Dim compilation = CreateCompilationWithMscorlib45AndVBRuntime({syntaxTree}, references:=allReferences, options:=If(compilationOptions, TestOptions.ReleaseDll))
874+
Dim compilation = CreateEmptyCompilation({syntaxTree}, references:=allReferences, options:=If(compilationOptions, TestOptions.ReleaseDll))
876875
VerifyOperationTreeAndDiagnosticsForTest(Of TSyntaxNode)(compilation, fileName, expectedOperationTree, expectedDiagnostics, which, additionalOperationTreeVerifier)
877876
End Sub
878877

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2+
3+
Imports System.Xml.Linq
4+
Imports Microsoft.CodeAnalysis.Text
5+
6+
Public Structure BasicTestSource
7+
8+
Public ReadOnly Property Value As Object
9+
10+
Private Sub New(value As Object)
11+
Me.Value = value
12+
End Sub
13+
14+
Public Function GetSyntaxTrees(
15+
Optional parseOptions As VisualBasicParseOptions = Nothing,
16+
Optional ByRef assemblyName As String = Nothing,
17+
Optional ByRef spans As IEnumerable(Of IEnumerable(Of TextSpan)) = Nothing) As SyntaxTree()
18+
19+
If Value Is Nothing Then
20+
Debug.Assert(parseOptions Is Nothing)
21+
Return Array.Empty(Of SyntaxTree)
22+
End If
23+
24+
Dim xmlSource = TryCast(Value, XElement)
25+
If xmlSource IsNot Nothing Then
26+
Return ParseSourceXml(xmlSource, parseOptions, assemblyName, spans).ToArray()
27+
End If
28+
29+
Dim source = TryCast(Value, String)
30+
If source IsNot Nothing Then
31+
Return New SyntaxTree() {VisualBasicSyntaxTree.ParseText(source, parseOptions)}
32+
End If
33+
34+
Dim sources = TryCast(Value, String())
35+
If sources IsNot Nothing Then
36+
Return sources.Select(Function(s) VisualBasicSyntaxTree.ParseText(s, parseOptions)).ToArray()
37+
End If
38+
39+
Dim tree = TryCast(Value, SyntaxTree)
40+
If tree IsNot Nothing Then
41+
Debug.Assert(parseOptions Is Nothing)
42+
Return New SyntaxTree() {tree}
43+
End If
44+
45+
Dim trees = TryCast(Value, SyntaxTree())
46+
If trees IsNot Nothing Then
47+
Debug.Assert(parseOptions Is Nothing)
48+
Return trees
49+
End If
50+
51+
Throw New Exception($"Unexpected value: {Value}")
52+
End Function
53+
54+
''' <summary>
55+
'''
56+
''' </summary>
57+
''' <param name="source">The sources compile according to the following schema
58+
''' &lt;compilation name="assemblyname[optional]"&gt;
59+
''' &lt;file name="file1.vb[optional]"&gt;
60+
''' source
61+
''' &lt;/file&gt;
62+
''' &lt;/compilation&gt;
63+
''' </param>
64+
Public Shared Widening Operator CType(source As XElement) As BasicTestSource
65+
Return New BasicTestSource(source)
66+
End Operator
67+
68+
Public Shared Widening Operator CType(source As String) As BasicTestSource
69+
Return New BasicTestSource(source)
70+
End Operator
71+
72+
Public Shared Widening Operator CType(source As String()) As BasicTestSource
73+
Return New BasicTestSource(source)
74+
End Operator
75+
76+
Public Shared Widening Operator CType(source As SyntaxTree) As BasicTestSource
77+
Return New BasicTestSource(source)
78+
End Operator
79+
80+
Public Shared Widening Operator CType(source As SyntaxTree()) As BasicTestSource
81+
Return New BasicTestSource(source)
82+
End Operator
83+
End Structure

0 commit comments

Comments
 (0)