From dcdf47c86f85107da35b5ea3c57b17e9f824c84d Mon Sep 17 00:00:00 2001 From: Huangli Wu Date: Tue, 5 Dec 2017 10:22:38 -0800 Subject: [PATCH] fix sgen if/else issue (#25562) * Port the if/else fix from SG to corefx. Fix another if/else issue when write member elements only when the total number of members is bigger than 1000 * Use goto instead of do while. * Use do while. * Add missing Indent++ when IsSequence=true. --- .../Generator/XmlSerializationReader.cs | 58 ++++++++++++++----- .../Generator/XmlSerializationWriter.cs | 10 ++-- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs index 655dfaa141f8..1513aaac38b2 100644 --- a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs +++ b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs @@ -1057,7 +1057,7 @@ private void WriteDerivedTypes(StructMapping mapping, bool isTypedReturn, string { for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) { - Writer.Write("else if ("); + Writer.Write("if ("); WriteQNameEqual("xsiType", derived.TypeName, derived.Namespace); Writer.WriteLine(")"); Writer.Indent++; @@ -1094,7 +1094,7 @@ private void WriteEnumAndArrayTypes() if (m is EnumMapping) { EnumMapping mapping = (EnumMapping)m; - Writer.Write("else if ("); + Writer.Write("if ("); WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace); Writer.WriteLine(") {"); Writer.Indent++; @@ -1117,7 +1117,7 @@ private void WriteEnumAndArrayTypes() ArrayMapping mapping = (ArrayMapping)m; if (mapping.TypeDesc.HasDefaultConstructor) { - Writer.Write("else if ("); + Writer.Write("if ("); WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace); Writer.WriteLine(") {"); Writer.Indent++; @@ -1264,10 +1264,10 @@ private void WriteLiteralStructMethod(StructMapping structMapping) Writer.Indent--; } Writer.WriteLine("}"); + Writer.WriteLine("else {"); + Writer.Indent++; WriteDerivedTypes(structMapping, !useReflection && !structMapping.TypeDesc.IsRoot, typeName); if (structMapping.TypeDesc.IsRoot) WriteEnumAndArrayTypes(); - Writer.WriteLine("else"); - Writer.Indent++; if (structMapping.TypeDesc.IsRoot) Writer.Write("return ReadTypedPrimitive(("); else @@ -1277,6 +1277,9 @@ private void WriteLiteralStructMethod(StructMapping structMapping) Writer.Indent--; Writer.WriteLine("}"); + if (structMapping.TypeDesc.IsRoot) + Writer.Indent--; + Writer.WriteLine("}"); if (structMapping.TypeDesc.IsNullable) Writer.WriteLine("if (isNull) return null;"); @@ -2217,16 +2220,28 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e ElementAccessor e = elements[j]; string ns = e.Form == XmlSchemaForm.Qualified ? e.Namespace : ""; if (!isSequence && e.Any && (e.Name == null || e.Name.Length == 0)) continue; - if (!firstElement || (!isSequence && count > 0)) + + if (!isSequence) { - Writer.Write("else "); + if (firstElement && count == 0) + { + Writer.WriteLine("do {"); + Writer.Indent++; + } } - else if (isSequence) + else { - Writer.Write("case "); - Writer.Write(cases.ToString(CultureInfo.InvariantCulture)); - Writer.WriteLine(":"); - Writer.Indent++; + if (!firstElement || (!isSequence && count > 0)) + { + Writer.Write("else "); + } + else if (isSequence) + { + Writer.Write("case "); + Writer.Write(cases.ToString(CultureInfo.InvariantCulture)); + Writer.WriteLine(":"); + Writer.Indent++; + } } count++; firstElement = false; @@ -2316,6 +2331,10 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e Writer.Write(member.ParamsReadSource); Writer.WriteLine(" = true;"); } + if (!isSequence) + { + Writer.WriteLine("break;"); + } Writer.Indent--; Writer.WriteLine("}"); } @@ -2342,10 +2361,10 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e if (count > 0) { if (isSequence) + { Writer.WriteLine("default:"); - else - Writer.WriteLine("else {"); - Writer.Indent++; + Writer.Indent++; + } } WriteMemberElementsElse(anyElement, elementElseString); if (count > 0) @@ -2355,7 +2374,14 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e Writer.WriteLine("break;"); } Writer.Indent--; - Writer.WriteLine("}"); + if (!isSequence) + { + Writer.WriteLine("} while (false);"); + } + else + { + Writer.WriteLine("}"); + } } } diff --git a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs index 22c8fe63366f..215d587fcb48 100644 --- a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs +++ b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs @@ -745,7 +745,7 @@ private void WriteDerivedTypes(StructMapping mapping) for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) { string fullTypeName = derived.TypeDesc.CSharpName; - Writer.Write("else if ("); + Writer.Write("if ("); WriteTypeCompare("t", fullTypeName, derived.TypeDesc.UseReflection); Writer.WriteLine(") {"); Writer.Indent++; @@ -783,7 +783,7 @@ private void WriteEnumAndArrayTypes() { EnumMapping mapping = (EnumMapping)m; string fullTypeName = mapping.TypeDesc.CSharpName; - Writer.Write("else if ("); + Writer.Write("if ("); WriteTypeCompare("t", fullTypeName, mapping.TypeDesc.UseReflection); Writer.WriteLine(") {"); Writer.Indent++; @@ -815,7 +815,7 @@ private void WriteEnumAndArrayTypes() ArrayMapping mapping = m as ArrayMapping; if (mapping == null || m.IsSoap) continue; string fullTypeName = mapping.TypeDesc.CSharpName; - Writer.Write("else if ("); + Writer.Write("if ("); if (mapping.TypeDesc.IsArray) WriteArrayTypeCompare("t", fullTypeName, mapping.TypeDesc.ArrayElementTypeDesc.CSharpName, mapping.TypeDesc.UseReflection); else @@ -885,12 +885,12 @@ private void WriteStructMethod(StructMapping mapping) WriteTypeCompare("t", fullTypeName, mapping.TypeDesc.UseReflection); Writer.WriteLine(") {"); Writer.WriteLine("}"); + Writer.WriteLine("else {"); + Writer.Indent++; WriteDerivedTypes(mapping); if (mapping.TypeDesc.IsRoot) WriteEnumAndArrayTypes(); - Writer.WriteLine("else {"); - Writer.Indent++; if (mapping.TypeDesc.IsRoot) { Writer.WriteLine("WriteTypedPrimitive(n, ns, o, true);");