随着一些更多的挖掘,我发现了这个:
对于任何相关的已访问 Decl ( VisitXXXDecl
),我可以这样做:
virtual bool VisitDecl(Decl* d)
{
ASTContext& ctx = d->getASTContext();
SourceManager& sm = ctx.getSourceManager();
const RawComment* rc = d->getASTContext().getRawCommentForDeclNoCache(d);
if (rc)
{
//Found comment!
SourceRange range = rc->getSourceRange();
PresumedLoc startPos = sm.getPresumedLoc(range.getBegin());
PresumedLoc endPos = sm.getPresumedLoc(range.getEnd());
std::string raw = rc->getRawText(sm);
std::string brief = rc->getBriefText(ctx);
// ... Do something with positions or comments
}
// ...
}
请注意,这标识了(据我所见......)代码中当前声明的上方(和相邻!)行中的注释,并且采用以下格式之一:
/// Comment
/** Comment */
//! Comment
例如,在以下情况下:
/// A field with a long long comment
/// A two-liner
long long LongLongData;
raw
将会:
/// A field with a long long comment
/// A two-liner
并且brief
将是:
A field with a long long comment A two-liner
无论哪种方式,它都足以满足我的需求。