5
5
#![ deny( clippy:: perf) ]
6
6
#![ deny( clippy:: style) ]
7
7
8
- use proc_macro:: { token_stream, Delimiter , Group , TokenStream , TokenTree } ;
8
+ use crate :: syn:: Lit ;
9
+ use proc_macro:: { token_stream, Delimiter , Group , Literal , TokenStream , TokenTree } ;
9
10
10
11
fn try_ident ( it : & mut token_stream:: IntoIter ) -> Option < String > {
11
12
if let Some ( TokenTree :: Ident ( ident) ) = it. next ( ) {
@@ -15,21 +16,21 @@ fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
15
16
}
16
17
}
17
18
18
- fn try_literal ( it : & mut token_stream:: IntoIter ) -> Option < String > {
19
+ fn try_literal ( it : & mut token_stream:: IntoIter ) -> Option < Literal > {
19
20
if let Some ( TokenTree :: Literal ( literal) ) = it. next ( ) {
20
- Some ( literal. to_string ( ) )
21
+ Some ( literal)
21
22
} else {
22
23
None
23
24
}
24
25
}
25
26
26
- fn try_byte_string ( it : & mut token_stream:: IntoIter ) -> Option < String > {
27
- try_literal ( it) . and_then ( |byte_string| {
28
- if byte_string. starts_with ( "b\" " ) && byte_string. ends_with ( '\"' ) {
29
- Some ( byte_string[ 2 ..byte_string. len ( ) - 1 ] . to_string ( ) )
30
- } else {
31
- None
27
+ fn try_string ( it : & mut token_stream:: IntoIter ) -> Option < String > {
28
+ try_literal ( it) . and_then ( |literal| match Lit :: new ( literal) {
29
+ Lit :: Str ( s) => {
30
+ assert ! ( s. suffix( ) . is_empty( ) , "Unexpected suffix" ) ;
31
+ Some ( s. value ( ) )
32
32
}
33
+ _ => None ,
33
34
} )
34
35
}
35
36
@@ -45,7 +46,7 @@ fn expect_punct(it: &mut token_stream::IntoIter) -> char {
45
46
}
46
47
}
47
48
48
- fn expect_literal ( it : & mut token_stream:: IntoIter ) -> String {
49
+ fn expect_literal ( it : & mut token_stream:: IntoIter ) -> Literal {
49
50
try_literal ( it) . expect ( "Expected Literal" )
50
51
}
51
52
@@ -57,8 +58,8 @@ fn expect_group(it: &mut token_stream::IntoIter) -> Group {
57
58
}
58
59
}
59
60
60
- fn expect_byte_string ( it : & mut token_stream:: IntoIter ) -> String {
61
- try_byte_string ( it) . expect ( "Expected byte string" )
61
+ fn expect_string ( it : & mut token_stream:: IntoIter ) -> String {
62
+ try_string ( it) . expect ( "Expected string" )
62
63
}
63
64
64
65
#[ derive( Clone , PartialEq ) ]
@@ -71,7 +72,7 @@ fn expect_array_fields(it: &mut token_stream::IntoIter) -> ParamType {
71
72
assert_eq ! ( expect_punct( it) , '<' ) ;
72
73
let vals = expect_ident ( it) ;
73
74
assert_eq ! ( expect_punct( it) , ',' ) ;
74
- let max_length_str = expect_literal ( it) ;
75
+ let max_length_str = expect_literal ( it) . to_string ( ) ;
75
76
let max_length = max_length_str
76
77
. parse :: < usize > ( )
77
78
. expect ( "Expected usize length" ) ;
@@ -102,15 +103,15 @@ fn expect_end(it: &mut token_stream::IntoIter) {
102
103
fn get_literal ( it : & mut token_stream:: IntoIter , expected_name : & str ) -> String {
103
104
assert_eq ! ( expect_ident( it) , expected_name) ;
104
105
assert_eq ! ( expect_punct( it) , ':' ) ;
105
- let literal = expect_literal ( it) ;
106
+ let literal = expect_literal ( it) . to_string ( ) ;
106
107
assert_eq ! ( expect_punct( it) , ',' ) ;
107
108
literal
108
109
}
109
110
110
- fn get_byte_string ( it : & mut token_stream:: IntoIter , expected_name : & str ) -> String {
111
+ fn get_string ( it : & mut token_stream:: IntoIter , expected_name : & str ) -> String {
111
112
assert_eq ! ( expect_ident( it) , expected_name) ;
112
113
assert_eq ! ( expect_punct( it) , ':' ) ;
113
- let byte_string = expect_byte_string ( it) ;
114
+ let byte_string = expect_string ( it) ;
114
115
assert_eq ! ( expect_punct( it) , ',' ) ;
115
116
byte_string
116
117
}
@@ -125,31 +126,31 @@ fn __build_modinfo_string_base(
125
126
let string = if builtin {
126
127
// Built-in modules prefix their modinfo strings by `module.`.
127
128
format ! (
128
- "{module}.{field}={content}" ,
129
+ "{module}.{field}={content}\0 " ,
129
130
module = module,
130
131
field = field,
131
132
content = content
132
133
)
133
134
} else {
134
135
// Loadable modules' modinfo strings go as-is.
135
- format ! ( "{field}={content}" , field = field, content = content)
136
+ format ! ( "{field}={content}\0 " , field = field, content = content)
136
137
} ;
137
138
138
139
format ! (
139
140
"
140
141
{cfg}
141
142
#[link_section = \" .modinfo\" ]
142
143
#[used]
143
- pub static {variable}: [u8; {length}] = *b \" {string}\\ 0 \" ;
144
+ pub static {variable}: [u8; {length}] = *{string};
144
145
" ,
145
146
cfg = if builtin {
146
147
"#[cfg(not(MODULE))]"
147
148
} else {
148
149
"#[cfg(MODULE)]"
149
150
} ,
150
151
variable = variable,
151
- length = string. len( ) + 1 ,
152
- string = string,
152
+ length = string. len( ) ,
153
+ string = Literal :: byte_string ( string. as_bytes ( ) ) ,
153
154
)
154
155
}
155
156
@@ -242,10 +243,14 @@ fn try_simple_param_val(
242
243
match param_type {
243
244
"bool" => Box :: new ( |param_it| try_ident ( param_it) ) ,
244
245
"str" => Box :: new ( |param_it| {
245
- try_byte_string ( param_it)
246
- . map ( |s| format ! ( "kernel::module_param::StringParam::Ref(b\" {}\" )" , s) )
246
+ try_string ( param_it) . map ( |s| {
247
+ format ! (
248
+ "kernel::module_param::StringParam::Ref({})" ,
249
+ Literal :: byte_string( s. as_bytes( ) )
250
+ )
251
+ } )
247
252
} ) ,
248
- _ => Box :: new ( |param_it| try_literal ( param_it) ) ,
253
+ _ => Box :: new ( |param_it| try_literal ( param_it) . map ( |x| x . to_string ( ) ) ) ,
249
254
}
250
255
}
251
256
@@ -349,14 +354,12 @@ impl ModuleInfo {
349
354
350
355
match key. as_str ( ) {
351
356
"type" => info. type_ = expect_ident ( it) ,
352
- "name" => info. name = expect_byte_string ( it) ,
353
- "author" => info. author = Some ( expect_byte_string ( it) ) ,
354
- "description" => info. description = Some ( expect_byte_string ( it) ) ,
355
- "license" => info. license = expect_byte_string ( it) ,
356
- "alias" => info. alias = Some ( expect_byte_string ( it) ) ,
357
- "alias_rtnl_link" => {
358
- info. alias = Some ( format ! ( "rtnl-link-{}" , expect_byte_string( it) ) )
359
- }
357
+ "name" => info. name = expect_string ( it) ,
358
+ "author" => info. author = Some ( expect_string ( it) ) ,
359
+ "description" => info. description = Some ( expect_string ( it) ) ,
360
+ "license" => info. license = expect_string ( it) ,
361
+ "alias" => info. alias = Some ( expect_string ( it) ) ,
362
+ "alias_rtnl_link" => info. alias = Some ( format ! ( "rtnl-link-{}" , expect_string( it) ) ) ,
360
363
"params" => info. params = Some ( expect_group ( it) ) ,
361
364
_ => panic ! (
362
365
"Unknown key \" {}\" . Valid keys are: {:?}." ,
@@ -426,7 +429,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
426
429
let mut param_it = group. stream ( ) . into_iter ( ) ;
427
430
let param_default = get_default ( & param_type, & mut param_it) ;
428
431
let param_permissions = get_literal ( & mut param_it, "permissions" ) ;
429
- let param_description = get_byte_string ( & mut param_it, "description" ) ;
432
+ let param_description = get_string ( & mut param_it, "description" ) ;
430
433
expect_end ( & mut param_it) ;
431
434
432
435
// TODO: more primitive types
@@ -719,29 +722,29 @@ pub fn module_misc_device(ts: TokenStream) -> TokenStream {
719
722
720
723
kernel::prelude::module! {{
721
724
type: {module},
722
- name: b \" {name}\" ,
725
+ name: {name},
723
726
{author}
724
727
{description}
725
- license: b \" {license}\" ,
728
+ license: {license},
726
729
{alias}
727
730
}}
728
731
" ,
729
732
module = module,
730
733
type_ = info. type_,
731
- name = info. name,
734
+ name = Literal :: string ( & info. name) ,
732
735
author = info
733
736
. author
734
- . map( |v| format!( "author: b \" {} \" ," , v ) )
737
+ . map( |v| format!( "author: {} ," , Literal :: string ( & v ) ) )
735
738
. unwrap_or_else( || "" . to_string( ) ) ,
736
739
description = info
737
740
. description
738
- . map( |v| format!( "description: b \" {} \" ," , v ) )
741
+ . map( |v| format!( "description: {} ," , Literal :: string ( & v ) ) )
739
742
. unwrap_or_else( || "" . to_string( ) ) ,
740
743
alias = info
741
744
. alias
742
- . map( |v| format!( "alias: b \" {} \" ," , v ) )
745
+ . map( |v| format!( "alias: {} ," , Literal :: string ( & v ) ) )
743
746
. unwrap_or_else( || "" . to_string( ) ) ,
744
- license = info. license
747
+ license = Literal :: string ( & info. license)
745
748
)
746
749
. parse ( )
747
750
. expect ( "Error parsing formatted string into token stream." )
0 commit comments