Skip to content

rust: take str literal instead bstr literal in module! macro #858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions drivers/android/rust_binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ use {context::Context, thread::Thread};

module! {
type: BinderModule,
name: b"rust_binder",
author: b"Wedson Almeida Filho",
description: b"Android Binder",
license: b"GPL",
name: "rust_binder",
author: "Wedson Almeida Filho",
description: "Android Binder",
license: "GPL",
}

trait DeliverToRead {
Expand Down
8 changes: 4 additions & 4 deletions drivers/char/hw_random/bcm2835_rng_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use kernel::{

module_platform_driver! {
type: RngDriver,
name: b"bcm2835_rng_rust",
author: b"Rust for Linux Contributors",
description: b"BCM2835 Random Number Generator (RNG) driver",
license: b"GPL v2",
name: "bcm2835_rng_rust",
author: "Rust for Linux Contributors",
description: "BCM2835 Random Number Generator (RNG) driver",
license: "GPL v2",
}

struct RngDevice;
Expand Down
6 changes: 3 additions & 3 deletions drivers/gpio/gpio_pl061_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl power::Operations for PL061Device {

module_amba_driver! {
type: PL061Device,
name: b"pl061_gpio",
author: b"Wedson Almeida Filho",
license: b"GPL",
name: "pl061_gpio",
author: "Wedson Almeida Filho",
license: "GPL",
}
6 changes: 3 additions & 3 deletions rust/kernel/amba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ unsafe impl device::RawDevice for Device {
///
/// module_amba_driver! {
/// type: MyDriver,
/// name: b"module_name",
/// author: b"Author name",
/// license: b"GPL",
/// name: "module_name",
/// author: "Author name",
/// license: "GPL",
/// }
/// ```
#[macro_export]
Expand Down
8 changes: 4 additions & 4 deletions rust/kernel/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,10 +799,10 @@ impl<T: Type + Sync> crate::Module for Module<T> {
///
/// module_fs! {
/// type: MyFs,
/// name: b"my_fs_kernel_module",
/// author: b"Rust for Linux Contributors",
/// description: b"My very own file system kernel module!",
/// license: b"GPL",
/// name: "my_fs_kernel_module",
/// author: "Rust for Linux Contributors",
/// description: "My very own file system kernel module!",
/// license: "GPL",
/// }
///
/// struct MyFs;
Expand Down
8 changes: 4 additions & 4 deletions rust/kernel/miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ impl<T: file::Operations<OpenData = ()>> crate::Module for Module<T> {
///
/// module_misc_device! {
/// type: MyFile,
/// name: b"my_miscdev_kernel_module",
/// author: b"Rust for Linux Contributors",
/// description: b"My very own misc device kernel module!",
/// license: b"GPL",
/// name: "my_miscdev_kernel_module",
/// author: "Rust for Linux Contributors",
/// description: "My very own misc device kernel module!",
/// license: "GPL",
/// }
///
/// #[derive(Default)]
Expand Down
6 changes: 3 additions & 3 deletions rust/kernel/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ unsafe impl device::RawDevice for Device {
///
/// module_platform_driver! {
/// type: MyDriver,
/// name: b"module_name",
/// author: b"Author name",
/// license: b"GPL",
/// name: "module_name",
/// author: "Author name",
/// license: "GPL",
/// }
/// ```
#[macro_export]
Expand Down
32 changes: 27 additions & 5 deletions rust/macros/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ pub(crate) fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String>
})
}

pub(crate) fn try_string(it: &mut token_stream::IntoIter) -> Option<String> {
try_literal(it).and_then(|string| {
if string.starts_with('\"') && string.ends_with('\"') {
let content = &string[1..string.len() - 1];
if content.contains('\\') {
panic!("Escape sequences in string literals not yet handled");
}
Some(content.to_string())
} else if string.starts_with("r\"") {
panic!("Raw string literals are not yet handled");
} else {
None
}
})
}

pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
try_ident(it).expect("Expected Ident")
}
Expand All @@ -52,8 +68,14 @@ pub(crate) fn expect_group(it: &mut token_stream::IntoIter) -> Group {
}
}

pub(crate) fn expect_byte_string(it: &mut token_stream::IntoIter) -> String {
try_byte_string(it).expect("Expected byte string")
pub(crate) fn expect_string(it: &mut token_stream::IntoIter) -> String {
try_string(it).expect("Expected string")
}

pub(crate) fn expect_string_ascii(it: &mut token_stream::IntoIter) -> String {
let string = try_string(it).expect("Expected string");
assert!(string.is_ascii(), "Expected ASCII string");
string
}

pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {
Expand All @@ -70,10 +92,10 @@ pub(crate) fn get_literal(it: &mut token_stream::IntoIter, expected_name: &str)
literal
}

pub(crate) fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
pub(crate) fn get_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
assert_eq!(expect_ident(it), expected_name);
assert_eq!(expect_punct(it), ':');
let byte_string = expect_byte_string(it);
let string = expect_string(it);
assert_eq!(expect_punct(it), ',');
byte_string
string
}
12 changes: 6 additions & 6 deletions rust/macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ use proc_macro::TokenStream;
///
/// module!{
/// type: MyModule,
/// name: b"my_kernel_module",
/// author: b"Rust for Linux Contributors",
/// description: b"My very own kernel module!",
/// license: b"GPL",
/// name: "my_kernel_module",
/// author: "Rust for Linux Contributors",
/// description: "My very own kernel module!",
/// license: "GPL",
/// params: {
/// my_i32: i32 {
/// default: 42,
/// permissions: 0o000,
/// description: b"Example of i32",
/// description: "Example of i32",
/// },
/// writeable_i32: i32 {
/// default: 42,
/// permissions: 0o644,
/// description: b"Example of i32",
/// description: "Example of i32",
/// },
/// },
/// }
Expand Down
14 changes: 7 additions & 7 deletions rust/macros/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ impl ModuleInfo {

match key.as_str() {
"type" => info.type_ = expect_ident(it),
"name" => info.name = expect_byte_string(it),
"author" => info.author = Some(expect_byte_string(it)),
"description" => info.description = Some(expect_byte_string(it)),
"license" => info.license = expect_byte_string(it),
"alias" => info.alias = Some(expect_byte_string(it)),
"name" => info.name = expect_string_ascii(it),
"author" => info.author = Some(expect_string(it)),
"description" => info.description = Some(expect_string(it)),
"license" => info.license = expect_string_ascii(it),
"alias" => info.alias = Some(expect_string_ascii(it)),
"alias_rtnl_link" => {
info.alias = Some(format!("rtnl-link-{}", expect_byte_string(it)))
info.alias = Some(format!("rtnl-link-{}", expect_string_ascii(it)))
}
"params" => info.params = Some(expect_group(it)),
_ => panic!(
Expand Down Expand Up @@ -347,7 +347,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
let mut param_it = group.stream().into_iter();
let param_default = get_default(&param_type, &mut param_it);
let param_permissions = get_literal(&mut param_it, "permissions");
let param_description = get_byte_string(&mut param_it, "description");
let param_description = get_string(&mut param_it, "description");
expect_end(&mut param_it);

// TODO: More primitive types.
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_chrdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use kernel::{chrdev, file};

module! {
type: RustChrdev,
name: b"rust_chrdev",
author: b"Rust for Linux Contributors",
description: b"Rust character device sample",
license: b"GPL",
name: "rust_chrdev",
author: "Rust for Linux Contributors",
description: "Rust character device sample",
license: "GPL",
}

struct RustFile;
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_echo_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ impl kernel::Module for RustEchoServer {

module! {
type: RustEchoServer,
name: b"rust_echo_server",
author: b"Rust for Linux Contributors",
description: b"Rust tcp echo sample",
license: b"GPL v2",
name: "rust_echo_server",
author: "Rust for Linux Contributors",
description: "Rust tcp echo sample",
license: "GPL v2",
}
6 changes: 3 additions & 3 deletions samples/rust/rust_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use kernel::{c_str, fs};

module_fs! {
type: RustFs,
name: b"rust_fs",
author: b"Rust for Linux Contributors",
license: b"GPL",
name: "rust_fs",
author: "Rust for Linux Contributors",
license: "GPL",
}

struct RustFs;
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use kernel::prelude::*;

module! {
type: RustMinimal,
name: b"rust_minimal",
author: b"Rust for Linux Contributors",
description: b"Rust minimal sample",
license: b"GPL",
name: "rust_minimal",
author: "Rust for Linux Contributors",
description: "Rust minimal sample",
license: "GPL",
}

struct RustMinimal {
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use kernel::{

module! {
type: RustMiscdev,
name: b"rust_miscdev",
author: b"Rust for Linux Contributors",
description: b"Rust miscellaneous device sample",
license: b"GPL",
name: "rust_miscdev",
author: "Rust for Linux Contributors",
description: "Rust miscellaneous device sample",
license: "GPL",
}

const MAX_TOKENS: usize = 3;
Expand Down
18 changes: 9 additions & 9 deletions samples/rust/rust_module_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ use kernel::prelude::*;

module! {
type: RustModuleParameters,
name: b"rust_module_parameters",
author: b"Rust for Linux Contributors",
description: b"Rust module parameters sample",
license: b"GPL",
name: "rust_module_parameters",
author: "Rust for Linux Contributors",
description: "Rust module parameters sample",
license: "GPL",
params: {
my_bool: bool {
default: true,
permissions: 0,
description: b"Example of bool",
description: "Example of bool",
},
my_i32: i32 {
default: 42,
permissions: 0o644,
description: b"Example of i32",
description: "Example of i32",
},
my_str: str {
default: b"default str val",
permissions: 0o644,
description: b"Example of a string param",
description: "Example of a string param",
},
my_usize: usize {
default: 42,
permissions: 0o644,
description: b"Example of usize",
description: "Example of usize",
},
my_array: ArrayParam<i32, 3> {
default: [0, 1],
permissions: 0,
description: b"Example of array",
description: "Example of array",
},
},
}
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_netfilter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use kernel::prelude::*;

module! {
type: RustNetfilter,
name: b"rust_netfilter",
author: b"Rust for Linux Contributors",
description: b"Rust netfilter sample",
license: b"GPL",
name: "rust_netfilter",
author: "Rust for Linux Contributors",
description: "Rust netfilter sample",
license: "GPL",
}

struct RustNetfilter {
Expand Down
4 changes: 2 additions & 2 deletions samples/rust/rust_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use kernel::{module_platform_driver, of, platform, prelude::*};

module_platform_driver! {
type: Driver,
name: b"rust_platform",
license: b"GPL",
name: "rust_platform",
license: "GPL",
}

struct Driver;
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use kernel::{pr_cont, str::CStr, ThisModule};

module! {
type: RustPrint,
name: b"rust_print",
author: b"Rust for Linux Contributors",
description: b"Rust printing macros sample",
license: b"GPL",
name: "rust_print",
author: "Rust for Linux Contributors",
description: "Rust printing macros sample",
license: "GPL",
}

struct RustPrint;
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use kernel::{

module_misc_device! {
type: RandomFile,
name: b"rust_random",
author: b"Rust for Linux Contributors",
description: b"Just use /dev/urandom: Now with early-boot safety",
license: b"GPL",
name: "rust_random",
author: "Rust for Linux Contributors",
description: "Just use /dev/urandom: Now with early-boot safety",
license: "GPL",
}

struct RandomFile;
Expand Down
8 changes: 4 additions & 4 deletions samples/rust/rust_selftests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use kernel::prelude::*;

module! {
type: RustSelftests,
name: b"rust_selftests",
author: b"Rust for Linux Contributors",
description: b"Self test cases for Rust",
license: b"GPL",
name: "rust_selftests",
author: "Rust for Linux Contributors",
description: "Self test cases for Rust",
license: "GPL",
}

struct RustSelftests;
Expand Down
Loading