Skip to content

Commit f14ba77

Browse files
committed
tools/docgen: simplify comment_and_requirement
Simplify the logic by handling the case where `prev` is a comment that may/may not have a feature req previous to the comment up-front. Then, when we're looking at a `prev` that wasn't a comment, but was `kind == expression_statement` we can recurse with `prev_prev` to handle it as a comment that may/may not have a feature req previous to itself. Lastly, if we know we don't have a comment and we don't have an expression statement, try to see if we have a bare feature requirement. We know cbindgen won't make a comment ahead of these and so can disregard `prev_prev` for this case.
1 parent 3fad732 commit f14ba77

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

tools/src/bin/docgen/main.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,31 +202,33 @@ fn comment_and_requirement(
202202
src: &[u8],
203203
) -> Result<(Option<Comment>, Option<Feature>), Box<dyn Error>> {
204204
let prev_prev = prev.prev_named_sibling();
205-
let mut maybe_comment = Comment::new(prev, src).ok();
206205

207-
// If node wasn't a comment, see if it was an expression_statement
208-
// that itself was preceded by a comment. This skips over
209-
// expression-like preprocessor attributes on function decls.
210-
if let (None, "expression_statement", Some(prev_prev)) =
211-
(&maybe_comment, prev.kind(), prev_prev)
212-
{
213-
maybe_comment = Comment::new(prev_prev, src).ok();
206+
// In the simple case, `prev` is a comment and `prev_prev` may
207+
// be a feature requirement.
208+
if let Ok(comment) = Comment::new(prev, src) {
209+
let feature = match prev_prev {
210+
Some(prev_prev) => Feature::new(prev_prev, src).ok(),
211+
None => None,
212+
};
213+
return Ok((Some(comment), feature));
214214
}
215215

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

224-
// Otherwise, check the prev of the comment for a feature requirement
225-
let Some(prev_prev) = prev_prev else {
226-
return Ok((maybe_comment, None));
227-
};
228-
229-
Ok((maybe_comment, Feature::new(prev_prev, src).ok()))
226+
// If `prev` wasn't a comment, or an expression_statement preceded by a comment,
227+
// then it's either a bare feature requirement or we have no metadata to return.
228+
Ok(match Feature::new(prev, src).ok() {
229+
Some(feat_req) => (None, Some(feat_req)),
230+
None => (None, None),
231+
})
230232
}
231233

232234
#[derive(Debug, Default, Serialize)]

0 commit comments

Comments
 (0)