Skip to content

Commit

Permalink
rust: prepare doctests
Browse files Browse the repository at this point in the history
In preparation for enabling them later.

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
  • Loading branch information
ojeda committed Jun 7, 2021
1 parent 6b94964 commit ca3bf88
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 20 deletions.
3 changes: 3 additions & 0 deletions rust/bindgen_parameters
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
# If SMP is disabled, `arch_spinlock_t` is defined as a ZST which triggers a Rust
# warning. We don't need to peek into it anyway.
--opaque-type spinlock

# `seccomp`'s comment gets understood as a doctest
--no-doc-comments
7 changes: 5 additions & 2 deletions rust/kernel/build_assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
/// be called, a build error will be triggered.
///
/// # Examples
/// ```no_run
/// ```
/// # use kernel::build_error;
/// #[inline]
/// fn foo(a: usize) -> usize {
/// a.checked_add(1).unwrap_or_else(|| build_error!("overflow"))
Expand Down Expand Up @@ -38,7 +39,8 @@ macro_rules! build_error {
/// These examples show that different types of [`assert!`] will trigger errors
/// at different stage of compilation. It is preferred to err as early as
/// possible, so [`static_assert!`] should be used whenever possible.
/// ```no_run
/// ```compile_fail
/// # use kernel::prelude::*;
/// fn foo() {
/// static_assert!(1 > 1); // Compile-time error
/// build_assert!(1 > 1); // Build-time error
Expand All @@ -49,6 +51,7 @@ macro_rules! build_error {
/// When the condition refers to generic parameters or parameters of an inline function,
/// [`static_assert!`] cannot be used. Use `build_assert!` in this scenario.
/// ```no_run
/// # use kernel::prelude::*;
/// fn foo<const N: usize>() {
/// // `static_assert!(N > 1);` is not allowed
/// build_assert!(N > 1); // Build-time check
Expand Down
11 changes: 9 additions & 2 deletions rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ where
///
/// # Examples
///
/// ```rust,no_run
/// ```ignore
/// # use kernel::from_kernel_result;
/// # use kernel::c_types;
/// # use kernel::bindings;
/// unsafe extern "C" fn probe_callback(
/// pdev: *mut bindings::platform_device,
/// ) -> c_types::c_int {
Expand Down Expand Up @@ -219,7 +222,11 @@ macro_rules! from_kernel_result {
///
/// # Examples
///
/// ```rust,no_run
/// ```ignore
/// # use kernel::prelude::*;
/// # use kernel::from_kernel_err_ptr;
/// # use kernel::c_types;
/// # use kernel::bindings;
/// fn devm_platform_ioremap_resource(
/// pdev: &mut PlatformDevice,
/// index: u32,
Expand Down
4 changes: 4 additions & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ impl<'a> Drop for KParamGuard<'a> {
/// # Example
///
/// ```
/// # use kernel::prelude::*;
/// # use kernel::offset_of;
/// struct Test {
/// a: u64,
/// b: u32,
Expand Down Expand Up @@ -193,6 +195,8 @@ macro_rules! offset_of {
/// # Example
///
/// ```
/// # use kernel::prelude::*;
/// # use kernel::container_of;
/// struct Test {
/// a: u64,
/// b: u32,
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/module_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ macro_rules! impl_module_param {
/// Generate a static [`kernel_param_ops`](../../../include/linux/moduleparam.h) struct.
///
/// # Example
/// ```rust
/// ```ignore
/// make_param_ops!(
/// /// Documentation for new param ops.
/// PARAM_OPS_MYTYPE, // Name for the static.
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//!
//! # Examples
//!
//! ```rust,no_run
//! ```
//! use kernel::prelude::*;
//! ```
Expand Down
19 changes: 19 additions & 0 deletions rust/kernel/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub fn call_printk_cont(args: fmt::Arguments<'_>) {
///
/// Public but hidden since it should only be used from public macros.
#[doc(hidden)]
#[cfg(not(testlib))]
#[macro_export]
macro_rules! print_macro (
// The non-continuation cases (most of them, e.g. `INFO`).
Expand All @@ -189,6 +190,15 @@ macro_rules! print_macro (
);
);

// Stub for doctests
#[cfg(testlib)]
#[macro_export]
macro_rules! print_macro (
($format_string:path, $e:expr, $($arg:tt)+) => (
()
);
);

// We could use a macro to generate these macros. However, doing so ends
// up being a bit ugly: it requires the dollar token trick to escape `$` as
// well as playing with the `doc` attribute. Furthermore, they cannot be easily
Expand All @@ -213,6 +223,7 @@ macro_rules! print_macro (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// pr_emerg!("hello {}\n", "there");
/// ```
#[macro_export]
Expand All @@ -237,6 +248,7 @@ macro_rules! pr_emerg (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// pr_alert!("hello {}\n", "there");
/// ```
#[macro_export]
Expand All @@ -261,6 +273,7 @@ macro_rules! pr_alert (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// pr_crit!("hello {}\n", "there");
/// ```
#[macro_export]
Expand All @@ -285,6 +298,7 @@ macro_rules! pr_crit (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// pr_err!("hello {}\n", "there");
/// ```
#[macro_export]
Expand All @@ -309,6 +323,7 @@ macro_rules! pr_err (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// pr_warn!("hello {}\n", "there");
/// ```
#[macro_export]
Expand All @@ -333,6 +348,7 @@ macro_rules! pr_warn (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// pr_notice!("hello {}\n", "there");
/// ```
#[macro_export]
Expand All @@ -357,6 +373,7 @@ macro_rules! pr_notice (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// pr_info!("hello {}\n", "there");
/// ```
#[macro_export]
Expand All @@ -382,6 +399,8 @@ macro_rules! pr_info (
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// # use kernel::pr_cont;
/// pr_info!("hello");
/// pr_cont!(" {}\n", "there");
/// ```
Expand Down
1 change: 1 addition & 0 deletions rust/kernel/static_assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/// # Examples
///
/// ```
/// # use kernel::prelude::*;
/// static_assert!(42 > 24);
/// static_assert!(core::mem::size_of::<u8>() == 1);
///
Expand Down
8 changes: 6 additions & 2 deletions rust/kernel/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub type BStr = [u8];
///
/// # Examples
///
/// ```rust,no_run
/// ```
/// # use kernel::b_str;
/// # use kernel::str::BStr;
/// const MY_BSTR: &'static BStr = b_str!("My awesome BStr!");
/// ```
#[macro_export]
Expand Down Expand Up @@ -242,7 +244,9 @@ where
///
/// # Examples
///
/// ```rust,no_run
/// ```
/// # use kernel::c_str;
/// # use kernel::str::CStr;
/// const MY_CSTR: &'static CStr = c_str!("My awesome CStr!");
/// ```
#[macro_export]
Expand Down
4 changes: 2 additions & 2 deletions rust/kernel/sync/locked_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use core::{cell::UnsafeCell, ops::Deref, ptr};
/// locked; we enforce at run time that the right `InnerDirectory` is locked.
///
/// ```
/// use super::Mutex;
/// use alloc::{string::String, vec::Vec};
/// # use kernel::prelude::*;
/// use kernel::sync::{LockedBy, Mutex};
///
/// struct InnerFile {
/// bytes_used: u64,
Expand Down
17 changes: 9 additions & 8 deletions rust/kernel/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
//!
//! # Example
//!
//! ```
//! fn test() {
//! // SAFETY: `init` is called below.
//! let data = alloc::sync::Arc::pin(unsafe { Mutex::new(0) });
//! mutex_init!(data.as_ref(), "test::data");
//! *data.lock() = 10;
//! pr_info!("{}\n", *data.lock());
//! }
//! ```no_run
//! # use kernel::prelude::*;
//! # use kernel::mutex_init;
//! # use kernel::sync::Mutex;
//! // SAFETY: `init` is called below.
//! let data = alloc::sync::Arc::pin(unsafe { Mutex::new(0) });
//! mutex_init!(data.as_ref(), "test::data");
//! *data.lock() = 10;
//! pr_info!("{}\n", *data.lock());
//! ```
use crate::str::CStr;
Expand Down
4 changes: 4 additions & 0 deletions rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ impl<T: PointerWrapper + Deref> PointerWrapper for Pin<T> {
/// In the example below, we have multiple exit paths and we want to log regardless of which one is
/// taken:
/// ```
/// # use kernel::prelude::*;
/// # use kernel::ScopeGuard;
/// fn example1(arg: bool) {
/// let _log = ScopeGuard::new(|| pr_info!("example1 completed\n"));
///
Expand All @@ -119,6 +121,8 @@ impl<T: PointerWrapper + Deref> PointerWrapper for Pin<T> {
/// In the example below, we want to log the same message on all early exits but a different one on
/// the main exit path:
/// ```
/// # use kernel::prelude::*;
/// # use kernel::ScopeGuard;
/// fn example2(arg: bool) {
/// let log = ScopeGuard::new(|| pr_info!("example2 returned early\n"));
///
Expand Down
4 changes: 2 additions & 2 deletions rust/macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use proc_macro::TokenStream;
///
/// # Examples
///
/// ```rust,no_run
/// ```ignore
/// use kernel::prelude::*;
///
/// module!{
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
///
/// # Examples
///
/// ```rust,no_run
/// ```ignore
/// use kernel::prelude::*;
///
/// module_misc_device! {
Expand Down

0 comments on commit ca3bf88

Please sign in to comment.