Browse Source

Add basic support for C# 8.0 enhanced using statements.

pull/2126/head
Siegfried Pammer 5 years ago
parent
commit
9e0e8d1cbc
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 4
      ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs
  3. 14
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  4. 20
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/AsyncForeach.cs
  5. 93
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UsingVariables.cs
  6. 32
      ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs
  7. 2
      ICSharpCode.Decompiler/CSharp/Syntax/Statements/UsingStatement.cs
  8. 18
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs
  9. 18
      ICSharpCode.Decompiler/DecompilerSettings.cs
  10. 9
      ILSpy/Properties/Resources.Designer.cs
  11. 3
      ILSpy/Properties/Resources.resx

1
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -94,6 +94,7 @@ @@ -94,6 +94,7 @@
<Compile Include="TestAssemblyResolver.cs" />
<Compile Include="TestCases\Correctness\DeconstructionTests.cs" />
<Compile Include="TestCases\Correctness\StringConcat.cs" />
<None Include="TestCases\Pretty\UsingVariables.cs" />
<None Include="TestCases\Pretty\AsyncForeach.cs" />
<Compile Include="TestCases\Pretty\DeconstructionTests.cs" />
<Compile Include="TestCases\ILPretty\Issue2104.cs" />

4
ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs

@ -109,13 +109,13 @@ namespace ICSharpCode.Decompiler.Tests @@ -109,13 +109,13 @@ namespace ICSharpCode.Decompiler.Tests
[Test]
public void FSharpUsing_Debug()
{
Run(settings: new DecompilerSettings { RemoveDeadStores = true });
Run(settings: new DecompilerSettings { RemoveDeadStores = true, UseEnhancedUsing = false });
}
[Test]
public void FSharpUsing_Release()
{
Run(settings: new DecompilerSettings { RemoveDeadStores = true });
Run(settings: new DecompilerSettings { RemoveDeadStores = true, UseEnhancedUsing = false });
}
[Test]

14
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -178,6 +178,15 @@ namespace ICSharpCode.Decompiler.Tests @@ -178,6 +178,15 @@ namespace ICSharpCode.Decompiler.Tests
[Test]
public void Using([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
RunForLibrary(
cscOptions: cscOptions,
decompilerSettings: new DecompilerSettings { UseEnhancedUsing = false }
);
}
[Test]
public void UsingVariables([ValueSource(nameof(dotnetCoreOnlyOptions))] CompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions);
}
@ -335,7 +344,10 @@ namespace ICSharpCode.Decompiler.Tests @@ -335,7 +344,10 @@ namespace ICSharpCode.Decompiler.Tests
[Test]
public void AsyncUsing([ValueSource(nameof(dotnetCoreOnlyOptions))] CompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions);
RunForLibrary(
cscOptions: cscOptions,
decompilerSettings: new DecompilerSettings { UseEnhancedUsing = false }
);
}
[Test]

20
ICSharpCode.Decompiler.Tests/TestCases/Pretty/AsyncForeach.cs

@ -1,4 +1,22 @@ @@ -1,4 +1,22 @@
using System.Collections.Generic;
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

93
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UsingVariables.cs

@ -0,0 +1,93 @@ @@ -0,0 +1,93 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
//using System.Threading.Tasks;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public class UsingVariables
{
public IDisposable GetDisposable()
{
return null;
}
public IAsyncDisposable GetAsyncDisposable()
{
return null;
}
private void Use(IDisposable disposable)
{
}
private void Use(IAsyncDisposable disposable)
{
}
public void SimpleUsingVar()
{
Console.WriteLine("before using");
using IDisposable disposable = GetDisposable();
Console.WriteLine("inside using");
Use(disposable);
}
public void NotAUsingVar()
{
Console.WriteLine("before using");
using (IDisposable disposable = GetDisposable()) {
Console.WriteLine("inside using");
Use(disposable);
}
Console.WriteLine("outside using");
}
public void UsingVarInNestedBlocks(bool condition)
{
if (condition) {
using IDisposable disposable = GetDisposable();
Console.WriteLine("inside using");
Use(disposable);
}
Console.WriteLine("outside using");
}
public void MultipleUsingVars(IDisposable other)
{
Console.WriteLine("before using");
using IDisposable disposable = GetDisposable();
Console.WriteLine("inside outer using");
using IDisposable disposable2 = other;
Console.WriteLine("inside inner using");
Use(disposable);
Use(disposable2);
}
//public async Task SimpleUsingVarAsync(IAsyncDisposable other)
//{
// Console.WriteLine("before using");
// await using IAsyncDisposable asyncDisposable = GetAsyncDisposable();
// Console.WriteLine("inside using");
// Use(asyncDisposable);
//}
}
}

32
ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -1889,16 +1889,36 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -1889,16 +1889,36 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
WriteKeyword(UsingStatement.AwaitRole);
}
WriteKeyword(UsingStatement.UsingKeywordRole);
Space(policy.SpaceBeforeUsingParentheses);
LPar();
Space(policy.SpacesWithinUsingParentheses);
if (usingStatement.IsEnhanced) {
Space();
} else {
Space(policy.SpaceBeforeUsingParentheses);
LPar();
Space(policy.SpacesWithinUsingParentheses);
}
usingStatement.ResourceAcquisition.AcceptVisitor(this);
Space(policy.SpacesWithinUsingParentheses);
RPar();
if (usingStatement.IsEnhanced) {
Semicolon();
} else {
Space(policy.SpacesWithinUsingParentheses);
RPar();
}
WriteEmbeddedStatement(usingStatement.EmbeddedStatement);
if (usingStatement.IsEnhanced) {
if (usingStatement.EmbeddedStatement is BlockStatement blockStatement) {
StartNode(blockStatement);
foreach (var node in blockStatement.Statements) {
node.AcceptVisitor(this);
}
EndNode(blockStatement);
} else {
usingStatement.EmbeddedStatement.AcceptVisitor(this);
}
} else {
WriteEmbeddedStatement(usingStatement.EmbeddedStatement);
}
EndNode(usingStatement);
}

2
ICSharpCode.Decompiler/CSharp/Syntax/Statements/UsingStatement.cs

@ -53,6 +53,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -53,6 +53,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
get { return GetChildByRole(Roles.LPar); }
}
public bool IsEnhanced { get; set; }
/// <summary>
/// Either a VariableDeclarationStatement, or an Expression.
/// </summary>

18
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -1123,5 +1123,23 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -1123,5 +1123,23 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
return base.VisitFixedStatement(fixedStatement);
}
#endregion
#region C# 8.0 Using variables
public override AstNode VisitUsingStatement(UsingStatement usingStatement)
{
usingStatement = (UsingStatement)base.VisitUsingStatement(usingStatement);
if (!context.Settings.UseEnhancedUsing)
return usingStatement;
if (usingStatement.GetNextStatement() != null || !(usingStatement.Parent is BlockStatement))
return usingStatement;
if (!(usingStatement.ResourceAcquisition is VariableDeclarationStatement))
return usingStatement;
usingStatement.IsEnhanced = true;
return usingStatement;
}
#endregion
}
}

18
ICSharpCode.Decompiler/DecompilerSettings.cs

@ -112,6 +112,7 @@ namespace ICSharpCode.Decompiler @@ -112,6 +112,7 @@ namespace ICSharpCode.Decompiler
readOnlyMethods = false;
asyncUsingAndForEachStatement = false;
asyncEnumerator = false;
useEnhancedUsing = false;
staticLocalFunctions = false;
ranges = false;
switchExpressions = false;
@ -474,6 +475,23 @@ namespace ICSharpCode.Decompiler @@ -474,6 +475,23 @@ namespace ICSharpCode.Decompiler
}
}
bool useEnhancedUsing = true;
/// <summary>
/// Use enhanced using statements.
/// </summary>
[Category("C# 8.0 / VS 2019")]
[Description("DecompilerSettings.UseEnhancedUsing")]
public bool UseEnhancedUsing {
get { return useEnhancedUsing; }
set {
if (useEnhancedUsing != value) {
useEnhancedUsing = value;
OnPropertyChanged();
}
}
}
bool alwaysUseBraces = true;
/// <summary>

9
ILSpy/Properties/Resources.Designer.cs generated

@ -1100,6 +1100,15 @@ namespace ICSharpCode.ILSpy.Properties { @@ -1100,6 +1100,15 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Use enhanced using variable declarations.
/// </summary>
public static string DecompilerSettings_UseEnhancedUsing {
get {
return ResourceManager.GetString("DecompilerSettings.UseEnhancedUsing", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Use expression-bodied member syntax for get-only properties.
/// </summary>

3
ILSpy/Properties/Resources.resx

@ -396,6 +396,9 @@ Are you sure you want to continue?</value> @@ -396,6 +396,9 @@ Are you sure you want to continue?</value>
<data name="DecompilerSettings.UseDiscards" xml:space="preserve">
<value>Use discards</value>
</data>
<data name="DecompilerSettings.UseEnhancedUsing" xml:space="preserve">
<value>Use enhanced using variable declarations</value>
</data>
<data name="DecompilerSettings.UseExpressionBodiedMemberSyntaxForGetOnlyProperties" xml:space="preserve">
<value>Use expression-bodied member syntax for get-only properties</value>
</data>

Loading…
Cancel
Save