Skip to content

Commit 581f098

Browse files
madsmtmsimlay
andcommitted
Support doc comments in header-translator
Auto-generate documentation comments in translated code. We use `clang_Cursor_getParsedComment` to parse the comment to avoid the Sphinx-like syntax (and yeah, that function seems to have a fair number of bugs, but that can be improved upstream). Part of #309. Originally authored in #435. Co-authored-by: Sebastian Imlay <sebastian.imlay@gmail.com>
1 parent 839be2e commit 581f098

File tree

12 files changed

+575
-55
lines changed

12 files changed

+575
-55
lines changed

crates/header-translator/src/documentation.rs

Lines changed: 468 additions & 0 deletions
Large diffs are not rendered by default.

crates/header-translator/src/global_analysis.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,13 @@ fn update_module(module: &mut Module) {
6060
let mut iter = mem::take(&mut module.stmts).into_iter().peekable();
6161
while let Some(stmt) = iter.next() {
6262
if let Stmt::AliasDecl {
63-
id,
64-
availability: _,
65-
ty,
66-
kind: None,
63+
id, ty, kind: None, ..
6764
} = &stmt
6865
{
6966
if let Some(Stmt::EnumDecl {
7067
id: enum_id,
71-
availability: _,
7268
ty: enum_ty,
73-
kind: _,
74-
variants: _,
75-
sendable: _,
69+
..
7670
}) = iter.peek_mut()
7771
{
7872
if enum_ty.is_typedef_to(&id.name) {

crates/header-translator/src/id.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -497,22 +497,6 @@ impl ItemIdentifier {
497497

498498
ItemIdentifierPathInRelationTo(self, other)
499499
}
500-
501-
/// Generate a markdown link to Apple's documentation.
502-
///
503-
/// This is best effort only, and doesn't work for functions and methods,
504-
/// and possibly some renamed classes and traits. Additionally, the link
505-
/// may redirect.
506-
pub(crate) fn doc_link(&self) -> impl fmt::Display + '_ {
507-
FormatterFn(|f| {
508-
write!(
509-
f,
510-
"[Apple's documentation](https://developer.apple.com/documentation/{}/{}?language=objc)",
511-
self.library_name().to_lowercase(),
512-
self.name.to_lowercase()
513-
)
514-
})
515-
}
516500
}
517501

518502
impl ItemIdentifier<Option<String>> {

crates/header-translator/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod cfgs;
1515
mod config;
1616
mod context;
1717
mod display_helper;
18+
pub mod documentation;
1819
mod expr;
1920
mod fn_utils;
2021
mod global_analysis;

crates/header-translator/src/library.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@ see that for related crates.", self.data.krate)?;
457457
// We don't have the manpower to document the safety of methods.
458458
writeln!(f, "#![allow(clippy::missing_safety_doc)]")?;
459459

460+
// Our auto-generated rustdoc is kinda bad.
461+
writeln!(f, "#![allow(clippy::doc_lazy_continuation)]")?;
462+
writeln!(f, "#![allow(rustdoc::broken_intra_doc_links)]")?;
463+
writeln!(f, "#![allow(rustdoc::bare_urls)]")?;
464+
writeln!(f, "#![allow(rustdoc::unportable_markdown)]")?;
465+
writeln!(f, "#![allow(rustdoc::invalid_html_tags)]")?;
466+
460467
writeln!(f)?;
461468

462469
if self.data.link {

crates/header-translator/src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,14 @@ fn get_translation_unit<'i: 'c, 'c>(
416416
"-fobjc-exceptions",
417417
"-fobjc-abi-version=2", // 3??
418418
"-fblocks",
419+
// We're parsing system headers, but still want comments from there.
420+
//
421+
// See: https://clang.llvm.org/docs/UsersManual.html#comment-parsing-options
422+
"-fretain-comments-from-system-headers",
423+
// Tell Clang to parse non-doc comments too.
419424
// "-fparse-all-comments",
420-
// TODO: "-fretain-comments-from-system-headers"
425+
// Explicitly pass the sysroot (we aren't invoked through
426+
// `/usr/bin/clang` which is what usually passes it).
421427
"-isysroot",
422428
sdk.path.to_str().unwrap(),
423429
// See ClangImporter.cpp and Foundation/NSObjCRuntime.h

crates/header-translator/src/method.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::availability::Availability;
66
use crate::config::MethodData;
77
use crate::context::Context;
88
use crate::display_helper::FormatterFn;
9+
use crate::documentation::Documentation;
910
use crate::id::ItemIdentifier;
1011
use crate::immediate_children;
1112
use crate::objc2_utils::in_selector_family;
@@ -269,6 +270,7 @@ pub struct Method {
269270
weak_property: bool,
270271
must_use: bool,
271272
encoding: String,
273+
documentation: Documentation,
272274
}
273275

274276
#[derive(Debug)]
@@ -523,6 +525,7 @@ impl Method {
523525
weak_property: false,
524526
must_use: modifiers.must_use,
525527
encoding,
528+
documentation: Documentation::from_entity(&entity),
526529
},
527530
))
528531
}
@@ -587,7 +590,7 @@ impl Method {
587590

588591
Some(Method {
589592
selector: getter_sel.clone(),
590-
fn_name: getter_sel,
593+
fn_name: getter_sel.clone(),
591594
availability: availability.clone(),
592595
is_class,
593596
is_optional: entity.is_objc_optional(),
@@ -603,6 +606,7 @@ impl Method {
603606
weak_property: false,
604607
must_use: modifiers.must_use,
605608
encoding: encoding.clone(),
609+
documentation: Documentation::from_entity(&entity),
606610
})
607611
} else {
608612
None
@@ -648,6 +652,7 @@ impl Method {
648652
weak_property: attributes.map(|a| a.weak).unwrap_or(false),
649653
must_use: modifiers.must_use,
650654
encoding,
655+
documentation: Documentation::property_setter(&getter_sel),
651656
})
652657
} else {
653658
None
@@ -741,6 +746,7 @@ impl fmt::Display for Method {
741746
// Attributes
742747
//
743748

749+
write!(f, "{}", self.documentation.fmt(None))?;
744750
write!(f, "{}", self.availability)?;
745751

746752
if self.must_use {

0 commit comments

Comments
 (0)