Browse Source

Fix #2162: handle VariableInitializers like AssignmentExpressions in InsertParenthesesVisitor

pull/2176/head
Siegfried Pammer 6 years ago
parent
commit
8e9ecf6c36
  1. 2
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs
  2. 2
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1454.cs
  3. 6
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue684.cs
  4. 16
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs
  5. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs
  6. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/FixProxyCalls.cs
  7. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InlineAssignmentTest.cs
  8. 8
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs
  9. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ThrowExpressions.cs
  10. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs
  11. 21
      ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs

2
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs

@ -12,7 +12,7 @@ namespace Issue1389
private static void UnusedResultOfIsinst() private static void UnusedResultOfIsinst()
{ {
_ = (GetObject() is TypeCode); _ = GetObject() is TypeCode;
} }
private static bool BoolResultOfIsinst() private static bool BoolResultOfIsinst()

2
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1454.cs

@ -13,7 +13,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty
for (int i = 0; i < array.Length; i++) for (int i = 0; i < array.Length; i++)
{ {
int num2 = array[i]; int num2 = array[i];
num2 -= ((num2 >> 1) & 0x55555555); num2 -= (num2 >> 1) & 0x55555555;
num2 = (num2 & 0x33333333) + ((num2 >> 2) & 0x33333333); num2 = (num2 & 0x33333333) + ((num2 >> 2) & 0x33333333);
num2 = ((num2 + (num2 >> 4)) & 0xF0F0F0F) * 16843009 >> 24; num2 = ((num2 + (num2 >> 4)) & 0xF0F0F0F) * 16843009 >> 24;
num += num2; num += num2;

6
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue684.cs

@ -11,7 +11,7 @@ public static class Issue684
bool num2 = num >= 1000; bool num2 = num >= 1000;
if (!num2) if (!num2)
{ {
num2 = (num < 2); num2 = num < 2;
} }
if (num2) if (num2)
{ {
@ -25,7 +25,7 @@ public static class Issue684
Console.WriteLine(num3); Console.WriteLine(num3);
for (; i <= num; i += num3) for (; i <= num; i += num3)
{ {
int num4 = array[i] = 1; int num4 = (array[i] = 1);
} }
i = num3; i = num3;
while (true) while (true)
@ -33,7 +33,7 @@ public static class Issue684
bool num5 = i <= num; bool num5 = i <= num;
if (num5) if (num5)
{ {
num5 = (array[i] != 0); num5 = array[i] != 0;
} }
if (!num5) if (!num5)
{ {

16
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs

@ -247,14 +247,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
} }
} }
public static Func<string, string, bool> test0 = (string a, string b) => string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b); public static Func<string, string, bool> test0 = ((string a, string b) => string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b));
public static Func<string, string, bool> test1 = (string a, string b) => string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b); public static Func<string, string, bool> test1 = ((string a, string b) => string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b));
public static Func<string, string, bool> test2 = (string a, string b) => !string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b); public static Func<string, string, bool> test2 = ((string a, string b) => !string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b));
public static Func<string, string, bool> test3 = (string a, string b) => !string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b); public static Func<string, string, bool> test3 = ((string a, string b) => !string.IsNullOrEmpty(a) || !string.IsNullOrEmpty(b));
public static Func<string, string, bool> test4 = (string a, string b) => string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b); public static Func<string, string, bool> test4 = ((string a, string b) => string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b));
public static Func<string, string, bool> test5 = (string a, string b) => string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b); public static Func<string, string, bool> test5 = ((string a, string b) => string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b));
public static Func<string, string, bool> test6 = (string a, string b) => !string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b); public static Func<string, string, bool> test6 = ((string a, string b) => !string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b));
public static Func<string, string, bool> test7 = (string a, string b) => !string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b); public static Func<string, string, bool> test7 = ((string a, string b) => !string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b));
public static void Test(this string a) public static void Test(this string a)
{ {

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs

@ -607,7 +607,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
2012 2012
}.All(set.Add)); }.All(set.Add));
Func<Func<object, object, bool>, bool> sink = (Func<object, object, bool> f) => f(null, null); Func<Func<object, object, bool>, bool> sink = ((Func<object, object, bool> f) => f(null, null));
ToCode(X(), () => sink(object.Equals)); ToCode(X(), () => sink(object.Equals));
} }
@ -623,7 +623,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public void NestedLambda() public void NestedLambda()
{ {
Func<Func<int>, int> call = (Func<int> f) => f(); Func<Func<int>, int> call = ((Func<int> f) => f());
//no params //no params
ToCode(X(), () => call(() => 42)); ToCode(X(), () => call(() => 42));
//one param //one param

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/FixProxyCalls.cs

@ -57,7 +57,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty
{ {
protected internal override string Test(string test) protected internal override string Test(string test)
{ {
Func<string, string> func = (string a) => base.Test(a); Func<string, string> func = ((string a) => base.Test(a));
test = string.Join(test, "aa"); test = string.Join(test, "aa");
return func(test); return func(test);
} }

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InlineAssignmentTest.cs

@ -146,7 +146,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public bool BoolPropertyTest(object x) public bool BoolPropertyTest(object x)
{ {
return BoolProperty = (x != null); return BoolProperty = x != null;
} }
} }
} }

8
ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs

@ -235,18 +235,18 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public static IEnumerable<char> Issue1310a(bool test) public static IEnumerable<char> Issue1310a(bool test)
{ {
#if ROSLYN && OPT #if ROSLYN && OPT
IEnumerable<char> obj = test ? (from c in Enumerable.Range(0, 255) IEnumerable<char> obj = (test ? (from c in Enumerable.Range(0, 255)
where char.IsLetter((char)c) where char.IsLetter((char)c)
select (char)c) : (from c in Enumerable.Range(0, 255) select (char)c) : (from c in Enumerable.Range(0, 255)
where char.IsDigit((char)c) where char.IsDigit((char)c)
select (char)c); select (char)c));
return obj.Concat(obj); return obj.Concat(obj);
#else #else
IEnumerable<char> enumerable = test ? (from c in Enumerable.Range(0, 255) IEnumerable<char> enumerable = (test ? (from c in Enumerable.Range(0, 255)
where char.IsLetter((char)c) where char.IsLetter((char)c)
select (char)c) : (from c in Enumerable.Range(0, 255) select (char)c) : (from c in Enumerable.Range(0, 255)
where char.IsDigit((char)c) where char.IsDigit((char)c)
select (char)c); select (char)c));
return enumerable.Concat(enumerable); return enumerable.Concat(enumerable);
#endif #endif
} }

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ThrowExpressions.cs

@ -14,8 +14,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public ArgumentCheckingCtor(object simpleObj, int? nullableInt) public ArgumentCheckingCtor(object simpleObj, int? nullableInt)
{ {
this.simpleObj = (simpleObj ?? throw new ArgumentNullException("simpleObj")); this.simpleObj = simpleObj ?? throw new ArgumentNullException("simpleObj");
this.nullableInt = (nullableInt ?? throw new ArgumentNullException("nullableInt")); this.nullableInt = nullableInt ?? throw new ArgumentNullException("nullableInt");
} }
public ArgumentCheckingCtor(string input) public ArgumentCheckingCtor(string input)

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeAnalysisTests.cs

@ -35,7 +35,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
return (int)(uint64Field & 0x7FFFFFFF); return (int)(uint64Field & 0x7FFFFFFF);
} }
set { set {
uint64Field = ((uint64Field & 0xFFFFFFFF80000000uL) | (ulong)value); uint64Field = (uint64Field & 0xFFFFFFFF80000000uL) | (ulong)value;
} }
} }
@ -44,7 +44,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
return (ushort)((uint64Field & 0xFFFF000000000000uL) >> 48); return (ushort)((uint64Field & 0xFFFF000000000000uL) >> 48);
} }
set { set {
uint64Field = ((uint64Field & 0xFFFFFFFFFFFFuL) | ((ulong)value << 48)); uint64Field = (uint64Field & 0xFFFFFFFFFFFFuL) | ((ulong)value << 48);
} }
} }

21
ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs

@ -425,19 +425,30 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
{ {
// assignment is right-associative // assignment is right-associative
ParenthesizeIfRequired(assignmentExpression.Left, PrecedenceLevel.Assignment + 1); ParenthesizeIfRequired(assignmentExpression.Left, PrecedenceLevel.Assignment + 1);
if (InsertParenthesesForReadability && !(assignmentExpression.Right is DirectionExpression)) HandleAssignmentRHS(assignmentExpression.Right);
base.VisitAssignmentExpression(assignmentExpression);
}
private void HandleAssignmentRHS(Expression right)
{
if (InsertParenthesesForReadability && !(right is DirectionExpression))
{ {
ParenthesizeIfRequired(assignmentExpression.Right, PrecedenceLevel.RelationalAndTypeTesting + 1); ParenthesizeIfRequired(right, PrecedenceLevel.Conditional + 1);
} }
else else
{ {
ParenthesizeIfRequired(assignmentExpression.Right, PrecedenceLevel.Assignment); ParenthesizeIfRequired(right, PrecedenceLevel.Assignment);
} }
base.VisitAssignmentExpression(assignmentExpression);
} }
// don't need to handle lambdas, they have lowest precedence and unambiguous associativity public override void VisitVariableInitializer(VariableInitializer variableInitializer)
{
if (!variableInitializer.Initializer.IsNull)
HandleAssignmentRHS(variableInitializer.Initializer);
base.VisitVariableInitializer(variableInitializer);
}
// don't need to handle lambdas, they have lowest precedence and unambiguous associativity
public override void VisitQueryExpression(QueryExpression queryExpression) public override void VisitQueryExpression(QueryExpression queryExpression)
{ {
// Query expressions are strange beasts: // Query expressions are strange beasts:

Loading…
Cancel
Save