From 99b4726d9119c16f76cbea8716f387c084d1bac7 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 17 May 2011 16:23:46 +0200 Subject: [PATCH 1/8] fix https://github.com/icsharpcode/ILSpy/issues/177 --- ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs index 1c770b634f..eb5e9d2baa 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs @@ -963,7 +963,9 @@ namespace ICSharpCode.NRefactory.CSharp case '\v': return "\\v"; default: - if (char.IsControl(ch) || char.IsSurrogate(ch)) { + if (char.IsControl(ch) || char.IsSurrogate(ch) || + // print all uncommon white spaces as numbers + (char.IsWhiteSpace(ch) && ch != ' ')) { return "\\u" + ((int)ch).ToString("x4"); } else { return ch.ToString(); From 5997f81668454263750ed0bb3a3b12f4e83acf5a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 20 May 2011 11:17:39 +0200 Subject: [PATCH 2/8] add foldings to documentation comments; closes #165 --- .../CSharp/OutputVisitor/IOutputFormatter.cs | 2 +- ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs | 4 +++- .../CSharp/OutputVisitor/TextWriterOutputFormatter.cs | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs index ac1966296c..58f8c23ed3 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs @@ -39,6 +39,6 @@ namespace ICSharpCode.NRefactory.CSharp void NewLine(); - void WriteComment(CommentType commentType, string content); + void WriteComment(CommentType commentType, string content, bool isLastLine = false); } } diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs index eb5e9d2baa..e715324ef6 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs @@ -2147,7 +2147,9 @@ namespace ICSharpCode.NRefactory.CSharp // "1.0 / /*comment*/a", then we need to insert a space in front of the comment. formatter.Space(); } - formatter.WriteComment(comment.CommentType, comment.Content); + bool isContinuing = (comment.NextSibling is Comment && ((Comment)comment.NextSibling).CommentType == comment.CommentType); + + formatter.WriteComment(comment.CommentType, comment.Content, !isContinuing); lastWritten = LastWritten.Whitespace; return null; } diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs index f8c1340e58..c6081d4f51 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs @@ -88,7 +88,7 @@ namespace ICSharpCode.NRefactory.CSharp indentation--; } - public void WriteComment(CommentType commentType, string content) + public void WriteComment(CommentType commentType, string content, bool isLastLine = false) { WriteIndentation(); switch (commentType) { From 9a35ee440463757af542006512fabb51d74b40b1 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 20 May 2011 12:46:16 +0200 Subject: [PATCH 3/8] Applied some of the optimizations suggested by Kris Vandermotten. #150 --- ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs | 4 ++-- ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs | 2 +- .../CSharp/Parser/TypeSystemConvertVisitor.cs | 4 ++-- ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs b/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs index b888cdea23..813f472137 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs @@ -52,7 +52,7 @@ namespace ICSharpCode.NRefactory.CSharp public int PointerRank { get { - return GetChildrenByRole(PointerRole).Count(); + return GetChildrenByRole(PointerRole).Count; } set { if (value < 0) @@ -141,7 +141,7 @@ namespace ICSharpCode.NRefactory.CSharp } public int Dimensions { - get { return 1 + GetChildrenByRole(Roles.Comma).Count(); } + get { return 1 + GetChildrenByRole(Roles.Comma).Count; } set { int d = this.Dimensions; while (d > value) { diff --git a/ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs b/ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs index 70f1ddca48..6db26e523a 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs @@ -96,7 +96,7 @@ namespace ICSharpCode.NRefactory.CSharp { if (name == null) throw new ArgumentNullException("name"); - IsVerbatim = name.StartsWith ("@"); + IsVerbatim = name.Length > 0 && name[0] == '@'; this.Name = IsVerbatim ? name.Substring (1) : name; this.startLocation = location; } diff --git a/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs b/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs index 66865d53c8..756adce400 100644 --- a/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs @@ -263,7 +263,7 @@ namespace ICSharpCode.NRefactory.CSharp #region Fields public override IEntity VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data) { - bool isSingleField = fieldDeclaration.Variables.Count() == 1; + bool isSingleField = fieldDeclaration.Variables.Count == 1; Modifiers modifiers = fieldDeclaration.Modifiers; DefaultField field = null; foreach (VariableInitializer vi in fieldDeclaration.Variables) { @@ -476,7 +476,7 @@ namespace ICSharpCode.NRefactory.CSharp #region Events public override IEntity VisitEventDeclaration(EventDeclaration eventDeclaration, object data) { - bool isSingleEvent = eventDeclaration.Variables.Count() == 1; + bool isSingleEvent = eventDeclaration.Variables.Count == 1; Modifiers modifiers = eventDeclaration.Modifiers; DefaultEvent ev = null; foreach (VariableInitializer vi in eventDeclaration.Variables) { diff --git a/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs b/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs index 3bcea943b3..162f5c2bf3 100644 --- a/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs @@ -241,7 +241,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver ResolveResult VisitFieldOrEventDeclaration(AttributedNode fieldOrEventDeclaration) { - int initializerCount = fieldOrEventDeclaration.GetChildrenByRole(FieldDeclaration.Roles.Variable).Count(); + int initializerCount = fieldOrEventDeclaration.GetChildrenByRole(FieldDeclaration.Roles.Variable).Count; ResolveResult result = null; for (AstNode node = fieldOrEventDeclaration.FirstChild; node != null; node = node.NextSibling) { if (node.Role == FieldDeclaration.Roles.Variable) { @@ -939,7 +939,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver firstInitializer != null ? firstInitializer.Initializer : null, false); - int initializerCount = variableDeclarationStatement.Variables.Count(); + int initializerCount = variableDeclarationStatement.Variables.Count; ResolveResult result = null; for (AstNode node = variableDeclarationStatement.FirstChild; node != null; node = node.NextSibling) { if (node.Role == FieldDeclaration.Roles.Variable) { From 7738421802527f3a52f56dce7209dccf13f545ff Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 20 May 2011 18:40:22 +0200 Subject: [PATCH 4/8] Fixed position of XML comments on nested types. --- .../Documentation/XmlDocumentationProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs b/ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs index 315f212ebd..70d88aa164 100644 --- a/ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs +++ b/ICSharpCode.NRefactory/Documentation/XmlDocumentationProvider.cs @@ -120,7 +120,7 @@ namespace ICSharpCode.NRefactory.Documentation static string GetRedirectionTarget(string target) { - string programFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); + string programFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); programFilesDir = AppendDirectorySeparator(programFilesDir); string corSysDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(); From 3974a801e1d5f2deb437f542335f5aa36a4dfac4 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 20 May 2011 13:23:44 +0200 Subject: [PATCH 5/8] remove isLastLine from interface - use stack instead; fix null reference if resolve of TypeDefinition fails --- .../CSharp/OutputVisitor/IOutputFormatter.cs | 2 +- ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs | 4 +--- .../CSharp/OutputVisitor/TextWriterOutputFormatter.cs | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs index 58f8c23ed3..ac1966296c 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs @@ -39,6 +39,6 @@ namespace ICSharpCode.NRefactory.CSharp void NewLine(); - void WriteComment(CommentType commentType, string content, bool isLastLine = false); + void WriteComment(CommentType commentType, string content); } } diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs index e715324ef6..eb5e9d2baa 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs @@ -2147,9 +2147,7 @@ namespace ICSharpCode.NRefactory.CSharp // "1.0 / /*comment*/a", then we need to insert a space in front of the comment. formatter.Space(); } - bool isContinuing = (comment.NextSibling is Comment && ((Comment)comment.NextSibling).CommentType == comment.CommentType); - - formatter.WriteComment(comment.CommentType, comment.Content, !isContinuing); + formatter.WriteComment(comment.CommentType, comment.Content); lastWritten = LastWritten.Whitespace; return null; } diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs index c6081d4f51..f8c1340e58 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs @@ -88,7 +88,7 @@ namespace ICSharpCode.NRefactory.CSharp indentation--; } - public void WriteComment(CommentType commentType, string content, bool isLastLine = false) + public void WriteComment(CommentType commentType, string content) { WriteIndentation(); switch (commentType) { From a238bf1cfe08bf68a2ae22997331d20dcab70be0 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 20 May 2011 21:33:38 +0200 Subject: [PATCH 6/8] When decompiling a field, also decompile constructors to check whether there's an initializer on the field. When decompiling a constructor, display field initializers outside of the constructor. Closes #3. --- ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs index eb5e9d2baa..ee76d82eed 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs @@ -2147,7 +2147,9 @@ namespace ICSharpCode.NRefactory.CSharp // "1.0 / /*comment*/a", then we need to insert a space in front of the comment. formatter.Space(); } + formatter.StartNode(comment); formatter.WriteComment(comment.CommentType, comment.Content); + formatter.EndNode(comment); lastWritten = LastWritten.Whitespace; return null; } From b07228e982fda026aa895c949ec01ebb13c8b662 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 21 May 2011 00:45:59 +0200 Subject: [PATCH 7/8] Fixed issues with detection of using statements. --- .../CSharp/Analysis/DefiniteAssignmentAnalysis.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ICSharpCode.NRefactory/CSharp/Analysis/DefiniteAssignmentAnalysis.cs b/ICSharpCode.NRefactory/CSharp/Analysis/DefiniteAssignmentAnalysis.cs index d4cc6e1c53..452ca686a5 100644 --- a/ICSharpCode.NRefactory/CSharp/Analysis/DefiniteAssignmentAnalysis.cs +++ b/ICSharpCode.NRefactory/CSharp/Analysis/DefiniteAssignmentAnalysis.cs @@ -167,12 +167,14 @@ namespace ICSharpCode.NRefactory.CSharp.Analysis /// This method can be used to restrict the analysis to only a part of the method. /// Only the control flow paths that are fully contained within the selected part will be analyzed. /// - /// Both 'start' and 'end' are inclusive. - public void SetAnalyzedRange(Statement start, Statement end) + /// By default, both 'start' and 'end' are inclusive. + public void SetAnalyzedRange(Statement start, Statement end, bool startInclusive = true, bool endInclusive = true) { - Debug.Assert(beginNodeDict.ContainsKey(start) && endNodeDict.ContainsKey(end)); - int startIndex = beginNodeDict[start].Index; - int endIndex = endNodeDict[end].Index; + var dictForStart = startInclusive ? beginNodeDict : endNodeDict; + var dictForEnd = endInclusive ? endNodeDict : beginNodeDict; + Debug.Assert(dictForStart.ContainsKey(start) && dictForEnd.ContainsKey(end)); + int startIndex = dictForStart[start].Index; + int endIndex = dictForEnd[end].Index; if (startIndex > endIndex) throw new ArgumentException("The start statement must be lexically preceding the end statement"); this.analyzedRangeStart = startIndex; From eb6f6c19c1dbb910c6485e5f5a905ca7b2c2b1a8 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 21 May 2011 17:21:59 +0200 Subject: [PATCH 8/8] Make ConvertChar and ConvertString public. --- .../CSharp/OutputVisitor/OutputVisitor.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs index ee76d82eed..da524257d0 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs @@ -941,7 +941,11 @@ namespace ICSharpCode.NRefactory.CSharp return ConvertChar(ch); } - static string ConvertChar(char ch) + /// + /// Gets the escape sequence for the specified character. + /// + /// This method does not convert ' or ". + public static string ConvertChar(char ch) { switch (ch) { case '\\': @@ -973,7 +977,10 @@ namespace ICSharpCode.NRefactory.CSharp } } - static string ConvertString(string str) + /// + /// Converts special characters to escape sequences within the given string. + /// + public static string ConvertString(string str) { StringBuilder sb = new StringBuilder(); foreach (char ch in str) {