2020#include < clang/AST/CommentCommandTraits.h>
2121#include < clang/AST/ASTContext.h>
2222#include < clang/AST/RawCommentList.h>
23+ #include < clang/Lex/Lexer.h>
24+ #include < clang/Basic/SourceLocation.h>
2325#ifdef _MSC_VER
2426#pragma warning(push)
2527#pragma warning(disable: 5054) // C5054: operator '+': deprecated between enumerations of different types
3335#include < llvm/Support/JSON.h>
3436#include < ranges>
3537
38+ #ifdef NDEBUG
39+ #define MRDOCS_COMMENT_TRACE (D, C )
40+ #else
41+
42+ # define MRDOCS_COMMENT_TRACE_MERGE_ (a, b ) a##b
43+ # define MRDOCS_COMMENT_TRACE_LABEL_ (a ) MRDOCS_COMMENT_TRACE_MERGE_(comment_content_, a)
44+ # define MRDOCS_COMMENT_TRACE_UNIQUE_NAME MRDOCS_COMMENT_TRACE_LABEL_ (__LINE__)
45+
46+ namespace detail {
47+ template <class T >
48+ void
49+ dumpCommentContent (T const * C, clang::ASTContext const & Ctx, llvm::SmallString<1024 >& contents)
50+ {
51+ if (!C)
52+ {
53+ return ;
54+ }
55+ llvm::raw_svector_ostream os (contents);
56+ if constexpr (std::derived_from<T, clang::comments::Comment>)
57+ {
58+ auto const * CC = static_cast <clang::comments::Comment const *>(C);
59+ clang::SourceRange const R = CC->getSourceRange ();
60+ clang::SourceManager const & SM = Ctx.getSourceManager ();
61+ contents = clang::Lexer::getSourceText (
62+ clang::CharSourceRange::getTokenRange (R),
63+ SM,
64+ Ctx.getLangOpts ());
65+ }
66+ }
67+
68+ template <class T >
69+ requires (!std::is_pointer_v<T>)
70+ void
71+ dumpCommentContent (T const & C, clang::ASTContext const & Ctx, llvm::SmallString<1024 >& contents)
72+ {
73+ dumpCommentContent (&C, Ctx, contents);
74+ }
75+ } // namespace detail
76+
77+ #define MRDOCS_COMMENT_TRACE (D, C ) \
78+ SmallString<1024 > MRDOCS_COMMENT_TRACE_UNIQUE_NAME; \
79+ ::detail::dumpCommentContent (D, C, MRDOCS_COMMENT_TRACE_UNIQUE_NAME); \
80+ report::debug (" {}" , std::string_view(MRDOCS_COMMENT_TRACE_UNIQUE_NAME.str()))
81+ #endif
82+
83+
3684/* AST Types
3785
3886 Comment
@@ -509,10 +557,12 @@ JavadocVisitor::
509557visitChildren (
510558 Comment const * C)
511559{
560+ MRDOCS_COMMENT_TRACE (C, ctx_);
512561 ScopeExitRestore s1 (it_, C->child_begin ());
513562 ScopeExitRestore s2 (end_, C->child_end ());
514563 while (it_ != end_)
515564 {
565+ MRDOCS_COMMENT_TRACE (*it_, ctx_);
516566 visit (*it_);
517567 ++it_; // must happen after
518568 }
@@ -597,6 +647,7 @@ Javadoc
597647JavadocVisitor::
598648build ()
599649{
650+ MRDOCS_COMMENT_TRACE (FC_, ctx_);
600651 visit (FC_);
601652 return std::move (jd_);
602653}
@@ -606,6 +657,7 @@ JavadocVisitor::
606657visitComment (
607658 Comment const * C)
608659{
660+ MRDOCS_COMMENT_TRACE (C, ctx_);
609661 visitChildren (C);
610662}
611663
@@ -620,6 +672,7 @@ JavadocVisitor::
620672visitTextComment (
621673 TextComment const * C)
622674{
675+ MRDOCS_COMMENT_TRACE (C, ctx_);
623676 llvm::StringRef s = C->getText ();
624677 // If this is the first text comment in the
625678 // paragraph then remove all the leading space.
@@ -642,6 +695,7 @@ Expected<JavadocVisitor::TagComponents>
642695JavadocVisitor::
643696parseHTMLTag (HTMLStartTagComment const * C)
644697{
698+ MRDOCS_COMMENT_TRACE (C, ctx_);
645699 TagComponents res;
646700 res.tag = C->getTagName ().str ();
647701
@@ -700,6 +754,7 @@ JavadocVisitor::
700754visitHTMLStartTagComment (
701755 HTMLStartTagComment const * C)
702756{
757+ MRDOCS_COMMENT_TRACE (C, ctx_);
703758 MRDOCS_ASSERT (C->child_begin () == C->child_end ());
704759 PresumedLoc const loc = sm_.getPresumedLoc (C->getBeginLoc ());
705760 auto filename = files::makePosixStyle (loc.getFilename ());
@@ -766,6 +821,7 @@ JavadocVisitor::
766821visitHTMLEndTagComment (
767822 HTMLEndTagComment const * C)
768823{
824+ MRDOCS_COMMENT_TRACE (C, ctx_);
769825 MRDOCS_ASSERT (C->child_begin () == C->child_end ());
770826 --htmlTagNesting_;
771827}
@@ -923,6 +979,7 @@ JavadocVisitor::
923979visitInlineCommandComment (
924980 InlineCommandComment const * C)
925981{
982+ MRDOCS_COMMENT_TRACE (C, ctx_);
926983 auto const * cmd = ctx_
927984 .getCommentCommandTraits ()
928985 .getCommandInfo (C->getCommandID ());
@@ -1074,6 +1131,7 @@ JavadocVisitor::
10741131visitParagraphComment (
10751132 ParagraphComment const * C)
10761133{
1134+ MRDOCS_COMMENT_TRACE (C, ctx_);
10771135 if (block_)
10781136 return visitChildren (C);
10791137 doc::Paragraph paragraph;
@@ -1089,6 +1147,7 @@ JavadocVisitor::
10891147visitBlockCommandComment (
10901148 BlockCommandComment const * C)
10911149{
1150+ MRDOCS_COMMENT_TRACE (C, ctx_);
10921151 auto const * cmd = ctx_
10931152 .getCommentCommandTraits ()
10941153 .getCommandInfo (C->getCommandID ());
@@ -1451,6 +1510,7 @@ JavadocVisitor::
14511510visitParamCommandComment (
14521511 ParamCommandComment const * C)
14531512{
1513+ MRDOCS_COMMENT_TRACE (C, ctx_);
14541514 doc::Param param;
14551515 if (C->hasParamName ())
14561516 {
@@ -1494,6 +1554,7 @@ JavadocVisitor::
14941554visitTParamCommandComment (
14951555 TParamCommandComment const * C)
14961556{
1557+ MRDOCS_COMMENT_TRACE (C, ctx_);
14971558 doc::TParam tparam;
14981559 if (C->hasParamName ())
14991560 {
@@ -1534,6 +1595,7 @@ JavadocVisitor::
15341595visitVerbatimBlockComment (
15351596 VerbatimBlockComment const * C)
15361597{
1598+ MRDOCS_COMMENT_TRACE (C, ctx_);
15371599 doc::Code code;
15381600 auto scope = enterScope (code);
15391601 // if(C->hasNonWhitespaceParagraph())
@@ -1546,6 +1608,7 @@ JavadocVisitor::
15461608visitVerbatimLineComment (
15471609 VerbatimLineComment const * C)
15481610{
1611+ MRDOCS_COMMENT_TRACE (C, ctx_);
15491612 // VFALCO This doesn't seem to be used
15501613 // in any of my codebases, follow up
15511614}
@@ -1555,6 +1618,7 @@ JavadocVisitor::
15551618visitVerbatimBlockLineComment (
15561619 VerbatimBlockLineComment const * C)
15571620{
1621+ MRDOCS_COMMENT_TRACE (C, ctx_);
15581622 emplaceText<doc::Text>(true , C->getText ().str ());
15591623}
15601624
@@ -1565,6 +1629,7 @@ JavadocVisitor::
15651629goodArgCount (std::size_t n,
15661630 InlineCommandComment const & C)
15671631{
1632+ MRDOCS_COMMENT_TRACE (C, ctx_);
15681633 if (C.getNumArgs () != n)
15691634 {
15701635 auto loc = sm_.getPresumedLoc (C.getBeginLoc ());
@@ -1611,7 +1676,9 @@ parseJavadoc(
16111676 Config const & config,
16121677 Diagnostics& diags)
16131678{
1614- auto result = JavadocVisitor (FC, D, config, diags).build ();
1679+ MRDOCS_COMMENT_TRACE (FC, D->getASTContext ());
1680+ JavadocVisitor visitor (FC, D, config, diags);
1681+ auto result = visitor.build ();
16151682 if (jd == nullptr )
16161683 {
16171684 // Do not create javadocs which have no nodes
0 commit comments