提交 d66839db 编写于 作者: J Julien Couvreur 提交者: GitHub

Pass LangVersion thru ICompilerOptionsHostObject.SetCompilerOptions when above 15 (VB) (#19112)

上级 86c7d6cb
......@@ -82,12 +82,6 @@ public bool GenerateFullPaths
get { return _store.GetOrDefault(nameof(GenerateFullPaths), false); }
}
public string LangVersion
{
set { _store[nameof(LangVersion)] = value; }
get { return (string)_store[nameof(LangVersion)]; }
}
public string ModuleAssemblyName
{
set { _store[nameof(ModuleAssemblyName)] = value; }
......
......@@ -382,6 +382,12 @@ protected override Encoding StandardOutputEncoding
}
}
public string LangVersion
{
set { _store[nameof(LangVersion)] = value; }
get { return (string)_store[nameof(LangVersion)]; }
}
#endregion
/// <summary>
......@@ -733,6 +739,7 @@ internal void AddResponseFileCommandsForSwitchesSinceInitialReleaseThatAreNeeded
commandLine.AppendSwitchIfNotNull("/checksumalgorithm:", ChecksumAlgorithm);
commandLine.AppendSwitchWithSplitting("/instrument:", Instrument, ",", ';', ',');
commandLine.AppendSwitchIfNotNull("/sourcelink:", SourceLink);
commandLine.AppendSwitchIfNotNull("/langversion:", LangVersion);
AddFeatures(commandLine, Features);
AddEmbeddedFilesToCommandLine(commandLine);
......
......@@ -79,12 +79,6 @@ public ITaskItem[] Imports
get { return (ITaskItem[])_store[nameof(Imports)]; }
}
public string LangVersion
{
set { _store[nameof(LangVersion)] = value; }
get { return (string)_store[nameof(LangVersion)]; }
}
public string ModuleAssemblyName
{
set { _store[nameof(ModuleAssemblyName)] = value; }
......@@ -909,7 +903,7 @@ private bool InitializeHostCompiler(IVbcHostObject vbcHostObject)
}
// Check for support of the LangVersion property
if (vbcHostObject is IVbcHostObject3)
if (vbcHostObject is IVbcHostObject3 && !DeferToICompilerOptionsHostObject(LangVersion, vbcHostObject))
{
IVbcHostObject3 vbcHostObject3 = (IVbcHostObject3)vbcHostObject;
CheckHostObjectSupport(param = nameof(LangVersion), vbcHostObject3.SetLanguageVersion(LangVersion));
......@@ -960,6 +954,35 @@ private bool InitializeHostCompiler(IVbcHostObject vbcHostObject)
return true;
}
// VbcHostObject doesn't support VB versions beyond 15,
// so the LangVersion will be passed through ICompilerOptionsHostObject.SetCompilerOptions instead
private static bool DeferToICompilerOptionsHostObject(string langVersion, IVbcHostObject vbcHostObject)
{
if (!(vbcHostObject is ICompilerOptionsHostObject))
{
return false;
}
if (langVersion == null)
{
// CVbcMSBuildHostObject::SetLanguageVersion can handle null
return false;
}
// CVbcMSBuildHostObject::SetLanguageVersion can handle versions up to 15
var supportedList = new[]
{
"9", "9.0",
"10", "10.0",
"11", "11.0",
"12", "12.0",
"14", "14.0",
"15", "15.0"
};
return Array.IndexOf(supportedList, langVersion) < 0;
}
/// <summary>
/// This method will get called during Execute() if a host object has been passed into the Vbc
/// task. Returns one of the following values to indicate what the next action should be:
......
......@@ -47,6 +47,112 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim
End Using
End Sub
<WpfFact()>
<Trait(Traits.Feature, Traits.Features.ProjectSystemShims)>
Public Sub SetCompilerOptions_LangVersion14()
Using environment = New TestEnvironment()
Dim project = CreateVisualBasicProject(environment, "Test")
Dim compilerOptionsHost = DirectCast(project, Implementation.ProjectSystem.Interop.ICompilerOptionsHostObject)
Dim supported As Boolean
compilerOptionsHost.SetCompilerOptions("/langversion:14", supported)
Assert.True(supported)
Dim workspaceProject = environment.Workspace.CurrentSolution.Projects.Single()
Dim options = DirectCast(workspaceProject.ParseOptions, VisualBasicParseOptions)
' SetCompilerOptions only handles versions 15.3 and up
Assert.Equal(LanguageVersion.VisualBasic15, options.LanguageVersion)
Assert.Equal(LanguageVersion.VisualBasic15, options.SpecifiedLanguageVersion)
project.Disconnect()
End Using
End Sub
<WpfFact()>
<Trait(Traits.Feature, Traits.Features.ProjectSystemShims)>
Public Sub SetCompilerOptions_LangVersion15()
Using environment = New TestEnvironment()
Dim project = CreateVisualBasicProject(environment, "Test")
Dim compilerOptionsHost = DirectCast(project, Implementation.ProjectSystem.Interop.ICompilerOptionsHostObject)
Dim supported As Boolean
compilerOptionsHost.SetCompilerOptions("/langversion:15", supported)
Assert.True(supported)
Dim workspaceProject = environment.Workspace.CurrentSolution.Projects.Single()
Dim options = DirectCast(workspaceProject.ParseOptions, VisualBasicParseOptions)
Assert.Equal(LanguageVersion.VisualBasic15, options.LanguageVersion)
Assert.Equal(LanguageVersion.VisualBasic15, options.SpecifiedLanguageVersion)
project.Disconnect()
End Using
End Sub
<WpfFact()>
<Trait(Traits.Feature, Traits.Features.ProjectSystemShims)>
Public Sub SetCompilerOptions_LangVersionDefault()
Using environment = New TestEnvironment()
Dim project = CreateVisualBasicProject(environment, "Test")
Dim compilerOptionsHost = DirectCast(project, Implementation.ProjectSystem.Interop.ICompilerOptionsHostObject)
Dim supported As Boolean
compilerOptionsHost.SetCompilerOptions("/langversion:Default", supported)
Assert.True(supported)
Dim workspaceProject = environment.Workspace.CurrentSolution.Projects.Single()
Dim options = DirectCast(workspaceProject.ParseOptions, VisualBasicParseOptions)
Assert.Equal(LanguageVersion.Default.MapSpecifiedToEffectiveVersion(), options.LanguageVersion)
Assert.Equal(LanguageVersion.Default.MapSpecifiedToEffectiveVersion(), options.SpecifiedLanguageVersion)
project.Disconnect()
End Using
End Sub
<WpfFact()>
<Trait(Traits.Feature, Traits.Features.ProjectSystemShims)>
Public Sub SetCompilerOptions_LangVersion15_3()
Using environment = New TestEnvironment()
Dim project = CreateVisualBasicProject(environment, "Test")
Dim compilerOptionsHost = DirectCast(project, Implementation.ProjectSystem.Interop.ICompilerOptionsHostObject)
Dim supported As Boolean
compilerOptionsHost.SetCompilerOptions("/langversion:15.3", supported)
Assert.True(supported)
Dim workspaceProject = environment.Workspace.CurrentSolution.Projects.Single()
Dim options = DirectCast(workspaceProject.ParseOptions, VisualBasicParseOptions)
Assert.Equal(LanguageVersion.VisualBasic15_3, options.LanguageVersion)
Assert.Equal(LanguageVersion.VisualBasic15_3, options.SpecifiedLanguageVersion)
project.Disconnect()
End Using
End Sub
<WpfFact()>
<Trait(Traits.Feature, Traits.Features.ProjectSystemShims)>
Public Sub SetCompilerOptions_LangVersionLatest()
Using environment = New TestEnvironment()
Dim project = CreateVisualBasicProject(environment, "Test")
Dim compilerOptionsHost = DirectCast(project, Implementation.ProjectSystem.Interop.ICompilerOptionsHostObject)
Dim supported As Boolean
compilerOptionsHost.SetCompilerOptions("/langversion:latest", supported)
Assert.True(supported)
Dim workspaceProject = environment.Workspace.CurrentSolution.Projects.Single()
Dim options = DirectCast(workspaceProject.ParseOptions, VisualBasicParseOptions)
Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion(), options.LanguageVersion)
Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion(), options.SpecifiedLanguageVersion)
project.Disconnect()
End Using
End Sub
<WpfFact()>
<Trait(Traits.Feature, Traits.Features.ProjectSystemShims)>
<WorkItem(530980, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530980")>
......
......@@ -392,7 +392,18 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.ProjectSystemShim
Protected Overrides Function CreateParseOptions(commandLineArguments As CommandLineArguments) As ParseOptions
Dim baseParseOptions = DirectCast(MyBase.CreateParseOptions(commandLineArguments), VisualBasicParseOptions)
Return VisualBasicProjectOptionsHelper.CreateParseOptions(baseParseOptions, _rawOptions)
Dim resultParseOptions = VisualBasicProjectOptionsHelper.CreateParseOptions(baseParseOptions, _rawOptions)
Dim commandLineOptions = DirectCast(commandLineArguments.ParseOptions, VisualBasicParseOptions)
If commandLineOptions.LanguageVersion > LanguageVersion.VisualBasic15 Then
' For language versions after VB 15, we expect the version to be passed from MSBuild to the IDE
' via command-line arguments (`ICompilerOptionsHostObject.SetCompilerOptions`)
' instead of using `IVbcHostObject3.SetLanguageVersion`
resultParseOptions = resultParseOptions.WithLanguageVersion(commandLineOptions.LanguageVersion)
End If
Return resultParseOptions
End Function
Private Shadows Sub UpdateOptions()
......
......@@ -880,7 +880,7 @@ public bool SetLanguageVersion(string languageVersion)
{
if (!string.IsNullOrWhiteSpace(languageVersion))
{
_commandLineArgs.Add("/languageversion:" + languageVersion);
_commandLineArgs.Add("/langversion:" + languageVersion);
}
return true;
......
......@@ -580,6 +580,27 @@ public void TestOpenProject_VisualBasic_WithoutOutputPath()
Assert.NotEmpty(project.OutputFilePath);
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public void TestOpenProject_VisualBasic_WithLanguageVersion15_3()
{
CreateFiles(GetMultiProjectSolutionFiles()
.ReplaceFileElement(@"VisualBasicProject\VisualBasicProject.vbproj", "LangVersion", "15.3"));
var project = MSBuildWorkspace.Create().OpenProjectAsync(GetSolutionFileName(@"VisualBasicProject\VisualBasicProject.vbproj")).Result;
Assert.Equal(VB.LanguageVersion.VisualBasic15_3, ((VB.VisualBasicParseOptions)project.ParseOptions).LanguageVersion);
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public void TestOpenProject_VisualBasic_WithLatestLanguageVersion()
{
CreateFiles(GetMultiProjectSolutionFiles()
.ReplaceFileElement(@"VisualBasicProject\VisualBasicProject.vbproj", "LangVersion", "Latest"));
var project = MSBuildWorkspace.Create().OpenProjectAsync(GetSolutionFileName(@"VisualBasicProject\VisualBasicProject.vbproj")).Result;
Assert.Equal(VB.LanguageVersion.VisualBasic15_3, ((VB.VisualBasicParseOptions)project.ParseOptions).LanguageVersion);
Assert.Equal(VB.LanguageVersion.Latest, ((VB.VisualBasicParseOptions)project.ParseOptions).SpecifiedLanguageVersion);
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public void TestOpenProject_VisualBasic_WithoutAssemblyName()
{
......
......@@ -38,6 +38,7 @@
</PropertyGroup>
<PropertyGroup>
<OptionExplicit>On</OptionExplicit>
<LangVersion>15</LangVersion>
</PropertyGroup>
<PropertyGroup>
<OptionCompare>Binary</OptionCompare>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册