Skip to content

Commit f89209c

Browse files
authored
Feat/improved clean comment pass (#1928)
* Code cleanup * Improved XML style comment parsing * Fix test errors
1 parent 6fe8c66 commit f89209c

File tree

10 files changed

+355
-94
lines changed

10 files changed

+355
-94
lines changed

src/AST/Comment.cs

+37-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace CppSharp.AST
56
{
@@ -109,7 +110,7 @@ public interface ICommentVisitor<out T>
109110
T VisitTParamCommand(TParamCommandComment comment);
110111
T VisitVerbatimBlock(VerbatimBlockComment comment);
111112
T VisitVerbatimLine(VerbatimLineComment comment);
112-
T VisitParagraphCommand(ParagraphComment comment);
113+
T VisitParagraph(ParagraphComment comment);
113114
T VisitFull(FullComment comment);
114115
T VisitHTMLStartTag(HTMLStartTagComment comment);
115116
T VisitHTMLEndTag(HTMLEndTagComment comment);
@@ -129,20 +130,13 @@ public abstract class Comment
129130

130131
public static string GetMultiLineCommentPrologue(CommentKind kind)
131132
{
132-
switch (kind)
133+
return kind switch
133134
{
134-
case CommentKind.BCPL:
135-
case CommentKind.BCPLExcl:
136-
return "//";
137-
case CommentKind.C:
138-
case CommentKind.JavaDoc:
139-
case CommentKind.Qt:
140-
return " *";
141-
case CommentKind.BCPLSlash:
142-
return "///";
143-
default:
144-
throw new ArgumentOutOfRangeException();
145-
}
135+
CommentKind.BCPL or CommentKind.BCPLExcl => "//",
136+
CommentKind.C or CommentKind.JavaDoc or CommentKind.Qt => " *",
137+
CommentKind.BCPLSlash => "///",
138+
_ => throw new ArgumentOutOfRangeException()
139+
};
146140
}
147141

148142
public static string GetLineCommentPrologue(CommentKind kind)
@@ -375,7 +369,7 @@ public ParagraphComment()
375369

376370
public override void Visit<T>(ICommentVisitor<T> visitor)
377371
{
378-
visitor.VisitParagraphCommand(this);
372+
visitor.VisitParagraph(this);
379373
}
380374
}
381375

@@ -416,10 +410,17 @@ public struct Attribute
416410
{
417411
public string Name;
418412
public string Value;
413+
414+
public override string ToString()
415+
{
416+
return $"{Name}=\"{Value}\"";
417+
}
419418
}
420419

421420
public List<Attribute> Attributes;
422421

422+
public bool SelfClosing { get; set; }
423+
423424
public HTMLStartTagComment()
424425
{
425426
Kind = DocumentationCommentKind.HTMLStartTagComment;
@@ -430,6 +431,15 @@ public override void Visit<T>(ICommentVisitor<T> visitor)
430431
{
431432
visitor.VisitHTMLStartTag(this);
432433
}
434+
435+
public override string ToString()
436+
{
437+
var attrStr = string.Empty;
438+
if (Attributes.Count != 0)
439+
attrStr = " " + string.Join(' ', Attributes.Select(x => x.ToString()));
440+
441+
return $"<{TagName}{attrStr}{(SelfClosing ? "/" : "")}>";
442+
}
433443
}
434444

435445
/// <summary>
@@ -446,6 +456,11 @@ public override void Visit<T>(ICommentVisitor<T> visitor)
446456
{
447457
visitor.VisitHTMLEndTag(this);
448458
}
459+
460+
public override string ToString()
461+
{
462+
return $"</{TagName}>";
463+
}
449464
}
450465

451466
/// <summary>
@@ -464,6 +479,13 @@ public override void Visit<T>(ICommentVisitor<T> visitor)
464479
{
465480
visitor.VisitText(this);
466481
}
482+
483+
public override string ToString()
484+
{
485+
return Text;
486+
}
487+
488+
public bool IsEmpty => string.IsNullOrEmpty(Text) && !HasTrailingNewline;
467489
}
468490

469491
/// <summary>

src/CppParser/Comments.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ void Parser::HandleComments(const clang::Decl* D, Declaration* Decl)
263263
{
264264
using namespace clang;
265265

266-
const clang::RawComment* RC = 0;
267-
if (!(RC = c->getASTContext().getRawCommentForAnyRedecl(D)))
266+
const clang::RawComment* RC = c->getASTContext().getRawCommentForAnyRedecl(D);
267+
if (!RC)
268268
return;
269269

270270
auto RawComment = WalkRawComment(RC);

src/Generator.Tests/Passes/TestPasses.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,38 @@ public void TestFunctionToStaticPass()
123123
[Test]
124124
public void TestCleanCommentsPass()
125125
{
126-
var c = AstContext.FindClass("TestCommentsPass").FirstOrDefault();
126+
var c = AstContext.Class("TestCommentsPass");
127+
var c2 = AstContext.Class("TestCommentsPass2");
127128

128129
passBuilder.AddPass(new CleanCommentsPass());
129130
passBuilder.RunPasses(pass => pass.VisitDeclaration(c));
131+
passBuilder.RunPasses(pass => pass.VisitClassDecl(c2));
130132

131133
var para = (ParagraphComment)c.Comment.FullComment.Blocks[0];
132134
var textGenerator = new TextGenerator();
133135
textGenerator.Print(para, CommentKind.BCPLSlash);
134136

135137
Assert.That(textGenerator.StringBuilder.ToString().Trim(),
136138
Is.EqualTo("/// <summary>A simple test.</summary>"));
139+
140+
var textGenerator2 = new TextGenerator();
141+
textGenerator2.Print(c2.Methods[0].Comment.FullComment, CommentKind.BCPLSlash);
142+
143+
Assert.That(textGenerator2.StringBuilder.ToString().Trim().Replace("\r\n", "\n"),
144+
Is.EqualTo(
145+
"/// <summary>Gets a value</summary>\n" +
146+
"/// <returns>One</returns>"
147+
));
148+
149+
var textGenerator3 = new TextGenerator();
150+
textGenerator3.Print(c2.Methods[1].Comment.FullComment, CommentKind.BCPLSlash);
151+
152+
Assert.That(textGenerator3.StringBuilder.ToString().Trim().Replace("\r\n", "\n"),
153+
Is.EqualTo(
154+
"/// <summary>Sets a value. Get it with <see cref=\"GetValueWithComment\"/></summary>\n" +
155+
"/// <param name=\"value\">The value to set</param>\n" +
156+
"/// <returns>The parameter (typeof<float>)</returns>"
157+
));
137158
}
138159

139160
[Test]

0 commit comments

Comments
 (0)