Browse Source

Fixed #453 - incorrect comment generation.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/642/head
Dimitar Dobrev 9 years ago
parent
commit
1d3e821093
  1. 90
      src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
  2. 5
      tests/Common/Common.cpp
  3. 23
      tests/Common/Common.h

90
src/Generator/Generators/CSharp/CSharpCommentPrinter.cs

@ -1,4 +1,5 @@
using System.Text; using System.Collections.Generic;
using System.Text;
using System.Web.Util; using System.Web.Util;
using CppSharp.AST; using CppSharp.AST;
@ -8,25 +9,22 @@ namespace CppSharp.Generators.CSharp
{ {
public static string CommentToString(this Comment comment, string commentPrefix) public static string CommentToString(this Comment comment, string commentPrefix)
{ {
var summaryAdded = false; int boundary = 0;
var remarksAdded = false; var commentLines = GetCommentLines(comment, ref boundary);
return CommentToString( TrimSection(commentLines, 0, boundary);
comment, ref summaryAdded, ref remarksAdded, commentPrefix).ToString(); TrimSection(commentLines, boundary, commentLines.Count);
return FormatComment(commentLines, boundary, commentPrefix);
} }
private static StringBuilder CommentToString(Comment comment, private static List<string> GetCommentLines(Comment comment, ref int boundary)
ref bool summaryAdded, ref bool remarksAdded, string commentPrefix)
{ {
var commentBuilder = new StringBuilder(); var commentLines = new List<string>();
switch (comment.Kind) switch (comment.Kind)
{ {
case CommentKind.FullComment: case CommentKind.FullComment:
var fullComment = (FullComment) comment; var fullComment = (FullComment) comment;
foreach (var block in fullComment.Blocks) foreach (var block in fullComment.Blocks)
commentBuilder.Append(CommentToString(block, commentLines.AddRange(GetCommentLines(block, ref boundary));
ref summaryAdded, ref remarksAdded, commentPrefix));
if (remarksAdded)
commentBuilder.AppendFormat("{0} </remarks>", commentPrefix);
break; break;
case CommentKind.BlockCommandComment: case CommentKind.BlockCommandComment:
break; break;
@ -39,10 +37,12 @@ namespace CppSharp.Generators.CSharp
case CommentKind.VerbatimLineComment: case CommentKind.VerbatimLineComment:
break; break;
case CommentKind.ParagraphComment: case CommentKind.ParagraphComment:
var summaryParagraph = boundary == 0;
var paragraphComment = (ParagraphComment) comment; var paragraphComment = (ParagraphComment) comment;
foreach (var inlineContentComment in paragraphComment.Content) foreach (var inlineContentComment in paragraphComment.Content)
commentBuilder.Append(CommentToString(inlineContentComment, commentLines.AddRange(GetCommentLines(inlineContentComment, ref boundary));
ref summaryAdded, ref remarksAdded, commentPrefix)); if (summaryParagraph)
boundary = commentLines.Count;
break; break;
case CommentKind.HTMLTagComment: case CommentKind.HTMLTagComment:
break; break;
@ -51,21 +51,9 @@ namespace CppSharp.Generators.CSharp
case CommentKind.HTMLEndTagComment: case CommentKind.HTMLEndTagComment:
break; break;
case CommentKind.TextComment: case CommentKind.TextComment:
if (!summaryAdded) commentLines.Add(GetText(comment));
commentBuilder.AppendFormat("{0} <summary>", commentPrefix).AppendLine(); if (boundary == 0)
if (summaryAdded && !remarksAdded) boundary = commentLines.Count;
{
commentBuilder.AppendFormat("{0} <remarks>", commentPrefix).AppendLine();
remarksAdded = true;
}
commentBuilder.AppendFormat(
"{0} <para>{1}</para>", commentPrefix, GetText(comment));
commentBuilder.AppendLine();
if (!summaryAdded)
{
commentBuilder.AppendFormat("{0} </summary>", commentPrefix).AppendLine();
summaryAdded = true;
}
break; break;
case CommentKind.InlineContentComment: case CommentKind.InlineContentComment:
break; break;
@ -74,7 +62,7 @@ namespace CppSharp.Generators.CSharp
case CommentKind.VerbatimBlockLineComment: case CommentKind.VerbatimBlockLineComment:
break; break;
} }
return commentBuilder; return commentLines;
} }
private static string GetText(Comment comment) private static string GetText(Comment comment)
@ -84,5 +72,47 @@ namespace CppSharp.Generators.CSharp
return HtmlEncoder.HtmlEncode( return HtmlEncoder.HtmlEncode(
text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text); text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text);
} }
private static void TrimSection(List<string> commentLines, int start, int end)
{
for (int i = start; i < end; i++)
{
if (string.IsNullOrWhiteSpace(commentLines[i]))
commentLines.RemoveAt(i--);
else
break;
}
for (int i = end - 1; i >= start; i--)
{
if (string.IsNullOrWhiteSpace(commentLines[i]))
commentLines.RemoveAt(i);
else
break;
}
}
private static string FormatComment(List<string> commentLines, int boundary, string commentPrefix)
{
var commentBuilder = new StringBuilder();
commentBuilder.AppendLine("<summary>");
for (int i = 0; i < boundary; i++)
{
commentBuilder.AppendFormat("{0} <para>{1}</para>", commentPrefix, commentLines[i]);
commentBuilder.AppendLine();
}
commentBuilder.Append("</summary>");
if (boundary < commentLines.Count)
{
commentBuilder.AppendLine();
commentBuilder.AppendLine("<remarks>");
for (int i = boundary; i < commentLines.Count; i++)
{
commentBuilder.AppendFormat("{0} <para>{1}</para>", commentPrefix, commentLines[i]);
commentBuilder.AppendLine();
}
commentBuilder.Append("</remarks>");
}
return commentBuilder.ToString();
}
} }
} }

5
tests/Common/Common.cpp

@ -581,6 +581,11 @@ int OverridesNonDirectVirtual::retInt()
return 3; return 3;
} }
const char * TestComments::GetIOHandlerControlSequence(char ch)
{
return 0;
}
AbstractWithVirtualDtor::AbstractWithVirtualDtor() AbstractWithVirtualDtor::AbstractWithVirtualDtor()
{ {
} }

23
tests/Common/Common.h

@ -987,7 +987,28 @@ AbstractTemplate<T>::AbstractTemplate()
/** Note that to prevent extra memory use due to vtable pointer, %HashBase intentionally does not declare a virtual destructor /** Note that to prevent extra memory use due to vtable pointer, %HashBase intentionally does not declare a virtual destructor
and therefore %HashBase pointers should never be used. and therefore %HashBase pointers should never be used.
*/ */
class DLL_API TestComments {}; class DLL_API TestComments
{
public:
//----------------------------------------------------------------------
/// Get the string that needs to be written to the debugger stdin file
/// handle when a control character is typed.
///
/// Some GUI programs will intercept "control + char" sequences and want
/// to have them do what normally would happen when using a real
/// terminal, so this function allows GUI programs to emulate this
/// functionality.
///
/// @param[in] ch
/// The character that was typed along with the control key
///
/// @return
/// The string that should be written into the file handle that is
/// feeding the input stream for the debugger, or NULL if there is
/// no string for this control key.
//----------------------------------------------------------------------
const char * GetIOHandlerControlSequence(char ch);
};
class DLL_API AbstractWithVirtualDtor class DLL_API AbstractWithVirtualDtor
{ {

Loading…
Cancel
Save