From c63d1faefb2dbe4406ca01ac445a2b28d1aca0b5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 12 Nov 2020 15:39:14 +0100 Subject: [PATCH] Generate doc cfg attributes for more beautiful docs --- src/codegen/general.rs | 14 +++++++++++--- src/codegen/sys/functions.rs | 5 +++++ src/codegen/sys/statics.rs | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/codegen/general.rs b/src/codegen/general.rs index 7201b72c1..63de4a0e0 100644 --- a/src/codegen/general.rs +++ b/src/codegen/general.rs @@ -51,6 +51,11 @@ pub fn uses(w: &mut dyn Write, env: &Env, imports: &Imports) -> Result<()> { "#[cfg(any({},feature = \"dox\"))]", scope.constraints.join(", ") )?; + writeln!( + w, + "#[cfg_attr(feature = \"dox\", doc(cfg({})))]", + scope.constraints.join(", ") + )?; } version_condition(w, env, scope.version, false, 0)?; @@ -517,7 +522,8 @@ pub fn version_condition_string( Some(v) if v > env.config.min_cfg_version => { let comment = if commented { "//" } else { "" }; Some(format!( - "{}{}#[cfg(any({}, feature = \"dox\"))]", + "{0}{1}#[cfg(any({2}, feature = \"dox\"))]\n\ + {0}{1}#[cfg_attr(feature = \"dox\", doc(cfg({2})))]", tabs(indent), comment, v.to_cfg() @@ -536,7 +542,8 @@ pub fn not_version_condition( if let Some(v) = version { let comment = if commented { "//" } else { "" }; let s = format!( - "{}{}#[cfg(any(not({}), feature = \"dox\"))]", + "{0}{1}#[cfg(any(not({2}), feature = \"dox\"))]\n\ + {0}{1}#[cfg_attr(feature = \"dox\", doc(cfg(not({2}))))]", tabs(indent), comment, v.to_cfg() @@ -587,7 +594,8 @@ pub fn cfg_condition_string( Some(v) => { let comment = if commented { "//" } else { "" }; Some(format!( - "{}{}#[cfg(any({}, feature = \"dox\"))]", + "{0}{1}#[cfg(any({2}, feature = \"dox\"))]\n\ + {0}{1}#[cfg_attr(feature = \"dox\", doc(cfg({2})))]", tabs(indent), comment, v diff --git a/src/codegen/sys/functions.rs b/src/codegen/sys/functions.rs index 6dce6a259..53d4d1b97 100644 --- a/src/codegen/sys/functions.rs +++ b/src/codegen/sys/functions.rs @@ -254,6 +254,11 @@ fn generate_object_funcs( // since we work with gir-files from Linux, some function names need to be adjusted if is_windows_utf8 { writeln!(w, " {}#[cfg(any(windows, feature = \"dox\"))]", comment)?; + writeln!( + w, + " {}#[cfg_attr(feature = \"dox\", doc(cfg(windows)))]", + comment + )?; writeln!(w, " {}pub fn {}_utf8{};", comment, name, sig)?; version_condition(w, env, version, commented, 1)?; } diff --git a/src/codegen/sys/statics.rs b/src/codegen/sys/statics.rs index e3a59bb5b..fa9ab9488 100644 --- a/src/codegen/sys/statics.rs +++ b/src/codegen/sys/statics.rs @@ -6,6 +6,7 @@ pub fn begin(w: &mut dyn Write) -> Result<()> { "", "#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]", "#![allow(clippy::approx_constant, clippy::type_complexity, clippy::unreadable_literal)]", + "#![cfg_attr(feature = \"dox\", feature(doc_cfg))]", "", "extern crate libc;", ];