From a17c67bf380995a946d7ccdf23d4c730c09ef1ff Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 9 Jan 2008 12:37:02 +0000 Subject: [PATCH] Fixed forum-7193: CSharpOutputVisitor bugs git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2799 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Lexer/CSharp/Lexer.cs | 6 ++-- .../CSharp/CSharpOutputVisitor.cs | 30 +++++++++++-------- .../Src/Visitors/ToCSharpConvertVisitor.cs | 14 +++++++++ .../Test/Lexer/CSharp/NumberLexerTest.cs | 3 ++ .../Test/Output/CSharp/CSharpOutputTest.cs | 24 +++++++++++++-- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs b/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs index 6638f93ced..477eeb23b8 100644 --- a/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs +++ b/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs @@ -286,10 +286,10 @@ namespace ICSharpCode.NRefactory.Parser.CSharp islong = true; if (!isunsigned && (peek == 'u' || peek == 'U')) { ReaderRead(); - suffix = "lu"; + suffix = "Lu"; isunsigned = true; } else { - suffix = isunsigned ? "ul" : "l"; + suffix = isunsigned ? "uL" : "L"; } } } @@ -344,7 +344,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp isunsigned = true; } else if (result > uint.MaxValue) { islong = true; - } else if (result > int.MaxValue) { + } else if (islong == false && result > int.MaxValue) { isunsigned = true; } diff --git a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs index b9f13ddace..7a59bf92b3 100644 --- a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs @@ -1087,6 +1087,22 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } public override object TrackedVisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data) + { + TypeReference type = localVariableDeclaration.GetTypeForVariable(0); + for (int i = 1; i < localVariableDeclaration.Variables.Count; ++i) { + if (localVariableDeclaration.GetTypeForVariable(i) != type) + return TrackedVisitLocalVariableDeclarationSeparateTypes(localVariableDeclaration, data); + } + // all variables have the same type + OutputModifier(localVariableDeclaration.Modifier); + TrackVisit(type ?? new TypeReference("object"), data); + outputFormatter.Space(); + AppendCommaSeparatedList(localVariableDeclaration.Variables); + outputFormatter.PrintToken(Tokens.Semicolon); + return null; + } + + object TrackedVisitLocalVariableDeclarationSeparateTypes(LocalVariableDeclaration localVariableDeclaration, object data) { for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) { VariableDeclaration v = (VariableDeclaration)localVariableDeclaration.Variables[i]; @@ -1307,18 +1323,6 @@ namespace ICSharpCode.NRefactory.PrettyPrinter TrackVisit(stmt, data); outputFormatter.NewLine(); } - - // Check if a 'break' should be auto inserted. - if (switchSection.Children.Count == 0 || - !(switchSection.Children[switchSection.Children.Count - 1] is BreakStatement || - switchSection.Children[switchSection.Children.Count - 1] is ContinueStatement || - switchSection.Children[switchSection.Children.Count - 1] is ReturnStatement)) { - outputFormatter.Indent(); - outputFormatter.PrintToken(Tokens.Break); - outputFormatter.PrintToken(Tokens.Semicolon); - outputFormatter.NewLine(); - } - --outputFormatter.IndentationLevel; return null; } @@ -1780,7 +1784,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter outputFormatter.PrintText("u"); } if (val is long || val is ulong) { - outputFormatter.PrintText("l"); + outputFormatter.PrintText("L"); } } else { outputFormatter.PrintText(val.ToString()); diff --git a/src/Libraries/NRefactory/Project/Src/Visitors/ToCSharpConvertVisitor.cs b/src/Libraries/NRefactory/Project/Src/Visitors/ToCSharpConvertVisitor.cs index fe6ba0a2bc..4727b1f829 100644 --- a/src/Libraries/NRefactory/Project/Src/Visitors/ToCSharpConvertVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/Visitors/ToCSharpConvertVisitor.cs @@ -211,5 +211,19 @@ namespace ICSharpCode.NRefactory.Visitors else throw new ArgumentException(); } + + public override object VisitSwitchSection(SwitchSection switchSection, object data) + { + // Check if a 'break' should be auto inserted. + if (switchSection.Children.Count == 0 || + !(switchSection.Children[switchSection.Children.Count - 1] is BreakStatement || + switchSection.Children[switchSection.Children.Count - 1] is ContinueStatement || + switchSection.Children[switchSection.Children.Count - 1] is ThrowStatement || + switchSection.Children[switchSection.Children.Count - 1] is ReturnStatement)) + { + switchSection.Children.Add(new BreakStatement()); + } + return base.VisitSwitchSection(switchSection, data); + } } } diff --git a/src/Libraries/NRefactory/Test/Lexer/CSharp/NumberLexerTest.cs b/src/Libraries/NRefactory/Test/Lexer/CSharp/NumberLexerTest.cs index 4faae45ea1..b6e63066a0 100644 --- a/src/Libraries/NRefactory/Test/Lexer/CSharp/NumberLexerTest.cs +++ b/src/Libraries/NRefactory/Test/Lexer/CSharp/NumberLexerTest.cs @@ -74,6 +74,9 @@ namespace ICSharpCode.NRefactory.Tests.Lexer.CSharp { CheckToken("0x99F", 0x99F); CheckToken("0xAB1f", 0xAB1f); + CheckToken("0xffffffff", 0xffffffff); + CheckToken("0xffffffffL", 0xffffffffL); + CheckToken("0xffffffffuL", 0xffffffffuL); } [Test] diff --git a/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs b/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs index 2099e75e5e..8a0441f35b 100644 --- a/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs +++ b/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs @@ -185,6 +185,26 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter TestStatement("do { } while (true);"); } + [Test] + public void Switch() + { + TestStatement("switch (a) {" + + " case 0:" + + " case 1:" + + " break;" + + " case 2:" + + " return;" + + " default:" + + " throw new Exception(); " + + "}"); + } + + [Test] + public void MultipleVariableForLoop() + { + TestStatement("for (int a = 0, b = 0; b < 100; ++b,a--) { }"); + } + [Test] public void SizeOf() { @@ -225,13 +245,13 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter [Test] public void LongInteger() { - TestExpression("12l"); + TestExpression("12L"); } [Test] public void LongUnsignedInteger() { - TestExpression("12ul"); + TestExpression("12uL"); } [Test]