55#![ deny( clippy:: perf) ]
66#![ deny( clippy:: style) ]
77
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 } ;
910
1011fn try_ident ( it : & mut token_stream:: IntoIter ) -> Option < String > {
1112 if let Some ( TokenTree :: Ident ( ident) ) = it. next ( ) {
@@ -15,21 +16,21 @@ fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
1516 }
1617}
1718
18- fn try_literal ( it : & mut token_stream:: IntoIter ) -> Option < String > {
19+ fn try_literal ( it : & mut token_stream:: IntoIter ) -> Option < Literal > {
1920 if let Some ( TokenTree :: Literal ( literal) ) = it. next ( ) {
20- Some ( literal. to_string ( ) )
21+ Some ( literal)
2122 } else {
2223 None
2324 }
2425}
2526
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 ( ) )
3232 }
33+ _ => None ,
3334 } )
3435}
3536
@@ -45,7 +46,7 @@ fn expect_punct(it: &mut token_stream::IntoIter) -> char {
4546 }
4647}
4748
48- fn expect_literal ( it : & mut token_stream:: IntoIter ) -> String {
49+ fn expect_literal ( it : & mut token_stream:: IntoIter ) -> Literal {
4950 try_literal ( it) . expect ( "Expected Literal" )
5051}
5152
@@ -57,8 +58,8 @@ fn expect_group(it: &mut token_stream::IntoIter) -> Group {
5758 }
5859}
5960
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" )
6263}
6364
6465#[ derive( Clone , PartialEq ) ]
@@ -71,7 +72,7 @@ fn expect_array_fields(it: &mut token_stream::IntoIter) -> ParamType {
7172 assert_eq ! ( expect_punct( it) , '<' ) ;
7273 let vals = expect_ident ( it) ;
7374 assert_eq ! ( expect_punct( it) , ',' ) ;
74- let max_length_str = expect_literal ( it) ;
75+ let max_length_str = expect_literal ( it) . to_string ( ) ;
7576 let max_length = max_length_str
7677 . parse :: < usize > ( )
7778 . expect ( "Expected usize length" ) ;
@@ -102,15 +103,15 @@ fn expect_end(it: &mut token_stream::IntoIter) {
102103fn get_literal ( it : & mut token_stream:: IntoIter , expected_name : & str ) -> String {
103104 assert_eq ! ( expect_ident( it) , expected_name) ;
104105 assert_eq ! ( expect_punct( it) , ':' ) ;
105- let literal = expect_literal ( it) ;
106+ let literal = expect_literal ( it) . to_string ( ) ;
106107 assert_eq ! ( expect_punct( it) , ',' ) ;
107108 literal
108109}
109110
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 {
111112 assert_eq ! ( expect_ident( it) , expected_name) ;
112113 assert_eq ! ( expect_punct( it) , ':' ) ;
113- let byte_string = expect_byte_string ( it) ;
114+ let byte_string = expect_string ( it) ;
114115 assert_eq ! ( expect_punct( it) , ',' ) ;
115116 byte_string
116117}
@@ -125,31 +126,31 @@ fn __build_modinfo_string_base(
125126 let string = if builtin {
126127 // Built-in modules prefix their modinfo strings by `module.`.
127128 format ! (
128- "{module}.{field}={content}" ,
129+ "{module}.{field}={content}\0 " ,
129130 module = module,
130131 field = field,
131132 content = content
132133 )
133134 } else {
134135 // Loadable modules' modinfo strings go as-is.
135- format ! ( "{field}={content}" , field = field, content = content)
136+ format ! ( "{field}={content}\0 " , field = field, content = content)
136137 } ;
137138
138139 format ! (
139140 "
140141 {cfg}
141142 #[link_section = \" .modinfo\" ]
142143 #[used]
143- pub static {variable}: [u8; {length}] = *b \" {string}\\ 0 \" ;
144+ pub static {variable}: [u8; {length}] = *{string};
144145 " ,
145146 cfg = if builtin {
146147 "#[cfg(not(MODULE))]"
147148 } else {
148149 "#[cfg(MODULE)]"
149150 } ,
150151 variable = variable,
151- length = string. len( ) + 1 ,
152- string = string,
152+ length = string. len( ) ,
153+ string = Literal :: byte_string ( string. as_bytes ( ) ) ,
153154 )
154155}
155156
@@ -242,10 +243,14 @@ fn try_simple_param_val(
242243 match param_type {
243244 "bool" => Box :: new ( |param_it| try_ident ( param_it) ) ,
244245 "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+ } )
247252 } ) ,
248- _ => Box :: new ( |param_it| try_literal ( param_it) ) ,
253+ _ => Box :: new ( |param_it| try_literal ( param_it) . map ( |x| x . to_string ( ) ) ) ,
249254 }
250255}
251256
@@ -349,14 +354,12 @@ impl ModuleInfo {
349354
350355 match key. as_str ( ) {
351356 "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) ) ) ,
360363 "params" => info. params = Some ( expect_group ( it) ) ,
361364 _ => panic ! (
362365 "Unknown key \" {}\" . Valid keys are: {:?}." ,
@@ -426,7 +429,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
426429 let mut param_it = group. stream ( ) . into_iter ( ) ;
427430 let param_default = get_default ( & param_type, & mut param_it) ;
428431 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" ) ;
430433 expect_end ( & mut param_it) ;
431434
432435 // TODO: more primitive types
@@ -719,29 +722,29 @@ pub fn module_misc_device(ts: TokenStream) -> TokenStream {
719722
720723 kernel::prelude::module! {{
721724 type: {module},
722- name: b \" {name}\" ,
725+ name: {name},
723726 {author}
724727 {description}
725- license: b \" {license}\" ,
728+ license: {license},
726729 {alias}
727730 }}
728731 " ,
729732 module = module,
730733 type_ = info. type_,
731- name = info. name,
734+ name = Literal :: string ( & info. name) ,
732735 author = info
733736 . author
734- . map( |v| format!( "author: b \" {} \" ," , v ) )
737+ . map( |v| format!( "author: {} ," , Literal :: string ( & v ) ) )
735738 . unwrap_or_else( || "" . to_string( ) ) ,
736739 description = info
737740 . description
738- . map( |v| format!( "description: b \" {} \" ," , v ) )
741+ . map( |v| format!( "description: {} ," , Literal :: string ( & v ) ) )
739742 . unwrap_or_else( || "" . to_string( ) ) ,
740743 alias = info
741744 . alias
742- . map( |v| format!( "alias: b \" {} \" ," , v ) )
745+ . map( |v| format!( "alias: {} ," , Literal :: string ( & v ) ) )
743746 . unwrap_or_else( || "" . to_string( ) ) ,
744- license = info. license
747+ license = Literal :: string ( & info. license)
745748 )
746749 . parse ( )
747750 . expect ( "Error parsing formatted string into token stream." )
0 commit comments