@@ -102,7 +102,6 @@ impl ProcMacroLibraryLibloading {
102
102
let lib = Library :: new ( file) . map_err ( |e| e. to_string ( ) ) ?;
103
103
104
104
let exported_macros = {
105
- // data already implies reference
106
105
let macros: libloading:: Symbol < & & [ ProcMacro ] > = unsafe { lib. get ( symbol_name. as_bytes ( ) ) }
107
106
. map_err ( |e| e. to_string ( ) ) ?;
108
107
@@ -199,44 +198,51 @@ impl Expander {
199
198
200
199
pub fn expand (
201
200
& self ,
202
- code : & str ,
203
- macro_to_expand : & str ,
201
+ macro_name : & str ,
202
+ macro_body : & str ,
203
+ attributes : Option < & String > ,
204
204
) -> Result < String , proc_macro:: bridge:: PanicMessage > {
205
- let token_stream =
206
- parse_string ( code) . expect ( & format ! ( "Error while parsing this code: '{}'" , code) ) ;
205
+ let parsed_body = parse_string ( macro_body) . expect (
206
+ & format ! ( "Error while parsing this code: '{}'" , macro_body)
207
+ ) ;
208
+
209
+ let parsed_attributes = attributes. map_or ( proc_macro2:: TokenStream :: new ( ) , |attr| {
210
+ parse_string ( attr) . expect (
211
+ & format ! ( "Error while parsing this code: '{}'" , attr)
212
+ )
213
+ } ) ;
207
214
208
215
for lib in & self . libs {
209
216
for proc_macro in & lib. exported_macros {
210
217
match proc_macro {
211
218
ProcMacro :: CustomDerive {
212
219
trait_name, client, ..
213
- } if * trait_name == macro_to_expand => {
220
+ } if * trait_name == macro_name => {
214
221
let res = client. run (
215
222
& EXEC_STRATEGY ,
216
223
rustc_server:: Rustc :: default ( ) ,
217
- token_stream ,
224
+ parsed_body ,
218
225
) ;
219
226
220
227
return res. map ( |token_stream| token_stream. to_string ( ) ) ;
221
228
}
222
229
223
- ProcMacro :: Bang { name, client } if * name == macro_to_expand => {
230
+ ProcMacro :: Bang { name, client } if * name == macro_name => {
224
231
let res = client. run (
225
232
& EXEC_STRATEGY ,
226
233
rustc_server:: Rustc :: default ( ) ,
227
- token_stream ,
234
+ parsed_body ,
228
235
) ;
229
236
230
237
return res. map ( |token_stream| token_stream. to_string ( ) ) ;
231
238
}
232
239
233
- ProcMacro :: Attr { name, client } if * name == macro_to_expand => {
234
- // fixme attr macro needs two inputs
240
+ ProcMacro :: Attr { name, client } if * name == macro_name => {
235
241
let res = client. run (
236
242
& EXEC_STRATEGY ,
237
243
rustc_server:: Rustc :: default ( ) ,
238
- proc_macro2 :: TokenStream :: new ( ) ,
239
- token_stream ,
244
+ parsed_attributes ,
245
+ parsed_body ,
240
246
) ;
241
247
242
248
return res. map ( |token_stream| token_stream. to_string ( ) ) ;
@@ -260,7 +266,7 @@ pub fn expand_task(task: &ExpansionTask) -> ExpansionResult {
260
266
& format ! ( "Cannot expand with provided libraries: ${:?}" , & task. libs)
261
267
) ;
262
268
263
- let result = match expander. expand ( & task. macro_body , & task. macro_name ) {
269
+ let result = match expander. expand ( & task. macro_name , & task. macro_body , task . attributes . as_ref ( ) ) {
264
270
Ok ( expansion) => ExpansionResult :: Success { expansion } ,
265
271
266
272
Err ( msg) => {
0 commit comments