Skip to content

Commit ad82ec9

Browse files
committed
tools/docgen: document comment_and_requirement
Also rename some bindings for clarity. The input node is the previous sibling node for a to-be-documented item so call it `prev`. We also look at `prev`'s previous sibling node, so call those instances `prev_prev`.
1 parent 4adbb82 commit ad82ec9

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

tools/src/bin/docgen/main.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,49 @@ fn process_doc_item(item: Node, src: &[u8]) -> Result<Option<Item>, Box<dyn Erro
183183
}))
184184
}
185185

186+
/// Convert the preceding sibling of a to-be-processed item into associated metadata
187+
///
188+
/// An item `Node` to be processed for documentation will typically have associated
189+
/// metadata `Node`s preceding it in the parse tree. This function returns an optional
190+
/// `Comment` and/or optional `Feature` from processing the sibling `prev` `Node`.
191+
///
192+
/// The potential cases we care about are:
193+
/// * `prev` is not a comment, and not a feature requirement.
194+
/// * `prev` is a Comment, and has no feature requirement before it.
195+
/// * `prev` is a Comment, and has a feature requirement before it.
196+
/// * `prev` is a bare feature requirement
197+
///
198+
/// cbindgen won't create a comment before a feature requirement so we don't have to
199+
/// consider that case.
186200
fn comment_and_requirement(
187-
node: Node,
201+
prev: Node,
188202
src: &[u8],
189203
) -> Result<(Option<Comment>, Option<Feature>), Box<dyn Error>> {
190-
let mut maybe_comment = Comment::new(node, src).ok();
204+
let mut maybe_comment = Comment::new(prev, src).ok();
191205

192206
// If node wasn't a comment, see if it was an expression_statement
193207
// that itself was preceded by a comment. This skips over
194208
// expression-like preprocessor attributes on function decls.
195-
if let (None, "expression_statement", Some(prev)) =
196-
(&maybe_comment, node.kind(), node.prev_sibling())
209+
if let (None, "expression_statement", Some(prev_prev)) =
210+
(&maybe_comment, prev.kind(), prev.prev_sibling())
197211
{
198-
maybe_comment = Comment::new(prev, src).ok();
212+
maybe_comment = Comment::new(prev_prev, src).ok();
199213
}
200214

201215
// If prev wasn't a comment, see if it was a feature requirement.
202216
if maybe_comment.is_none() {
203-
return Ok(match Feature::new(node, src).ok() {
217+
return Ok(match Feature::new(prev, src).ok() {
204218
Some(feat_req) => (None, Some(feat_req)),
205219
None => (None, None),
206220
});
207221
}
208222

209223
// Otherwise, check the prev of the comment for a feature requirement
210-
let Some(prev) = node.prev_named_sibling() else {
224+
let Some(prev_prev) = prev.prev_named_sibling() else {
211225
return Ok((maybe_comment, None));
212226
};
213227

214-
Ok((maybe_comment, Feature::new(prev, src).ok()))
228+
Ok((maybe_comment, Feature::new(prev_prev, src).ok()))
215229
}
216230

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

0 commit comments

Comments
 (0)