Skip to content
42 changes: 22 additions & 20 deletions tools/src/bin/docgen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,31 +202,33 @@ fn comment_and_requirement(
src: &[u8],
) -> Result<(Option<Comment>, Option<Feature>), Box<dyn Error>> {
let prev_prev = prev.prev_named_sibling();
let mut maybe_comment = Comment::new(prev, src).ok();

// If node wasn't a comment, see if it was an expression_statement
// that itself was preceded by a comment. This skips over
// expression-like preprocessor attributes on function decls.
if let (None, "expression_statement", Some(prev_prev)) =
(&maybe_comment, prev.kind(), prev_prev)
{
maybe_comment = Comment::new(prev_prev, src).ok();
// In the simple case, `prev` is a comment and `prev_prev` may
// be a feature requirement.
if let Ok(comment) = Comment::new(prev, src) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is still a little confusing to me, it seems to have a bunch of things going on that interact, making it hard to understand what the logical changes are. Suggest at least splitting out a commit that only does the maybe_comment.is_none() inversion thing?

let feature = match prev_prev {
Some(prev_prev) => Feature::new(prev_prev, src).ok(),
None => None,
};
return Ok((Some(comment), feature));
}

// If prev wasn't a comment, see if it was a feature requirement.
if maybe_comment.is_none() {
return Ok(match Feature::new(prev, src).ok() {
Some(feat_req) => (None, Some(feat_req)),
None => (None, None),
});
// If `prev` wasn't a comment, see if it was an expression_statement
// that itself was preceded by a comment. This skips over
// expression-like preprocessor attributes on function decls.
if prev.kind() == "expression_statement" {
return match prev_prev {
Some(prev_prev) => comment_and_requirement(prev_prev, src),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduction of recursion seems kind of sudden, maybe deserves its own commit?

None => Ok((None, None)),
};
}

// Otherwise, check the prev of the comment for a feature requirement
let Some(prev_prev) = prev_prev else {
return Ok((maybe_comment, None));
};

Ok((maybe_comment, Feature::new(prev_prev, src).ok()))
// If `prev` wasn't a comment, or an expression_statement preceded by a comment,
// then it's either a bare feature requirement or we have no metadata to return.
Ok(match Feature::new(prev, src).ok() {
Some(feat_req) => (None, Some(feat_req)),
None => (None, None),
})
}

#[derive(Debug, Default, Serialize)]
Expand Down