Skip to content

Commit c5e2b4b

Browse files
author
Maksim Volkau
committed
fixed: #487; also remove double (( in if conditions; use og AllowPrintIL inside PrintCSharp instead of AllowPrintCS
1 parent cbe9348 commit c5e2b4b

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10813,7 +10813,8 @@ private static bool AvoidParens(EnclosedIn enclosedIn) =>
1081310813
enclosedIn == EnclosedIn.AvoidParens |
1081410814
enclosedIn == EnclosedIn.LambdaBody |
1081510815
enclosedIn == EnclosedIn.Block | // statement in a block don't need the parens as well
10816-
enclosedIn == EnclosedIn.Return;
10816+
enclosedIn == EnclosedIn.Return |
10817+
enclosedIn == EnclosedIn.IfTest;
1081710818

1081810819
private static StringBuilder AddParenIfNeeded(this StringBuilder sb, char paren, bool avoidParen = false) =>
1081910820
avoidParen ? sb : sb.Append(paren);

src/FastExpressionCompiler/TestTools.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public static class TestTools
3838
static TestTools()
3939
{
4040
#if DEBUG
41-
AllowPrintIL = true;
41+
// AllowPrintIL = true;
4242
AllowPrintCS = true;
43-
AllowPrintExpression = true;
43+
// AllowPrintExpression = true;
4444
#endif
4545
}
4646

@@ -93,7 +93,7 @@ public static void PrintExpression(this IDelegateDebugInfo debugInfo, bool compl
9393
public static void PrintCSharp(this Expression expr, bool completeTypeNames = false, bool stripNamespace = true,
9494
[CallerMemberName] string caller = "", [CallerFilePath] string filePath = "")
9595
{
96-
if (!AllowPrintIL) return;
96+
if (!AllowPrintCS) return;
9797
Console.WriteLine();
9898
Console.WriteLine($"//{Path.GetFileNameWithoutExtension(filePath)}.{caller}");
9999

@@ -129,7 +129,6 @@ public static void PrintCSharp(this Expression expr, CodePrinter.ObjectToCode ob
129129
[CallerMemberName] string caller = "", [CallerFilePath] string filePath = "")
130130
{
131131
if (!AllowPrintCS) return;
132-
133132
Console.WriteLine();
134133
Console.WriteLine($"//{Path.GetFileNameWithoutExtension(filePath)}.{caller}");
135134
var sb = expr.ToCSharpString(new StringBuilder(1024), ToCSharpPrinter.EnclosedIn.AvoidParens, notRecognizedToCode: objectToCode, stripNamespace: stripNamespace).AppendSemicolonOnce();

test/FastExpressionCompiler.IssueTests/Issue440_Errors_with_simplified_Switch_cases.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public int Run()
1717
{
1818
Switch_with_single_case_without_default();
1919
#if !NETFRAMEWORK
20+
Switch_with_no_cases_but_default();
2021
// Switch without cases is not supported in .NET 472
2122
Switch_with_no_cases();
22-
Switch_with_no_cases_but_default();
2323
return 3;
2424
#endif
2525
return 1;
@@ -66,7 +66,7 @@ public void Switch_with_no_cases()
6666
);
6767

6868
var expr = Lambda<Func<int>>(block);
69-
expr.PrintCSharp(); // todo: @fixme for the empty switch
69+
expr.PrintCSharp(); // todo: @fixme for the empty switch - do not output the switch at all
7070

7171
var fs = expr.CompileSys();
7272
fs.PrintIL();

test/FastExpressionCompiler.IssueTests/Issue83_linq2db.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Reflection;
55
using System.Threading;
66

7-
87
#pragma warning disable IDE1006 // Naming Styles for linq2db
98
#pragma warning disable 649 // Unassigned fields
109

@@ -17,7 +16,6 @@ namespace FastExpressionCompiler.LightExpression.IssueTests
1716
namespace FastExpressionCompiler.IssueTests
1817
#endif
1918
{
20-
2119
public sealed class Issue83_linq2db : ITest
2220
{
2321
public int Run()

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ public class Program
1111
{
1212
public static void Main()
1313
{
14+
// TestTools.AllowPrintCS = false;
15+
// TestTools.AllowPrintIL = false;
16+
// TestTools.AllowPrintExpression = false;
17+
1418
// ILGeneratorTools.DisableILGeneratorPooling = true;
1519
// LightExpression.ILGeneratorTools.DisableILGeneratorPooling = true;
1620

17-
new Issue458_Support_TryFault().Run();
21+
new Issue440_Errors_with_simplified_Switch_cases().Run(); // test print cs
1822

23+
// new LoopTests().Run(); // test print cs
24+
// new Issue458_Support_TryFault().Run();
1925
// new Issue127_Switch_is_supported().Run();
2026
// new Issue316_in_parameter().Run();
2127
// new LightExpression.IssueTests.Issue347_InvalidProgramException_on_compiling_an_expression_that_returns_a_record_which_implements_IList().Run();

test/FastExpressionCompiler.UnitTests/LoopTests.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22

3-
43
#if LIGHT_EXPRESSION
54
using static FastExpressionCompiler.LightExpression.Expression;
65
namespace FastExpressionCompiler.LightExpression.UnitTests
@@ -9,7 +8,6 @@ namespace FastExpressionCompiler.LightExpression.UnitTests
98
namespace FastExpressionCompiler.UnitTests
109
#endif
1110
{
12-
1311
public class LoopTests : ITest
1412
{
1513
public int Run()
@@ -24,7 +22,6 @@ public int Run()
2422
return 7;
2523
}
2624

27-
2825
public void Loop_with_return()
2926
{
3027
var intVariable = Variable(typeof(int), "i");
@@ -40,14 +37,15 @@ public void Loop_with_return()
4037
Label(returnLabel));
4138

4239
var loopLambda = Lambda<Action>(lambdaBody);
40+
loopLambda.PrintCSharp();
41+
4342
var loopFunc = loopLambda.CompileFast(true);
4443

4544
Asserts.IsNotNull(loopFunc);
4645

4746
loopFunc.Invoke();
4847
}
4948

50-
5149
public void Loop_with_break()
5250
{
5351
var intVariable = Variable(typeof(int), "i");
@@ -62,13 +60,13 @@ public void Loop_with_break()
6260
breakLabel)
6361
);
6462
var loopFunc = loopLambda.CompileFast(true);
63+
loopLambda.PrintCSharp();
6564

6665
Asserts.IsNotNull(loopFunc);
6766

6867
loopFunc.Invoke();
6968
}
7069

71-
7270
public void Loop_with_unused_break()
7371
{
7472
var intVariable = Variable(typeof(int), "i");
@@ -84,14 +82,14 @@ public void Loop_with_unused_break()
8482
var lambdaBody = Block(new[] { intVariable }, loop, Label(returnLabel));
8583

8684
var loopLambda = Lambda<Action>(lambdaBody);
85+
loopLambda.PrintCSharp();
8786
var loopFunc = loopLambda.CompileFast(true);
8887

8988
Asserts.IsNotNull(loopFunc);
9089

9190
loopFunc.Invoke();
9291
}
9392

94-
9593
public void Loop_with_break_and_continue()
9694
{
9795
var intVariable1 = Variable(typeof(int), "i");
@@ -115,14 +113,14 @@ public void Loop_with_break_and_continue()
115113
var lambdaBody = Block(new[] { intVariable1, intVariable2 }, loopWithBreakAndContinue);
116114

117115
var loopLambda = Lambda<Action>(lambdaBody);
116+
loopLambda.PrintCSharp();
118117
var loopFunc = loopLambda.CompileFast(true);
119118

120119
Asserts.IsNotNull(loopFunc);
121120

122121
loopFunc.Invoke();
123122
}
124123

125-
126124
public void Loop_with_unused_continue()
127125
{
128126
var intVariable = Variable(typeof(int), "i");
@@ -138,14 +136,14 @@ public void Loop_with_unused_continue()
138136
var lambdaBody = Block(new[] { intVariable }, loop);
139137

140138
var loopLambda = Lambda<Action>(lambdaBody);
139+
loopLambda.PrintCSharp();
141140
var loopFunc = loopLambda.CompileFast(true);
142141

143142
Asserts.IsNotNull(loopFunc);
144143

145144
loopFunc.Invoke();
146145
}
147146

148-
149147
public void Loop_with_unused_break_and_continue()
150148
{
151149
var intVariable = Variable(typeof(int), "i");
@@ -162,14 +160,14 @@ public void Loop_with_unused_break_and_continue()
162160
var lambdaBody = Block(new[] { intVariable }, loop, Label(returnLabel));
163161

164162
var loopLambda = Lambda<Action>(lambdaBody);
163+
loopLambda.PrintCSharp();
165164
var loopFunc = loopLambda.CompileFast(true);
166165

167166
Asserts.IsNotNull(loopFunc);
168167

169168
loopFunc.Invoke();
170169
}
171170

172-
173171
public void Loop_with_return_value()
174172
{
175173
var intVariable = Variable(typeof(int), "i");
@@ -184,6 +182,7 @@ public void Loop_with_return_value()
184182
var lambdaBody = Block(new[] { intVariable }, loop, Label(returnLabel, Constant(3)));
185183

186184
var loopLambda = Lambda<Func<int>>(lambdaBody);
185+
loopLambda.PrintCSharp();
187186
var loopFunc = loopLambda.CompileFast(true);
188187

189188
Asserts.IsNotNull(loopFunc);

0 commit comments

Comments
 (0)