Browse Source

Manipulated comments to avoid duplication of summary block. (#876)

Fixes #759 Works around: https://bugs.llvm.org/show_bug.cgi?id=33333
pull/881/head
Kimon Topouzidis 8 years ago committed by Dimitar Dobrev
parent
commit
d7c8a61ecc
  1. 14
      src/Generator.Tests/Passes/TestPasses.cs
  2. 1
      src/Generator/Driver.cs
  3. 4
      src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
  4. 4
      src/Generator/Generators/CodeGenerator.cs
  5. 105
      src/Generator/Passes/CleanCommentsPass.cs
  6. 5
      tests/Native/Passes.h

14
src/Generator.Tests/Passes/TestPasses.cs

@ -62,6 +62,20 @@ namespace CppSharp.Generator.Tests.Passes @@ -62,6 +62,20 @@ namespace CppSharp.Generator.Tests.Passes
Assert.IsNotNull(c.Method("Start"));
}
[Test]
public void TestCleanCommentsPass()
{
var c = AstContext.FindClass("TestCommentsPass").FirstOrDefault();
passBuilder.AddPass(new CleanCommentsPass());
passBuilder.RunPasses(pass => pass.VisitDeclaration(c));
var para = (ParagraphComment)c.Comment.FullComment.Blocks[0];
var s = para.CommentToString(CommentKind.BCPLSlash);
Assert.That(s, Is.EqualTo("/// <summary>A simple test.</summary>"));
}
[Test]
public void TestCaseRenamePass()
{

1
src/Generator/Driver.cs

@ -304,6 +304,7 @@ namespace CppSharp @@ -304,6 +304,7 @@ namespace CppSharp
TranslationUnitPasses.AddPass(new CheckAmbiguousFunctions());
TranslationUnitPasses.AddPass(new CheckOperatorsOverloadsPass());
TranslationUnitPasses.AddPass(new CheckVirtualOverrideReturnCovariance());
TranslationUnitPasses.AddPass(new CleanCommentsPass());
Generator.SetupPasses();

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

@ -93,6 +93,10 @@ namespace CppSharp.Generators.CSharp @@ -93,6 +93,10 @@ namespace CppSharp.Generators.CSharp
var text = textComment.Text;
if (trim)
text = text.Trim();
if (Helpers.RegexTag.IsMatch(text))
return String.Empty;
return HtmlEncoder.HtmlEncode(
text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text);
}

4
src/Generator/Generators/CodeGenerator.cs

@ -6,6 +6,7 @@ using System.Web.Util; @@ -6,6 +6,7 @@ using System.Web.Util;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators.CSharp;
using System.Text.RegularExpressions;
namespace CppSharp.Generators
{
@ -142,7 +143,7 @@ namespace CppSharp.Generators @@ -142,7 +143,7 @@ namespace CppSharp.Generators
if (comment.FullComment != null)
{
PushBlock(BlockKind.BlockComment);
WriteLine(comment.FullComment.CommentToString(CommentKind));
WriteLine(comment.FullComment.CommentToString(DocumentationCommentKind));
PopBlock();
return;
}
@ -393,6 +394,7 @@ namespace CppSharp.Generators @@ -393,6 +394,7 @@ namespace CppSharp.Generators
public static class Helpers
{
public static Regex RegexTag = new Regex(@"^(<|</)[a-zA-Z][\w\-]*?>?$");
public static readonly string InternalStruct = Generator.GeneratedIdentifier("Internal");
public static readonly string InstanceField = Generator.GeneratedIdentifier("instance");
public static readonly string InstanceIdentifier = Generator.GeneratedIdentifier("Instance");

105
src/Generator/Passes/CleanCommentsPass.cs

@ -0,0 +1,105 @@ @@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CppSharp.AST;
using CppSharp.Generators.CSharp;
using System.Text.RegularExpressions;
namespace CppSharp.Passes
{
public class CleanCommentsPass : TranslationUnitPass, ICommentVisitor<bool>
{
public bool VisitBlockCommand(BlockCommandComment comment)
{
return true;
}
public override bool VisitDeclaration(Declaration decl)
{
if (!base.VisitDeclaration(decl))
return false;
if (decl.Comment != null)
{
var fullComment = decl.Comment.FullComment;
VisitFull(fullComment);
}
return true;
}
public bool VisitFull(FullComment comment)
{
foreach (var block in comment.Blocks)
block.Visit(this);
return true;
}
#region Comments Visit
public bool VisitHTMLEndTag(HTMLEndTagComment comment)
{
return true;
}
public bool VisitHTMLStartTag(HTMLStartTagComment comment)
{
return true;
}
public bool VisitInlineCommand(InlineCommandComment comment)
{
return true;
}
public bool VisitParagraphCommand(ParagraphComment comment)
{
bool tag = false;
foreach (var item in comment.Content.Where(c => c.Kind == DocumentationCommentKind.TextComment))
{
TextComment com = (TextComment) item;
if (Generators.Helpers.RegexTag.IsMatch(com.Text))
tag = true;
else if (tag)
com.Text = com.Text.Substring(1);
if (com.Text.StartsWith("<", StringComparison.Ordinal))
com.Text = $"{com.Text}{">"}";
else if (com.Text.StartsWith(">", StringComparison.Ordinal))
com.Text = com.Text.Substring(1);
}
return true;
}
public bool VisitParamCommand(ParamCommandComment comment)
{
return true;
}
public bool VisitText(TextComment comment)
{
return true;
}
public bool VisitTParamCommand(TParamCommandComment comment)
{
return true;
}
public bool VisitVerbatimBlock(VerbatimBlockComment comment)
{
return true;
}
public bool VisitVerbatimBlockLine(VerbatimBlockLineComment comment)
{
return true;
}
public bool VisitVerbatimLine(VerbatimLineComment comment)
{
return true;
}
#endregion
}
}

5
tests/Native/Passes.h

@ -26,6 +26,11 @@ struct TestRename @@ -26,6 +26,11 @@ struct TestRename
int lowerCaseField;
};
/// <summary>A simple test.</summary>
class TestCommentsPass
{
};
struct TestReadOnlyProperties
{
int readOnlyProperty;

Loading…
Cancel
Save